This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0
master
宇天 2019-01-28 15:22:05 +08:00
parent 49b7a26e62
commit 76ced919e0
6 changed files with 120 additions and 448 deletions

View File

@ -1,6 +1,7 @@
## Anot.js ## Anot.js 2.x
> Anot 是Anot not only templateEngine的缩写。 它是一款迷你,易用、高性能的前端MVVM框架, fork于avalon。进行了大量的重构,精简部分冗余的API, 同时针对组件拓展进行了优化。 > Anot 是Anot not only templateEngine的缩写。 它是一款迷你,易用、高性能的前端MVVM框架, fork于avalon。进行了大量的重构,精简部分冗余的API, 同时针对组件拓展进行了优化。
>> 2.x版本为 全新版本, 只兼容支持type="module"的浏览器。
```bash ```bash
# 开发模式 # 开发模式
@ -12,13 +13,9 @@ npm start
# 打包 # 打包
npm run prod npm run prod
``` ```
执行完, 会打包为6个版本, 分别是 执行完, 会打包为2个版本, 分别是
- anot.js 普通版(可用于webpack) - anot.js 普通版(需要支持es6 module的现代浏览器)
- anot-touch.js 普通带触摸版(可用于webpack) - anot-touch.js 带触摸的版本(需要支持es6 module的现代浏览器)
- anot.shim.js 自带AMD加载版
- anot-touch.shim.js 自带AMD加载带触摸版
- anot.next.js 未来版(需要支持es6 module的现代浏览器)
- anot-touch.next.js 带触摸的未来版(需要支持es6 module的现代浏览器)
### 文档: ### 文档:

View File

