Compare commits
No commits in common. "master" and "2.0.0" have entirely different histories.
|
@ -3,7 +3,6 @@
|
||||||
.LSOverride
|
.LSOverride
|
||||||
.idea
|
.idea
|
||||||
.vscode
|
.vscode
|
||||||
package-lock.json
|
|
||||||
node_modules/
|
node_modules/
|
||||||
dist/
|
dist/
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @authors yutent (yutent.io@gmail.com)
|
* @authors yutent (yutent@doui.cc)
|
||||||
* @date 2018-08-04 01:00:06
|
* @date 2018-08-04 01:00:06
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
require('es.shim')
|
require('es.shim')
|
||||||
const fs = require('iofs')
|
const fs = require('iofs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const chokidar = require('chokidar')
|
const chokidar = require('chokidar')
|
||||||
const { minify } = require('terser')
|
const uglify = require('uglify-es')
|
||||||
const chalk = require('chalk')
|
const chalk = require('chalk')
|
||||||
const config = require('./package.json')
|
|
||||||
const log = console.log
|
const log = console.log
|
||||||
const VERSION = config.version
|
const VERSION = '2.0.0'
|
||||||
|
|
||||||
const PACK_DIR = path.resolve('./dist')
|
const PACK_DIR = path.resolve('./dist')
|
||||||
const SOURCE_DIR = path.resolve('./src/')
|
const SOURCE_DIR = path.resolve('./src/')
|
||||||
|
@ -58,9 +59,9 @@ export default _Anot
|
||||||
function comment({ touch } = {}) {
|
function comment({ touch } = {}) {
|
||||||
return `/*==================================================
|
return `/*==================================================
|
||||||
* Anot ${touch ? 'touch' : 'normal'} version for future browsers
|
* Anot ${touch ? 'touch' : 'normal'} version for future browsers
|
||||||
* @authors yutent<yutent.io@gmail.com>
|
* @authors yutent (yutent@doui.cc)
|
||||||
* @date ${new Date().format()}
|
* @date 2017-03-21 21:05:57
|
||||||
* @version v${VERSION}
|
* V${VERSION}
|
||||||
*
|
*
|
||||||
==================================================*/
|
==================================================*/
|
||||||
`
|
`
|
||||||
|
@ -114,8 +115,8 @@ function packNoCompress(file) {
|
||||||
* 打包带触摸事件的未来版的 anot
|
* 打包带触摸事件的未来版的 anot
|
||||||
* --------------------------------------------------------
|
* --------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
fs.echo(Buffer.concat([PAD_START, touchVer, PAD_END]), './dist/anot.touch.js')
|
fs.echo(Buffer.concat([PAD_START, touchVer, PAD_END]), './dist/anot-touch.js')
|
||||||
log('%s 打包完成...', chalk.green('anot.touch.js'))
|
log('%s 打包完成...', chalk.green('anot-touch.js'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打包并压缩
|
// 打包并压缩
|
||||||
|
@ -135,24 +136,22 @@ function packAndCompress() {
|
||||||
*/
|
*/
|
||||||
log('正在打包 anot.js...')
|
log('正在打包 anot.js...')
|
||||||
let normalVerPack = Buffer.concat([PAD_START, normalVer, PAD_END]).toString()
|
let normalVerPack = Buffer.concat([PAD_START, normalVer, PAD_END]).toString()
|
||||||
|
fs.echo(comment() + uglify.minify(normalVerPack).code, './dist/anot.js')
|
||||||
minify(normalVerPack, { sourceMap: false }).then(res => {
|
log(chalk.green('anot.js 打包压缩完成!'))
|
||||||
fs.echo(comment() + res.code, './dist/anot.js')
|
|
||||||
log(chalk.green('anot.js 打包压缩完成!'))
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* --------------------------------------------------------
|
* --------------------------------------------------------
|
||||||
* 打包带触摸事件的未来版的 anot
|
* 打包带触摸事件的未来版的 anot
|
||||||
* --------------------------------------------------------
|
* --------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
log('正在打包 anot.touch.js...')
|
log('正在打包 anot-touch.js...')
|
||||||
let touchVerPack = Buffer.concat([PAD_START, touchVer, PAD_END]).toString()
|
let touchVerPack = Buffer.concat([PAD_START, touchVer, PAD_END]).toString()
|
||||||
|
|
||||||
minify(touchVerPack, { sourceMap: false }).then(res => {
|
fs.echo(
|
||||||
fs.echo(comment({ touch: true }) + res.code, './dist/anot.touch.js')
|
comment({ touch: true }) + uglify.minify(touchVerPack).code,
|
||||||
log(chalk.green('anot.touch.js 打包压缩完成!'))
|
'./dist/anot-touch.js'
|
||||||
})
|
)
|
||||||
|
log(chalk.green('anot-touch.js 打包压缩完成!'))
|
||||||
}
|
}
|
||||||
|
|
||||||
let args = process.argv.slice(2)
|
let args = process.argv.slice(2)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "anot",
|
"name": "anot",
|
||||||
"version": "2.2.4",
|
"version": "2.0.0",
|
||||||
"description": "Anot - 迷你mvvm框架",
|
"description": "Anot - 迷你mvvm框架",
|
||||||
"main": "dist/anot.js",
|
"main": "dist/anot.js",
|
||||||
"files": ["dist"],
|
"files": ["dist"],
|
||||||
|
@ -14,9 +14,9 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"chalk": "^2.4.1",
|
"chalk": "^2.4.1",
|
||||||
"chokidar": "^2.0.4",
|
"chokidar": "^2.0.4",
|
||||||
"es.shim": "^2.0.1",
|
"es.shim": "^1.1.2",
|
||||||
"iofs": "^1.5.2",
|
"iofs": "^1.1.0",
|
||||||
"terser": "^5.0.0"
|
"uglify-es": "^3.3.9"
|
||||||
},
|
},
|
||||||
"repository": "https://github.com/yutent/anot.js.git",
|
"repository": "https://github.com/yutent/anot.js.git",
|
||||||
"author": "yutent",
|
"author": "yutent",
|
||||||
|
|
|
@ -16,7 +16,7 @@ npm run prod
|
||||||
```
|
```
|
||||||
执行完, 会打包为2个版本, 分别是
|
执行完, 会打包为2个版本, 分别是
|
||||||
- anot.js 普通版(需要支持es6 module的现代浏览器)
|
- anot.js 普通版(需要支持es6 module的现代浏览器)
|
||||||
- anot.touch.js 带触摸事件的版本(需要支持es6 module的现代浏览器)
|
- anot-touch.js 带触摸的版本(需要支持es6 module的现代浏览器)
|
||||||
|
|
||||||
|
|
||||||
### 文档:
|
### 文档:
|
||||||
|
|
|
@ -9,7 +9,7 @@ var DOC = window.document
|
||||||
var head = DOC.head //HEAD元素
|
var head = DOC.head //HEAD元素
|
||||||
head.insertAdjacentHTML(
|
head.insertAdjacentHTML(
|
||||||
'afterbegin',
|
'afterbegin',
|
||||||
'<anot skip class="anot-hide"><style id="anot-style">.anot-hide{ display: none!important }</style></anot>'
|
'<anot skip class="anot-hide"><style id="anot-style">.anot-hide{ display: none!important } slot{visibility:hidden;}</style></anot>'
|
||||||
)
|
)
|
||||||
var ifGroup = head.firstChild
|
var ifGroup = head.firstChild
|
||||||
|
|
||||||
|
@ -31,8 +31,6 @@ function createMap() {
|
||||||
return Object.create(null)
|
return Object.create(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
var encode = encodeURIComponent
|
|
||||||
var decode = decodeURIComponent
|
|
||||||
var subscribers = '$' + expose
|
var subscribers = '$' + expose
|
||||||
|
|
||||||
var nullObject = {} //作用类似于noop,只用于代码防御,千万不要在它上面添加属性
|
var nullObject = {} //作用类似于noop,只用于代码防御,千万不要在它上面添加属性
|
||||||
|
|
|
@ -4,7 +4,14 @@ let Anot = function(el) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*视浏览器情况采用最快的异步回调*/
|
/*视浏览器情况采用最快的异步回调*/
|
||||||
Anot.nextTick = (function() {
|
Anot.nextTick = new function() {
|
||||||
|
// jshint ignore:line
|
||||||
|
let tickImmediate = window.setImmediate
|
||||||
|
let tickObserver = window.MutationObserver
|
||||||
|
if (tickImmediate) {
|
||||||
|
return tickImmediate.bind(window)
|
||||||
|
}
|
||||||
|
|
||||||
let queue = []
|
let queue = []
|
||||||
function callback() {
|
function callback() {
|
||||||
let n = queue.length
|
let n = queue.length
|
||||||
|
@ -14,16 +21,21 @@ Anot.nextTick = (function() {
|
||||||
queue = queue.slice(n)
|
queue = queue.slice(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
let node = document.createTextNode('<!-- -->')
|
if (tickObserver) {
|
||||||
new MutationObserver(callback).observe(node, { characterData: true })
|
let node = document.createTextNode('anot')
|
||||||
|
new tickObserver(callback).observe(node, { characterData: true }) // jshint ignore:line
|
||||||
let bool = false
|
let bool = false
|
||||||
return function(fn) {
|
return function(fn) {
|
||||||
queue.push(fn)
|
queue.push(fn)
|
||||||
bool = !bool
|
bool = !bool
|
||||||
node.data = bool
|
node.data = bool
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})()
|
|
||||||
|
return function(fn) {
|
||||||
|
setTimeout(fn, 4)
|
||||||
|
}
|
||||||
|
}() // jshint ignore:line
|
||||||
|
|
||||||
/*********************************************************************
|
/*********************************************************************
|
||||||
* Anot的静态方法定义区 *
|
* Anot的静态方法定义区 *
|
||||||
|
@ -85,7 +97,10 @@ Anot.PropsTypes.isBoolean = function() {
|
||||||
/*判定是否是一个朴素的javascript对象(Object),不是DOM对象,不是BOM对象,不是自定义类的实例*/
|
/*判定是否是一个朴素的javascript对象(Object),不是DOM对象,不是BOM对象,不是自定义类的实例*/
|
||||||
Anot.isPlainObject = function(obj) {
|
Anot.isPlainObject = function(obj) {
|
||||||
// 简单的 typeof obj === "object"检测,会致使用isPlainObject(window)在opera下通不过
|
// 简单的 typeof obj === "object"检测,会致使用isPlainObject(window)在opera下通不过
|
||||||
return serialize.call(obj) === '[object Object]' && Object.getPrototypeOf(obj) === oproto
|
return (
|
||||||
|
serialize.call(obj) === '[object Object]' &&
|
||||||
|
Object.getPrototypeOf(obj) === oproto
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let VMODELS = (Anot.vmodels = {}) //所有vmodel都储存在这里
|
let VMODELS = (Anot.vmodels = {}) //所有vmodel都储存在这里
|
||||||
|
@ -165,7 +180,11 @@ Anot.mix = Anot.fn.mix = function() {
|
||||||
if (target === copy) {
|
if (target === copy) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if (deep && copy && (Anot.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) {
|
if (
|
||||||
|
deep &&
|
||||||
|
copy &&
|
||||||
|
(Anot.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))
|
||||||
|
) {
|
||||||
if (copyIsArray) {
|
if (copyIsArray) {
|
||||||
copyIsArray = false
|
copyIsArray = false
|
||||||
clone = src && Array.isArray(src) ? src : []
|
clone = src && Array.isArray(src) ? src : []
|
||||||
|
@ -271,11 +290,11 @@ Anot.mix({
|
||||||
t = t.trim()
|
t = t.trim()
|
||||||
let hook = hooks[t]
|
let hook = hooks[t]
|
||||||
if (typeof hook === 'object') {
|
if (typeof hook === 'object') {
|
||||||
t = hook.type || t
|
type = hook.type || type
|
||||||
phase = hook.phase || phase
|
phase = hook.phase || !!phase
|
||||||
fn = hook.fix ? hook.fix(el, fn) : fn
|
fn = hook.fix ? hook.fix(el, fn) : fn
|
||||||
}
|
}
|
||||||
el.addEventListener(t, fn, !!phase)
|
el.addEventListener(t, fn, phase)
|
||||||
})
|
})
|
||||||
return fn
|
return fn
|
||||||
},
|
},
|
||||||
|
@ -288,10 +307,10 @@ Anot.mix({
|
||||||
t = t.trim()
|
t = t.trim()
|
||||||
let hook = hooks[t]
|
let hook = hooks[t]
|
||||||
if (typeof hook === 'object') {
|
if (typeof hook === 'object') {
|
||||||
t = hook.type || t
|
type = hook.type || type
|
||||||
phase = hook.phase || phase
|
phase = hook.phase || !!phase
|
||||||
}
|
}
|
||||||
el.removeEventListener(t, fn, !!phase)
|
el.removeEventListener(t, fn, phase)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
/*读写删除元素节点的样式*/
|
/*读写删除元素节点的样式*/
|
||||||
|
@ -330,12 +349,13 @@ Anot.mix({
|
||||||
each: function(obj, fn) {
|
each: function(obj, fn) {
|
||||||
if (obj) {
|
if (obj) {
|
||||||
//排除null, undefined
|
//排除null, undefined
|
||||||
|
let i = 0
|
||||||
if (isArrayLike(obj)) {
|
if (isArrayLike(obj)) {
|
||||||
for (let i = 0, n = obj.length; i < n; i++) {
|
for (let n = obj.length; i < n; i++) {
|
||||||
if (fn(i, obj[i]) === false) break
|
if (fn(i, obj[i]) === false) break
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (let i in obj) {
|
for (i in obj) {
|
||||||
if (obj.hasOwnProperty(i) && fn(i, obj[i]) === false) {
|
if (obj.hasOwnProperty(i) && fn(i, obj[i]) === false) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -404,7 +424,7 @@ Anot.mix({
|
||||||
|
|
||||||
if ((this.type(val) == 'string' && val.trim() === '') || val === null) {
|
if ((this.type(val) == 'string' && val.trim() === '') || val === null) {
|
||||||
document.cookie =
|
document.cookie =
|
||||||
encode(key) +
|
encodeURIComponent(key) +
|
||||||
'=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=' +
|
'=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=' +
|
||||||
opt.domain +
|
opt.domain +
|
||||||
'; path=' +
|
'; path=' +
|
||||||
|
@ -428,9 +448,9 @@ Anot.mix({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
document.cookie =
|
document.cookie =
|
||||||
encode(key) +
|
encodeURIComponent(key) +
|
||||||
'=' +
|
'=' +
|
||||||
encode(val) +
|
encodeURIComponent(val) +
|
||||||
opt.expires +
|
opt.expires +
|
||||||
'; domain=' +
|
'; domain=' +
|
||||||
opt.domain +
|
opt.domain +
|
||||||
|
@ -444,11 +464,11 @@ Anot.mix({
|
||||||
return document.cookie
|
return document.cookie
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
decode(
|
decodeURIComponent(
|
||||||
document.cookie.replace(
|
document.cookie.replace(
|
||||||
new RegExp(
|
new RegExp(
|
||||||
'(?:(?:^|.*;)\\s*' +
|
'(?:(?:^|.*;)\\s*' +
|
||||||
encode(key).replace(/[\-\.\+\*]/g, '\\$&') +
|
encodeURIComponent(key).replace(/[\-\.\+\*]/g, '\\$&') +
|
||||||
'\\s*\\=\\s*([^;]*).*$)|^.*$'
|
'\\s*\\=\\s*([^;]*).*$)|^.*$'
|
||||||
),
|
),
|
||||||
'$1'
|
'$1'
|
||||||
|
@ -459,12 +479,13 @@ Anot.mix({
|
||||||
},
|
},
|
||||||
//获取url的参数
|
//获取url的参数
|
||||||
search: function(key) {
|
search: function(key) {
|
||||||
|
key += ''
|
||||||
let uri = location.search
|
let uri = location.search
|
||||||
|
|
||||||
if (!uri) {
|
if (!key || !uri) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
uri = decode(uri)
|
uri = decodeURIComponent(uri)
|
||||||
|
|
||||||
uri = uri.slice(1)
|
uri = uri.slice(1)
|
||||||
uri = uri.split('&')
|
uri = uri.split('&')
|
||||||
|
@ -485,19 +506,26 @@ Anot.mix({
|
||||||
obj[tmp[0]] = tmp[1]
|
obj[tmp[0]] = tmp[1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (key) {
|
return obj.hasOwnProperty(key) ? obj[key] : null
|
||||||
return obj.hasOwnProperty(key) ? obj[key] : null
|
|
||||||
} else {
|
|
||||||
return obj
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
//复制文本到粘贴板
|
//复制文本到粘贴板
|
||||||
copy: function(txt) {
|
copy: function(txt) {
|
||||||
|
if (!DOC.queryCommandSupported || !DOC.queryCommandSupported('copy')) {
|
||||||
|
return log('该浏览器不支持复制到粘贴板')
|
||||||
|
}
|
||||||
|
|
||||||
|
let ta = DOC.createElement('textarea')
|
||||||
|
ta.textContent = txt
|
||||||
|
ta.style.position = 'fixed'
|
||||||
|
ta.style.bottom = '-1000px'
|
||||||
|
DOC.body.appendChild(ta)
|
||||||
|
ta.select()
|
||||||
try {
|
try {
|
||||||
navigator.clipboard.writeText(txt)
|
DOC.execCommand('copy')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log('复制到粘贴板失败', err)
|
log('复制到粘贴板失败', err)
|
||||||
}
|
}
|
||||||
|
DOC.body.removeChild(ta)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,46 @@ Anot.contains = function(root, el) {
|
||||||
|
|
||||||
let eventHooks = Anot.eventHooks
|
let eventHooks = Anot.eventHooks
|
||||||
|
|
||||||
|
//针对firefox, chrome修正mouseenter, mouseleave(chrome30+)
|
||||||
|
if (!('onmouseenter' in root)) {
|
||||||
|
Anot.each(
|
||||||
|
{
|
||||||
|
mouseenter: 'mouseover',
|
||||||
|
mouseleave: 'mouseout'
|
||||||
|
},
|
||||||
|
function(origType, fixType) {
|
||||||
|
eventHooks[origType] = {
|
||||||
|
type: fixType,
|
||||||
|
fix: function(elem, fn) {
|
||||||
|
return function(e) {
|
||||||
|
let t = e.relatedTarget
|
||||||
|
if (!t || (t !== elem && !(elem.compareDocumentPosition(t) & 16))) {
|
||||||
|
delete e.type
|
||||||
|
e.type = origType
|
||||||
|
return fn.call(elem, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
//针对IE9+, w3c修正animationend
|
||||||
|
Anot.each(
|
||||||
|
{
|
||||||
|
AnimationEvent: 'animationend',
|
||||||
|
WebKitAnimationEvent: 'webkitAnimationEnd'
|
||||||
|
},
|
||||||
|
function(construct, fixType) {
|
||||||
|
if (window[construct] && !eventHooks.animationend) {
|
||||||
|
eventHooks.animationend = {
|
||||||
|
type: fixType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
if (DOC.onmousewheel === void 0) {
|
if (DOC.onmousewheel === void 0) {
|
||||||
/* IE6-11 chrome mousewheel wheelDetla 下 -120 上 120
|
/* IE6-11 chrome mousewheel wheelDetla 下 -120 上 120
|
||||||
firefox DOMMouseScroll detail 下3 上-3
|
firefox DOMMouseScroll detail 下3 上-3
|
||||||
|
|
|
@ -264,8 +264,8 @@ function observeObject(source, options) {
|
||||||
return (old = value.get.call(this))
|
return (old = value.get.call(this))
|
||||||
},
|
},
|
||||||
set: function(x) {
|
set: function(x) {
|
||||||
var older = old
|
var older = old,
|
||||||
var newer
|
newer
|
||||||
value.set.call(this, x)
|
value.set.call(this, x)
|
||||||
newer = this[key]
|
newer = this[key]
|
||||||
if (this.$fire && newer !== older) {
|
if (this.$fire && newer !== older) {
|
||||||
|
|
|
@ -50,13 +50,13 @@ function executeBindings(bindings, vmodels) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var roneTime = /^\s*::/
|
var roneTime = /^\s*::/
|
||||||
var rmsAttr = /^:(\w+)-?(.*)|@(.*)/
|
var rmsAttr = /:(\w+)-?(.*)|@(.*)/
|
||||||
|
|
||||||
var events = oneObject(
|
var events = oneObject(
|
||||||
'animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit'
|
'animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit'
|
||||||
)
|
)
|
||||||
var obsoleteAttrs = oneObject(
|
var obsoleteAttrs = oneObject(
|
||||||
'value,title,alt,checked,selected,disabled,readonly,loading,enabled,href,src'
|
'value,title,alt,checked,selected,disabled,readonly,enabled,href,src'
|
||||||
)
|
)
|
||||||
function bindingSorter(a, b) {
|
function bindingSorter(a, b) {
|
||||||
return a.priority - b.priority
|
return a.priority - b.priority
|
||||||
|
@ -85,12 +85,8 @@ function scanAttr(elem, vmodels, match) {
|
||||||
var param = match[2] || ''
|
var param = match[2] || ''
|
||||||
var eparam = match[3] || '' // 事件绑定的简写
|
var eparam = match[3] || '' // 事件绑定的简写
|
||||||
var value = attr.value
|
var value = attr.value
|
||||||
if (obsoleteAttrs[type]) {
|
if (events[type] || events[eparam]) {
|
||||||
param = type
|
param = type || eparam
|
||||||
type = 'attr'
|
|
||||||
}
|
|
||||||
if (eparam) {
|
|
||||||
param = eparam
|
|
||||||
type = 'on'
|
type = 'on'
|
||||||
}
|
}
|
||||||
if (directives[type]) {
|
if (directives[type]) {
|
||||||
|
@ -372,8 +368,6 @@ function scanText(textNode, vmodels, index) {
|
||||||
anotFragment.appendChild(node)
|
anotFragment.appendChild(node)
|
||||||
}
|
}
|
||||||
textNode.parentNode.replaceChild(anotFragment, textNode)
|
textNode.parentNode.replaceChild(anotFragment, textNode)
|
||||||
if (bindings.length) {
|
if (bindings.length) executeBindings(bindings, vmodels)
|
||||||
executeBindings(bindings, vmodels)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,8 +47,7 @@ var attrDir = Anot.directive('attr', {
|
||||||
},
|
},
|
||||||
update: function(val) {
|
update: function(val) {
|
||||||
var elem = this.element
|
var elem = this.element
|
||||||
var obj = Object.create(null)
|
var obj = {}
|
||||||
var isSVG = rsvg.test(elem)
|
|
||||||
|
|
||||||
val = toJson(val)
|
val = toJson(val)
|
||||||
|
|
||||||
|
@ -84,47 +83,36 @@ var attrDir = Anot.directive('attr', {
|
||||||
elem.style.cssText = obj[i]
|
elem.style.cssText = obj[i]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if (i === 'href' || i === 'src') {
|
||||||
if (i.slice(0, 6) === 'xlink:') {
|
|
||||||
var k = i
|
|
||||||
i = i.slice(6)
|
|
||||||
obj[i] = obj[k]
|
|
||||||
delete obj[k]
|
|
||||||
}
|
|
||||||
// 修正这些值的显示
|
|
||||||
if (obj[i] === false || obj[i] === null || obj[i] === undefined) {
|
|
||||||
obj[i] = ''
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
typeof elem[i] === 'boolean' ||
|
|
||||||
typeof elem[boolMap[i]] === 'boolean'
|
|
||||||
) {
|
|
||||||
var k = i
|
|
||||||
if (boolMap[i] && k !== boolMap[i]) {
|
|
||||||
k = boolMap[i]
|
|
||||||
}
|
|
||||||
//布尔属性必须使用el.xxx = true|false方式设值
|
|
||||||
obj[i] = !!obj[i]
|
|
||||||
elem[k] = obj[i]
|
|
||||||
|
|
||||||
if (!obj[i]) {
|
|
||||||
elem.removeAttribute(k)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//SVG只能使用setAttribute(xxx, yyy), HTML的固有属性必须elem.xxx = yyy
|
|
||||||
var isInnate = isSVG ? false : i in elem.cloneNode(false)
|
|
||||||
if (isInnate) {
|
|
||||||
elem[i] = obj[i]
|
elem[i] = obj[i]
|
||||||
} else {
|
} else {
|
||||||
if (typeof obj[i] === 'object') {
|
if (obj[i] === false || obj[i] === null || obj[i] === undefined) {
|
||||||
obj[i] = Date.isDate(obj[i])
|
obj[i] = ''
|
||||||
? obj[i].toISOString()
|
}
|
||||||
: JSON.stringify(obj[i])
|
|
||||||
|
if (typeof elem[boolMap[i]] === 'boolean') {
|
||||||
|
//布尔属性必须使用el.xxx = true|false方式设值
|
||||||
|
obj[i] = !!obj[i]
|
||||||
|
elem[boolMap[i]] = obj[i]
|
||||||
|
|
||||||
|
if (!obj[i]) {
|
||||||
|
elem.removeAttribute(boolMap[i])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//SVG只能使用setAttribute(xxx, yyy), VML只能使用elem.xxx = yyy ,HTML的固有属性必须elem.xxx = yyy
|
||||||
|
var isInnate = rsvg.test(elem) ? false : i in elem.cloneNode(false)
|
||||||
|
if (isInnate) {
|
||||||
|
elem[i] = obj[i]
|
||||||
|
} else {
|
||||||
|
if (typeof obj[i] === 'object') {
|
||||||
|
obj[i] = Date.isDate(obj[i])
|
||||||
|
? obj[i].toISOString()
|
||||||
|
: JSON.stringify(obj[i])
|
||||||
|
}
|
||||||
|
elem.setAttribute(i, obj[i])
|
||||||
}
|
}
|
||||||
elem.setAttribute(i, obj[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,26 +20,6 @@ function nodesToFrag(nodes) {
|
||||||
}
|
}
|
||||||
return frag
|
return frag
|
||||||
}
|
}
|
||||||
|
|
||||||
function _fetch(url) {
|
|
||||||
var xhr = new XMLHttpRequest()
|
|
||||||
var defer = Promise.defer()
|
|
||||||
xhr.open('GET', url, true)
|
|
||||||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
|
|
||||||
xhr.responseType = 'text'
|
|
||||||
xhr.onreadystatechange = function() {
|
|
||||||
if (this.readyState === 4) {
|
|
||||||
if (this.status >= 200 && this.status < 400) {
|
|
||||||
defer.resolve(this.response)
|
|
||||||
} else {
|
|
||||||
defer.reject(this)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
xhr.send(null)
|
|
||||||
return defer.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
Anot.directive('include', {
|
Anot.directive('include', {
|
||||||
init: directives.attr.init,
|
init: directives.attr.init,
|
||||||
update: function(val) {
|
update: function(val) {
|
||||||
|
@ -161,17 +141,27 @@ Anot.directive('include', {
|
||||||
scanTemplate(templatePool[val])
|
scanTemplate(templatePool[val])
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
_fetch(val)
|
fetch(val, {
|
||||||
|
method: 'get',
|
||||||
|
headers: {
|
||||||
|
'X-Requested-With': 'XMLHttpRequest'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
if (res.status >= 200 && res.status < 300) {
|
||||||
|
return res.text()
|
||||||
|
} else {
|
||||||
|
return Promise.reject(
|
||||||
|
`获取网络资源出错, ${res.status} (${res.statusText})`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(text => {
|
.then(text => {
|
||||||
templatePool[val] = text
|
templatePool[val] = text
|
||||||
scanTemplate(text)
|
scanTemplate(text)
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
log(
|
log(':include load [' + val + '] error\n%c%s', 'color:#f30', err)
|
||||||
':include load [' + val + '] error\n%c%s',
|
|
||||||
'color:#f30',
|
|
||||||
`获取网络资源出错, ${err.status} (${err.statusText})`
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue