Compare commits
No commits in common. "master" and "1.0.1" have entirely different histories.
|
@ -3,7 +3,6 @@
|
|||
.LSOverride
|
||||
.idea
|
||||
.vscode
|
||||
package-lock.json
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
|
|
325
pack.config.js
325
pack.config.js
|
@ -1,35 +1,39 @@
|
|||
/**
|
||||
*
|
||||
* @authors yutent (yutent.io@gmail.com)
|
||||
* @authors yutent (yutent@doui.cc)
|
||||
* @date 2018-08-04 01:00:06
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
require('es.shim')
|
||||
const fs = require('iofs')
|
||||
const path = require('path')
|
||||
const chokidar = require('chokidar')
|
||||
const { minify } = require('terser')
|
||||
const uglify = require('uglify-es')
|
||||
const chalk = require('chalk')
|
||||
const config = require('./package.json')
|
||||
const log = console.log
|
||||
const VERSION = config.version
|
||||
|
||||
const VERSION = '1.0.0'
|
||||
const PACK_QUEUE = [
|
||||
'anot.js',
|
||||
'anot.shim.js',
|
||||
'anot-touch.js',
|
||||
'anot-touch.shim.js'
|
||||
]
|
||||
const PACK_DIR = path.resolve('./dist')
|
||||
const SOURCE_DIR = path.resolve('./src/')
|
||||
|
||||
const PAD_START = Buffer.from(`
|
||||
const _Anot = (function() {
|
||||
var _Anot = (function() {
|
||||
`)
|
||||
|
||||
const PAD_END = Buffer.from(`
|
||||
|
||||
/*********************************************************************
|
||||
* DOMReady *
|
||||
**********************************************************************/
|
||||
|
||||
let readyList = []
|
||||
let isReady
|
||||
let fireReady = function(fn) {
|
||||
var readyList = []
|
||||
var isReady
|
||||
var fireReady = function(fn) {
|
||||
isReady = true
|
||||
while ((fn = readyList.shift())) {
|
||||
fn(Anot)
|
||||
|
@ -49,18 +53,164 @@ const PAD_END = Buffer.from(`
|
|||
fn(Anot)
|
||||
}
|
||||
}
|
||||
var _Anot = window.Anot
|
||||
Anot.noConflict = function(deep) {
|
||||
if (deep && window.Anot === Anot) {
|
||||
window.Anot = _Anot
|
||||
}
|
||||
return Anot
|
||||
}
|
||||
|
||||
window.Anot = Anot
|
||||
return Anot
|
||||
})()
|
||||
module.exports = _Anot
|
||||
`)
|
||||
const PAD_END_NEXT = Buffer.from(`
|
||||
|
||||
/*********************************************************************
|
||||
* css import *
|
||||
**********************************************************************/
|
||||
|
||||
var CSS_DEPS = {}
|
||||
function getBaseUrl() {
|
||||
if(window.LIBS_BASE_URL){
|
||||
return
|
||||
}
|
||||
var stack
|
||||
try {
|
||||
throw new Error() // 强制报错,以便捕获e.stack
|
||||
} catch (err) {
|
||||
stack = err.stack
|
||||
}
|
||||
stack = stack.trim().split(/[@ ]+/)
|
||||
if (window.safari) {
|
||||
stack = stack[1]
|
||||
} else {
|
||||
stack = stack.pop()
|
||||
}
|
||||
stack = stack.replace(/(:\\d+)?:\d+([\\w\\W]*)?$/i, '')
|
||||
window.LIBS_BASE_URL = stack.replace(/^([a-z\-]*):\\/\\/([^\\/]+)(\\/.*)?/, '$1://$2')
|
||||
}
|
||||
|
||||
function importCss(url, baseUrl) {
|
||||
url = url.replace(/^\\/+/, '/')
|
||||
if (baseUrl) {
|
||||
url = baseUrl + url
|
||||
} else {
|
||||
if (window.LIBS_BASE_URL) {
|
||||
url = window.LIBS_BASE_URL + url
|
||||
}
|
||||
}
|
||||
|
||||
if (CSS_DEPS[url]) {
|
||||
return
|
||||
}
|
||||
head.insertAdjacentHTML(
|
||||
'afterBegin',
|
||||
'<link rel="stylesheet" href="' + url + '">'
|
||||
)
|
||||
CSS_DEPS[url] = 1
|
||||
}
|
||||
getBaseUrl()
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* DOMReady *
|
||||
**********************************************************************/
|
||||
|
||||
var readyList = []
|
||||
var isReady
|
||||
var fireReady = function(fn) {
|
||||
isReady = true
|
||||
while ((fn = readyList.shift())) {
|
||||
fn(Anot)
|
||||
}
|
||||
}
|
||||
|
||||
if (DOC.readyState === 'complete') {
|
||||
setTimeout(fireReady) //如果在domReady之外加载
|
||||
} else {
|
||||
DOC.addEventListener('DOMContentLoaded', fireReady)
|
||||
}
|
||||
window.addEventListener('load', fireReady)
|
||||
Anot.ready = function(fn) {
|
||||
if (!isReady) {
|
||||
readyList.push(fn)
|
||||
} else {
|
||||
fn(Anot)
|
||||
}
|
||||
}
|
||||
window.importCss = importCss
|
||||
var _Anot = window.Anot
|
||||
Anot.noConflict = function(deep) {
|
||||
if (deep && window.Anot === Anot) {
|
||||
window.Anot = _Anot
|
||||
}
|
||||
return Anot
|
||||
}
|
||||
|
||||
window.Anot = Anot
|
||||
return Anot
|
||||
})()
|
||||
export default _Anot
|
||||
`)
|
||||
const PAD_START_SHIM = Buffer.from(`
|
||||
;(function() {
|
||||
`)
|
||||
const PAD_END_SHIM = Buffer.from(`
|
||||
/*********************************************************************
|
||||
* DOMReady *
|
||||
**********************************************************************/
|
||||
|
||||
function comment({ touch } = {}) {
|
||||
var readyList = []
|
||||
var isReady
|
||||
var fireReady = function(fn) {
|
||||
isReady = true
|
||||
var require = Anot.require
|
||||
if (require && require.checkDeps) {
|
||||
modules['domReady!'].state = 4
|
||||
require.checkDeps()
|
||||
}
|
||||
while ((fn = readyList.shift())) {
|
||||
fn(Anot)
|
||||
}
|
||||
}
|
||||
|
||||
if (DOC.readyState === 'complete') {
|
||||
setTimeout(fireReady) //如果在domReady之外加载
|
||||
} else {
|
||||
DOC.addEventListener('DOMContentLoaded', fireReady)
|
||||
}
|
||||
window.addEventListener('load', fireReady)
|
||||
Anot.ready = function(fn) {
|
||||
if (!isReady) {
|
||||
readyList.push(fn)
|
||||
} else {
|
||||
fn(Anot)
|
||||
}
|
||||
}
|
||||
// Map over Anot in case of overwrite
|
||||
var _Anot = window.Anot
|
||||
Anot.noConflict = function(deep) {
|
||||
if (deep && window.Anot === Anot) {
|
||||
window.Anot = _Anot
|
||||
}
|
||||
return Anot
|
||||
}
|
||||
|
||||
window.Anot = Anot
|
||||
})()
|
||||
`)
|
||||
|
||||
function comment({ amd, touch, next } = {}) {
|
||||
return `/*==================================================
|
||||
* Anot ${touch ? 'touch' : 'normal'} version for future browsers
|
||||
* @authors yutent<yutent.io@gmail.com>
|
||||
* @date ${new Date().format()}
|
||||
* @version v${VERSION}
|
||||
* Anot ${touch ? 'touch' : 'normal'} version ${amd ? 'with AMD loader' : ''} ${
|
||||
next ? 'for future browsers' : ''
|
||||
}
|
||||
* @authors yutent (yutent@doui.cc)
|
||||
* @date 2017-03-21 21:05:57
|
||||
* support IE10+ and modern browsers
|
||||
*
|
||||
==================================================*/
|
||||
`
|
||||
|
@ -97,25 +247,56 @@ function packNoCompress(file) {
|
|||
return BUFFER_CACHE[it]
|
||||
})
|
||||
let touchModule = fs.cat('./src/lib/touch.js')
|
||||
let amdModule = fs.cat('./src/lib/amd.js')
|
||||
|
||||
let normalVer = Buffer.concat(libs)
|
||||
let touchVer = Buffer.concat([normalVer, touchModule])
|
||||
let nextVer = Buffer.concat(libs)
|
||||
let touchNext = Buffer.concat([nextVer, touchModule])
|
||||
let shim = Buffer.concat([nextVer, amdModule])
|
||||
let touchShim = Buffer.concat([shim, touchModule])
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包未来版的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
fs.echo(Buffer.concat([PAD_START, normalVer, PAD_END]), './dist/anot.js')
|
||||
log('%s 打包完成...', chalk.green('anot.js'))
|
||||
fs.echo(
|
||||
Buffer.concat([PAD_START, nextVer, PAD_END_NEXT]),
|
||||
'./dist/anot.next.js'
|
||||
)
|
||||
log('%s 打包完成...', chalk.green('anot.next.js'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包带触摸事件的未来版的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
fs.echo(Buffer.concat([PAD_START, touchVer, PAD_END]), './dist/anot.touch.js')
|
||||
log('%s 打包完成...', chalk.green('anot.touch.js'))
|
||||
fs.echo(
|
||||
Buffer.concat([PAD_START, touchNext, PAD_END_NEXT]),
|
||||
'./dist/anot-touch.next.js'
|
||||
)
|
||||
log('%s 打包完成...', chalk.green('anot-touch.next.js'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包自带AMD加载器的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
fs.echo(
|
||||
Buffer.concat([PAD_START_SHIM, shim, PAD_END_SHIM]),
|
||||
'./dist/anot.shim.js'
|
||||
)
|
||||
log('%s 打包完成...', chalk.green('anot.shim.js'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包自带AMD加载器及触摸事件的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
fs.echo(
|
||||
Buffer.concat([PAD_START_SHIM, touchShim, PAD_END_SHIM]),
|
||||
'./dist/anot-touch.shim.js'
|
||||
)
|
||||
log('%s 打包完成...', chalk.green('anot-touch.shim.js'))
|
||||
}
|
||||
|
||||
// 打包并压缩
|
||||
|
@ -124,35 +305,101 @@ function packAndCompress() {
|
|||
return BUFFER_CACHE[it]
|
||||
})
|
||||
let touchModule = fs.cat('./src/lib/touch.js')
|
||||
let amdModule = fs.cat('./src/lib/amd.js')
|
||||
|
||||
let normalVer = Buffer.concat(libs)
|
||||
let touchVer = Buffer.concat([normalVer, touchModule])
|
||||
let normal = Buffer.concat(libs)
|
||||
let touchNormal = Buffer.concat([normal, touchModule])
|
||||
let shim = Buffer.concat([normal, amdModule])
|
||||
let touchShim = Buffer.concat([shim, touchModule])
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包普通版 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
log('正在打包 anot.js...')
|
||||
let normalVer = Buffer.concat([PAD_START, normal, PAD_END]).toString()
|
||||
fs.echo(comment() + uglify.minify(normalVer).code, './dist/anot.js')
|
||||
log(chalk.green('anot.js 打包压缩完成!'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包带触摸事件的普通版 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
log('正在打包 anot-touch.js...')
|
||||
let touchNormalVer = Buffer.concat([
|
||||
PAD_START,
|
||||
touchNormal,
|
||||
PAD_END
|
||||
]).toString()
|
||||
|
||||
fs.echo(
|
||||
comment({ touch: true }) + uglify.minify(touchNormalVer).code,
|
||||
'./dist/anot-touch.js'
|
||||
)
|
||||
|
||||
log(chalk.green('anot-touch.js 打包压缩完成...'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包自带AMD加载器的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
log('正在打包 anot.shim.js...')
|
||||
let shimVer = Buffer.concat([PAD_START_SHIM, shim, PAD_END_SHIM]).toString()
|
||||
fs.echo(
|
||||
comment({ amd: true }) + uglify.minify(shimVer).code,
|
||||
'./dist/anot.shim.js'
|
||||
)
|
||||
log(chalk.green('anot.shim.js 打包压缩完成!'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包自带AMD加载器及触摸事件的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
log('正在打包 anot-touch.shim.js...')
|
||||
let touchShimVer = Buffer.concat([
|
||||
PAD_START_SHIM,
|
||||
touchShim,
|
||||
PAD_END_SHIM
|
||||
]).toString()
|
||||
fs.echo(
|
||||
comment({ amd: true, touch: true }) + uglify.minify(touchShimVer).code,
|
||||
'./dist/anot-touch.shim.js'
|
||||
)
|
||||
log(chalk.green('anot-touch.shim.js 打包压缩完成...'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包未来版的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
log('正在打包 anot.js...')
|
||||
let normalVerPack = Buffer.concat([PAD_START, normalVer, PAD_END]).toString()
|
||||
|
||||
minify(normalVerPack, { sourceMap: false }).then(res => {
|
||||
fs.echo(comment() + res.code, './dist/anot.js')
|
||||
log(chalk.green('anot.js 打包压缩完成!'))
|
||||
})
|
||||
log('正在打包 anot.next.js...')
|
||||
let nextVer = Buffer.concat([PAD_START, normal, PAD_END_NEXT]).toString()
|
||||
fs.echo(
|
||||
comment({ next: true }) + uglify.minify(nextVer).code,
|
||||
'./dist/anot.next.js'
|
||||
)
|
||||
log(chalk.green('anot.next.js 打包压缩完成!'))
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------
|
||||
* 打包带触摸事件的未来版的 anot
|
||||
* --------------------------------------------------------
|
||||
*/
|
||||
log('正在打包 anot.touch.js...')
|
||||
let touchVerPack = Buffer.concat([PAD_START, touchVer, PAD_END]).toString()
|
||||
|
||||
minify(touchVerPack, { sourceMap: false }).then(res => {
|
||||
fs.echo(comment({ touch: true }) + res.code, './dist/anot.touch.js')
|
||||
log(chalk.green('anot.touch.js 打包压缩完成!'))
|
||||
})
|
||||
log('正在打包 anot-touch.next.js...')
|
||||
let touchNextVer = Buffer.concat([
|
||||
PAD_START,
|
||||
touchNormal,
|
||||
PAD_END_NEXT
|
||||
]).toString()
|
||||
fs.echo(
|
||||
comment({ touch: true, next: true }) + uglify.minify(touchNextVer).code,
|
||||
'./dist/anot-touch.next.js'
|
||||
)
|
||||
log(chalk.green('anot-touch.next.js 打包压缩完成!'))
|
||||
}
|
||||
|
||||
let args = process.argv.slice(2)
|
||||
|
@ -185,5 +432,7 @@ switch (mode) {
|
|||
break
|
||||
default:
|
||||
log(chalk.red('无效编译参数!'))
|
||||
let buf = Buffer.concat(loadFiles())
|
||||
log(buf.toString())
|
||||
break
|
||||
}
|
||||
|
|
20
package.json
20
package.json
|
@ -1,22 +1,30 @@
|
|||
{
|
||||
"name": "anot",
|
||||
"version": "2.2.4",
|
||||
"version": "1.0.1",
|
||||
"description": "Anot - 迷你mvvm框架",
|
||||
"main": "dist/anot.js",
|
||||
"files": ["dist"],
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"start": "npm run dev",
|
||||
"dev": "node ./pack.config.js dev",
|
||||
"prod": "node ./pack.config.js prod"
|
||||
},
|
||||
"keywords": ["anot", "avalon", "mvvm", "doui", "yutent"],
|
||||
"keywords": [
|
||||
"anot",
|
||||
"avalon",
|
||||
"mvvm",
|
||||
"doui",
|
||||
"yutent"
|
||||
],
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"chalk": "^2.4.1",
|
||||
"chokidar": "^2.0.4",
|
||||
"es.shim": "^2.0.1",
|
||||
"iofs": "^1.5.2",
|
||||
"terser": "^5.0.0"
|
||||
"es.shim": "^1.1.2",
|
||||
"iofs": "^1.1.0",
|
||||
"uglify-es": "^3.3.9"
|
||||
},
|
||||
"repository": "https://github.com/yutent/anot.js.git",
|
||||
"author": "yutent",
|
||||
|
|
16
readme.md
16
readme.md
|
@ -1,8 +1,6 @@
|
|||
## Anot.js 2.x
|
||||
> Anot 是Anot not only templateEngine的缩写。 它是一款迷你,易用、高性能的前端MVVM框架, fork于avalon。
|
||||
>2.0版进行了大量的精简, 移除了非现代浏览器的兼容代码, 移除组件API, 正式引入web component。
|
||||
## Anot.js
|
||||
> Anot 是Anot not only templateEngine的缩写。 它是一款迷你,易用、高性能的前端MVVM框架, fork于avalon。进行了大量的重构,精简部分冗余的API, 同时针对组件拓展进行了优化。
|
||||
|
||||
>> 2.x版本为 全新版本, 只兼容支持type="module"的浏览器。
|
||||
|
||||
```bash
|
||||
# 开发模式
|
||||
|
@ -14,9 +12,13 @@ npm start
|
|||
# 打包
|
||||
npm run prod
|
||||
```
|
||||
执行完, 会打包为2个版本, 分别是
|
||||
- anot.js 普通版(需要支持es6 module的现代浏览器)
|
||||
- anot.touch.js 带触摸事件的版本(需要支持es6 module的现代浏览器)
|
||||
执行完, 会打包为6个版本, 分别是
|
||||
- anot.js 普通版(可用于webpack)
|
||||
- anot-touch.js 普通带触摸版(可用于webpack)
|
||||
- anot.shim.js 自带AMD加载版
|
||||
- anot-touch.shim.js 自带AMD加载带触摸版
|
||||
- anot.next.js 未来版(需要支持es6 module的现代浏览器)
|
||||
- anot-touch.next.js 带触摸的未来版(需要支持es6 module的现代浏览器)
|
||||
|
||||
|
||||
### 文档:
|
||||
|
|
|
@ -2,13 +2,16 @@
|
|||
* 全局变量及方法 *
|
||||
**********************************************************************/
|
||||
var bindingID = 1024
|
||||
|
||||
var IEVersion = 0
|
||||
if (window.VBArray) {
|
||||
IEVersion = document.documentMode || (window.XMLHttpRequest ? 7 : 6)
|
||||
}
|
||||
var expose = generateID()
|
||||
//http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function
|
||||
var DOC = window.document
|
||||
var head = DOC.head //HEAD元素
|
||||
head.insertAdjacentHTML(
|
||||
'afterbegin',
|
||||
'afterBegin',
|
||||
'<anot skip class="anot-hide"><style id="anot-style">.anot-hide{ display: none!important }</style></anot>'
|
||||
)
|
||||
var ifGroup = head.firstChild
|
||||
|
@ -31,8 +34,6 @@ function createMap() {
|
|||
return Object.create(null)
|
||||
}
|
||||
|
||||
var encode = encodeURIComponent
|
||||
var decode = decodeURIComponent
|
||||
var subscribers = '$' + expose
|
||||
|
||||
var nullObject = {} //作用类似于noop,只用于代码防御,千万不要在它上面添加属性
|
||||
|
@ -44,6 +45,7 @@ var ohasOwn = oproto.hasOwnProperty
|
|||
var serialize = oproto.toString
|
||||
var ap = Array.prototype
|
||||
var aslice = ap.slice
|
||||
var W3C = window.dispatchEvent
|
||||
var root = DOC.documentElement
|
||||
var anotFragment = DOC.createDocumentFragment()
|
||||
var cinerator = DOC.createElement('div')
|
||||
|
|
|
@ -3,24 +3,352 @@
|
|||
// ===============================
|
||||
// ========== Promise ============
|
||||
// ===============================
|
||||
;(function(nativePromise) {
|
||||
function _yes(val) {
|
||||
return val
|
||||
}
|
||||
|
||||
if (!Promise.defer) {
|
||||
Promise.defer = function() {
|
||||
let obj = {}
|
||||
obj.promise = new Promise((resolve, reject) => {
|
||||
obj.resolve = resolve
|
||||
obj.reject = reject
|
||||
function _no(err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
function done(callback) {
|
||||
return this.then(callback, _no)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
//成功的回调
|
||||
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方法
|
||||
if (!String.prototype.splice) {
|
||||
Object.defineProperty(String.prototype, 'splice', {
|
||||
value: function(start, len, fill) {
|
||||
let length = this.length
|
||||
let argLen = arguments.length
|
||||
var length = this.length,
|
||||
argLen = arguments.length
|
||||
|
||||
fill = fill === undefined ? '' : fill
|
||||
|
||||
|
@ -30,11 +358,8 @@ if (!String.prototype.splice) {
|
|||
|
||||
//处理负数
|
||||
if (start < 0) {
|
||||
if (Math.abs(start) >= length) {
|
||||
start = 0
|
||||
} else {
|
||||
start = length + start
|
||||
}
|
||||
if (Math.abs(start) >= length) start = 0
|
||||
else start = length + start
|
||||
}
|
||||
|
||||
if (argLen === 1) {
|
||||
|
@ -42,8 +367,8 @@ if (!String.prototype.splice) {
|
|||
} else {
|
||||
len -= 0
|
||||
|
||||
let strl = this.slice(0, start)
|
||||
let strr = this.slice(start + len)
|
||||
var strl = this.slice(0, start),
|
||||
strr = this.slice(start + len)
|
||||
|
||||
return strl + fill + strr
|
||||
}
|
||||
|
@ -56,10 +381,10 @@ if (!Date.prototype.getFullWeek) {
|
|||
//获取当天是本年度第几周
|
||||
Object.defineProperty(Date.prototype, 'getFullWeek', {
|
||||
value: function() {
|
||||
let thisYear = this.getFullYear()
|
||||
let that = new Date(thisYear, 0, 1)
|
||||
let firstDay = that.getDay() || 1
|
||||
let numsOfToday = (this - that) / 86400000
|
||||
var thisYear = this.getFullYear(),
|
||||
that = new Date(thisYear, 0, 1),
|
||||
firstDay = that.getDay() || 1,
|
||||
numsOfToday = (this - that) / 86400000
|
||||
return Math.ceil((numsOfToday + firstDay) / 7)
|
||||
},
|
||||
enumerable: false
|
||||
|
@ -68,10 +393,10 @@ if (!Date.prototype.getFullWeek) {
|
|||
//获取当天是本月第几周
|
||||
Object.defineProperty(Date.prototype, 'getWeek', {
|
||||
value: function() {
|
||||
let today = this.getDate()
|
||||
let thisMonth = this.getMonth()
|
||||
let thisYear = this.getFullYear()
|
||||
let firstDay = new Date(thisYear, thisMonth, 1).getDay()
|
||||
var today = this.getDate(),
|
||||
thisMonth = this.getMonth(),
|
||||
thisYear = this.getFullYear(),
|
||||
firstDay = new Date(thisYear, thisMonth, 1).getDay()
|
||||
return Math.ceil((today + firstDay) / 7)
|
||||
},
|
||||
enumerable: false
|
||||
|
@ -92,8 +417,8 @@ if (!Date.prototype.format) {
|
|||
Object.defineProperty(Date.prototype, 'format', {
|
||||
value: function(str) {
|
||||
str = str || 'Y-m-d H:i:s'
|
||||
let week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
||||
let dt = {
|
||||
var week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
|
||||
dt = {
|
||||
fullyear: this.getFullYear(),
|
||||
year: this.getYear(),
|
||||
fullweek: this.getFullWeek(),
|
||||
|
@ -104,8 +429,8 @@ if (!Date.prototype.format) {
|
|||
hours: this.getHours(),
|
||||
minutes: this.getMinutes(),
|
||||
seconds: this.getSeconds()
|
||||
}
|
||||
let re
|
||||
},
|
||||
re
|
||||
|
||||
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours
|
||||
|
||||
|
@ -127,7 +452,7 @@ if (!Date.prototype.format) {
|
|||
D: dt.day
|
||||
}
|
||||
|
||||
for (let i in re) {
|
||||
for (var i in re) {
|
||||
str = str.replace(new RegExp(i, 'g'), re[i])
|
||||
}
|
||||
return str
|
||||
|
|
158
src/02-core.js
158
src/02-core.js
|
@ -1,29 +1,41 @@
|
|||
let Anot = function(el) {
|
||||
var Anot = function(el) {
|
||||
//创建jQuery式的无new 实例化结构
|
||||
return new Anot.init(el)
|
||||
}
|
||||
|
||||
/*视浏览器情况采用最快的异步回调*/
|
||||
Anot.nextTick = (function() {
|
||||
let queue = []
|
||||
Anot.nextTick = new function() {
|
||||
// jshint ignore:line
|
||||
var tickImmediate = window.setImmediate
|
||||
var tickObserver = window.MutationObserver
|
||||
if (tickImmediate) {
|
||||
return tickImmediate.bind(window)
|
||||
}
|
||||
|
||||
var queue = []
|
||||
function callback() {
|
||||
let n = queue.length
|
||||
for (let i = 0; i < n; i++) {
|
||||
var n = queue.length
|
||||
for (var i = 0; i < n; i++) {
|
||||
queue[i]()
|
||||
}
|
||||
queue = queue.slice(n)
|
||||
}
|
||||
|
||||
let node = document.createTextNode('<!-- -->')
|
||||
new MutationObserver(callback).observe(node, { characterData: true })
|
||||
|
||||
let bool = false
|
||||
if (tickObserver) {
|
||||
var node = document.createTextNode('anot')
|
||||
new tickObserver(callback).observe(node, { characterData: true }) // jshint ignore:line
|
||||
var bool = false
|
||||
return function(fn) {
|
||||
queue.push(fn)
|
||||
bool = !bool
|
||||
node.data = bool
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
return function(fn) {
|
||||
setTimeout(fn, 4)
|
||||
}
|
||||
}() // jshint ignore:line
|
||||
|
||||
/*********************************************************************
|
||||
* Anot的静态方法定义区 *
|
||||
|
@ -85,14 +97,17 @@ Anot.PropsTypes.isBoolean = function() {
|
|||
/*判定是否是一个朴素的javascript对象(Object),不是DOM对象,不是BOM对象,不是自定义类的实例*/
|
||||
Anot.isPlainObject = function(obj) {
|
||||
// 简单的 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都储存在这里
|
||||
var VMODELS = (Anot.vmodels = {}) //所有vmodel都储存在这里
|
||||
Anot.init = function(source) {
|
||||
if (Anot.isPlainObject(source)) {
|
||||
let $id = source.$id
|
||||
let vm = null
|
||||
var $id = source.$id
|
||||
var vm = null
|
||||
if (!$id) {
|
||||
log('warning: vm必须指定id')
|
||||
}
|
||||
|
@ -101,12 +116,12 @@ Anot.init = function(source) {
|
|||
VMODELS[$id] = vm
|
||||
|
||||
Anot.nextTick(function() {
|
||||
let $elem = document.querySelector('[anot=' + vm.$id + ']')
|
||||
var $elem = document.querySelector('[anot=' + vm.$id + ']')
|
||||
if ($elem) {
|
||||
if ($elem === DOC.body) {
|
||||
scanTag($elem, [])
|
||||
} else {
|
||||
let _parent = $elem
|
||||
var _parent = $elem
|
||||
while ((_parent = _parent.parentNode)) {
|
||||
if (_parent.__VM__) {
|
||||
break
|
||||
|
@ -126,7 +141,7 @@ Anot.fn = Anot.prototype = Anot.init.prototype
|
|||
|
||||
//与jQuery.extend方法,可用于浅拷贝,深拷贝
|
||||
Anot.mix = Anot.fn.mix = function() {
|
||||
let options,
|
||||
var options,
|
||||
name,
|
||||
src,
|
||||
copy,
|
||||
|
@ -165,7 +180,11 @@ Anot.mix = Anot.fn.mix = function() {
|
|||
if (target === copy) {
|
||||
continue
|
||||
}
|
||||
if (deep && copy && (Anot.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) {
|
||||
if (
|
||||
deep &&
|
||||
copy &&
|
||||
(Anot.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))
|
||||
) {
|
||||
if (copyIsArray) {
|
||||
copyIsArray = false
|
||||
clone = src && Array.isArray(src) ? src : []
|
||||
|
@ -184,8 +203,12 @@ Anot.mix = Anot.fn.mix = function() {
|
|||
}
|
||||
|
||||
function cacheStore(tpye, key, val) {
|
||||
if (!window[tpye]) {
|
||||
return log('该浏览器不支持本地储存' + tpye)
|
||||
}
|
||||
|
||||
if (this.type(key) === 'object') {
|
||||
for (let i in key) {
|
||||
for (var i in key) {
|
||||
window[tpye].setItem(i, key[i])
|
||||
}
|
||||
return
|
||||
|
@ -210,7 +233,7 @@ function cacheStore(tpye, key, val) {
|
|||
/*判定是否类数组,如节点集合,纯数组,arguments与拥有非负整数的length属性的纯JS对象*/
|
||||
function isArrayLike(obj) {
|
||||
if (obj && typeof obj === 'object') {
|
||||
let n = obj.length,
|
||||
var n = obj.length,
|
||||
str = serialize.call(obj)
|
||||
if (/(Array|List|Collection|Map|Arguments)\]$/.test(str)) {
|
||||
return true
|
||||
|
@ -252,7 +275,7 @@ Anot.mix({
|
|||
end = start || 0
|
||||
start = 0
|
||||
}
|
||||
let index = -1,
|
||||
var index = -1,
|
||||
length = Math.max(0, Math.ceil((end - start) / step)),
|
||||
result = new Array(length)
|
||||
while (++index < length) {
|
||||
|
@ -265,33 +288,33 @@ Anot.mix({
|
|||
eventHooks: {},
|
||||
/*绑定事件*/
|
||||
bind: function(el, type, fn, phase) {
|
||||
let hooks = Anot.eventHooks
|
||||
var hooks = Anot.eventHooks
|
||||
type = type.split(',')
|
||||
Anot.each(type, function(i, t) {
|
||||
t = t.trim()
|
||||
let hook = hooks[t]
|
||||
var hook = hooks[t]
|
||||
if (typeof hook === 'object') {
|
||||
t = hook.type || t
|
||||
phase = hook.phase || phase
|
||||
type = hook.type || type
|
||||
phase = hook.phase || !!phase
|
||||
fn = hook.fix ? hook.fix(el, fn) : fn
|
||||
}
|
||||
el.addEventListener(t, fn, !!phase)
|
||||
el.addEventListener(t, fn, phase)
|
||||
})
|
||||
return fn
|
||||
},
|
||||
/*卸载事件*/
|
||||
unbind: function(el, type, fn, phase) {
|
||||
let hooks = Anot.eventHooks
|
||||
var hooks = Anot.eventHooks
|
||||
type = type.split(',')
|
||||
fn = fn || noop
|
||||
Anot.each(type, function(i, t) {
|
||||
t = t.trim()
|
||||
let hook = hooks[t]
|
||||
var hook = hooks[t]
|
||||
if (typeof hook === 'object') {
|
||||
t = hook.type || t
|
||||
phase = hook.phase || phase
|
||||
type = hook.type || type
|
||||
phase = hook.phase || !!phase
|
||||
}
|
||||
el.removeEventListener(t, fn, !!phase)
|
||||
el.removeEventListener(t, fn, phase)
|
||||
})
|
||||
},
|
||||
/*读写删除元素节点的样式*/
|
||||
|
@ -299,9 +322,8 @@ Anot.mix({
|
|||
if (node instanceof Anot) {
|
||||
node = node[0]
|
||||
}
|
||||
var prop = /[_-]/.test(name) ? camelize(name) : name
|
||||
var fn
|
||||
|
||||
var prop = /[_-]/.test(name) ? camelize(name) : name,
|
||||
fn
|
||||
name = Anot.cssName(prop) || prop
|
||||
if (value === void 0 || typeof value === 'boolean') {
|
||||
//获取样式
|
||||
|
@ -310,7 +332,7 @@ Anot.mix({
|
|||
name = 'backgroundColor'
|
||||
}
|
||||
var val = fn(node, name)
|
||||
return value === true ? +val || 0 : val
|
||||
return value === true ? parseFloat(val) || 0 : val
|
||||
} else if (value === '') {
|
||||
//请除样式
|
||||
node.style[name] = ''
|
||||
|
@ -330,12 +352,13 @@ Anot.mix({
|
|||
each: function(obj, fn) {
|
||||
if (obj) {
|
||||
//排除null, undefined
|
||||
var i = 0
|
||||
if (isArrayLike(obj)) {
|
||||
for (let i = 0, n = obj.length; i < n; i++) {
|
||||
for (var n = obj.length; i < n; i++) {
|
||||
if (fn(i, obj[i]) === false) break
|
||||
}
|
||||
} else {
|
||||
for (let i in obj) {
|
||||
for (i in obj) {
|
||||
if (obj.hasOwnProperty(i) && fn(i, obj[i]) === false) {
|
||||
break
|
||||
}
|
||||
|
@ -356,7 +379,7 @@ Anot.mix({
|
|||
},
|
||||
/*移除数组中第一个匹配传参的那个元素,返回布尔表示成功与否*/
|
||||
remove: function(target, item) {
|
||||
let index = target.indexOf(item)
|
||||
var index = target.indexOf(item)
|
||||
if (~index) return Anot.Array.removeAt(target, index)
|
||||
return false
|
||||
}
|
||||
|
@ -368,12 +391,12 @@ Anot.mix({
|
|||
* @return
|
||||
*/
|
||||
ls: function() {
|
||||
let args = aslice.call(arguments, 0)
|
||||
var args = aslice.call(arguments, 0)
|
||||
args.unshift('localStorage')
|
||||
return cacheStore.apply(this, args)
|
||||
},
|
||||
ss: function() {
|
||||
let args = aslice.call(arguments, 0)
|
||||
var args = aslice.call(arguments, 0)
|
||||
args.unshift('sessionStorage')
|
||||
return cacheStore.apply(this, args)
|
||||
},
|
||||
|
@ -404,7 +427,7 @@ Anot.mix({
|
|||
|
||||
if ((this.type(val) == 'string' && val.trim() === '') || val === null) {
|
||||
document.cookie =
|
||||
encode(key) +
|
||||
encodeURIComponent(key) +
|
||||
'=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=' +
|
||||
opt.domain +
|
||||
'; path=' +
|
||||
|
@ -428,9 +451,9 @@ Anot.mix({
|
|||
}
|
||||
}
|
||||
document.cookie =
|
||||
encode(key) +
|
||||
encodeURIComponent(key) +
|
||||
'=' +
|
||||
encode(val) +
|
||||
encodeURIComponent(val) +
|
||||
opt.expires +
|
||||
'; domain=' +
|
||||
opt.domain +
|
||||
|
@ -444,11 +467,11 @@ Anot.mix({
|
|||
return document.cookie
|
||||
}
|
||||
return (
|
||||
decode(
|
||||
decodeURIComponent(
|
||||
document.cookie.replace(
|
||||
new RegExp(
|
||||
'(?:(?:^|.*;)\\s*' +
|
||||
encode(key).replace(/[\-\.\+\*]/g, '\\$&') +
|
||||
encodeURIComponent(key).replace(/[\-\.\+\*]/g, '\\$&') +
|
||||
'\\s*\\=\\s*([^;]*).*$)|^.*$'
|
||||
),
|
||||
'$1'
|
||||
|
@ -459,21 +482,19 @@ Anot.mix({
|
|||
},
|
||||
//获取url的参数
|
||||
search: function(key) {
|
||||
let uri = location.search
|
||||
key += ''
|
||||
var uri = location.search
|
||||
|
||||
if (!uri) {
|
||||
return null
|
||||
}
|
||||
uri = decode(uri)
|
||||
if (!key || !uri) return null
|
||||
|
||||
uri = uri.slice(1)
|
||||
uri = uri.split('&')
|
||||
|
||||
let obj = {}
|
||||
for (let i = 0, item; (item = uri[i++]); ) {
|
||||
let tmp = item.split('=')
|
||||
var obj = {}
|
||||
for (var i = 0, item; (item = uri[i++]); ) {
|
||||
var tmp = item.split('=')
|
||||
tmp[1] = tmp.length < 2 ? null : tmp[1]
|
||||
tmp[1] = tmp[1]
|
||||
tmp[1] = decodeURIComponent(tmp[1])
|
||||
if (obj.hasOwnProperty(tmp[0])) {
|
||||
if (typeof obj[tmp[0]] === 'object') {
|
||||
obj[tmp[0]].push(tmp[1])
|
||||
|
@ -485,26 +506,33 @@ Anot.mix({
|
|||
obj[tmp[0]] = tmp[1]
|
||||
}
|
||||
}
|
||||
if (key) {
|
||||
return obj.hasOwnProperty(key) ? obj[key] : null
|
||||
} else {
|
||||
return obj
|
||||
}
|
||||
},
|
||||
//复制文本到粘贴板
|
||||
copy: function(txt) {
|
||||
try {
|
||||
navigator.clipboard.writeText(txt)
|
||||
} catch (err) {
|
||||
log('复制到粘贴板失败', err)
|
||||
if (!DOC.queryCommandSupported || !DOC.queryCommandSupported('copy')) {
|
||||
return log('该浏览器不支持复制到粘贴板')
|
||||
}
|
||||
|
||||
var ta = DOC.createElement('textarea')
|
||||
ta.textContent = txt
|
||||
ta.style.position = 'fixed'
|
||||
ta.style.bottom = '-1000px'
|
||||
DOC.body.appendChild(ta)
|
||||
ta.select()
|
||||
try {
|
||||
DOC.execCommand('copy')
|
||||
} catch (err) {
|
||||
log('复制到粘贴板失败')
|
||||
}
|
||||
DOC.body.removeChild(ta)
|
||||
}
|
||||
})
|
||||
|
||||
let bindingHandlers = (Anot.bindingHandlers = {})
|
||||
let bindingExecutors = (Anot.bindingExecutors = {})
|
||||
var bindingHandlers = (Anot.bindingHandlers = {})
|
||||
var bindingExecutors = (Anot.bindingExecutors = {})
|
||||
|
||||
let directives = (Anot.directives = {})
|
||||
var directives = (Anot.directives = {})
|
||||
Anot.directive = function(name, obj) {
|
||||
bindingHandlers[name] = obj.init = obj.init || noop
|
||||
bindingExecutors[name] = obj.update = obj.update || noop
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// https://github.com/rsms/js-lru
|
||||
let Cache = new function() {
|
||||
var Cache = new function() {
|
||||
// jshint ignore:line
|
||||
function LRU(maxLength) {
|
||||
this.size = 0
|
||||
|
@ -8,10 +8,10 @@ let Cache = new function() {
|
|||
this._keymap = {}
|
||||
}
|
||||
|
||||
let p = LRU.prototype
|
||||
var p = LRU.prototype
|
||||
|
||||
p.put = function(key, value) {
|
||||
let entry = {
|
||||
var entry = {
|
||||
key: key,
|
||||
value: value
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ let Cache = new function() {
|
|||
}
|
||||
|
||||
p.shift = function() {
|
||||
let entry = this.head
|
||||
var entry = this.head
|
||||
if (entry) {
|
||||
this.head = this.head.newer
|
||||
this.head.older = entry.newer = entry.older = this._keymap[
|
||||
|
@ -42,7 +42,7 @@ let Cache = new function() {
|
|||
}
|
||||
}
|
||||
p.get = function(key) {
|
||||
let entry = this._keymap[key]
|
||||
var entry = this._keymap[key]
|
||||
if (entry === void 0) return
|
||||
if (entry === this.tail) {
|
||||
return entry.value
|
||||
|
|
|
@ -17,9 +17,122 @@ Anot.contains = function(root, el) {
|
|||
}
|
||||
}
|
||||
|
||||
if (window.SVGElement) {
|
||||
var svgns = 'http://www.w3.org/2000/svg'
|
||||
var svg = DOC.createElementNS(svgns, 'svg')
|
||||
svg.innerHTML = '<circle cx="50" cy="50" r="40" fill="red" />'
|
||||
if (!rsvg.test(svg.firstChild)) {
|
||||
// #409
|
||||
/* jshint ignore:start */
|
||||
function enumerateNode(node, targetNode) {
|
||||
if (node && node.childNodes) {
|
||||
var nodes = node.childNodes
|
||||
for (var i = 0, el; (el = nodes[i++]); ) {
|
||||
if (el.tagName) {
|
||||
var svg = DOC.createElementNS(svgns, el.tagName.toLowerCase())
|
||||
// copy attrs
|
||||
ap.forEach.call(el.attributes, function(attr) {
|
||||
svg.setAttribute(attr.name, attr.value)
|
||||
})
|
||||
// 递归处理子节点
|
||||
enumerateNode(el, svg)
|
||||
targetNode.appendChild(svg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* jshint ignore:end */
|
||||
Object.defineProperties(SVGElement.prototype, {
|
||||
outerHTML: {
|
||||
//IE9-11,firefox不支持SVG元素的innerHTML,outerHTML属性
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: function() {
|
||||
return new XMLSerializer().serializeToString(this)
|
||||
},
|
||||
set: function(html) {
|
||||
var tagName = this.tagName.toLowerCase(),
|
||||
par = this.parentNode,
|
||||
frag = Anot.parseHTML(html)
|
||||
// 操作的svg,直接插入
|
||||
if (tagName === 'svg') {
|
||||
par.insertBefore(frag, this)
|
||||
// svg节点的子节点类似
|
||||
} else {
|
||||
var newFrag = DOC.createDocumentFragment()
|
||||
enumerateNode(frag, newFrag)
|
||||
par.insertBefore(newFrag, this)
|
||||
}
|
||||
par.removeChild(this)
|
||||
}
|
||||
},
|
||||
innerHTML: {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: function() {
|
||||
var s = this.outerHTML
|
||||
var ropen = new RegExp(
|
||||
'<' + this.nodeName + '\\b(?:(["\'])[^"]*?(\\1)|[^>])*>',
|
||||
'i'
|
||||
)
|
||||
var rclose = new RegExp('</' + this.nodeName + '>$', 'i')
|
||||
return s.replace(ropen, '').replace(rclose, '')
|
||||
},
|
||||
set: function(html) {
|
||||
if (Anot.clearHTML) {
|
||||
Anot.clearHTML(this)
|
||||
var frag = Anot.parseHTML(html)
|
||||
enumerateNode(frag, this)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//========================= event binding ====================
|
||||
|
||||
let eventHooks = Anot.eventHooks
|
||||
var 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) {
|
||||
var 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) {
|
||||
/* IE6-11 chrome mousewheel wheelDetla 下 -120 上 120
|
||||
|
|
10
src/06-vm.js
10
src/06-vm.js
|
@ -264,8 +264,8 @@ function observeObject(source, options) {
|
|||
return (old = value.get.call(this))
|
||||
},
|
||||
set: function(x) {
|
||||
var older = old
|
||||
var newer
|
||||
var older = old,
|
||||
newer
|
||||
value.set.call(this, x)
|
||||
newer = this[key]
|
||||
if (this.$fire && newer !== older) {
|
||||
|
@ -307,6 +307,7 @@ function observeObject(source, options) {
|
|||
$vmodel[name] = source[name]
|
||||
})
|
||||
|
||||
/* jshint ignore:start */
|
||||
// hideProperty($vmodel, '$ups', null)
|
||||
hideProperty($vmodel, '$id', 'anonymous')
|
||||
hideProperty($vmodel, '$up', old ? old.$up : null)
|
||||
|
@ -317,6 +318,7 @@ function observeObject(source, options) {
|
|||
hideProperty($vmodel, '$events', {})
|
||||
hideProperty($vmodel, '$refs', {})
|
||||
hideProperty($vmodel, '$children', [])
|
||||
hideProperty($vmodel, '$components', [])
|
||||
hideProperty($vmodel, 'hasOwnProperty', trackBy)
|
||||
hideProperty($vmodel, '$mounted', mounted)
|
||||
if (options.watch) {
|
||||
|
@ -341,7 +343,9 @@ function observeObject(source, options) {
|
|||
}
|
||||
})
|
||||
}
|
||||
/* jshint ignore:end */
|
||||
|
||||
//必须设置了$active,$events
|
||||
simple.forEach(function(name) {
|
||||
var oldVal = old && old[name]
|
||||
var val = ($vmodel[name] = state[name])
|
||||
|
@ -369,11 +373,9 @@ function observeObject(source, options) {
|
|||
|
||||
$vmodel.$active = true
|
||||
|
||||
if ($vmodel.$id !== 'anonymous') {
|
||||
if (old && old.$up && old.$up.$children) {
|
||||
old.$up.$children.push($vmodel)
|
||||
}
|
||||
}
|
||||
|
||||
return $vmodel
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ var newProto = {
|
|||
return this
|
||||
},
|
||||
pushArray: function(arr) {
|
||||
return this.push.apply(this, toJson(arr))
|
||||
return this.push.apply(this, arr)
|
||||
},
|
||||
remove: function(el) {
|
||||
//移除第一个等于给定值的元素
|
||||
|
@ -67,7 +67,9 @@ var newProto = {
|
|||
_splice.call(this.$track, 0, this.length)
|
||||
_splice.call(this, 0, this.length)
|
||||
}
|
||||
|
||||
if (!W3C) {
|
||||
this.$model = toJson(this)
|
||||
}
|
||||
this.notify()
|
||||
this._.length = this.length
|
||||
},
|
||||
|
@ -87,7 +89,9 @@ arrayMethods.forEach(function(method) {
|
|||
}
|
||||
var result = original.apply(this, args)
|
||||
addTrack(this.$track, method, args)
|
||||
|
||||
if (!W3C) {
|
||||
this.$model = toJson(this)
|
||||
}
|
||||
this.notify()
|
||||
this._.length = this.length
|
||||
return result
|
||||
|
@ -116,7 +120,9 @@ arrayMethods.forEach(function(method) {
|
|||
}
|
||||
if (hasSort) {
|
||||
sortByIndex(this.$track, indexes)
|
||||
|
||||
if (!W3C) {
|
||||
this.$model = toJson(this)
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
return this
|
||||
|
|
|
@ -31,6 +31,21 @@ function camelize(target) {
|
|||
})
|
||||
|
||||
Anot.fn.mix({
|
||||
hasClass: function(cls) {
|
||||
var el = this[0] || {} //IE10+, chrome8+, firefox3.6+, safari5.1+,opera11.5+支持classList,chrome24+,firefox26+支持classList2.0
|
||||
return el.nodeType === 1 && el.classList.contains(cls)
|
||||
},
|
||||
toggleClass: function(value, stateVal) {
|
||||
var className,
|
||||
i = 0
|
||||
var classNames = String(value).match(/\S+/g) || []
|
||||
var isBool = typeof stateVal === 'boolean'
|
||||
while ((className = classNames[i++])) {
|
||||
var state = isBool ? stateVal : !this.hasClass(className)
|
||||
this[state ? 'addClass' : 'removeClass'](className)
|
||||
}
|
||||
return this
|
||||
},
|
||||
attr: function(name, value) {
|
||||
if (arguments.length === 2) {
|
||||
this[0].setAttribute(name, value)
|
||||
|
@ -40,24 +55,25 @@ Anot.fn.mix({
|
|||
}
|
||||
},
|
||||
data: function(name, value) {
|
||||
var len = arguments.length
|
||||
var dataset = this[0].dataset
|
||||
name = hyphen(name || '')
|
||||
if (!name) {
|
||||
len = 0
|
||||
}
|
||||
switch (len) {
|
||||
name = 'data-' + hyphen(name || '')
|
||||
switch (arguments.length) {
|
||||
case 2:
|
||||
dataset[name] = value
|
||||
this.attr(name, value)
|
||||
return this
|
||||
case 1:
|
||||
var val = dataset[name]
|
||||
var val = this.attr(name)
|
||||
return parseData(val)
|
||||
case 0:
|
||||
var ret = createMap()
|
||||
for (var i in dataset) {
|
||||
ret[i] = parseData(dataset[i])
|
||||
var ret = {}
|
||||
ap.forEach.call(this[0].attributes, function(attr) {
|
||||
if (attr) {
|
||||
name = attr.name
|
||||
if (!name.indexOf('data-')) {
|
||||
name = camelize(name.slice(5))
|
||||
ret[name] = parseData(attr.value)
|
||||
}
|
||||
}
|
||||
})
|
||||
return ret
|
||||
}
|
||||
},
|
||||
|
@ -143,6 +159,27 @@ Anot.fn.mix({
|
|||
}
|
||||
})
|
||||
|
||||
if (root.dataset) {
|
||||
Anot.fn.data = function(name, val) {
|
||||
name = name && camelize(name)
|
||||
var dataset = this[0].dataset
|
||||
switch (arguments.length) {
|
||||
case 2:
|
||||
dataset[name] = val
|
||||
return this
|
||||
case 1:
|
||||
val = dataset[name]
|
||||
return parseData(val)
|
||||
case 0:
|
||||
var ret = createMap()
|
||||
for (name in dataset) {
|
||||
ret[name] = parseData(dataset[name])
|
||||
}
|
||||
return ret
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Anot.parseJSON = JSON.parse
|
||||
|
||||
var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/
|
||||
|
|
117
src/13-scan.js
117
src/13-scan.js
|
@ -6,6 +6,14 @@
|
|||
var stopScan = oneObject(
|
||||
'area,base,basefont,br,col,command,embed,hr,img,input,link,meta,param,source,track,wbr,noscript,script,style,textarea'.toUpperCase()
|
||||
)
|
||||
function isWidget(el) {
|
||||
//如果是组件,则返回组件的名字
|
||||
var name = el.nodeName.toLowerCase()
|
||||
if (/^anot-([a-z][a-z0-9\-]*)$/.test(name)) {
|
||||
return RegExp.$1
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function isRef(el) {
|
||||
return el.hasAttribute('ref') ? el.getAttribute('ref') : null
|
||||
|
@ -49,21 +57,83 @@ function executeBindings(bindings, vmodels) {
|
|||
bindings.length = 0
|
||||
}
|
||||
|
||||
//https://github.com/RubyLouvre/Anot/issues/636
|
||||
var mergeTextNodes =
|
||||
IEVersion && window.MutationObserver
|
||||
? function(elem) {
|
||||
var node = elem.firstChild,
|
||||
text
|
||||
while (node) {
|
||||
var aaa = node.nextSibling
|
||||
if (node.nodeType === 3) {
|
||||
if (text) {
|
||||
text.nodeValue += node.nodeValue
|
||||
elem.removeChild(node)
|
||||
} else {
|
||||
text = node
|
||||
}
|
||||
} else {
|
||||
text = null
|
||||
}
|
||||
node = aaa
|
||||
}
|
||||
}
|
||||
: 0
|
||||
var roneTime = /^\s*::/
|
||||
var rmsAttr = /^:(\w+)-?(.*)|@(.*)/
|
||||
var rmsAttr = /:(\w+)-?(.*)/
|
||||
|
||||
var events = oneObject(
|
||||
'animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit'
|
||||
)
|
||||
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) {
|
||||
return a.priority - b.priority
|
||||
}
|
||||
|
||||
var rnoCollect = /^(:\S+|data-\S+|on[a-z]+|style|class)$/
|
||||
var ronattr = '__fn__'
|
||||
var specifiedVars = [':disabled', ':loading', ':value']
|
||||
var filterTypes = ['html', 'text', 'attr', 'data']
|
||||
function getOptionsFromTag(elem, vmodels) {
|
||||
var attributes = aslice.call(elem.attributes, 0)
|
||||
var ret = {}
|
||||
var vm = vmodels[0] || {}
|
||||
|
||||
for (var i = 0, attr; (attr = attributes[i++]); ) {
|
||||
var name = attr.name
|
||||
if (
|
||||
(attr.specified && !rnoCollect.test(name)) ||
|
||||
specifiedVars.includes(name)
|
||||
) {
|
||||
elem.removeAttribute(name)
|
||||
if (name.indexOf(ronattr) === 0) {
|
||||
name = attr.value.slice(6)
|
||||
ret[name] = elem[attr.value]
|
||||
delete elem[attr.value]
|
||||
} else {
|
||||
var camelizeName = camelize(name)
|
||||
if (camelizeName.indexOf('@') === 0) {
|
||||
camelizeName = camelizeName.slice(1)
|
||||
attr.value = attr.value.replace(/\(.*\)$/, '')
|
||||
if (vm.$id.slice(0, 10) === 'proxy-each') {
|
||||
vm = vm.$up
|
||||
}
|
||||
if (
|
||||
vm.hasOwnProperty(attr.value) &&
|
||||
typeof vm[attr.value] === 'function'
|
||||
) {
|
||||
ret[camelizeName] = vm[attr.value].bind(vm)
|
||||
}
|
||||
} else {
|
||||
ret[camelizeName] = parseData(attr.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
function scanAttr(elem, vmodels, match) {
|
||||
var scanNode = true
|
||||
|
@ -83,14 +153,9 @@ function scanAttr(elem, vmodels, match) {
|
|||
//如果是以指定前缀命名的
|
||||
var type = match[1]
|
||||
var param = match[2] || ''
|
||||
var eparam = match[3] || '' // 事件绑定的简写
|
||||
var value = attr.value
|
||||
if (obsoleteAttrs[type]) {
|
||||
if (events[type]) {
|
||||
param = type
|
||||
type = 'attr'
|
||||
}
|
||||
if (eparam) {
|
||||
param = eparam
|
||||
type = 'on'
|
||||
}
|
||||
if (directives[type]) {
|
||||
|
@ -108,7 +173,6 @@ function scanAttr(elem, vmodels, match) {
|
|||
(directives[type].priority || type.charCodeAt(0) * 10) +
|
||||
(Number(param.replace(/\D/g, '')) || 0)
|
||||
}
|
||||
// 如果指令允许使用过滤器
|
||||
if (filterTypes.includes(type)) {
|
||||
var filters = getToken(value).filters
|
||||
binding.expr = binding.expr.replace(filters, '')
|
||||
|
@ -149,7 +213,12 @@ function scanAttr(elem, vmodels, match) {
|
|||
executeBindings(bindings, vmodels)
|
||||
}
|
||||
}
|
||||
if (scanNode && !stopScan[elem.tagName]) {
|
||||
if (
|
||||
scanNode &&
|
||||
!stopScan[elem.tagName] &&
|
||||
(isWidget(elem) ? elem.msResolved : 1)
|
||||
) {
|
||||
mergeTextNodes && mergeTextNodes(elem)
|
||||
scanNodeList(elem, vmodels) //扫描子孙元素
|
||||
}
|
||||
}
|
||||
|
@ -172,13 +241,35 @@ function scanNodeArray(nodes, vmodels) {
|
|||
switch (node.nodeType) {
|
||||
case 1:
|
||||
var elem = node
|
||||
if (elem.parentNode && elem.parentNode.nodeType === 1) {
|
||||
if (
|
||||
!elem.msResolved &&
|
||||
elem.parentNode &&
|
||||
elem.parentNode.nodeType === 1
|
||||
) {
|
||||
var widget = isWidget(elem)
|
||||
|
||||
if (widget) {
|
||||
elem.setAttribute('is-widget', '')
|
||||
elem.removeAttribute(':if')
|
||||
elem.removeAttribute(':if-loop')
|
||||
componentQueue.push({
|
||||
element: elem,
|
||||
vmodels: vmodels,
|
||||
name: widget
|
||||
})
|
||||
if (Anot.components[widget]) {
|
||||
// log(widget, Anot.components)
|
||||
//确保所有:attr-name扫描完再处理
|
||||
_delay_component(widget)
|
||||
}
|
||||
} else {
|
||||
// 非组件才检查 ref属性
|
||||
var ref = isRef(elem)
|
||||
if (ref && vmodels.length) {
|
||||
vmodels[0].$refs[ref] = elem
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scanTag(node, vmodels) //扫描元素节点
|
||||
|
||||
|
@ -372,8 +463,6 @@ function scanText(textNode, vmodels, index) {
|
|||
anotFragment.appendChild(node)
|
||||
}
|
||||
textNode.parentNode.replaceChild(anotFragment, textNode)
|
||||
if (bindings.length) {
|
||||
executeBindings(bindings, vmodels)
|
||||
}
|
||||
if (bindings.length) executeBindings(bindings, vmodels)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,339 @@
|
|||
var componentQueue = []
|
||||
var widgetList = []
|
||||
var componentHooks = {
|
||||
__init__: noop,
|
||||
componentWillMount: noop,
|
||||
componentDidMount: noop,
|
||||
childComponentDidMount: noop,
|
||||
componentWillUnmount: noop,
|
||||
render: function() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function parseSlot(collections, vms) {
|
||||
var arr = aslice.call(collections, 0)
|
||||
var obj = { __extra__: [] }
|
||||
arr.forEach(function(elem) {
|
||||
switch (elem.nodeType) {
|
||||
case 1:
|
||||
var slot = elem.getAttribute('slot')
|
||||
|
||||
if (slot) {
|
||||
obj[slot] = obj[slot] || []
|
||||
elem.removeAttribute('slot')
|
||||
obj[slot].push(elem.outerHTML)
|
||||
} else {
|
||||
var txt = elem.outerHTML
|
||||
if (isWidget(elem) || /:[\w-]*=".*"/.test(txt)) {
|
||||
break
|
||||
}
|
||||
if (rexpr.test(txt)) {
|
||||
var expr = normalizeExpr(txt)
|
||||
txt = parseExpr(expr, vms, {}).apply(0, vms)
|
||||
}
|
||||
|
||||
obj.__extra__.push(txt)
|
||||
}
|
||||
|
||||
break
|
||||
case 3:
|
||||
var txt = elem.textContent.trim()
|
||||
if (txt) {
|
||||
obj.__extra__.push(txt)
|
||||
}
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
elem.parentNode.removeChild(elem)
|
||||
})
|
||||
return obj
|
||||
}
|
||||
|
||||
function parseVmValue(vm, key, val) {
|
||||
if (arguments.length === 2) {
|
||||
var oval = Function('o', 'return o.' + key)(vm)
|
||||
if (oval && typeof oval === 'object') {
|
||||
try {
|
||||
return oval.$model
|
||||
} catch (err) {}
|
||||
}
|
||||
return oval
|
||||
} else if (arguments.length === 3) {
|
||||
Function('o', 'v', 'return o.' + key + ' = v')(vm, val)
|
||||
}
|
||||
}
|
||||
|
||||
Anot.components = {}
|
||||
Anot.component = function(name, opts) {
|
||||
if (opts) {
|
||||
Anot.components[name] = Anot.mix({}, componentHooks, opts)
|
||||
}
|
||||
for (var i = 0, obj; (obj = componentQueue[i]); i++) {
|
||||
if (name === obj.name) {
|
||||
componentQueue.splice(i, 1)
|
||||
i--
|
||||
// (obj, Anot.components[name], obj.element, obj.name)
|
||||
;(function(host, hooks, elem, widget) {
|
||||
//如果elem已从Document里移除,直接返回
|
||||
if (!Anot.contains(DOC, elem) || elem.msResolved) {
|
||||
Anot.Array.remove(componentQueue, host)
|
||||
return
|
||||
}
|
||||
|
||||
var dependencies = 1
|
||||
|
||||
//===========收集各种配置=======
|
||||
if (elem.getAttribute(':attr-uuid')) {
|
||||
//如果还没有解析完,就延迟一下 #1155
|
||||
return
|
||||
}
|
||||
hooks.watch = hooks.watch || {}
|
||||
var parentVm = host.vmodels.concat().pop()
|
||||
var state = {}
|
||||
var props = getOptionsFromTag(elem, host.vmodels)
|
||||
var $id = props.uuid || generateID(widget)
|
||||
var slots = { __extra__: [] }
|
||||
|
||||
// 对象组件的子父vm关系, 只存最顶层的$components对象中,
|
||||
while (parentVm.$up && parentVm.$up.__WIDGET__ === name) {
|
||||
parentVm = parentVm.$up
|
||||
}
|
||||
|
||||
if (elem.childNodes.length) {
|
||||
slots = parseSlot(elem.childNodes, host.vmodels)
|
||||
}
|
||||
var txtContent = slots.__extra__.join('')
|
||||
delete slots.__extra__
|
||||
elem.text = function() {
|
||||
return txtContent
|
||||
}
|
||||
|
||||
if (props.hasOwnProperty(':disabled')) {
|
||||
var disabledKey = props[':disabled']
|
||||
var disabledKeyReverse = false
|
||||
if (disabledKey.indexOf('!') === 0) {
|
||||
disabledKey = disabledKey.slice(1)
|
||||
disabledKeyReverse = true
|
||||
}
|
||||
state.disabled = parseVmValue(parentVm, disabledKey)
|
||||
if (disabledKeyReverse) {
|
||||
state.disabled = !state.disabled
|
||||
}
|
||||
|
||||
parentVm.$watch(disabledKey, function(val) {
|
||||
if (disabledKeyReverse) {
|
||||
val = !val
|
||||
}
|
||||
Anot.vmodels[$id].disabled = val
|
||||
})
|
||||
|
||||
delete props[':disabled']
|
||||
}
|
||||
if (props.hasOwnProperty(':loading')) {
|
||||
var loadingKey = props[':loading']
|
||||
var loadingKeyReverse = false
|
||||
if (loadingKey.indexOf('!') === 0) {
|
||||
loadingKey = loadingKey.slice(1)
|
||||
loadingKeyReverse = true
|
||||
}
|
||||
state.loading = parseVmValue(parentVm, loadingKey)
|
||||
if (loadingKeyReverse) {
|
||||
state.loading = !state.loading
|
||||
}
|
||||
parentVm.$watch(loadingKey, function(val) {
|
||||
if (loadingKeyReverse) {
|
||||
val = !val
|
||||
}
|
||||
Anot.vmodels[$id].loading = val
|
||||
})
|
||||
delete props[':loading']
|
||||
}
|
||||
|
||||
// :value可实现双向同步值
|
||||
if (props.hasOwnProperty(':value')) {
|
||||
var valueKey = props[':value']
|
||||
var valueWatcher = function() {
|
||||
var val = parseVmValue(parentVm, valueKey)
|
||||
Anot.vmodels[$id].value = val
|
||||
}
|
||||
var childValueWatcher = function() {
|
||||
var val = this.value
|
||||
if (val && typeof val === 'object') {
|
||||
val = val.$model
|
||||
}
|
||||
parseVmValue(parentVm, valueKey, val)
|
||||
}
|
||||
state.value = parseVmValue(parentVm, valueKey)
|
||||
|
||||
if (hooks.watch.value) {
|
||||
hooks.watch.value = [hooks.watch.value]
|
||||
} else {
|
||||
hooks.watch.value = []
|
||||
}
|
||||
if (hooks.watch['value.length']) {
|
||||
hooks.watch['value.length'] = [hooks.watch['value.length']]
|
||||
} else {
|
||||
hooks.watch['value.length'] = []
|
||||
}
|
||||
if (hooks.watch['value.*']) {
|
||||
hooks.watch['value.*'] = [hooks.watch['value.*']]
|
||||
} else {
|
||||
hooks.watch['value.*'] = []
|
||||
}
|
||||
|
||||
parentVm.$watch(valueKey, valueWatcher)
|
||||
if (Array.isArray(state.value)) {
|
||||
parentVm.$watch(valueKey + '.*', valueWatcher)
|
||||
parentVm.$watch(valueKey + '.length', valueWatcher)
|
||||
hooks.watch['value.*'].push(childValueWatcher)
|
||||
hooks.watch['value.length'].push(childValueWatcher)
|
||||
} else {
|
||||
hooks.watch.value.push(childValueWatcher)
|
||||
}
|
||||
|
||||
delete props[':value']
|
||||
}
|
||||
|
||||
delete props.uuid
|
||||
delete props.name
|
||||
delete props.isWidget
|
||||
|
||||
hooks.props = hooks.props || {}
|
||||
hooks.state = hooks.state || {}
|
||||
|
||||
Object.assign(hooks.props, props)
|
||||
Object.assign(hooks.state, state)
|
||||
|
||||
var __READY__ = false
|
||||
|
||||
hooks.__init__.call(elem, hooks.props, hooks.state, function next() {
|
||||
__READY__ = true
|
||||
|
||||
delete elem.text
|
||||
})
|
||||
|
||||
if (!__READY__) {
|
||||
return
|
||||
}
|
||||
|
||||
hooks.$id = $id
|
||||
|
||||
//==========构建VM=========
|
||||
var {
|
||||
componentWillMount,
|
||||
componentDidMount,
|
||||
childComponentDidMount,
|
||||
componentWillUnmount,
|
||||
render
|
||||
} = hooks
|
||||
|
||||
delete hooks.__init__
|
||||
delete hooks.componentWillMount
|
||||
delete hooks.componentDidMount
|
||||
delete hooks.childComponentDidMount
|
||||
delete hooks.componentWillUnmount
|
||||
|
||||
var vmodel = Anot(hooks)
|
||||
Anot.vmodels[vmodel.$id] = vmodel
|
||||
hideProperty(vmodel, '__WIDGET__', name)
|
||||
hideProperty(vmodel, '$recycle', function() {
|
||||
for (var i in this.$events) {
|
||||
var ev = this.$events[i] || []
|
||||
var len = ev.length
|
||||
while (len--) {
|
||||
if (ev[len].type === null || ev[len].type === 'user-watcher') {
|
||||
ev.splice(len, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
delete vmodel.$mounted
|
||||
|
||||
// 对象组件的子父vm关系, 只存最顶层的$components对象中,
|
||||
// 而子vm, 无论向下多少级, 他们的$up对象也只存最顶层的组件vm
|
||||
parentVm.$components.push(vmodel)
|
||||
if (parentVm.__WIDGET__ === name) {
|
||||
vmodel.$up = parentVm
|
||||
}
|
||||
|
||||
elem.msResolved = 1 //防止二进扫描此元素
|
||||
|
||||
componentWillMount.call(vmodel)
|
||||
|
||||
Anot.clearHTML(elem)
|
||||
var html = render.call(vmodel, slots) || ''
|
||||
|
||||
html = html.replace(/<\w+[^>]*>/g, function(m, s) {
|
||||
return m.replace(/[\n\t\s]{1,}/g, ' ')
|
||||
})
|
||||
|
||||
elem.innerHTML = html
|
||||
|
||||
hideProperty(vmodel, '$elem', elem)
|
||||
elem.__VM__ = vmodel
|
||||
|
||||
Anot.fireDom(elem, 'datasetchanged', {
|
||||
vm: vmodel,
|
||||
childReady: 1
|
||||
})
|
||||
|
||||
var children = 0
|
||||
var removeFn = Anot.bind(elem, 'datasetchanged', function(ev) {
|
||||
if (ev.childReady) {
|
||||
dependencies += ev.childReady
|
||||
if (vmodel.$id !== ev.vm.$id) {
|
||||
if (ev.childReady === -1) {
|
||||
children++
|
||||
childComponentDidMount.call(vmodel, ev.vm)
|
||||
}
|
||||
ev.stopPropagation()
|
||||
}
|
||||
}
|
||||
if (dependencies === 0) {
|
||||
var timer = setTimeout(function() {
|
||||
clearTimeout(timer)
|
||||
elem.removeAttribute('is-widget')
|
||||
componentDidMount.call(vmodel)
|
||||
}, children ? Math.max(children * 17, 100) : 17)
|
||||
|
||||
Anot.unbind(elem, 'datasetchanged', removeFn)
|
||||
//==================
|
||||
host.rollback = function() {
|
||||
try {
|
||||
componentWillUnmount.call(vmodel)
|
||||
} catch (e) {}
|
||||
parentVm.$recycle && parentVm.$recycle()
|
||||
Anot.Array.remove(parentVm.$components, vmodel)
|
||||
delete Anot.vmodels[vmodel.$id]
|
||||
}
|
||||
injectDisposeQueue(host, widgetList)
|
||||
if (window.chrome) {
|
||||
elem.addEventListener('DOMNodeRemovedFromDocument', function() {
|
||||
setTimeout(rejectDisposeQueue)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
scanTag(elem, [vmodel])
|
||||
|
||||
if (!elem.childNodes.length) {
|
||||
Anot.fireDom(elem, 'datasetchanged', {
|
||||
vm: vmodel,
|
||||
childReady: -1
|
||||
})
|
||||
} else {
|
||||
var id2 = setTimeout(function() {
|
||||
clearTimeout(id2)
|
||||
Anot.fireDom(elem, 'datasetchanged', {
|
||||
vm: vmodel,
|
||||
childReady: -1
|
||||
})
|
||||
}, 17)
|
||||
}
|
||||
})(obj, toJson(Anot.components[name]), obj.element, obj.name) // jshint ignore:line
|
||||
}
|
||||
}
|
||||
}
|
110
src/16-:attr.js
110
src/16-:attr.js
|
@ -9,6 +9,25 @@ bools.replace(rword, function(name) {
|
|||
boolMap[name.toLowerCase()] = name
|
||||
})
|
||||
|
||||
var propMap = {
|
||||
//属性名映射
|
||||
'accept-charset': 'acceptCharset',
|
||||
char: 'ch',
|
||||
charoff: 'chOff',
|
||||
class: 'className',
|
||||
for: 'htmlFor',
|
||||
'http-equiv': 'httpEquiv'
|
||||
}
|
||||
|
||||
var anomaly = [
|
||||
'accessKey,bgColor,cellPadding,cellSpacing,codeBase,codeType,colSpan',
|
||||
'dateTime,defaultValue,frameBorder,longDesc,maxLength,marginWidth,marginHeight',
|
||||
'rowSpan,tabIndex,useMap,vSpace,valueType,vAlign'
|
||||
].join(',')
|
||||
anomaly.replace(rword, function(name) {
|
||||
propMap[name.toLowerCase()] = name
|
||||
})
|
||||
|
||||
var attrDir = Anot.directive('attr', {
|
||||
init: function(binding) {
|
||||
//{{aaa}} --> aaa
|
||||
|
@ -27,9 +46,8 @@ var attrDir = Anot.directive('attr', {
|
|||
'data-loaded',
|
||||
binding.vmodels
|
||||
)
|
||||
// 是否直接替换当前容器
|
||||
var outer = (binding.includeReplace = elem.hasAttribute('replace'))
|
||||
if (elem.hasAttribute('cache')) {
|
||||
var outer = (binding.includeReplace = !!Anot(elem).data('includeReplace'))
|
||||
if (Anot(elem).data('cache')) {
|
||||
binding.templateCache = {}
|
||||
}
|
||||
binding.start = DOC.createComment(':include')
|
||||
|
@ -47,18 +65,18 @@ var attrDir = Anot.directive('attr', {
|
|||
},
|
||||
update: function(val) {
|
||||
var elem = this.element
|
||||
var obj = Object.create(null)
|
||||
var isSVG = rsvg.test(elem)
|
||||
var obj = {}
|
||||
var vm = this.vmodels[0]
|
||||
|
||||
val = toJson(val)
|
||||
|
||||
if (this.param) {
|
||||
if (val && typeof val === 'object') {
|
||||
if (typeof val === 'object' && val !== null) {
|
||||
if (Array.isArray(val)) {
|
||||
obj[this.param] = val
|
||||
} else {
|
||||
if (Date.isDate(val)) {
|
||||
obj[this.param] = val.toISOString()
|
||||
obj[this.param] = val.toUTCString()
|
||||
} else {
|
||||
obj[this.param] = val
|
||||
}
|
||||
|
@ -67,12 +85,10 @@ var attrDir = Anot.directive('attr', {
|
|||
obj[this.param] = val
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
!val ||
|
||||
typeof val !== 'object' ||
|
||||
Array.isArray(val) ||
|
||||
Date.isDate(val)
|
||||
) {
|
||||
if (!val || typeof val !== 'object' || Array.isArray(val)) {
|
||||
return
|
||||
}
|
||||
if (Date.isDate(val)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -81,50 +97,72 @@ var attrDir = Anot.directive('attr', {
|
|||
|
||||
for (var i in obj) {
|
||||
if (i === 'style') {
|
||||
elem.style.cssText = obj[i]
|
||||
console.error('设置style样式, 请改用 :css指令')
|
||||
continue
|
||||
}
|
||||
|
||||
if (i.slice(0, 6) === 'xlink:') {
|
||||
var k = i
|
||||
i = i.slice(6)
|
||||
obj[i] = obj[k]
|
||||
delete obj[k]
|
||||
// 通过属性设置回调,必须以@符号开头
|
||||
if (i.indexOf('@') === 0) {
|
||||
if (typeof obj[i] !== 'function') {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if (i === 'href' || i === 'src') {
|
||||
//处理IE67自动转义的问题
|
||||
if (!root.hasAttribute) obj[i] = obj[i].replace(/&/g, '&')
|
||||
|
||||
elem[i] = obj[i]
|
||||
|
||||
//chrome v37- 下embed标签动态设置的src,无法发起请求
|
||||
if (window.chrome && elem.tagName === 'EMBED') {
|
||||
var _parent = elem.parentNode
|
||||
var com = DOC.createComment(':src')
|
||||
_parent.replaceChild(com, elem)
|
||||
_parent.replaceChild(elem, com)
|
||||
}
|
||||
} else {
|
||||
var k = i
|
||||
//古董IE下,部分属性名字要进行映射
|
||||
if (!W3C && propMap[k]) {
|
||||
k = propMap[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]
|
||||
}
|
||||
if (typeof elem[boolMap[k]] === 'boolean') {
|
||||
//布尔属性必须使用el.xxx = true|false方式设值
|
||||
obj[i] = !!obj[i]
|
||||
elem[k] = obj[i]
|
||||
elem[boolMap[k]] = !!obj[i]
|
||||
|
||||
//如果为false, IE全系列下相当于setAttribute(xxx, ''),会影响到样式,需要进一步处理
|
||||
if (!obj[i]) {
|
||||
obj[i] = !!obj[i]
|
||||
}
|
||||
if (obj[i] === false) {
|
||||
elem.removeAttribute(k)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
//SVG只能使用setAttribute(xxx, yyy), HTML的固有属性必须elem.xxx = yyy
|
||||
var isInnate = isSVG ? false : i in elem.cloneNode(false)
|
||||
//SVG只能使用setAttribute(xxx, yyy), VML只能使用elem.xxx = yyy ,HTML的固有属性必须elem.xxx = yyy
|
||||
var isInnate = rsvg.test(elem)
|
||||
? false
|
||||
: DOC.namespaces && isVML(elem)
|
||||
? true
|
||||
: k in elem.cloneNode(false)
|
||||
if (isInnate) {
|
||||
elem[i] = obj[i]
|
||||
elem[k] = obj[i]
|
||||
} else {
|
||||
if (typeof obj[i] === 'object') {
|
||||
obj[i] = Date.isDate(obj[i])
|
||||
? obj[i].toISOString()
|
||||
? obj[i].toUTCString()
|
||||
: JSON.stringify(obj[i])
|
||||
} else if (typeof obj[i] === 'function') {
|
||||
k = ronattr + camelize(k.slice(1))
|
||||
elem[k] = obj[i].bind(vm)
|
||||
obj[i] = k
|
||||
}
|
||||
elem.setAttribute(k, obj[i])
|
||||
}
|
||||
elem.setAttribute(i, obj[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,24 @@
|
|||
//类名定义 :class="{xx: yy}" :class="xx"
|
||||
//类名定义, :class="xx:yy" :class="{xx: yy}" :class="xx" :class="{{xx}}"
|
||||
Anot.directive('class', {
|
||||
init: function(binding) {
|
||||
binding.expr = binding.expr.replace(/\n/g, ' ').replace(/\s+/g, ' ')
|
||||
var expr = []
|
||||
if (!/^\{.*\}$/.test(binding.expr)) {
|
||||
expr = binding.expr.split(':')
|
||||
expr[1] = (expr[1] && expr[1].trim()) || 'true'
|
||||
var arr = expr[0].split(/\s+/)
|
||||
binding.expr =
|
||||
'{' +
|
||||
arr
|
||||
.map(function(it) {
|
||||
return it + ': ' + expr[1]
|
||||
})
|
||||
.join(', ') +
|
||||
'}'
|
||||
} else if (/^\{\{.*\}\}$/.test(binding.expr)) {
|
||||
binding.expr = binding.expr.slice(2, -2)
|
||||
}
|
||||
|
||||
if (binding.type === 'hover' || binding.type === 'active') {
|
||||
var expr = new Function('return ' + binding.expr)()
|
||||
|
||||
//确保只绑定一次
|
||||
if (!binding.hasBindEvent) {
|
||||
var elem = binding.element
|
||||
|
@ -18,16 +31,16 @@ Anot.directive('class', {
|
|||
activate = 'mousedown'
|
||||
abandon = 'mouseup'
|
||||
var fn0 = $elem.bind('mouseleave', function() {
|
||||
$elem.removeClass(expr)
|
||||
$elem.removeClass(expr[0])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var fn1 = $elem.bind(activate, function() {
|
||||
$elem.addClass(expr)
|
||||
$elem.addClass(expr[0])
|
||||
})
|
||||
var fn2 = $elem.bind(abandon, function() {
|
||||
$elem.removeClass(expr)
|
||||
$elem.removeClass(expr[0])
|
||||
})
|
||||
binding.rollback = function() {
|
||||
$elem.unbind('mouseleave', fn0)
|
||||
|
@ -59,8 +72,9 @@ Anot.directive('class', {
|
|||
obj = obj.$model
|
||||
}
|
||||
|
||||
var $elem = Anot(this.element)
|
||||
for (var i in obj) {
|
||||
this.element.classList.toggle(i, !!obj[i])
|
||||
$elem.toggleClass(i, !!obj[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -4,22 +4,22 @@ Anot.directive('css', {
|
|||
init: directives.attr.init,
|
||||
update: function(val) {
|
||||
var $elem = Anot(this.element)
|
||||
if (this.param) {
|
||||
$elem.css(this.param, val)
|
||||
} else {
|
||||
if (typeof val !== 'object') {
|
||||
return log(
|
||||
':css指令格式错误 %c %s="%s"',
|
||||
'color:#f00',
|
||||
this.name,
|
||||
this.expr
|
||||
)
|
||||
}
|
||||
if (!this.param) {
|
||||
var obj = val
|
||||
if (!Anot.isPlainObject(obj)) {
|
||||
obj = val.$model
|
||||
try {
|
||||
if (typeof val === 'object') {
|
||||
if (!Anot.isPlainObject(val)) obj = val.$model
|
||||
} else {
|
||||
obj = new Function('return ' + val)()
|
||||
}
|
||||
$elem.css(obj)
|
||||
for (var i in obj) {
|
||||
$elem.css(i, obj[i])
|
||||
}
|
||||
} catch (err) {
|
||||
log('样式格式错误 %c %s="%s"', 'color:#f00', this.name, this.expr)
|
||||
}
|
||||
} else {
|
||||
$elem.css(this.param, val)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -3,25 +3,17 @@ Anot.directive('data', {
|
|||
priority: 100,
|
||||
init: directives.attr.init,
|
||||
update: function(val) {
|
||||
var $el = Anot(this.element)
|
||||
if (this.param) {
|
||||
$el.data(this.param, val)
|
||||
} else {
|
||||
if (typeof val !== 'object') {
|
||||
return log(
|
||||
':data指令格式错误 %c %s="%s"',
|
||||
'color:#f00',
|
||||
this.name,
|
||||
this.expr
|
||||
)
|
||||
}
|
||||
var obj = val
|
||||
if (!Anot.isPlainObject(obj)) {
|
||||
obj = val.$model
|
||||
}
|
||||
if (typeof obj === 'object' && obj !== null) {
|
||||
if (!Anot.isPlainObject(obj)) obj = val.$model
|
||||
|
||||
for (var i in obj) {
|
||||
$el.data(i, obj[i])
|
||||
this.element.setAttribute('data-' + i, obj[i])
|
||||
}
|
||||
} else {
|
||||
if (!this.param) return
|
||||
|
||||
this.element.setAttribute('data-' + this.param, obj)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
129
src/20-:rule.js
129
src/20-:rule.js
|
@ -0,0 +1,129 @@
|
|||
/*------ 表单验证 -------*/
|
||||
var __rules = {}
|
||||
Anot.validate = function(key, cb) {
|
||||
if (!__rules[key]) {
|
||||
throw new Error('validate [' + key + '] not exists.')
|
||||
}
|
||||
if (typeof cb === 'function') {
|
||||
__rules[key].event = cb
|
||||
}
|
||||
var result = __rules[key].result
|
||||
for (var k in result) {
|
||||
if (!result[k].passed) {
|
||||
return result[k]
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Anot.directive('rule', {
|
||||
priority: 2010,
|
||||
init: function(binding) {
|
||||
if (binding.param && !__rules[binding.param]) {
|
||||
__rules[binding.param] = {
|
||||
event: noop,
|
||||
result: {}
|
||||
}
|
||||
}
|
||||
binding.target = __rules[binding.param]
|
||||
},
|
||||
update: function(opt) {
|
||||
var _this = this
|
||||
var elem = this.element
|
||||
if (!['INPUT', 'TEXTAREA'].includes(elem.nodeName)) {
|
||||
return
|
||||
}
|
||||
if (elem.msBinded) {
|
||||
return
|
||||
}
|
||||
if (this.target) {
|
||||
this.target.result[elem.expr] = { key: elem.expr }
|
||||
}
|
||||
var target = this.target
|
||||
|
||||
// 0: 验证通过
|
||||
// 10001: 不能为空
|
||||
// 10002: 必须为合法数字
|
||||
// 10003: Email格式错误
|
||||
// 10004: 手机格式错误
|
||||
// 10005: 必须为纯中文
|
||||
// 10006: 格式匹配错误(正则)
|
||||
// 10011: 输入值超过指定最大长度
|
||||
// 10012: 输入值短于指定最小长度
|
||||
// 10021: 输入值大于指定最大数值
|
||||
// 10022: 输入值小于指定最小数值
|
||||
// 10031: 与指定的表单的值不一致
|
||||
function checked(ev) {
|
||||
var val = elem.value
|
||||
var code = 0
|
||||
|
||||
if (opt.require && (val === '' || val === null)) {
|
||||
code = 10001
|
||||
}
|
||||
|
||||
if (code === 0 && opt.isNumeric) {
|
||||
code = !isFinite(val) ? 10002 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.isEmail)
|
||||
code = !/^[\w\.\-]+@\w+([\.\-]\w+)*\.\w+$/.test(val) ? 10003 : 0
|
||||
|
||||
if (code === 0 && opt.isPhone) {
|
||||
code = !/^1[34578]\d{9}$/.test(val) ? 10004 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.isCN) {
|
||||
code = !/^[\u4e00-\u9fa5]+$/.test(val) ? 10005 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.exp) {
|
||||
code = !opt.exp.test(val) ? 10006 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.maxLen) {
|
||||
code = val.length > opt.maxLen ? 10011 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.minLen) {
|
||||
code = val.length < opt.minLen ? 10012 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.hasOwnProperty('max')) {
|
||||
code = val > opt.max ? 10021 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.hasOwnProperty('min')) {
|
||||
code = val < opt.min ? 10022 : 0
|
||||
}
|
||||
|
||||
if (code === 0 && opt.eq) {
|
||||
var eqVal = parseVmValue(_this.vmodels[0], opt.eq)
|
||||
code = val !== eqVal ? 10031 : 0
|
||||
}
|
||||
|
||||
target.result[elem.expr].code = code
|
||||
target.result[elem.expr].passed = opt.require
|
||||
? code === 0
|
||||
: val
|
||||
? code === 0
|
||||
: true
|
||||
|
||||
var passed = true
|
||||
for (var k in target.result) {
|
||||
if (!target.result[k].passed) {
|
||||
passed = false
|
||||
target.event(target.result[k])
|
||||
break
|
||||
}
|
||||
}
|
||||
if (passed) {
|
||||
target.event(true)
|
||||
}
|
||||
}
|
||||
Anot(elem).bind('blur', checked)
|
||||
this.rollback = function() {
|
||||
Anot(elem).unbind('blur', checked)
|
||||
}
|
||||
elem.msBinded = true
|
||||
checked()
|
||||
}
|
||||
})
|
|
@ -72,11 +72,7 @@ var duplexBinding = Anot.directive('duplex', {
|
|||
return
|
||||
}
|
||||
|
||||
var lastValue = binding.pipe(
|
||||
val,
|
||||
binding,
|
||||
'get'
|
||||
)
|
||||
var lastValue = binding.pipe(val, binding, 'get')
|
||||
binding.oldValue = val
|
||||
binding.setter(lastValue)
|
||||
|
||||
|
@ -86,11 +82,7 @@ var duplexBinding = Anot.directive('duplex', {
|
|||
switch (binding.xtype) {
|
||||
case 'radio':
|
||||
bound('click', function() {
|
||||
var lastValue = binding.pipe(
|
||||
elem.value,
|
||||
binding,
|
||||
'get'
|
||||
)
|
||||
var lastValue = binding.pipe(elem.value, binding, 'get')
|
||||
binding.setter(lastValue)
|
||||
callback.call(elem, lastValue)
|
||||
})
|
||||
|
@ -103,11 +95,7 @@ var duplexBinding = Anot.directive('duplex', {
|
|||
log(':duplex应用于checkbox上要对应一个数组')
|
||||
array = [array]
|
||||
}
|
||||
var val = binding.pipe(
|
||||
elem.value,
|
||||
binding,
|
||||
'get'
|
||||
)
|
||||
var val = binding.pipe(elem.value, binding, 'get')
|
||||
Anot.Array[method](array, val)
|
||||
callback.call(elem, array)
|
||||
})
|
||||
|
@ -118,27 +106,21 @@ var duplexBinding = Anot.directive('duplex', {
|
|||
case 'input':
|
||||
bound('input', updateVModel)
|
||||
bound('keyup', updateVModel)
|
||||
if (!IEVersion) {
|
||||
bound('compositionstart', compositionStart)
|
||||
bound('compositionend', compositionEnd)
|
||||
bound('DOMAutoComplete', updateVModel)
|
||||
}
|
||||
break
|
||||
case 'select':
|
||||
bound('change', function() {
|
||||
var val = Anot(elem).val() //字符串或字符串数组
|
||||
if (Array.isArray(val)) {
|
||||
val = val.map(function(v) {
|
||||
return binding.pipe(
|
||||
v,
|
||||
binding,
|
||||
'get'
|
||||
)
|
||||
return binding.pipe(v, binding, 'get')
|
||||
})
|
||||
} else {
|
||||
val = binding.pipe(
|
||||
val,
|
||||
binding,
|
||||
'get'
|
||||
)
|
||||
val = binding.pipe(val, binding, 'get')
|
||||
}
|
||||
if (val + '' !== binding.oldValue) {
|
||||
try {
|
||||
|
@ -194,11 +176,7 @@ var duplexBinding = Anot.directive('duplex', {
|
|||
elem.value = value
|
||||
break
|
||||
case 'change':
|
||||
curValue = this.pipe(
|
||||
value,
|
||||
this,
|
||||
'set'
|
||||
) //fix #673
|
||||
curValue = this.pipe(value, this, 'set') //fix #673
|
||||
if (curValue !== this.oldValue) {
|
||||
var fixCaret = false
|
||||
if (elem.msFocus) {
|
||||
|
@ -223,11 +201,7 @@ var duplexBinding = Anot.directive('duplex', {
|
|||
break
|
||||
case 'checkbox':
|
||||
var array = [].concat(value) //强制转换为数组
|
||||
curValue = this.pipe(
|
||||
elem.value,
|
||||
this,
|
||||
'get'
|
||||
)
|
||||
curValue = this.pipe(elem.value, this, 'get')
|
||||
elem.checked = array.indexOf(curValue) > -1
|
||||
break
|
||||
case 'select':
|
||||
|
|
|
@ -3,18 +3,13 @@ Anot.directive('if', {
|
|||
update: function(val) {
|
||||
var binding = this
|
||||
var elem = this.element
|
||||
var stamp = (binding.stamp = Date.now())
|
||||
var stamp = (binding.stamp = +new Date())
|
||||
var par
|
||||
var after = function() {
|
||||
if (stamp !== binding.stamp) {
|
||||
return
|
||||
}
|
||||
if (stamp !== binding.stamp) return
|
||||
binding.recoverNode = null
|
||||
}
|
||||
if (binding.recoverNode) {
|
||||
binding.recoverNode() // 还原现场,有移动节点的都需要还原现场
|
||||
}
|
||||
|
||||
if (binding.recoverNode) binding.recoverNode() // 还原现场,有移动节点的都需要还原现场
|
||||
try {
|
||||
if (!elem.parentNode) return
|
||||
par = elem.parentNode
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
var getXHR = function() {
|
||||
return new window.XMLHttpRequest() // jshint ignore:line
|
||||
}
|
||||
//将所有远程加载的模板,以字符串形式存放到这里
|
||||
var templatePool = (Anot.templateCache = {})
|
||||
|
||||
|
@ -20,53 +23,27 @@ function nodesToFrag(nodes) {
|
|||
}
|
||||
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', {
|
||||
init: directives.attr.init,
|
||||
update: function(val) {
|
||||
if (!val) {
|
||||
return
|
||||
}
|
||||
|
||||
var binding = this
|
||||
var elem = this.element
|
||||
var vmodels = binding.vmodels
|
||||
var loaded = binding.includeLoaded // 加载完的回调
|
||||
var rendered = binding.includeRendered // 渲染完的回调
|
||||
var rendered = binding.includeRendered
|
||||
var effectClass = binding.effectName && binding.effectClass // 是否开启动画
|
||||
var templateCache = binding.templateCache // 是否开启 缓存
|
||||
var outer = binding.includeReplace // 是否替换容器
|
||||
var templateCache = binding.templateCache // 是否data-include-cache
|
||||
var outer = binding.includeReplace // 是否data-include-replace
|
||||
var loaded = binding.includeLoaded
|
||||
var target = outer ? elem.parentNode : elem
|
||||
var _ele = binding._element // replace binding.element === binding.end
|
||||
var _ele = binding._element // data-include-replace binding.element === binding.end
|
||||
|
||||
binding.recoverNodes = binding.recoverNodes || Anot.noop
|
||||
|
||||
var scanTemplate = function(text) {
|
||||
var _stamp = (binding._stamp = Date.now()) // 过滤掉频繁操作
|
||||
var _stamp = (binding._stamp = +new Date()) // 过滤掉频繁操作
|
||||
if (loaded) {
|
||||
var newText = loaded.apply(target, [text].concat(vmodels))
|
||||
if (typeof newText === 'string') {
|
||||
text = newText
|
||||
}
|
||||
if (typeof newText === 'string') text = newText
|
||||
}
|
||||
if (rendered) {
|
||||
checkScan(
|
||||
|
@ -127,7 +104,7 @@ Anot.directive('include', {
|
|||
if (outer && effectClass) {
|
||||
enterEl = _ele
|
||||
enterEl.innerHTML = '' // 清空
|
||||
enterEl.setAttribute('skip', '')
|
||||
enterEl.setAttribute(':skip', 'true')
|
||||
target.insertBefore(enterEl, binding.end.nextSibling) // 插入到bingding.end之后避免被错误的移动
|
||||
before = function() {
|
||||
enterEl.insertBefore(fragment, null) // 插入节点
|
||||
|
@ -156,23 +133,51 @@ Anot.directive('include', {
|
|||
Anot.effect.apply(enterEl, 'enter', before, after)
|
||||
}
|
||||
|
||||
if (templatePool[val]) {
|
||||
if (!val) return
|
||||
|
||||
var el = val
|
||||
|
||||
if (typeof el === 'object') {
|
||||
if (el.nodeType !== 1) return log('include 不支持非DOM对象')
|
||||
} else {
|
||||
el = DOC.getElementById(val)
|
||||
if (!el) {
|
||||
if (typeof templatePool[val] === 'string') {
|
||||
Anot.nextTick(function() {
|
||||
scanTemplate(templatePool[val])
|
||||
})
|
||||
} else if (Array.isArray(templatePool[val])) {
|
||||
//#805 防止在循环绑定中发出许多相同的请求
|
||||
templatePool[val].push(scanTemplate)
|
||||
} else {
|
||||
_fetch(val)
|
||||
.then(text => {
|
||||
var xhr = getXHR()
|
||||
xhr.onload = function() {
|
||||
if (xhr.status !== 200)
|
||||
return log('获取网络资源出错, httpError[' + xhr.status + ']')
|
||||
|
||||
var text = xhr.responseText
|
||||
for (var f = 0, fn; (fn = templatePool[val][f++]); ) {
|
||||
fn(text)
|
||||
}
|
||||
templatePool[val] = text
|
||||
scanTemplate(text)
|
||||
})
|
||||
.catch(err => {
|
||||
log(
|
||||
':include load [' + val + '] error\n%c%s',
|
||||
'color:#f30',
|
||||
`获取网络资源出错, ${err.status} (${err.statusText})`
|
||||
)
|
||||
})
|
||||
}
|
||||
xhr.onerror = function() {
|
||||
log(':include load [' + val + '] error')
|
||||
}
|
||||
templatePool[val] = [scanTemplate]
|
||||
xhr.open('GET', val, true)
|
||||
if ('withCredentials' in xhr) {
|
||||
xhr.withCredentials = true
|
||||
}
|
||||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
|
||||
xhr.send(null)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Anot.nextTick(function() {
|
||||
scanTemplate(el.value || el.innerText || el.innerHTML)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
|
@ -125,7 +125,7 @@ Anot.directive('for', {
|
|||
var valueItem = value[i]
|
||||
proxy[param] = valueItem //index
|
||||
if (Object(valueItem) === valueItem) {
|
||||
hideProperty(valueItem, '$ups', valueItem.$ups || {})
|
||||
valueItem.$ups = valueItem.$ups || {}
|
||||
valueItem.$ups[param] = proxy
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -4,9 +4,11 @@ function parseDisplay(nodeName, val) {
|
|||
if (!parseDisplay[key]) {
|
||||
var node = DOC.createElement(nodeName)
|
||||
root.appendChild(node)
|
||||
|
||||
if (W3C) {
|
||||
val = getComputedStyle(node, null).display
|
||||
|
||||
} else {
|
||||
val = node.currentStyle.display
|
||||
}
|
||||
root.removeChild(node)
|
||||
parseDisplay[key] = val
|
||||
}
|
||||
|
@ -25,7 +27,7 @@ Anot.directive('visible', {
|
|||
stamp
|
||||
var noEffect = !this.effectName
|
||||
if (!this.stamp) {
|
||||
stamp = this.stamp = Date.now()
|
||||
stamp = this.stamp = +new Date()
|
||||
if (val) {
|
||||
elem.style.display = binding.display || ''
|
||||
if (Anot(elem).css('display') === 'none') {
|
||||
|
|
|
@ -88,7 +88,7 @@ var filters = (Anot.filters = {
|
|||
// <a href="jav ascript:alert('XSS');">IE67chrome</a>
|
||||
// <a href="jav	ascript:alert('XSS');">IE67chrome</a>
|
||||
// <a href="jav
ascript:alert('XSS');">IE67chrome</a>
|
||||
xss: function(str) {
|
||||
sanitize: function(str) {
|
||||
return str.replace(rscripts, '').replace(ropen, function(a, b) {
|
||||
var match = a.toLowerCase().match(/<(\w+)\s/)
|
||||
if (match) {
|
||||
|
@ -127,20 +127,25 @@ var filters = (Anot.filters = {
|
|||
},
|
||||
number: numberFormat,
|
||||
//日期格式化,类似php的date函数,
|
||||
date: function(stamp, str) {
|
||||
var oDate = stamp
|
||||
|
||||
if (!Date.isDate(oDate)) {
|
||||
var tmp = +oDate
|
||||
if (tmp === tmp) {
|
||||
oDate = tmp
|
||||
date: function(stamp, str, second) {
|
||||
second = second === undefined ? false : true
|
||||
var oDate
|
||||
if (!Date.isDate(stamp)) {
|
||||
if (!/[^\d]/.test(stamp)) {
|
||||
stamp -= 0
|
||||
if (second) {
|
||||
stamp *= 1000
|
||||
}
|
||||
}
|
||||
|
||||
oDate = new Date(oDate)
|
||||
if (oDate.toString() === 'Invalid Date') {
|
||||
oDate = new Date(stamp)
|
||||
if (oDate + '' === 'Invalid Date') {
|
||||
return 'Invalid Date'
|
||||
}
|
||||
} else {
|
||||
oDate = stamp
|
||||
}
|
||||
return oDate.format(str)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -0,0 +1,677 @@
|
|||
/*********************************************************************
|
||||
* AMD加载器 *
|
||||
**********************************************************************/
|
||||
|
||||
//https://www.devbridge.com/articles/understanding-amd-requirejs/
|
||||
//http://maxogden.com/nested-dependencies.html
|
||||
var modules = (Anot.modules = {
|
||||
'domReady!': {
|
||||
exports: Anot,
|
||||
state: 3
|
||||
},
|
||||
Anot: {
|
||||
exports: Anot,
|
||||
state: 4
|
||||
}
|
||||
})
|
||||
//Object(modules[id]).state拥有如下值
|
||||
// undefined 没有定义
|
||||
// 1(send) 已经发出请求
|
||||
// 2(loading) 已经被执行但还没有执行完成,在这个阶段define方法会被执行
|
||||
// 3(loaded) 执行完毕,通过onload/onreadystatechange回调判定,在这个阶段checkDeps方法会执行
|
||||
// 4(execute) 其依赖也执行完毕, 值放到exports对象上,在这个阶段fireFactory方法会执行
|
||||
modules.exports = modules.Anot
|
||||
var otherRequire = window.require
|
||||
var otherDefine = window.define
|
||||
var innerRequire
|
||||
plugins.loader = function(builtin) {
|
||||
var flag = innerRequire && builtin
|
||||
window.require = flag ? innerRequire : otherRequire
|
||||
window.define = flag ? innerRequire.define : otherDefine
|
||||
}
|
||||
new function() {
|
||||
// jshint ignore:line
|
||||
var loadings = [] //正在加载中的模块列表
|
||||
var factorys = [] //放置define方法的factory函数
|
||||
var rjsext = /\.js$/i
|
||||
|
||||
function makeRequest(name, config) {
|
||||
//1. 去掉querystring, hash
|
||||
var query = ''
|
||||
name = name.replace(rquery, function(match) {
|
||||
query = match
|
||||
return ''
|
||||
})
|
||||
|
||||
//2. 去掉扩展名
|
||||
var ext = '.js' //默认拓展名
|
||||
var res = 'js' // 默认资源类型
|
||||
var suffix = ['.js', '.css']
|
||||
name = name.replace(/\.[a-z0-9]+$/g, function(match) {
|
||||
ext = match
|
||||
res = suffix.indexOf(match) > -1 ? match.slice(1) : 'text'
|
||||
return ''
|
||||
})
|
||||
|
||||
//补上协议, 避免引入依赖时判断不正确
|
||||
if (/^\/\//.test(name)) {
|
||||
name = location.protocol + name
|
||||
}
|
||||
var req = Anot.mix(
|
||||
{
|
||||
query: query,
|
||||
ext: ext,
|
||||
res: res,
|
||||
name: name,
|
||||
toUrl: toUrl
|
||||
},
|
||||
config
|
||||
)
|
||||
req.toUrl(name)
|
||||
return req
|
||||
}
|
||||
|
||||
function fireRequest(req) {
|
||||
var name = req.name
|
||||
var res = req.res
|
||||
//1. 如果该模块已经发出请求,直接返回
|
||||
var module = modules[name]
|
||||
var urlNoQuery = name && req.urlNoQuery
|
||||
if (module && module.state >= 1) {
|
||||
return name
|
||||
}
|
||||
module = modules[urlNoQuery]
|
||||
if (module && module.state >= 3) {
|
||||
innerRequire(module.deps || [], module.factory, urlNoQuery)
|
||||
return urlNoQuery
|
||||
}
|
||||
if (name && !module) {
|
||||
module = modules[urlNoQuery] = {
|
||||
id: urlNoQuery,
|
||||
state: 1 //send
|
||||
}
|
||||
var wrap = function(obj) {
|
||||
resources[res] = obj
|
||||
obj.load(name, req, function(a) {
|
||||
if (arguments.length && a !== void 0) {
|
||||
module.exports = a
|
||||
}
|
||||
module.state = 4
|
||||
checkDeps()
|
||||
})
|
||||
}
|
||||
|
||||
if (!resources[res]) {
|
||||
innerRequire([res], wrap)
|
||||
} else {
|
||||
wrap(resources[res])
|
||||
}
|
||||
}
|
||||
return name ? urlNoQuery : res + '!'
|
||||
}
|
||||
|
||||
//核心API之一 require
|
||||
var requireQueue = []
|
||||
var isUserFirstRequire = false
|
||||
innerRequire = Anot.require = function(
|
||||
array,
|
||||
factory,
|
||||
parentUrl,
|
||||
defineConfig
|
||||
) {
|
||||
if (!isUserFirstRequire) {
|
||||
requireQueue.push(Anot.slice(arguments))
|
||||
if (arguments.length <= 2) {
|
||||
isUserFirstRequire = true
|
||||
var queue = requireQueue.splice(0, requireQueue.length),
|
||||
args
|
||||
while ((args = queue.shift())) {
|
||||
innerRequire.apply(null, args)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
if (!Array.isArray(array)) {
|
||||
Anot.error('require方法的第一个参数应为数组 ' + array)
|
||||
}
|
||||
var deps = [] // 放置所有依赖项的完整路径
|
||||
var uniq = createMap()
|
||||
var id = parentUrl || 'callback' + setTimeout('1') // jshint ignore:line
|
||||
|
||||
defineConfig = defineConfig || createMap()
|
||||
defineConfig.baseUrl = kernel.baseUrl
|
||||
var isBuilt = !!defineConfig.built
|
||||
if (parentUrl) {
|
||||
defineConfig.parentUrl = parentUrl.substr(0, parentUrl.lastIndexOf('/'))
|
||||
defineConfig.mapUrl = parentUrl.replace(rjsext, '')
|
||||
}
|
||||
if (isBuilt) {
|
||||
var req = makeRequest(defineConfig.defineName, defineConfig)
|
||||
id = req.urlNoQuery
|
||||
} else {
|
||||
array.forEach(function(name) {
|
||||
if (!name) {
|
||||
return
|
||||
}
|
||||
var req = makeRequest(name, defineConfig)
|
||||
var url = fireRequest(req) //加载资源,并返回该资源的完整地址
|
||||
|
||||
if (url) {
|
||||
if (!uniq[url]) {
|
||||
deps.push(url)
|
||||
uniq[url] = !0
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var module = modules[id]
|
||||
if (!module || module.state !== 4) {
|
||||
modules[id] = {
|
||||
id: id,
|
||||
deps: isBuilt ? array.concat() : deps,
|
||||
factory: factory || noop,
|
||||
state: 3
|
||||
}
|
||||
}
|
||||
if (!module) {
|
||||
//如果此模块是定义在另一个JS文件中, 那必须等该文件加载完毕, 才能放到检测列队中
|
||||
loadings.push(id)
|
||||
}
|
||||
checkDeps()
|
||||
}
|
||||
|
||||
//核心API之二 require
|
||||
innerRequire.define = function(name, deps, factory) {
|
||||
//模块名,依赖列表,模块本身
|
||||
if (typeof name !== 'string') {
|
||||
factory = deps
|
||||
deps = name
|
||||
name = 'anonymous'
|
||||
}
|
||||
if (!Array.isArray(deps)) {
|
||||
factory = deps
|
||||
deps = []
|
||||
}
|
||||
var config = {
|
||||
built: !isUserFirstRequire, //用r.js打包后,所有define会放到requirejs之前
|
||||
defineName: name
|
||||
}
|
||||
var args = [deps, factory, config]
|
||||
factory.require = function(url) {
|
||||
args.splice(2, 0, url)
|
||||
if (modules[url]) {
|
||||
modules[url].state = 3 //loaded
|
||||
var isCycle = false
|
||||
try {
|
||||
isCycle = checkCycle(modules[url].deps, url)
|
||||
} catch (e) {}
|
||||
if (isCycle) {
|
||||
Anot.error(
|
||||
url +
|
||||
'模块与之前的模块存在循环依赖,请不要直接用script标签引入' +
|
||||
url +
|
||||
'模块'
|
||||
)
|
||||
}
|
||||
}
|
||||
delete factory.require //释放内存
|
||||
innerRequire.apply(null, args) //0,1,2 --> 1,2,0
|
||||
}
|
||||
|
||||
//根据标准,所有遵循W3C标准的浏览器,script标签会按标签的出现顺序执行。
|
||||
//老的浏览器中,加载也是按顺序的:一个文件下载完成后,才开始下载下一个文件。
|
||||
//较新的浏览器中(IE8+ 、FireFox3.5+ 、Chrome4+ 、Safari4+),为了减小请求时间以优化体验,
|
||||
//下载可以是并行的,但是执行顺序还是按照标签出现的顺序。
|
||||
//但如果script标签是动态插入的, 就未必按照先请求先执行的原则了,目测只有firefox遵守
|
||||
//唯一比较一致的是,IE10+及其他标准浏览器,一旦开始解析脚本, 就会一直堵在那里,直接脚本解析完毕
|
||||
//亦即,先进入loading阶段的script标签(模块)必然会先进入loaded阶段
|
||||
var url = config.built ? 'unknown' : getCurrentScript()
|
||||
if (url) {
|
||||
var module = modules[url]
|
||||
if (module) {
|
||||
module.state = 2
|
||||
}
|
||||
factory.require(url)
|
||||
} else {
|
||||
//合并前后的safari,合并后的IE6-9走此分支
|
||||
factorys.push(factory)
|
||||
}
|
||||
}
|
||||
//核心API之三 require.config(settings)
|
||||
innerRequire.config = kernel
|
||||
//核心API之四 define.amd 标识其符合AMD规范
|
||||
innerRequire.define.amd = modules
|
||||
|
||||
//==========================对用户配置项进行再加工==========================
|
||||
var allpaths = (kernel['orig.paths'] = createMap())
|
||||
var allmaps = (kernel['orig.map'] = createMap())
|
||||
var allpackages = (kernel['packages'] = [])
|
||||
var allargs = (kernel['orig.args'] = createMap())
|
||||
Anot.mix(plugins, {
|
||||
paths: function(hash) {
|
||||
Anot.mix(allpaths, hash)
|
||||
kernel.paths = makeIndexArray(allpaths)
|
||||
},
|
||||
map: function(hash) {
|
||||
Anot.mix(allmaps, hash)
|
||||
var list = makeIndexArray(allmaps, 1, 1)
|
||||
Anot.each(list, function(_, item) {
|
||||
item.val = makeIndexArray(item.val)
|
||||
})
|
||||
kernel.map = list
|
||||
},
|
||||
packages: function(array) {
|
||||
array = array.concat(allpackages)
|
||||
var uniq = createMap()
|
||||
var ret = []
|
||||
for (var i = 0, pkg; (pkg = array[i++]); ) {
|
||||
pkg = typeof pkg === 'string' ? { name: pkg } : pkg
|
||||
var name = pkg.name
|
||||
if (!uniq[name]) {
|
||||
var url = joinPath(pkg.location || name, pkg.main || 'main')
|
||||
url = url.replace(rjsext, '')
|
||||
ret.push(pkg)
|
||||
uniq[name] = pkg.location = url
|
||||
pkg.reg = makeMatcher(name)
|
||||
}
|
||||
}
|
||||
kernel.packages = ret.sort()
|
||||
},
|
||||
urlArgs: function(hash) {
|
||||
if (typeof hash === 'string') {
|
||||
hash = { '*': hash }
|
||||
}
|
||||
Anot.mix(allargs, hash)
|
||||
kernel.urlArgs = makeIndexArray(allargs, 1)
|
||||
},
|
||||
baseUrl: function(url) {
|
||||
if (!isAbsUrl(url)) {
|
||||
var baseElement = head.getElementsByTagName('base')[0]
|
||||
if (baseElement) {
|
||||
head.removeChild(baseElement)
|
||||
}
|
||||
var node = DOC.createElement('a')
|
||||
node.href = url
|
||||
url = node.href
|
||||
if (baseElement) {
|
||||
head.insertBefore(baseElement, head.firstChild)
|
||||
}
|
||||
}
|
||||
if (url.length > 3) kernel.baseUrl = url
|
||||
},
|
||||
shim: function(obj) {
|
||||
for (var i in obj) {
|
||||
var value = obj[i]
|
||||
if (Array.isArray(value)) {
|
||||
value = obj[i] = {
|
||||
deps: value
|
||||
}
|
||||
}
|
||||
if (!value.exportsFn && (value.exports || value.init)) {
|
||||
value.exportsFn = makeExports(value)
|
||||
}
|
||||
}
|
||||
kernel.shim = obj
|
||||
}
|
||||
})
|
||||
|
||||
//==============================内部方法=================================
|
||||
function checkCycle(deps, nick) {
|
||||
//检测是否存在循环依赖
|
||||
for (var i = 0, id; (id = deps[i++]); ) {
|
||||
if (
|
||||
modules[id].state !== 4 &&
|
||||
(id === nick || checkCycle(modules[id].deps, nick))
|
||||
) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkFail(node, onError) {
|
||||
var id = trimQuery(node.src) //检测是否死链
|
||||
node.onload = node.onerror = null
|
||||
if (onError) {
|
||||
setTimeout(function() {
|
||||
head.removeChild(node)
|
||||
node = null // 处理旧式IE下的循环引用问题
|
||||
})
|
||||
log('加载 ' + id + ' 失败')
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
function checkDeps() {
|
||||
//检测此JS模块的依赖是否都已安装完毕,是则安装自身
|
||||
loop: for (var i = loadings.length, id; (id = loadings[--i]); ) {
|
||||
var obj = modules[id],
|
||||
deps = obj.deps
|
||||
|
||||
if (!deps) continue
|
||||
for (var j = 0, key; (key = deps[j]); j++) {
|
||||
if (Object(modules[key]).state !== 4) {
|
||||
continue loop
|
||||
}
|
||||
}
|
||||
//如果deps是空对象或者其依赖的模块的状态都是4
|
||||
if (obj.state !== 4) {
|
||||
loadings.splice(i, 1) //必须先移除再安装,防止在IE下DOM树建完后手动刷新页面,会多次执行它
|
||||
fireFactory(obj.id, obj.deps, obj.factory)
|
||||
checkDeps() //如果成功,则再执行一次,以防有些模块就差本模块没有安装好
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function loadJS(url, id, callback) {
|
||||
//通过script节点加载目标模块
|
||||
var node = DOC.createElement('script')
|
||||
node.className = subscribers //让getCurrentScript只处理类名为subscribers的script节点
|
||||
node.onload = function() {
|
||||
var factory = factorys.pop()
|
||||
factory && factory.require(id)
|
||||
if (callback) {
|
||||
callback()
|
||||
}
|
||||
id && loadings.push(id)
|
||||
checkDeps()
|
||||
}
|
||||
node.onerror = function() {
|
||||
checkFail(node, true)
|
||||
}
|
||||
|
||||
head.insertBefore(node, head.firstChild) //chrome下第二个参数不能为null
|
||||
node.src = url //插入到head的第一个节点前,防止IE6下head标签没闭合前使用appendChild抛错,更重要的是IE6下可以收窄getCurrentScript的寻找范围
|
||||
}
|
||||
|
||||
var resources = (innerRequire.plugins = {
|
||||
//三大常用资源插件 js!, css!, text!, domReady!
|
||||
domReady: {
|
||||
load: noop
|
||||
},
|
||||
js: {
|
||||
load: function(name, req, onLoad) {
|
||||
var url = req.url
|
||||
var id = req.urlNoQuery
|
||||
var shim = kernel.shim[name.replace(rjsext, '')]
|
||||
if (shim) {
|
||||
//shim机制
|
||||
innerRequire(shim.deps || [], function() {
|
||||
var args = Anot.slice(arguments)
|
||||
loadJS(url, id, function() {
|
||||
onLoad(shim.exportsFn ? shim.exportsFn.apply(0, args) : void 0)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
loadJS(url, id)
|
||||
}
|
||||
}
|
||||
},
|
||||
css: {
|
||||
load: function(name, req, onLoad) {
|
||||
var url = req.url
|
||||
head.insertAdjacentHTML(
|
||||
'afterBegin',
|
||||
'<link rel="stylesheet" href="' + url + '">'
|
||||
)
|
||||
onLoad()
|
||||
}
|
||||
},
|
||||
text: {
|
||||
load: function(name, req, onLoad) {
|
||||
var xhr = getXHR()
|
||||
xhr.onload = function() {
|
||||
var status = xhr.status
|
||||
if (status > 399 && status < 600) {
|
||||
Anot.error(url + ' 对应资源不存在或没有开启 CORS')
|
||||
} else {
|
||||
onLoad(xhr.responseText)
|
||||
}
|
||||
}
|
||||
xhr.open('GET', req.url, true)
|
||||
xhr.send()
|
||||
}
|
||||
}
|
||||
})
|
||||
innerRequire.checkDeps = checkDeps
|
||||
|
||||
var rquery = /(\?[^#]*)$/
|
||||
function trimQuery(url) {
|
||||
return (url || '').replace(rquery, '')
|
||||
}
|
||||
|
||||
function isAbsUrl(path) {
|
||||
//http://stackoverflow.com/questions/10687099/how-to-test-if-a-url-string-is-absolute-or-relative
|
||||
return /^(?:[a-z\-]+:)?\/\//i.test(String(path))
|
||||
}
|
||||
|
||||
function getCurrentScript() {
|
||||
// inspireb by https://github.com/samyk/jiagra/blob/master/jiagra.js
|
||||
var stack
|
||||
try {
|
||||
a.b.c() //强制报错,以便捕获e.stack
|
||||
} catch (e) {
|
||||
//safari5的sourceURL,firefox的fileName,它们的效果与e.stack不一样
|
||||
stack = e.stack
|
||||
}
|
||||
if (stack) {
|
||||
/**e.stack最后一行在所有支持的浏览器大致如下:
|
||||
*chrome23:
|
||||
* at http://113.93.50.63/data.js:4:1
|
||||
*firefox17:
|
||||
*@http://113.93.50.63/query.js:4
|
||||
*opera12:http://www.oldapps.com/opera.php?system=Windows_XP
|
||||
*@http://113.93.50.63/data.js:4
|
||||
*IE10:
|
||||
* at Global code (http://113.93.50.63/data.js:4:1)
|
||||
* //firefox4+ 可以用document.currentScript
|
||||
*/
|
||||
stack = stack.split(/[@ ]/g).pop() //取得最后一行,最后一个空格或@之后的部分
|
||||
stack = stack[0] === '(' ? stack.slice(1, -1) : stack.replace(/\s/, '') //去掉换行符
|
||||
return trimQuery(stack.replace(/(:\d+)?:\d+$/i, '')) //去掉行号与或许存在的出错字符起始位置
|
||||
}
|
||||
var nodes = head.getElementsByTagName('script') //只在head标签中寻找
|
||||
for (var i = nodes.length, node; (node = nodes[--i]); ) {
|
||||
if (node.className === subscribers && node.readyState === 'interactive') {
|
||||
var url = node.src
|
||||
return (node.className = trimQuery(url))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var rcallback = /^callback\d+$/
|
||||
function fireFactory(id, deps, factory) {
|
||||
var module = Object(modules[id])
|
||||
module.state = 4
|
||||
for (var i = 0, array = [], d; (d = deps[i++]); ) {
|
||||
if (d === 'exports') {
|
||||
var obj = module.exports || (module.exports = createMap())
|
||||
array.push(obj)
|
||||
} else {
|
||||
array.push(modules[d].exports)
|
||||
}
|
||||
}
|
||||
try {
|
||||
var ret = factory.apply(window, array)
|
||||
} catch (e) {
|
||||
log('执行[' + id + ']模块的factory抛错: ', e)
|
||||
}
|
||||
if (ret !== void 0) {
|
||||
module.exports = ret
|
||||
}
|
||||
if (rcallback.test(id)) {
|
||||
delete modules[id]
|
||||
}
|
||||
delete module.factory
|
||||
return ret
|
||||
}
|
||||
function toUrl(id) {
|
||||
if (id.indexOf(this.res + '!') === 0) {
|
||||
id = id.slice(this.res.length + 1) //处理define("css!style",[], function(){})的情况
|
||||
}
|
||||
var url = id
|
||||
//1. 是否命中paths配置项
|
||||
var usePath = 0
|
||||
var baseUrl = this.baseUrl
|
||||
var rootUrl = this.parentUrl || baseUrl
|
||||
eachIndexArray(id, kernel.paths, function(value, key) {
|
||||
url = url.replace(key, value)
|
||||
usePath = 1
|
||||
})
|
||||
//2. 是否命中packages配置项
|
||||
if (!usePath) {
|
||||
eachIndexArray(id, kernel.packages, function(value, key, item) {
|
||||
url = url.replace(item.name, item.location)
|
||||
})
|
||||
}
|
||||
//3. 是否命中map配置项
|
||||
if (this.mapUrl) {
|
||||
eachIndexArray(this.mapUrl, kernel.map, function(array) {
|
||||
eachIndexArray(url, array, function(mdValue, mdKey) {
|
||||
url = url.replace(mdKey, mdValue)
|
||||
rootUrl = baseUrl
|
||||
})
|
||||
})
|
||||
}
|
||||
var ext = this.ext
|
||||
if (ext && usePath && url.slice(-ext.length) === ext) {
|
||||
url = url.slice(0, -ext.length)
|
||||
}
|
||||
//4. 转换为绝对路径
|
||||
if (!isAbsUrl(url)) {
|
||||
rootUrl = this.built || /^\w/.test(url) ? baseUrl : rootUrl
|
||||
url = joinPath(rootUrl, url)
|
||||
}
|
||||
//5. 还原扩展名,query
|
||||
var urlNoQuery = url + ext
|
||||
url = urlNoQuery + this.query
|
||||
urlNoQuery = url.replace(rquery, function(a) {
|
||||
this.query = a
|
||||
return ''
|
||||
})
|
||||
//6. 处理urlArgs
|
||||
eachIndexArray(id, kernel.urlArgs, function(value) {
|
||||
url += (url.indexOf('?') === -1 ? '?' : '&') + value
|
||||
})
|
||||
this.url = url
|
||||
return (this.urlNoQuery = urlNoQuery)
|
||||
}
|
||||
|
||||
function makeIndexArray(hash, useStar, part) {
|
||||
//创建一个经过特殊算法排好序的数组
|
||||
var index = hash2array(hash, useStar, part)
|
||||
index.sort(descSorterByName)
|
||||
return index
|
||||
}
|
||||
|
||||
function makeMatcher(prefix) {
|
||||
return new RegExp('^' + prefix + '(/|$)')
|
||||
}
|
||||
|
||||
function makeExports(value) {
|
||||
return function() {
|
||||
var ret
|
||||
if (value.init) {
|
||||
ret = value.init.apply(window, arguments)
|
||||
}
|
||||
return ret || (value.exports && getGlobal(value.exports))
|
||||
}
|
||||
}
|
||||
|
||||
function hash2array(hash, useStar, part) {
|
||||
var array = []
|
||||
for (var key in hash) {
|
||||
// if (hash.hasOwnProperty(key)) {//hash是由createMap创建没有hasOwnProperty
|
||||
var item = {
|
||||
name: key,
|
||||
val: hash[key]
|
||||
}
|
||||
array.push(item)
|
||||
item.reg = key === '*' && useStar ? /^/ : makeMatcher(key)
|
||||
if (part && key !== '*') {
|
||||
item.reg = new RegExp('/' + key.replace(/^\//, '') + '(/|$)')
|
||||
}
|
||||
// }
|
||||
}
|
||||
return array
|
||||
}
|
||||
|
||||
function eachIndexArray(moduleID, array, matcher) {
|
||||
array = array || []
|
||||
for (var i = 0, el; (el = array[i++]); ) {
|
||||
if (el.reg.test(moduleID)) {
|
||||
matcher(el.val, el.name, el)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
// 根据元素的name项进行数组字符数逆序的排序函数
|
||||
function descSorterByName(a, b) {
|
||||
var aaa = a.name
|
||||
var bbb = b.name
|
||||
if (bbb === '*') {
|
||||
return -1
|
||||
}
|
||||
if (aaa === '*') {
|
||||
return 1
|
||||
}
|
||||
return bbb.length - aaa.length
|
||||
}
|
||||
|
||||
var rdeuce = /\/\w+\/\.\./
|
||||
function joinPath(a, b) {
|
||||
if (a.charAt(a.length - 1) !== '/') {
|
||||
a += '/'
|
||||
}
|
||||
if (b.slice(0, 2) === './') {
|
||||
//相对于兄弟路径
|
||||
return a + b.slice(2)
|
||||
}
|
||||
if (b.slice(0, 2) === '..') {
|
||||
//相对于父路径
|
||||
a += b
|
||||
while (rdeuce.test(a)) {
|
||||
a = a.replace(rdeuce, '')
|
||||
}
|
||||
return a
|
||||
}
|
||||
if (b.slice(0, 1) === '/') {
|
||||
return a + b.slice(1)
|
||||
}
|
||||
return a + b
|
||||
}
|
||||
|
||||
function getGlobal(value) {
|
||||
if (!value) {
|
||||
return value
|
||||
}
|
||||
var g = window
|
||||
value.split('.').forEach(function(part) {
|
||||
g = g[part]
|
||||
})
|
||||
return g
|
||||
}
|
||||
|
||||
var mainNode = DOC.scripts[DOC.scripts.length - 1]
|
||||
var dataMain = mainNode.getAttribute('data-main')
|
||||
if (dataMain) {
|
||||
plugins.baseUrl(dataMain)
|
||||
var href = kernel.baseUrl
|
||||
kernel.baseUrl = href.slice(0, href.lastIndexOf('/') + 1)
|
||||
loadJS(href.replace(rjsext, '') + '.js')
|
||||
} else {
|
||||
var loaderUrl = trimQuery(mainNode.src)
|
||||
kernel.baseUrl = loaderUrl.slice(0, loaderUrl.lastIndexOf('/') + 1)
|
||||
}
|
||||
}() // jshint ignore:line
|
||||
|
||||
Anot.config({
|
||||
loader: true
|
||||
})
|
||||
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define('Anot', [], function() {
|
||||
return Anot
|
||||
})
|
||||
}
|
Reference in New Issue