@ -1,20 +1,20 @@
/********************************************************************* /*********************************************************************
* 全局变量及方法 * * 全局变量及方法 *
**********************************************************************/ **********************************************************************/
var bindingID = 1024 let bindingID = 1024
var IEVersion = 0 let IEVersion = 0
if (window.VBArray) { if (window.VBArray) {
IEVersion = document.documentMode || (window.XMLHttpRequest ? 7 : 6) IEVersion = document.documentMode || (window.XMLHttpRequest ? 7 : 6)
} }
var expose = generateID() let expose = generateID()
//http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function //http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function
var DOC = window.document let DOC = window.document
var head = DOC.head //HEAD元素 let head = DOC.head //HEAD元素
head.insertAdjacentHTML( head.insertAdjacentHTML(
'afterBegin', 'afterBegin',
'<anot skip class="anot-hide"><style id="anot-style">.anot-hide{ display: none!important } slot{visibility:hidden;}</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 let ifGroup = head.firstChild
function log() { function log() {
// http://stackoverflow.com/questions/8785624/how-to-safely-wrap-console-log // http://stackoverflow.com/questions/8785624/how-to-safely-wrap-console-log
@ -34,22 +34,22 @@ function createMap() {
return Object.create(null) return Object.create(null)
} }
var subscribers = '$' + expose let subscribers = '$' + expose
var nullObject = {} //作用类似于noop只用于代码防御千万不要在它上面添加属性 let nullObject = {} //作用类似于noop只用于代码防御千万不要在它上面添加属性
var rword = /[^, ]+/g //切割字符串为一个个小块以空格或豆号分开它们结合replace实现字符串的forEach let rword = /[^, ]+/g //切割字符串为一个个小块以空格或豆号分开它们结合replace实现字符串的forEach
var rw20g = /\w+/g let rw20g = /\w+/g
var rsvg = /^\[object SVG\w*Element\]$/ let rsvg = /^\[object SVG\w*Element\]$/
var oproto = Object.prototype let oproto = Object.prototype
var ohasOwn = oproto.hasOwnProperty let ohasOwn = oproto.hasOwnProperty
var serialize = oproto.toString let serialize = oproto.toString
var ap = Array.prototype let ap = Array.prototype
var aslice = ap.slice let aslice = ap.slice
var W3C = window.dispatchEvent let W3C = window.dispatchEvent
var root = DOC.documentElement let root = DOC.documentElement
var anotFragment = DOC.createDocumentFragment() let anotFragment = DOC.createDocumentFragment()
var cinerator = DOC.createElement('div') let cinerator = DOC.createElement('div')
var class2type = { let class2type = {
'[object Boolean]': 'boolean', '[object Boolean]': 'boolean',
'[object Number]': 'number', '[object Number]': 'number',
'[object String]': 'string', '[object String]': 'string',
@ -74,9 +74,9 @@ function oneObject(array, val) {
if (typeof array === 'string') { if (typeof array === 'string') {
array = array.match(rword) || [] array = array.match(rword) || []
} }
var result = {}, let result = {},
value = val !== void 0 ? val : 1 value = val !== void 0 ? val : 1
for (var i = 0, n = array.length; i < n; i++) { for (let i = 0, n = array.length; i < n; i++) {
result[array[i]] = value result[array[i]] = value
} }
return result return result

View File

@ -3,352 +3,24 @@
// =============================== // ===============================
// ========== Promise ============ // ========== Promise ============
// =============================== // ===============================
;(function(nativePromise) {
function _yes(val) {
return val
}
function _no(err) { if (!Promise.defer) {
throw err Promise.defer = function() {
} let obj = {}
obj.promise = new Promise((resolve, reject) => {
function done(callback) { obj.resolve = resolve
return this.then(callback, _no) obj.reject = reject
}
function fail(callback) {
return this.then(_yes, callback)
}
function defer() {
var obj = {}
obj.promise = new _Promise(function(yes, no) {
obj.resolve = yes
obj.reject = no
}) })
return obj return obj
} }
//成功的回调
function _resolve(obj, val) {
if (obj._state !== 'pending') {
return
}
if (val && typeof val.then === 'function') {
var method = val instanceof _Promise ? '_then' : 'then'
val[method](
function(v) {
_transmit(obj, v, true)
},
function(v) {
_transmit(obj, v, false)
}
)
} else {
_transmit(obj, val, true)
}
}
//失败的回调
function _reject(obj, val) {
if (obj._state !== 'pending') {
return
}
_transmit(obj, val, false)
}
// 改变Promise的_fired值并保持用户传参触发所有回调
function _transmit(obj, val, isResolved) {
obj._fired = true
obj._val = val
obj._state = isResolved ? 'fulfilled' : 'rejected'
fireCallback(obj, function() {
for (var i in obj.callback) {
obj._fire(obj.callback[i].yes, obj.callback[i].no)
}
})
}
function fireCallback(obj, callback) {
var isAsync = false
if (typeof obj.async === 'boolean') {
isAsync = obj.async
} else {
isAsync = obj.async = true
}
if (isAsync) {
setTimeout(callback, 0)
} else {
callback()
}
}
function _some(bool, iterable) {
iterable = Array.isArray(iterable) ? iterable : []
var n = 0
var res = []
var end = false
return new _Promise(function(yes, no) {
if (!iterable.length) no(res)
function loop(obj, idx) {
obj.then(
function(val) {
if (!end) {
res[idx] = val
n++
if (bool || n >= iterable.length) {
yes(bool ? val : res)
end = true
}
}
},
function(val) {
end = true
no(val)
}
)
}
for (var i = 0, len = iterable.length; i < len; i++) {
loop(iterable[i], i)
}
})
}
//---------------------------
var _Promise = function(callback) {
this.callback = []
var _this = this
if (typeof this !== 'object') {
throw new TypeError('Promises must be constructed via new')
}
if (typeof callback !== 'function') {
throw new TypeError('Argument must be a function')
}
callback(
function(val) {
_resolve(_this, val)
},
function(val) {
_reject(_this, val)
}
)
}
var self = {
_state: 1,
_fired: 1,
_val: 1,
callback: 1
}
_Promise.prototype = {
constructor: _Promise,
_state: 'pending',
_fired: false,
_fire: function(yes, no) {
if (this._state === 'rejected') {
if (typeof no === 'function') no(this._val)
else throw this._val
} else {
if (typeof yes === 'function') yes(this._val)
}
},
_then: function(yes, no) {
if (this._fired) {
var _this = this
fireCallback(_this, function() {
_this._fire(yes, no)
})
} else {
this.callback.push({ yes: yes, no: no })
}
},
then: function(yes, no) {
yes = typeof yes === 'function' ? yes : _yes
no = typeof no === 'function' ? no : _no
var _this = this
var next = new _Promise(function(resolve, reject) {
_this._then(
function(val) {
try {
val = yes(val)
} catch (err) {
return reject(err)
}
resolve(val)
},
function(val) {
try {
val = no(val)
} catch (err) {
return reject(err)
}
resolve(val)
}
)
})
for (var i in _this) {
if (!self[i]) next[i] = _this[i]
}
return next
},
done: done,
catch: fail,
fail: fail
}
_Promise.all = function(arr) {
return _some(false, arr)
}
_Promise.race = function(arr) {
return _some(true, arr)
}
_Promise.defer = defer
_Promise.resolve = function(val) {
var obj = this.defer()
obj.resolve(val)
return obj.promise
}
_Promise.reject = function(val) {
var obj = this.defer()
obj.reject(val)
return obj.promise
}
if (/native code/.test(nativePromise)) {
nativePromise.prototype.done = done
nativePromise.prototype.fail = fail
if (!nativePromise.defer) {
nativePromise.defer = defer
}
}
window.Promise = nativePromise || _Promise
})(window.Promise)
if (!Object.assign) {
Object.defineProperty(Object, 'assign', {
enumerable: false,
value: function(target, first) {
'use strict'
if (target === undefined || target === null)
throw new TypeError('Can not convert first argument to object')
var to = Object(target)
for (var i = 0, len = arguments.length; i < len; i++) {
var next = arguments[i]
if (next === undefined || next === null) continue
var keys = Object.keys(Object(next))
for (var j = 0, n = keys.length; j < n; j++) {
var key = keys[j]
var desc = Object.getOwnPropertyDescriptor(next, key)
if (desc !== undefined && desc.enumerable) to[key] = next[key]
}
}
return to
}
})
}
if (!Array.from) {
Object.defineProperty(Array, 'from', {
enumerable: false,
value: (function() {
var toStr = Object.prototype.toString
var isCallable = function(fn) {
return (
typeof fn === 'function' || toStr.call(fn) === '[object Function]'
)
}
var toInt = function(val) {
var num = val - 0
if (isNaN(num)) return 0
if (num === 0 || isFinite(num)) return num
return (num > 0 ? 1 : -1) * Math.floor(Math.abs(num))
}
var maxInt = Math.pow(2, 53) - 1
var toLen = function(val) {
var len = toInt(val)
return Math.min(Math.max(len, 0), maxInt)
}
return function(arrLike) {
var _this = this
var items = Object(arrLike)
if (arrLike === null)
throw new TypeError(
'Array.from requires an array-like object - not null or undefined'
)
var mapFn = arguments.length > 1 ? arguments[1] : undefined
var other
if (mapFn !== undefined) {
if (!isCallable(mapFn))
throw new TypeError(
'Array.from: when provided, the second argument must be a function'
)
if (arguments.length > 2) other = arguments[2]
}
var len = toLen(items.length)
var arr = isCallable(_this) ? Object(new _this(len)) : new Array(len)
var k = 0
var kVal
while (k < len) {
kVal = items[k]
if (mapFn)
arr[k] =
other === 'undefined'
? mapFn(kVal, k)
: mapFn.call(other, kVal, k)
else arr[k] = kVal
k++
}
arr.length = len
return arr
}
})()
})
}
// 判断数组是否包含指定元素
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(val) {
for (var i in this) {
if (this[i] === val) return true
}
return false
},
enumerable: false
})
} }
//类似于Array 的splice方法 //类似于Array 的splice方法
if (!String.prototype.splice) { if (!String.prototype.splice) {
Object.defineProperty(String.prototype, 'splice', { Object.defineProperty(String.prototype, 'splice', {
value: function(start, len, fill) { value: function(start, len, fill) {
var length = this.length, let length = this.length
argLen = arguments.length let argLen = arguments.length
fill = fill === undefined ? '' : fill fill = fill === undefined ? '' : fill
@ -358,8 +30,11 @@ if (!String.prototype.splice) {
//处理负数 //处理负数
if (start < 0) { if (start < 0) {
if (Math.abs(start) >= length) start = 0 if (Math.abs(start) >= length) {
else start = length + start start = 0
} else {
start = length + start
}
} }
if (argLen === 1) { if (argLen === 1) {
@ -367,8 +42,8 @@ if (!String.prototype.splice) {
} else { } else {
len -= 0 len -= 0
var strl = this.slice(0, start), let strl = this.slice(0, start)
strr = this.slice(start + len) let strr = this.slice(start + len)
return strl + fill + strr return strl + fill + strr
} }
@ -381,10 +56,10 @@ if (!Date.prototype.getFullWeek) {
//获取当天是本年度第几周 //获取当天是本年度第几周
Object.defineProperty(Date.prototype, 'getFullWeek', { Object.defineProperty(Date.prototype, 'getFullWeek', {
value: function() { value: function() {
var thisYear = this.getFullYear(), let thisYear = this.getFullYear()
that = new Date(thisYear, 0, 1), let that = new Date(thisYear, 0, 1)
firstDay = that.getDay() || 1, let firstDay = that.getDay() || 1
numsOfToday = (this - that) / 86400000 let numsOfToday = (this - that) / 86400000
return Math.ceil((numsOfToday + firstDay) / 7) return Math.ceil((numsOfToday + firstDay) / 7)
}, },
enumerable: false enumerable: false
@ -393,10 +68,10 @@ if (!Date.prototype.getFullWeek) {
//获取当天是本月第几周 //获取当天是本月第几周
Object.defineProperty(Date.prototype, 'getWeek', { Object.defineProperty(Date.prototype, 'getWeek', {
value: function() { value: function() {
var today = this.getDate(), let today = this.getDate()
thisMonth = this.getMonth(), let thisMonth = this.getMonth()
thisYear = this.getFullYear(), let thisYear = this.getFullYear()
firstDay = new Date(thisYear, thisMonth, 1).getDay() let firstDay = new Date(thisYear, thisMonth, 1).getDay()
return Math.ceil((today + firstDay) / 7) return Math.ceil((today + firstDay) / 7)
}, },
enumerable: false enumerable: false
@ -417,8 +92,8 @@ if (!Date.prototype.format) {
Object.defineProperty(Date.prototype, 'format', { Object.defineProperty(Date.prototype, 'format', {
value: function(str) { value: function(str) {
str = str || 'Y-m-d H:i:s' str = str || 'Y-m-d H:i:s'
var week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], let week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
dt = { let dt = {
fullyear: this.getFullYear(), fullyear: this.getFullYear(),
year: this.getYear(), year: this.getYear(),
fullweek: this.getFullWeek(), fullweek: this.getFullWeek(),
@ -429,8 +104,8 @@ if (!Date.prototype.format) {
hours: this.getHours(), hours: this.getHours(),
minutes: this.getMinutes(), minutes: this.getMinutes(),
seconds: this.getSeconds() seconds: this.getSeconds()
}, }
re let re
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours
@ -452,7 +127,7 @@ if (!Date.prototype.format) {
D: dt.day D: dt.day
} }
for (var i in re) { for (let i in re) {
str = str.replace(new RegExp(i, 'g'), re[i]) str = str.replace(new RegExp(i, 'g'), re[i])
} }
return str return str

View File

@ -1,4 +1,4 @@
var Anot = function(el) { let Anot = function(el) {
//创建jQuery式的无new 实例化结构 //创建jQuery式的无new 实例化结构
return new Anot.init(el) return new Anot.init(el)
} }
@ -6,25 +6,25 @@ var Anot = function(el) {
/*视浏览器情况采用最快的异步回调*/ /*视浏览器情况采用最快的异步回调*/
Anot.nextTick = new function() { Anot.nextTick = new function() {
// jshint ignore:line // jshint ignore:line
var tickImmediate = window.setImmediate let tickImmediate = window.setImmediate
var tickObserver = window.MutationObserver let tickObserver = window.MutationObserver
if (tickImmediate) { if (tickImmediate) {
return tickImmediate.bind(window) return tickImmediate.bind(window)
} }
var queue = [] let queue = []
function callback() { function callback() {
var n = queue.length let n = queue.length
for (var i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
queue[i]() queue[i]()
} }
queue = queue.slice(n) queue = queue.slice(n)
} }
if (tickObserver) { if (tickObserver) {
var node = document.createTextNode('anot') let node = document.createTextNode('anot')
new tickObserver(callback).observe(node, { characterData: true }) // jshint ignore:line new tickObserver(callback).observe(node, { characterData: true }) // jshint ignore:line
var bool = false let bool = false
return function(fn) { return function(fn) {
queue.push(fn) queue.push(fn)
bool = !bool bool = !bool
@ -103,11 +103,11 @@ Anot.isPlainObject = function(obj) {
) )
} }
var VMODELS = (Anot.vmodels = {}) //所有vmodel都储存在这里 let VMODELS = (Anot.vmodels = {}) //所有vmodel都储存在这里
Anot.init = function(source) { Anot.init = function(source) {
if (Anot.isPlainObject(source)) { if (Anot.isPlainObject(source)) {
var $id = source.$id let $id = source.$id
var vm = null let vm = null
if (!$id) { if (!$id) {
log('warning: vm必须指定id') log('warning: vm必须指定id')
} }
@ -116,12 +116,12 @@ Anot.init = function(source) {
VMODELS[$id] = vm VMODELS[$id] = vm
Anot.nextTick(function() { Anot.nextTick(function() {
var $elem = document.querySelector('[anot=' + vm.$id + ']') let $elem = document.querySelector('[anot=' + vm.$id + ']')
if ($elem) { if ($elem) {
if ($elem === DOC.body) { if ($elem === DOC.body) {
scanTag($elem, []) scanTag($elem, [])
} else { } else {
var _parent = $elem let _parent = $elem
while ((_parent = _parent.parentNode)) { while ((_parent = _parent.parentNode)) {
if (_parent.__VM__) { if (_parent.__VM__) {
break break
@ -141,7 +141,7 @@ Anot.fn = Anot.prototype = Anot.init.prototype
//与jQuery.extend方法可用于浅拷贝深拷贝 //与jQuery.extend方法可用于浅拷贝深拷贝
Anot.mix = Anot.fn.mix = function() { Anot.mix = Anot.fn.mix = function() {
var options, let options,
name, name,
src, src,
copy, copy,
@ -208,7 +208,7 @@ function cacheStore(tpye, key, val) {
} }
if (this.type(key) === 'object') { if (this.type(key) === 'object') {
for (var i in key) { for (let i in key) {
window[tpye].setItem(i, key[i]) window[tpye].setItem(i, key[i])
} }
return return
@ -233,7 +233,7 @@ function cacheStore(tpye, key, val) {
/*判定是否类数组如节点集合纯数组arguments与拥有非负整数的length属性的纯JS对象*/ /*判定是否类数组如节点集合纯数组arguments与拥有非负整数的length属性的纯JS对象*/
function isArrayLike(obj) { function isArrayLike(obj) {
if (obj && typeof obj === 'object') { if (obj && typeof obj === 'object') {
var n = obj.length, let n = obj.length,
str = serialize.call(obj) str = serialize.call(obj)
if (/(Array|List|Collection|Map|Arguments)\]$/.test(str)) { if (/(Array|List|Collection|Map|Arguments)\]$/.test(str)) {
return true return true
@ -275,7 +275,7 @@ Anot.mix({
end = start || 0 end = start || 0
start = 0 start = 0
} }
var index = -1, let index = -1,
length = Math.max(0, Math.ceil((end - start) / step)), length = Math.max(0, Math.ceil((end - start) / step)),
result = new Array(length) result = new Array(length)
while (++index < length) { while (++index < length) {
@ -288,11 +288,11 @@ Anot.mix({
eventHooks: {}, eventHooks: {},
/*绑定事件*/ /*绑定事件*/
bind: function(el, type, fn, phase) { bind: function(el, type, fn, phase) {
var hooks = Anot.eventHooks let hooks = Anot.eventHooks
type = type.split(',') type = type.split(',')
Anot.each(type, function(i, t) { Anot.each(type, function(i, t) {
t = t.trim() t = t.trim()
var hook = hooks[t] let hook = hooks[t]
if (typeof hook === 'object') { if (typeof hook === 'object') {
type = hook.type || type type = hook.type || type
phase = hook.phase || !!phase phase = hook.phase || !!phase
@ -304,12 +304,12 @@ Anot.mix({
}, },
/*卸载事件*/ /*卸载事件*/
unbind: function(el, type, fn, phase) { unbind: function(el, type, fn, phase) {
var hooks = Anot.eventHooks let hooks = Anot.eventHooks
type = type.split(',') type = type.split(',')
fn = fn || noop fn = fn || noop
Anot.each(type, function(i, t) { Anot.each(type, function(i, t) {
t = t.trim() t = t.trim()
var hook = hooks[t] let hook = hooks[t]
if (typeof hook === 'object') { if (typeof hook === 'object') {
type = hook.type || type type = hook.type || type
phase = hook.phase || !!phase phase = hook.phase || !!phase
@ -322,7 +322,7 @@ Anot.mix({
if (node instanceof Anot) { if (node instanceof Anot) {
node = node[0] node = node[0]
} }
var prop = /[_-]/.test(name) ? camelize(name) : name, let prop = /[_-]/.test(name) ? camelize(name) : name,
fn fn
name = Anot.cssName(prop) || prop name = Anot.cssName(prop) || prop
if (value === void 0 || typeof value === 'boolean') { if (value === void 0 || typeof value === 'boolean') {
@ -331,7 +331,7 @@ Anot.mix({
if (name === 'background') { if (name === 'background') {
name = 'backgroundColor' name = 'backgroundColor'
} }
var val = fn(node, name) let val = fn(node, name)
return value === true ? parseFloat(val) || 0 : val return value === true ? parseFloat(val) || 0 : val
} else if (value === '') { } else if (value === '') {
//请除样式 //请除样式
@ -352,9 +352,9 @@ Anot.mix({
each: function(obj, fn) { each: function(obj, fn) {
if (obj) { if (obj) {
//排除null, undefined //排除null, undefined
var i = 0 let i = 0
if (isArrayLike(obj)) { if (isArrayLike(obj)) {
for (var 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 {
@ -379,7 +379,7 @@ Anot.mix({
}, },
/*移除数组中第一个匹配传参的那个元素,返回布尔表示成功与否*/ /*移除数组中第一个匹配传参的那个元素,返回布尔表示成功与否*/
remove: function(target, item) { remove: function(target, item) {
var index = target.indexOf(item) let index = target.indexOf(item)
if (~index) return Anot.Array.removeAt(target, index) if (~index) return Anot.Array.removeAt(target, index)
return false return false
} }
@ -391,12 +391,12 @@ Anot.mix({
* @return * @return
*/ */
ls: function() { ls: function() {
var args = aslice.call(arguments, 0) let args = aslice.call(arguments, 0)
args.unshift('localStorage') args.unshift('localStorage')
return cacheStore.apply(this, args) return cacheStore.apply(this, args)
}, },
ss: function() { ss: function() {
var args = aslice.call(arguments, 0) let args = aslice.call(arguments, 0)
args.unshift('sessionStorage') args.unshift('sessionStorage')
return cacheStore.apply(this, args) return cacheStore.apply(this, args)
}, },
@ -483,16 +483,16 @@ Anot.mix({
//获取url的参数 //获取url的参数
search: function(key) { search: function(key) {
key += '' key += ''
var uri = location.search let uri = location.search
if (!key || !uri) return null if (!key || !uri) return null
uri = uri.slice(1) uri = uri.slice(1)
uri = uri.split('&') uri = uri.split('&')
var obj = {} let obj = {}
for (var i = 0, item; (item = uri[i++]); ) { for (let i = 0, item; (item = uri[i++]); ) {
var tmp = item.split('=') let tmp = item.split('=')
tmp[1] = tmp.length < 2 ? null : tmp[1] tmp[1] = tmp.length < 2 ? null : tmp[1]
tmp[1] = decodeURIComponent(tmp[1]) tmp[1] = decodeURIComponent(tmp[1])
if (obj.hasOwnProperty(tmp[0])) { if (obj.hasOwnProperty(tmp[0])) {
@ -514,7 +514,7 @@ Anot.mix({
return log('该浏览器不支持复制到粘贴板') return log('该浏览器不支持复制到粘贴板')
} }
var ta = DOC.createElement('textarea') let ta = DOC.createElement('textarea')
ta.textContent = txt ta.textContent = txt
ta.style.position = 'fixed' ta.style.position = 'fixed'
ta.style.bottom = '-1000px' ta.style.bottom = '-1000px'
@ -529,10 +529,10 @@ Anot.mix({
} }
}) })
var bindingHandlers = (Anot.bindingHandlers = {}) let bindingHandlers = (Anot.bindingHandlers = {})
var bindingExecutors = (Anot.bindingExecutors = {}) let bindingExecutors = (Anot.bindingExecutors = {})
var directives = (Anot.directives = {}) let directives = (Anot.directives = {})
Anot.directive = function(name, obj) { Anot.directive = function(name, obj) {
bindingHandlers[name] = obj.init = obj.init || noop bindingHandlers[name] = obj.init = obj.init || noop
bindingExecutors[name] = obj.update = obj.update || noop bindingExecutors[name] = obj.update = obj.update || noop

View File

@ -1,5 +1,5 @@
// https://github.com/rsms/js-lru // https://github.com/rsms/js-lru
var Cache = new function() { let Cache = new function() {
// jshint ignore:line // jshint ignore:line
function LRU(maxLength) { function LRU(maxLength) {
this.size = 0 this.size = 0
@ -8,10 +8,10 @@ var Cache = new function() {
this._keymap = {} this._keymap = {}
} }
var p = LRU.prototype let p = LRU.prototype
p.put = function(key, value) { p.put = function(key, value) {
var entry = { let entry = {
key: key, key: key,
value: value value: value
} }
@ -32,7 +32,7 @@ var Cache = new function() {
} }
p.shift = function() { p.shift = function() {
var entry = this.head let entry = this.head
if (entry) { if (entry) {
this.head = this.head.newer this.head = this.head.newer
this.head.older = entry.newer = entry.older = this._keymap[ this.head.older = entry.newer = entry.older = this._keymap[
@ -42,7 +42,7 @@ var Cache = new function() {
} }
} }
p.get = function(key) { p.get = function(key) {
var entry = this._keymap[key] let entry = this._keymap[key]
if (entry === void 0) return if (entry === void 0) return
if (entry === this.tail) { if (entry === this.tail) {
return entry.value return entry.value

View File

@ -18,18 +18,18 @@ Anot.contains = function(root, el) {
} }
if (window.SVGElement) { if (window.SVGElement) {
var svgns = 'http://www.w3.org/2000/svg' let svgns = 'http://www.w3.org/2000/svg'
var svg = DOC.createElementNS(svgns, 'svg') let svg = DOC.createElementNS(svgns, 'svg')
svg.innerHTML = '<circle cx="50" cy="50" r="40" fill="red" />' svg.innerHTML = '<circle cx="50" cy="50" r="40" fill="red" />'
if (!rsvg.test(svg.firstChild)) { if (!rsvg.test(svg.firstChild)) {
// #409 // #409
/* jshint ignore:start */ /* jshint ignore:start */
function enumerateNode(node, targetNode) { function enumerateNode(node, targetNode) {
if (node && node.childNodes) { if (node && node.childNodes) {
var nodes = node.childNodes let nodes = node.childNodes
for (var i = 0, el; (el = nodes[i++]); ) { for (let i = 0, el; (el = nodes[i++]); ) {
if (el.tagName) { if (el.tagName) {
var svg = DOC.createElementNS(svgns, el.tagName.toLowerCase()) let svg = DOC.createElementNS(svgns, el.tagName.toLowerCase())
// copy attrs // copy attrs
ap.forEach.call(el.attributes, function(attr) { ap.forEach.call(el.attributes, function(attr) {
svg.setAttribute(attr.name, attr.value) svg.setAttribute(attr.name, attr.value)
@ -51,7 +51,7 @@ if (window.SVGElement) {
return new XMLSerializer().serializeToString(this) return new XMLSerializer().serializeToString(this)
}, },
set: function(html) { set: function(html) {
var tagName = this.tagName.toLowerCase(), let tagName = this.tagName.toLowerCase(),
par = this.parentNode, par = this.parentNode,
frag = Anot.parseHTML(html) frag = Anot.parseHTML(html)
// 操作的svg直接插入 // 操作的svg直接插入
@ -59,7 +59,7 @@ if (window.SVGElement) {
par.insertBefore(frag, this) par.insertBefore(frag, this)
// svg节点的子节点类似 // svg节点的子节点类似
} else { } else {
var newFrag = DOC.createDocumentFragment() let newFrag = DOC.createDocumentFragment()
enumerateNode(frag, newFrag) enumerateNode(frag, newFrag)
par.insertBefore(newFrag, this) par.insertBefore(newFrag, this)
} }
@ -70,18 +70,18 @@ if (window.SVGElement) {
enumerable: true, enumerable: true,
configurable: true, configurable: true,
get: function() { get: function() {
var s = this.outerHTML let s = this.outerHTML
var ropen = new RegExp( let ropen = new RegExp(
'<' + this.nodeName + '\\b(?:(["\'])[^"]*?(\\1)|[^>])*>', '<' + this.nodeName + '\\b(?:(["\'])[^"]*?(\\1)|[^>])*>',
'i' 'i'
) )
var rclose = new RegExp('</' + this.nodeName + '>$', 'i') let rclose = new RegExp('</' + this.nodeName + '>$', 'i')
return s.replace(ropen, '').replace(rclose, '') return s.replace(ropen, '').replace(rclose, '')
}, },
set: function(html) { set: function(html) {
if (Anot.clearHTML) { if (Anot.clearHTML) {
Anot.clearHTML(this) Anot.clearHTML(this)
var frag = Anot.parseHTML(html) let frag = Anot.parseHTML(html)
enumerateNode(frag, this) enumerateNode(frag, this)
} }
} }
@ -92,7 +92,7 @@ if (window.SVGElement) {
//========================= event binding ==================== //========================= event binding ====================
var eventHooks = Anot.eventHooks let eventHooks = Anot.eventHooks
//针对firefox, chrome修正mouseenter, mouseleave(chrome30+) //针对firefox, chrome修正mouseenter, mouseleave(chrome30+)
if (!('onmouseenter' in root)) { if (!('onmouseenter' in root)) {
@ -106,7 +106,7 @@ if (!('onmouseenter' in root)) {
type: fixType, type: fixType,
fix: function(elem, fn) { fix: function(elem, fn) {
return function(e) { return function(e) {
var t = e.relatedTarget let t = e.relatedTarget
if (!t || (t !== elem && !(elem.compareDocumentPosition(t) & 16))) { if (!t || (t !== elem && !(elem.compareDocumentPosition(t) & 16))) {
delete e.type delete e.type
e.type = origType e.type = origType