avalon-rails 1.3.9.1.20150212184918 → 1.4.1.1.20150404164109

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@
5
5
  http://weibo.com/jslouvre/
6
6
 
7
7
  Released under the MIT license
8
- avalon.modern.js 1.391 built in 2015.2.12
8
+ avalon.modern.js 1.41 built in 2015.4.4
9
9
  support IE10+ and other browsers
10
10
  ==================================================*/
11
11
  (function(global, factory) {
@@ -46,10 +46,23 @@ function log() {
46
46
  console.log.apply(console, arguments)
47
47
  }
48
48
  }
49
+ /**
50
+ * Creates a new object without a prototype. This object is useful for lookup without having to
51
+ * guard against prototypically inherited properties via hasOwnProperty.
52
+ *
53
+ * Related micro-benchmarks:
54
+ * - http://jsperf.com/object-create2
55
+ * - http://jsperf.com/proto-map-lookup/2
56
+ * - http://jsperf.com/for-in-vs-object-keys2
57
+ */
58
+ function createMap() {
59
+ return Object.create(null)
60
+ }
49
61
 
50
62
  var subscribers = "$" + expose
51
63
  var otherRequire = window.require
52
64
  var otherDefine = window.define
65
+ var innerRequire
53
66
  var stopRepeatAssign = false
54
67
  var rword = /[^, ]+/g //切割字符串为一个个小块,以空格或豆号分开它们,结合replace实现字符串的forEach
55
68
  var rcomplexType = /^(?:object|array)$/
@@ -87,16 +100,6 @@ function oneObject(array, val) {
87
100
  return result
88
101
  }
89
102
 
90
- function createCache(maxLength) {
91
- var keys = []
92
- function cache(key, value) {
93
- if (keys.push(key) > maxLength) {
94
- delete cache[keys.shift()]
95
- }
96
- return cache[key] = value;
97
- }
98
- return cache;
99
- }
100
103
  //生成UUID http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
101
104
  var generateID = function(prefix) {
102
105
  prefix = prefix || "avalon"
@@ -111,13 +114,60 @@ function IE() {
111
114
  }
112
115
  }
113
116
  var IEVersion = IE()
114
- /*********************************************************************
115
- * avalon的静态方法定义区 *
116
- **********************************************************************/
117
+
117
118
  avalon = function(el) { //创建jQuery式的无new 实例化结构
118
119
  return new avalon.init(el)
119
120
  }
120
121
 
122
+ /*视浏览器情况采用最快的异步回调*/
123
+ avalon.nextTick = new function() {// jshint ignore:line
124
+ var tickImmediate = window.setImmediate
125
+ var tickObserver = window.MutationObserver
126
+ var tickPost = W3C && window.postMessage
127
+ if (tickImmediate) {
128
+ return tickImmediate.bind(window)
129
+ }
130
+
131
+ var queue = []
132
+ function callback() {
133
+ var n = queue.length
134
+ for (var i = 0; i < n; i++) {
135
+ queue[i]()
136
+ }
137
+ queue = queue.slice(n)
138
+ }
139
+
140
+ if (tickObserver) {
141
+ var node = document.createTextNode("avalon")
142
+ new tickObserver(callback).observe(node, {characterData: true})// jshint ignore:line
143
+ return function(fn) {
144
+ queue.push(fn)
145
+ node.data = Math.random()
146
+ }
147
+ }
148
+
149
+ if (tickPost) {
150
+ window.addEventListener("message", function(e) {
151
+ var source = e.source
152
+ if ((source === window || source === null) && e.data === "process-tick") {
153
+ e.stopPropagation()
154
+ callback()
155
+ }
156
+ })
157
+
158
+ return function(fn) {
159
+ queue.push(fn)
160
+ window.postMessage('process-tick', '*')
161
+ }
162
+ }
163
+
164
+ return function(fn) {
165
+ setTimeout(fn, 0)
166
+ }
167
+ }// jshint ignore:line
168
+ /*********************************************************************
169
+ * avalon的静态方法定义区 *
170
+ **********************************************************************/
121
171
  avalon.init = function(el) {
122
172
  this[0] = this.element = el
123
173
  }
@@ -213,7 +263,7 @@ function _number(a, len) { //用于模拟slice, splice的效果
213
263
  avalon.mix({
214
264
  rword: rword,
215
265
  subscribers: subscribers,
216
- version: 1.391,
266
+ version: 1.41,
217
267
  ui: {},
218
268
  log: log,
219
269
  slice: function(nodes, start, end) {
@@ -222,7 +272,7 @@ avalon.mix({
222
272
  noop: noop,
223
273
  /*如果不用Error对象封装一下,str在控制台下可能会乱码*/
224
274
  error: function(str, e) {
225
- throw new (e || Error)(str)
275
+ throw new (e || Error)(str)// jshint ignore:line
226
276
  },
227
277
  /*将一个以空格或逗号隔开的字符串或数组,转换成一个键值都为1的对象*/
228
278
  oneObject: oneObject,
@@ -259,7 +309,7 @@ avalon.mix({
259
309
  if (typeof hook === "object") {
260
310
  type = hook.type
261
311
  if (hook.deel) {
262
- fn = hook.deel(el, type, fn, true)
312
+ fn = hook.deel(el, type, fn, phase)
263
313
  }
264
314
  }
265
315
  if (!fn.unbind)
@@ -374,35 +424,107 @@ function isArrayLike(obj) {
374
424
  }
375
425
  return false
376
426
  }
377
- /*视浏览器情况采用最快的异步回调(在avalon.ready里,还有一个分支,用于处理IE6-9)*/
378
- avalon.nextTick = window.setImmediate ? setImmediate.bind(window) : function(callback) {
379
- setTimeout(callback, 0) //IE10-11 or W3C
380
- }
427
+
428
+
429
+ // https://github.com/rsms/js-lru
430
+ var Cache = new function() {// jshint ignore:line
431
+ function LRU(maxLength) {
432
+ this.size = 0
433
+ this.limit = maxLength
434
+ this.head = this.tail = void 0
435
+ this._keymap = {}
436
+ }
437
+
438
+ var p = LRU.prototype
439
+
440
+ p.put = function(key, value) {
441
+ var entry = {
442
+ key: key,
443
+ value: value
444
+ }
445
+ this._keymap[key] = entry
446
+ if (this.tail) {
447
+ this.tail.newer = entry
448
+ entry.older = this.tail
449
+ } else {
450
+ this.head = entry
451
+ }
452
+ this.tail = entry
453
+ if (this.size === this.limit) {
454
+ this.shift()
455
+ } else {
456
+ this.size++
457
+ }
458
+ return value
459
+ }
460
+
461
+ p.shift = function() {
462
+ var entry = this.head
463
+ if (entry) {
464
+ this.head = this.head.newer
465
+ this.head.older =
466
+ entry.newer =
467
+ entry.older =
468
+ this._keymap[entry.key] = void 0
469
+ }
470
+ }
471
+ p.get = function(key) {
472
+ var entry = this._keymap[key]
473
+ if (entry === void 0)
474
+ return
475
+ if (entry === this.tail) {
476
+ return entry.value
477
+ }
478
+ // HEAD--------------TAIL
479
+ // <.older .newer>
480
+ // <--- add direction --
481
+ // A B C <D> E
482
+ if (entry.newer) {
483
+ if (entry === this.head) {
484
+ this.head = entry.newer
485
+ }
486
+ entry.newer.older = entry.older // C <-- E.
487
+ }
488
+ if (entry.older) {
489
+ entry.older.newer = entry.newer // C. --> E
490
+ }
491
+ entry.newer = void 0 // D --x
492
+ entry.older = this.tail // D. --> E
493
+ if (this.tail) {
494
+ this.tail.newer = entry // E. <-- D
495
+ }
496
+ this.tail = entry
497
+ return entry.value
498
+ }
499
+ return LRU
500
+ }// jshint ignore:line
381
501
 
382
502
  /*********************************************************************
383
503
  * DOM 底层补丁 *
384
504
  **********************************************************************/
385
- if (!root.contains) { //safari5+是把contains方法放在Element.prototype上而不是Node.prototype
386
- Node.prototype.contains = function(arg) {
505
+ //safari5+是把contains方法放在Element.prototype上而不是Node.prototype
506
+ if (!DOC.contains) {
507
+ Node.prototype.contains = function (arg) {
387
508
  return !!(this.compareDocumentPosition(arg) & 16)
388
509
  }
389
510
  }
390
- avalon.contains = function(root, el) {
511
+ avalon.contains = function (root, el) {
391
512
  try {
392
513
  while ((el = el.parentNode))
393
514
  if (el === root)
394
- return true;
515
+ return true
395
516
  return false
396
517
  } catch (e) {
397
518
  return false
398
519
  }
399
520
  }
521
+
400
522
  if (window.SVGElement) {
401
523
  var svgns = "http://www.w3.org/2000/svg"
402
524
  var svg = DOC.createElementNS(svgns, "svg")
403
525
  svg.innerHTML = '<circle cx="50" cy="50" r="40" fill="red" />'
404
526
  if (!rsvg.test(svg.firstChild)) {// #409
405
-
527
+ /* jshint ignore:start */
406
528
  function enumerateNode(node, targetNode) {
407
529
  if (node && node.childNodes) {
408
530
  var nodes = node.childNodes
@@ -411,7 +533,7 @@ if (window.SVGElement) {
411
533
  var svg = DOC.createElementNS(svgns,
412
534
  el.tagName.toLowerCase())
413
535
  // copy attrs
414
- ap.forEach.call(el.attributes, function(attr) {
536
+ ap.forEach.call(el.attributes, function (attr) {
415
537
  svg.setAttribute(attr.name, attr.value)
416
538
  })
417
539
  // 递归处理子节点
@@ -421,14 +543,15 @@ if (window.SVGElement) {
421
543
  }
422
544
  }
423
545
  }
546
+ /* jshint ignore:end */
424
547
  Object.defineProperties(SVGElement.prototype, {
425
548
  "outerHTML": {//IE9-11,firefox不支持SVG元素的innerHTML,outerHTML属性
426
549
  enumerable: true,
427
550
  configurable: true,
428
- get: function() {
551
+ get: function () {
429
552
  return new XMLSerializer().serializeToString(this)
430
553
  },
431
- set: function(html) {
554
+ set: function (html) {
432
555
  var tagName = this.tagName.toLowerCase(),
433
556
  par = this.parentNode,
434
557
  frag = avalon.parseHTML(html)
@@ -447,13 +570,13 @@ if (window.SVGElement) {
447
570
  "innerHTML": {
448
571
  enumerable: true,
449
572
  configurable: true,
450
- get: function() {
573
+ get: function () {
451
574
  var s = this.outerHTML
452
575
  var ropen = new RegExp("<" + this.nodeName + '\\b(?:(["\'])[^"]*?(\\1)|[^>])*>', "i")
453
576
  var rclose = new RegExp("<\/" + this.nodeName + ">$", "i")
454
577
  return s.replace(ropen, "").replace(rclose, "")
455
578
  },
456
- set: function(html) {
579
+ set: function (html) {
457
580
  if (avalon.clearHTML) {
458
581
  avalon.clearHTML(this)
459
582
  var frag = avalon.parseHTML(html)
@@ -465,17 +588,17 @@ if (window.SVGElement) {
465
588
  }
466
589
  }
467
590
  //========================= event binding ====================
468
- var eventHooks = avalon.eventHooks
591
+ var eventHooks = avalon.eventHooks
469
592
  //针对firefox, chrome修正mouseenter, mouseleave(chrome30+)
470
593
  if (!("onmouseenter" in root)) {
471
594
  avalon.each({
472
595
  mouseenter: "mouseover",
473
596
  mouseleave: "mouseout"
474
- }, function(origType, fixType) {
597
+ }, function (origType, fixType) {
475
598
  eventHooks[origType] = {
476
599
  type: fixType,
477
- deel: function(elem, fn) {
478
- return function(e) {
600
+ deel: function (elem, _, fn) {
601
+ return function (e) {
479
602
  var t = e.relatedTarget
480
603
  if (!t || (t !== elem && !(elem.compareDocumentPosition(t) & 16))) {
481
604
  delete e.type
@@ -491,7 +614,7 @@ if (!("onmouseenter" in root)) {
491
614
  avalon.each({
492
615
  AnimationEvent: "animationend",
493
616
  WebKitAnimationEvent: "webkitAnimationEnd"
494
- }, function(construct, fixType) {
617
+ }, function (construct, fixType) {
495
618
  if (window[construct] && !eventHooks.animationend) {
496
619
  eventHooks.animationend = {
497
620
  type: fixType
@@ -507,8 +630,8 @@ if (DOC.onmousewheel === void 0) {
507
630
  chrome wheel deltaY 下100 上-100 */
508
631
  eventHooks.mousewheel = {
509
632
  type: "wheel",
510
- deel: function(elem, fn) {
511
- return function(e) {
633
+ deel: function (elem, _, fn) {
634
+ return function (e) {
512
635
  e.wheelDeltaY = e.wheelDelta = e.deltaY > 0 ? -120 : 120
513
636
  e.wheelDeltaX = 0
514
637
  Object.defineProperty(e, "type", {
@@ -546,13 +669,14 @@ function escapeRegExp(target) {
546
669
  //将字符串安全格式化为正则表达式的源码
547
670
  return (target + "").replace(rregexp, "\\$&")
548
671
  }
549
- var innerRequire = noop
672
+
550
673
  var plugins = {
551
- loader: function(builtin) {
552
- window.define = builtin ? innerRequire.define : otherDefine
553
- window.require = builtin ? innerRequire : otherRequire
674
+ loader: function (builtin) {
675
+ var flag = innerRequire && builtin
676
+ window.require = flag ? innerRequire : otherRequire
677
+ window.define = flag ? innerRequire.define : otherDefine
554
678
  },
555
- interpolate: function(array) {
679
+ interpolate: function (array) {
556
680
  openTag = array[0]
557
681
  closeTag = array[1]
558
682
  if (openTag === closeTag) {
@@ -582,11 +706,15 @@ kernel.paths = {}
582
706
  kernel.shim = {}
583
707
  kernel.maxRepeatSize = 100
584
708
  avalon.config = kernel
709
+ var ravalon = /(\w+)\[(avalonctrl)="(\S+)"\]/
710
+ var findNodes = function(str) {
711
+ return DOC.querySelectorAll(str)
712
+ }
585
713
  /*********************************************************************
586
714
  * 事件总线 *
587
715
  **********************************************************************/
588
716
  var EventBus = {
589
- $watch: function(type, callback) {
717
+ $watch: function (type, callback) {
590
718
  if (typeof callback === "function") {
591
719
  var callbacks = this.$events[type]
592
720
  if (callbacks) {
@@ -599,7 +727,7 @@ var EventBus = {
599
727
  }
600
728
  return this
601
729
  },
602
- $unwatch: function(type, callback) {
730
+ $unwatch: function (type, callback) {
603
731
  var n = arguments.length
604
732
  if (n === 0) { //让此VM的所有$watch回调无效化
605
733
  this.$watch.backup = this.$events
@@ -617,13 +745,15 @@ var EventBus = {
617
745
  }
618
746
  return this
619
747
  },
620
- $fire: function(type) {
748
+ $fire: function (type) {
621
749
  var special, i, v, callback
622
750
  if (/^(\w+)!(\S+)$/.test(type)) {
623
751
  special = RegExp.$1
624
752
  type = RegExp.$2
625
753
  }
626
754
  var events = this.$events
755
+ if (!events)
756
+ return
627
757
  var args = aslice.call(arguments, 1)
628
758
  var detail = [type].concat(args)
629
759
  if (special === "all") {
@@ -646,22 +776,23 @@ var EventBus = {
646
776
  continue
647
777
  }
648
778
  //循环两个vmodel中的节点,查找匹配(向上匹配或者向下匹配)的节点并设置标识
649
- Array.prototype.forEach.call(eventNodes, function(node) {
650
- Array.prototype.forEach.call(elements, function(element) {
779
+ /* jshint ignore:start */
780
+ Array.prototype.forEach.call(eventNodes, function (node) {
781
+ Array.prototype.forEach.call(elements, function (element) {
651
782
  var ok = special === "down" ? element.contains(node) : //向下捕获
652
783
  node.contains(element) //向上冒泡
653
-
654
784
  if (ok) {
655
785
  node._avalon = v //符合条件的加一个标识
656
786
  }
657
787
  });
658
788
  })
789
+ /* jshint ignore:end */
659
790
  }
660
791
  }
661
792
  }
662
793
  var nodes = DOC.getElementsByTagName("*") //实现节点排序
663
794
  var alls = []
664
- Array.prototype.forEach.call(nodes, function(el) {
795
+ Array.prototype.forEach.call(nodes, function (el) {
665
796
  if (el._avalon) {
666
797
  alls.push(el._avalon)
667
798
  el._avalon = ""
@@ -691,16 +822,12 @@ var EventBus = {
691
822
  }
692
823
  }
693
824
 
694
- var ravalon = /(\w+)\[(avalonctrl)="(\S+)"\]/
695
- var findNodes = function(str) {
696
- return DOC.querySelectorAll(str)
697
- }
698
825
  /*********************************************************************
699
826
  * modelFactory *
700
827
  **********************************************************************/
701
828
  //avalon最核心的方法的两个方法之一(另一个是avalon.scan),返回一个ViewModel(VM)
702
- var VMODELS = avalon.vmodels = {} //所有vmodel都储存在这里
703
- avalon.define = function(id, factory) {
829
+ var VMODELS = avalon.vmodels = createMap() //所有vmodel都储存在这里
830
+ avalon.define = function (id, factory) {
704
831
  var $id = id.$id || id
705
832
  if (!$id) {
706
833
  log("warning: vm必须指定$id")
@@ -744,7 +871,7 @@ function isObservable(name, value, $skipArray) {
744
871
  return true
745
872
  }
746
873
  //ms-with,ms-each, ms-repeat绑定生成的代理对象储存池
747
- var midway = {}
874
+ var midway = createMap()
748
875
  function getNewValue(accessor, name, value, $vmodel) {
749
876
  switch (accessor.type) {
750
877
  case 0://计算属性
@@ -771,43 +898,29 @@ function getNewValue(accessor, name, value, $vmodel) {
771
898
  }
772
899
  }
773
900
 
774
- var defineProperty = Object.defineProperty
775
- var canHideOwn = true
776
- //如果浏览器不支持ecma262v5的Object.defineProperties或者存在BUG,比如IE8
777
- //标准浏览器使用__defineGetter__, __defineSetter__实现
778
- try {
779
- defineProperty({}, "_", {
780
- value: "x"
781
- })
782
- var defineProperties = Object.defineProperties
783
- } catch (e) {
784
- canHideOwn = false
785
- }
786
901
  function modelFactory(source, $special, $model) {
787
902
  if (Array.isArray(source)) {
788
903
  var arr = source.concat()
789
904
  source.length = 0
790
- var collection = Collection(source)
905
+ var collection = Collection(source)// jshint ignore:line
791
906
  collection.pushArray(arr)
792
907
  return collection
793
908
  }
794
- if (typeof source.nodeType === "number") {
795
- return source
796
- }
797
- if (source.$id && source.$events) { //fix IE6-8 createWithProxy $val: val引发的BUG
909
+ //0 null undefined || Node || VModel
910
+ if (!source || source.nodeType > 0 || (source.$id && source.$events)) {
798
911
  return source
799
912
  }
800
913
  if (!Array.isArray(source.$skipArray)) {
801
914
  source.$skipArray = []
802
915
  }
803
- source.$skipArray.$special = $special || {} //强制要监听的属性
916
+ source.$skipArray.$special = $special || createMap() //强制要监听的属性
804
917
  var $vmodel = {} //要返回的对象, 它在IE6-8下可能被偷龙转凤
805
918
  $model = $model || {} //vmodels.$model属性
806
- var $events = {} //vmodel.$events属性
807
- var watchedProperties = {} //监控属性
919
+ var $events = createMap() //vmodel.$events属性
920
+ var watchedProperties = createMap() //监控属性
808
921
  var initCallbacks = [] //初始化才执行的函数
809
922
  for (var i in source) {
810
- (function(name, val) {
923
+ (function (name, val) {
811
924
  $model[name] = val
812
925
  if (!isObservable(name, val, source.$skipArray)) {
813
926
  return //过滤所有非监控属性
@@ -815,7 +928,7 @@ function modelFactory(source, $special, $model) {
815
928
  //总共产生三种accessor
816
929
  $events[name] = []
817
930
  var valueType = avalon.type(val)
818
- var accessor = function(newValue) {
931
+ var accessor = function (newValue) {
819
932
  var name = accessor._name
820
933
  var $vmodel = this
821
934
  var $model = $vmodel.$model
@@ -834,19 +947,8 @@ function modelFactory(source, $special, $model) {
834
947
  }
835
948
  if (!isEqual(oldValue, newValue)) {
836
949
  $model[name] = newValue
837
- if ($events.$digest) {
838
- if (!accessor.pedding) {
839
- accessor.pedding = true
840
- setTimeout(function() {
841
- notifySubscribers($events[name]) //同步视图
842
- safeFire($vmodel, name, $model[name], oldValue) //触发$watch回调
843
- accessor.pedding = false
844
- })
845
- }
846
- } else {
847
- notifySubscribers($events[name]) //同步视图
848
- safeFire($vmodel, name, newValue, oldValue) //触发$watch回调
849
- }
950
+ notifySubscribers($events[name]) //同步视图
951
+ safeFire($vmodel, name, newValue, oldValue) //触发$watch回调
850
952
  }
851
953
  } else {
852
954
  if (accessor.type === 0) { //type 0 计算属性 1 监控属性 2 对象属性
@@ -855,17 +957,7 @@ function modelFactory(source, $special, $model) {
855
957
  if (oldValue !== newValue) {
856
958
  $model[name] = newValue
857
959
  //这里不用同步视图
858
- if ($events.$digest) {
859
- if (!accessor.pedding) {
860
- accessor.pedding = true
861
- setTimeout(function() {
862
- safeFire($vmodel, name, $model[name], oldValue) //触发$watch回调
863
- accessor.pedding = false
864
- })
865
- }
866
- } else {
867
- safeFire($vmodel, name, newValue, oldValue) //触发$watch回调
868
- }
960
+ safeFire($vmodel, name, newValue, oldValue) //触发$watch回调
869
961
  }
870
962
  return newValue
871
963
  } else {
@@ -880,11 +972,11 @@ function modelFactory(source, $special, $model) {
880
972
  accessor.set = val.set
881
973
  accessor.get = val.get
882
974
  accessor.type = 0
883
- initCallbacks.push(function() {
975
+ initCallbacks.push(function () {
884
976
  var data = {
885
- evaluator: function() {
977
+ evaluator: function () {
886
978
  data.type = Math.random(),
887
- data.element = null
979
+ data.element = null
888
980
  $model[name] = accessor.get.call($vmodel)
889
981
  },
890
982
  element: head,
@@ -900,7 +992,7 @@ function modelFactory(source, $special, $model) {
900
992
  //第2种为对象属性,产生子VM与监控数组
901
993
  accessor.type = 2
902
994
  accessor.valueType = valueType
903
- initCallbacks.push(function() {
995
+ initCallbacks.push(function () {
904
996
  var svmodel = modelFactory(val, 0, $model[name])
905
997
  accessor.svmodel = svmodel
906
998
  svmodel.$events[subscribers] = $events[name]
@@ -911,15 +1003,15 @@ function modelFactory(source, $special, $model) {
911
1003
  }
912
1004
  accessor._name = name
913
1005
  watchedProperties[name] = accessor
914
- })(i, source[i])
1006
+ })(i, source[i])// jshint ignore:line
915
1007
  }
916
1008
 
917
- $$skipArray.forEach(function(name) {
1009
+ $$skipArray.forEach(function (name) {
918
1010
  delete source[name]
919
1011
  delete $model[name] //这些特殊属性不应该在$model中出现
920
1012
  })
921
1013
 
922
- $vmodel = defineProperties($vmodel, descriptorFactory(watchedProperties), source) //生成一个空的ViewModel
1014
+ $vmodel = Object.defineProperties($vmodel, descriptorFactory(watchedProperties), source) //生成一个空的ViewModel
923
1015
  for (var name in source) {
924
1016
  if (!watchedProperties[name]) {
925
1017
  $vmodel[name] = source[name]
@@ -929,37 +1021,27 @@ function modelFactory(source, $special, $model) {
929
1021
  $vmodel.$id = generateID()
930
1022
  $vmodel.$model = $model
931
1023
  $vmodel.$events = $events
932
- for ( i in EventBus) {
933
- var fn = EventBus[i]
934
- if (!W3C) { //在IE6-8下,VB对象的方法里的this并不指向自身,需要用bind处理一下
935
- fn = fn.bind($vmodel)
936
- }
937
- $vmodel[i] = fn
1024
+ for (i in EventBus) {
1025
+ $vmodel[i] = EventBus[i]
938
1026
  }
939
1027
 
940
- if (canHideOwn) {
941
- Object.defineProperty($vmodel, "hasOwnProperty", {
942
- value: function(name) {
943
- return name in this.$model
944
- },
945
- writable: false,
946
- enumerable: false,
947
- configurable: true
948
- })
1028
+ Object.defineProperty($vmodel, "hasOwnProperty", {
1029
+ value: function (name) {
1030
+ return name in this.$model
1031
+ },
1032
+ writable: false,
1033
+ enumerable: false,
1034
+ configurable: true
1035
+ })
949
1036
 
950
- } else {
951
- $vmodel.hasOwnProperty = function(name) {
952
- return name in $vmodel.$model
953
- }
954
- }
955
- initCallbacks.forEach(function(cb) { //收集依赖
1037
+ initCallbacks.forEach(function (cb) { //收集依赖
956
1038
  cb()
957
1039
  })
958
1040
  return $vmodel
959
1041
  }
960
1042
 
961
1043
  //比较两个值是否相等
962
- var isEqual = Object.is || function(v1, v2) {
1044
+ var isEqual = Object.is || function (v1, v2) {
963
1045
  if (v1 === 0 && v2 === 0) {
964
1046
  return 1 / v1 === 1 / v2
965
1047
  } else if (v1 !== v1) {
@@ -975,8 +1057,8 @@ function safeFire(a, b, c, d) {
975
1057
  }
976
1058
  }
977
1059
 
978
- var descriptorFactory = W3C ? function(obj) {
979
- var descriptors = {}
1060
+ var descriptorFactory = function (obj) {
1061
+ var descriptors = createMap()
980
1062
  for (var i in obj) {
981
1063
  descriptors[i] = {
982
1064
  get: obj[i],
@@ -986,12 +1068,8 @@ var descriptorFactory = W3C ? function(obj) {
986
1068
  }
987
1069
  }
988
1070
  return descriptors
989
- } : function(a) {
990
- return a
991
1071
  }
992
1072
 
993
-
994
-
995
1073
  //应用于第2种accessor
996
1074
  function objectFactory(parent, name, value, valueType) {
997
1075
  //a为原来的VM, b为新数组或新对象
@@ -1014,16 +1092,17 @@ function objectFactory(parent, name, value, valueType) {
1014
1092
  }
1015
1093
  var ret = modelFactory(value)
1016
1094
  ret.$events[subscribers] = iterators
1017
- midway[ret.$id] = function(data) {
1095
+ midway[ret.$id] = function (data) {
1018
1096
  while (data = iterators.shift()) {
1019
- (function(el) {
1020
- avalon.nextTick(function() {
1021
- if (el.type) { //重新绑定
1097
+ (function (el) {
1098
+ avalon.nextTick(function () {
1099
+ var type = el.type
1100
+ if (type && bindingHandlers[type]) { //#753
1022
1101
  el.rollback && el.rollback() //还原 ms-with ms-on
1023
- bindingHandlers[el.type](el, el.vmodels)
1102
+ bindingHandlers[type](el, el.vmodels)
1024
1103
  }
1025
1104
  })
1026
- })(data)
1105
+ })(data)// jshint ignore:line
1027
1106
  }
1028
1107
  delete midway[ret.$id]
1029
1108
  }
@@ -1043,7 +1122,7 @@ function Collection(model) {
1043
1122
  array._ = modelFactory({
1044
1123
  length: model.length
1045
1124
  })
1046
- array._.$watch("length", function(a, b) {
1125
+ array._.$watch("length", function (a, b) {
1047
1126
  array.$fire("length", a, b)
1048
1127
  })
1049
1128
  for (var i in EventBus) {
@@ -1058,13 +1137,15 @@ function mutateArray(method, pos, n, index, method2, pos2, n2) {
1058
1137
  while (--loop) {
1059
1138
  switch (method) {
1060
1139
  case "add":
1061
- var array = this.$model.slice(pos, pos + n).map(function(el) {
1140
+ /* jshint ignore:start */
1141
+ var array = this.$model.slice(pos, pos + n).map(function (el) {
1062
1142
  if (rcomplexType.test(avalon.type(el))) {
1063
1143
  return el.$id ? el : modelFactory(el, 0, el)
1064
1144
  } else {
1065
1145
  return el
1066
1146
  }
1067
1147
  })
1148
+ /* jshint ignore:end */
1068
1149
  _splice.apply(this, [pos, 0].concat(array))
1069
1150
  this._fire("add", pos, n)
1070
1151
  break
@@ -1091,30 +1172,30 @@ function mutateArray(method, pos, n, index, method2, pos2, n2) {
1091
1172
  var _splice = ap.splice
1092
1173
  var CollectionPrototype = {
1093
1174
  _splice: _splice,
1094
- _fire: function(method, a, b) {
1175
+ _fire: function (method, a, b) {
1095
1176
  notifySubscribers(this.$events[subscribers], method, a, b)
1096
1177
  },
1097
- size: function() { //取得数组长度,这个函数可以同步视图,length不能
1178
+ size: function () { //取得数组长度,这个函数可以同步视图,length不能
1098
1179
  return this._.length
1099
1180
  },
1100
- pushArray: function(array) {
1181
+ pushArray: function (array) {
1101
1182
  var m = array.length, n = this.length
1102
1183
  if (m) {
1103
1184
  ap.push.apply(this.$model, array)
1104
- mutateArray.call(this, "add", n, m, n)
1185
+ mutateArray.call(this, "add", n, m, Math.max(0, n - 1))
1105
1186
  }
1106
1187
  return m + n
1107
1188
  },
1108
- push: function() {
1189
+ push: function () {
1109
1190
  //http://jsperf.com/closure-with-arguments
1110
1191
  var array = []
1111
1192
  var i, n = arguments.length
1112
1193
  for (i = 0; i < n; i++) {
1113
1194
  array[i] = arguments[i]
1114
1195
  }
1115
- return this.pushArray(arguments)
1196
+ return this.pushArray(array)
1116
1197
  },
1117
- unshift: function() {
1198
+ unshift: function () {
1118
1199
  var m = arguments.length, n = this.length
1119
1200
  if (m) {
1120
1201
  ap.unshift.apply(this.$model, arguments)
@@ -1122,22 +1203,22 @@ var CollectionPrototype = {
1122
1203
  }
1123
1204
  return m + n //IE67的unshift不会返回长度
1124
1205
  },
1125
- shift: function() {
1206
+ shift: function () {
1126
1207
  if (this.length) {
1127
1208
  var el = this.$model.shift()
1128
1209
  mutateArray.call(this, "del", 0, 1, 0)
1129
1210
  return el //返回被移除的元素
1130
1211
  }
1131
1212
  },
1132
- pop: function() {
1133
- var m = this.length
1134
- if (m) {
1213
+ pop: function () {
1214
+ var n = this.length
1215
+ if (n) {
1135
1216
  var el = this.$model.pop()
1136
- mutateArray.call(this, "del", m - 1, 1, Math.max(0, m - 2))
1217
+ mutateArray.call(this, "del", n - 1, 1, Math.max(0, n - 2))
1137
1218
  return el //返回被移除的元素
1138
1219
  }
1139
1220
  },
1140
- splice: function(start) {
1221
+ splice: function (start) {
1141
1222
  var m = arguments.length, args = [], change
1142
1223
  var removed = _splice.apply(this.$model, arguments)
1143
1224
  if (removed.length) { //如果用户删掉了元素
@@ -1158,27 +1239,27 @@ var CollectionPrototype = {
1158
1239
  return []
1159
1240
  }
1160
1241
  },
1161
- contains: function(el) { //判定是否包含
1242
+ contains: function (el) { //判定是否包含
1162
1243
  return this.indexOf(el) !== -1
1163
1244
  },
1164
- remove: function(el) { //移除第一个等于给定值的元素
1245
+ remove: function (el) { //移除第一个等于给定值的元素
1165
1246
  return this.removeAt(this.indexOf(el))
1166
1247
  },
1167
- removeAt: function(index) { //移除指定索引上的元素
1248
+ removeAt: function (index) { //移除指定索引上的元素
1168
1249
  if (index >= 0) {
1169
1250
  this.$model.splice(index, 1)
1170
1251
  return mutateArray.call(this, "del", index, 1, 0)
1171
1252
  }
1172
1253
  return []
1173
1254
  },
1174
- clear: function() {
1255
+ clear: function () {
1175
1256
  this.$model.length = this.length = this._.length = 0 //清空数组
1176
1257
  this._fire("clear", 0)
1177
1258
  return this
1178
1259
  },
1179
- removeAll: function(all) { //移除N个元素
1260
+ removeAll: function (all) { //移除N个元素
1180
1261
  if (Array.isArray(all)) {
1181
- all.forEach(function(el) {
1262
+ all.forEach(function (el) {
1182
1263
  this.remove(el)
1183
1264
  }, this)
1184
1265
  } else if (typeof all === "function") {
@@ -1192,13 +1273,13 @@ var CollectionPrototype = {
1192
1273
  this.clear()
1193
1274
  }
1194
1275
  },
1195
- ensure: function(el) {
1276
+ ensure: function (el) {
1196
1277
  if (!this.contains(el)) { //只有不存在才push
1197
1278
  this.push(el)
1198
1279
  }
1199
1280
  return this
1200
1281
  },
1201
- set: function(index, val) {
1282
+ set: function (index, val) {
1202
1283
  if (index >= 0) {
1203
1284
  var valueType = avalon.type(val)
1204
1285
  if (val && val.$model) {
@@ -1237,8 +1318,8 @@ function sortByIndex(array, indexes) {
1237
1318
  }
1238
1319
  }
1239
1320
 
1240
- "sort,reverse".replace(rword, function(method) {
1241
- CollectionPrototype[method] = function() {
1321
+ "sort,reverse".replace(rword, function (method) {
1322
+ CollectionPrototype[method] = function () {
1242
1323
  var newArray = this.$model//这是要排序的新数组
1243
1324
  var oldArray = newArray.concat() //保持原来状态的旧数组
1244
1325
  var mask = Math.random()
@@ -1414,7 +1495,7 @@ function notifySubscribers(list) { //通知依赖于这个访问器的订阅者
1414
1495
  * HTML处理(parseHTML, innerHTML, clearHTML) *
1415
1496
  **************************************************************************/
1416
1497
  //parseHTML的辅助变量
1417
- var tagHooks = new function() {
1498
+ var tagHooks = new function() {// jshint ignore:line
1418
1499
  avalon.mix(this, {
1419
1500
  option: DOC.createElement("select"),
1420
1501
  thead: DOC.createElement("table"),
@@ -1429,7 +1510,7 @@ var tagHooks = new function() {
1429
1510
  this.optgroup = this.option
1430
1511
  this.tbody = this.tfoot = this.colgroup = this.caption = this.thead
1431
1512
  this.th = this.td
1432
- }
1513
+ }// jshint ignore:line
1433
1514
 
1434
1515
  String("circle,defs,ellipse,image,line,path,polygon,polyline,rect,symbol,text,use").replace(rword, function(tag) {
1435
1516
  tagHooks[tag] = tagHooks.g //处理SVG
@@ -1438,16 +1519,20 @@ var rtagName = /<([\w:]+)/
1438
1519
  var rxhtml = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig
1439
1520
  var scriptTypes = oneObject(["", "text/javascript", "text/ecmascript", "application/ecmascript", "application/javascript"])
1440
1521
  var script = DOC.createElement("script")
1441
-
1522
+ var rhtml = /<|&#?\w+;/
1442
1523
  avalon.parseHTML = function(html) {
1524
+ var fragment = hyperspace.cloneNode(false)
1443
1525
  if (typeof html !== "string" ) {
1444
- return DOC.createDocumentFragment()
1526
+ return fragment
1527
+ }
1528
+ if (!rhtml.test(html)) {
1529
+ fragment.appendChild(DOC.createTextNode(html))
1530
+ return fragment
1445
1531
  }
1446
1532
  html = html.replace(rxhtml, "<$1></$2>").trim()
1447
1533
  var tag = (rtagName.exec(html) || ["", ""])[1].toLowerCase(),
1448
1534
  //取得其标签名
1449
1535
  wrapper = tagHooks[tag] || tagHooks._default,
1450
- fragment = hyperspace.cloneNode(false),
1451
1536
  firstChild
1452
1537
  wrapper.innerHTML = html
1453
1538
  var els = wrapper.getElementsByTagName("script")
@@ -1457,7 +1542,7 @@ avalon.parseHTML = function(html) {
1457
1542
  var neo = script.cloneNode(false) //FF不能省略参数
1458
1543
  ap.forEach.call(el.attributes, function(attr) {
1459
1544
  neo.setAttribute(attr.name, attr.value)
1460
- })
1545
+ })// jshint ignore:line
1461
1546
  neo.text = el.text
1462
1547
  el.parentNode.replaceChild(neo, el)
1463
1548
  }
@@ -1579,7 +1664,7 @@ function scanAttr(elem, vmodels) {
1579
1664
  //防止setAttribute, removeAttribute时 attributes自动被同步,导致for循环出错
1580
1665
  var attributes = elem.hasAttributes() ? avalon.slice(elem.attributes) : []
1581
1666
  var bindings = [],
1582
- msData = {},
1667
+ msData = createMap(),
1583
1668
  match
1584
1669
  for (var i = 0, attr; attr = attributes[i++]; ) {
1585
1670
  if (attr.specified) {
@@ -1594,8 +1679,9 @@ function scanAttr(elem, vmodels) {
1594
1679
  param = type
1595
1680
  type = "on"
1596
1681
  } else if (obsoleteAttrs[type]) {
1597
- log("ms-" + type + "已经被废弃,请使用ms-attr-*代替")
1682
+ log("warning!请改用ms-attr-" + type + "代替ms-" + type + "!")
1598
1683
  if (type === "enabled") {//吃掉ms-enabled绑定,用ms-disabled代替
1684
+ log("warning!ms-enabled或ms-attr-enabled已经被废弃")
1599
1685
  type = "disabled"
1600
1686
  value = "!(" + value + ")"
1601
1687
  }
@@ -1619,11 +1705,11 @@ function scanAttr(elem, vmodels) {
1619
1705
  if (type === "html" || type === "text") {
1620
1706
  var token = getToken(value)
1621
1707
  avalon.mix(binding, token)
1622
- binding.filters = binding.filters.replace(rhasHtml, function() {
1708
+ binding.filters = binding.filters.replace(rhasHtml, function () {
1623
1709
  binding.type = "html"
1624
1710
  binding.group = 1
1625
1711
  return ""
1626
- })
1712
+ })// jshint ignore:line
1627
1713
  }
1628
1714
  if (name === "ms-if-loop") {
1629
1715
  binding.priority += 100
@@ -1638,8 +1724,14 @@ function scanAttr(elem, vmodels) {
1638
1724
  }
1639
1725
  }
1640
1726
  }
1641
- if (msData["ms-attr-checked"] && msData["ms-duplex"]) {
1642
- log("warning!一个元素上不能同时定义ms-attr-checked与ms-duplex")
1727
+ var control = elem.type
1728
+ if (control && msData["ms-duplex"]) {
1729
+ if (msData["ms-attr-checked"] && /radio|checkbox/.test(control)) {
1730
+ log("warning!" + control + "控件不能同时定义ms-attr-checked与ms-duplex")
1731
+ }
1732
+ if (msData["ms-attr-value"] && /text|password/.test(control)) {
1733
+ log("warning!" + control + "控件不能同时定义ms-attr-value与ms-duplex")
1734
+ }
1643
1735
  }
1644
1736
  bindings.sort(bindingSorter)
1645
1737
  var scanNode = true
@@ -1712,7 +1804,7 @@ var rhasHtml = /\|\s*html\s*/,
1712
1804
  function getToken(value) {
1713
1805
  if (value.indexOf("|") > 0) {
1714
1806
  var scapegoat = value.replace( rstringLiteral, function(_){
1715
- return Math.pow(10,_.length)
1807
+ return Array(_.length+1).join("1")// jshint ignore:line
1716
1808
  })
1717
1809
  var index = scapegoat.replace(r11a, "\u1122\u3344").indexOf("|") //干掉所有短路或
1718
1810
  if (index > -1) {
@@ -1787,7 +1879,7 @@ function scanText(textNode, vmodels) {
1787
1879
  token.type = "html"
1788
1880
  token.group = 1
1789
1881
  return ""
1790
- })
1882
+ })// jshint ignore:line
1791
1883
  bindings.push(token) //收集带有插值表达式的文本
1792
1884
  }
1793
1885
  hyperspace.appendChild(node)
@@ -1811,17 +1903,17 @@ function camelize(target) {
1811
1903
  if (target.indexOf("-") < 0 && target.indexOf("_") < 0) {
1812
1904
  return target //提前判断,提高getStyle等的效率
1813
1905
  }
1814
- return target.replace(/[-_][^-_]/g, function(match) {
1906
+ return target.replace(/[-_][^-_]/g, function (match) {
1815
1907
  return match.charAt(1).toUpperCase()
1816
1908
  })
1817
1909
  }
1818
1910
 
1819
- "add,remove".replace(rword, function(method) {
1820
- avalon.fn[method + "Class"] = function(cls) {
1911
+ "add,remove".replace(rword, function (method) {
1912
+ avalon.fn[method + "Class"] = function (cls) {
1821
1913
  var el = this[0]
1822
1914
  //https://developer.mozilla.org/zh-CN/docs/Mozilla/Firefox/Releases/26
1823
1915
  if (cls && typeof cls === "string" && el && el.nodeType === 1) {
1824
- cls.replace(/\S+/g, function(c) {
1916
+ cls.replace(/\S+/g, function (c) {
1825
1917
  el.classList[method](c)
1826
1918
  })
1827
1919
  }
@@ -1830,11 +1922,11 @@ function camelize(target) {
1830
1922
  })
1831
1923
 
1832
1924
  avalon.fn.mix({
1833
- hasClass: function(cls) {
1925
+ hasClass: function (cls) {
1834
1926
  var el = this[0] || {} //IE10+, chrome8+, firefox3.6+, safari5.1+,opera11.5+支持classList,chrome24+,firefox26+支持classList2.0
1835
1927
  return el.nodeType === 1 && el.classList.contains(cls)
1836
1928
  },
1837
- toggleClass: function(value, stateVal) {
1929
+ toggleClass: function (value, stateVal) {
1838
1930
  var className, i = 0
1839
1931
  var classNames = value.split(/\s+/)
1840
1932
  var isBool = typeof stateVal === "boolean"
@@ -1844,7 +1936,7 @@ avalon.fn.mix({
1844
1936
  }
1845
1937
  return this
1846
1938
  },
1847
- attr: function(name, value) {
1939
+ attr: function (name, value) {
1848
1940
  if (arguments.length === 2) {
1849
1941
  this[0].setAttribute(name, value)
1850
1942
  return this
@@ -1852,7 +1944,7 @@ avalon.fn.mix({
1852
1944
  return this[0].getAttribute(name)
1853
1945
  }
1854
1946
  },
1855
- data: function(name, value) {
1947
+ data: function (name, value) {
1856
1948
  name = "data-" + hyphen(name || "")
1857
1949
  switch (arguments.length) {
1858
1950
  case 2:
@@ -1863,7 +1955,7 @@ avalon.fn.mix({
1863
1955
  return parseData(val)
1864
1956
  case 0:
1865
1957
  var ret = {}
1866
- ap.forEach.call(this[0].attributes, function(attr) {
1958
+ ap.forEach.call(this[0].attributes, function (attr) {
1867
1959
  if (attr) {
1868
1960
  name = attr.name
1869
1961
  if (!name.indexOf("data-")) {
@@ -1875,12 +1967,12 @@ avalon.fn.mix({
1875
1967
  return ret
1876
1968
  }
1877
1969
  },
1878
- removeData: function(name) {
1970
+ removeData: function (name) {
1879
1971
  name = "data-" + hyphen(name)
1880
1972
  this[0].removeAttribute(name)
1881
1973
  return this
1882
1974
  },
1883
- css: function(name, value) {
1975
+ css: function (name, value) {
1884
1976
  if (avalon.isPlainObject(name)) {
1885
1977
  for (var i in name) {
1886
1978
  avalon.css(this, i, name[i])
@@ -1890,7 +1982,7 @@ avalon.fn.mix({
1890
1982
  }
1891
1983
  return ret !== void 0 ? ret : this
1892
1984
  },
1893
- position: function() {
1985
+ position: function () {
1894
1986
  var offsetParent, offset,
1895
1987
  elem = this[0],
1896
1988
  parentOffset = {
@@ -1916,25 +2008,25 @@ avalon.fn.mix({
1916
2008
  left: offset.left - parentOffset.left - avalon.css(elem, "marginLeft", true)
1917
2009
  }
1918
2010
  },
1919
- offsetParent: function() {
2011
+ offsetParent: function () {
1920
2012
  var offsetParent = this[0].offsetParent
1921
2013
  while (offsetParent && avalon.css(offsetParent, "position") === "static") {
1922
2014
  offsetParent = offsetParent.offsetParent;
1923
2015
  }
1924
- return avalon(offsetParent)
2016
+ return avalon(offsetParent || root)
1925
2017
  },
1926
- bind: function(type, fn, phase) {
2018
+ bind: function (type, fn, phase) {
1927
2019
  if (this[0]) { //此方法不会链
1928
2020
  return avalon.bind(this[0], type, fn, phase)
1929
2021
  }
1930
2022
  },
1931
- unbind: function(type, fn, phase) {
2023
+ unbind: function (type, fn, phase) {
1932
2024
  if (this[0]) {
1933
2025
  avalon.unbind(this[0], type, fn, phase)
1934
2026
  }
1935
2027
  return this
1936
2028
  },
1937
- val: function(value) {
2029
+ val: function (value) {
1938
2030
  var node = this[0]
1939
2031
  if (node && node.nodeType === 1) {
1940
2032
  var get = arguments.length === 0
@@ -1953,7 +2045,7 @@ avalon.fn.mix({
1953
2045
  })
1954
2046
 
1955
2047
  if (root.dataset) {
1956
- avalon.fn.data = function(name, val) {
2048
+ avalon.fn.data = function (name, val) {
1957
2049
  var dataset = this[0].dataset
1958
2050
  switch (arguments.length) {
1959
2051
  case 2:
@@ -1963,8 +2055,8 @@ if (root.dataset) {
1963
2055
  val = dataset[name]
1964
2056
  return parseData(val)
1965
2057
  case 0:
1966
- var ret = {}
1967
- for (var name in dataset) {
2058
+ var ret = createMap()
2059
+ for (name in dataset) {
1968
2060
  ret[name] = parseData(dataset[name])
1969
2061
  }
1970
2062
  return ret
@@ -1988,15 +2080,15 @@ function parseData(data) {
1988
2080
  avalon.each({
1989
2081
  scrollLeft: "pageXOffset",
1990
2082
  scrollTop: "pageYOffset"
1991
- }, function(method, prop) {
1992
- avalon.fn[method] = function(val) {
2083
+ }, function (method, prop) {
2084
+ avalon.fn[method] = function (val) {
1993
2085
  var node = this[0] || {}, win = getWindow(node),
1994
2086
  top = method === "scrollTop"
1995
2087
  if (!arguments.length) {
1996
2088
  return win ? win[prop] : node[method]
1997
2089
  } else {
1998
2090
  if (win) {
1999
- win.scrollTo(!top ? val : avalon(win).scrollLeft(), top ? val : avalon(win).scrollTop())
2091
+ win.scrollTo(!top ? val : win[prop], top ? val : win[prop])
2000
2092
  } else {
2001
2093
  node[method] = val
2002
2094
  }
@@ -2009,14 +2101,14 @@ function getWindow(node) {
2009
2101
  }
2010
2102
 
2011
2103
  //=============================css相关==================================
2012
- var cssHooks = avalon.cssHooks = {}
2104
+ var cssHooks = avalon.cssHooks = createMap()
2013
2105
  var prefixes = ["", "-webkit-", "-moz-", "-ms-"]//去掉opera-15的支持
2014
2106
  var cssMap = {
2015
2107
  "float": "cssFloat"
2016
2108
  }
2017
2109
  avalon.cssNumber = oneObject("columnCount,order,fillOpacity,fontWeight,lineHeight,opacity,orphans,widows,zIndex,zoom")
2018
2110
 
2019
- avalon.cssName = function(name, host, camelCase) {
2111
+ avalon.cssName = function (name, host, camelCase) {
2020
2112
  if (cssMap[name]) {
2021
2113
  return cssMap[name]
2022
2114
  }
@@ -2029,15 +2121,15 @@ avalon.cssName = function(name, host, camelCase) {
2029
2121
  }
2030
2122
  return null
2031
2123
  }
2032
- cssHooks["@:set"] = function(node, name, value) {
2124
+ cssHooks["@:set"] = function (node, name, value) {
2033
2125
  node.style[name] = value
2034
2126
  }
2035
2127
 
2036
- cssHooks["@:get"] = function(node, name) {
2128
+ cssHooks["@:get"] = function (node, name) {
2037
2129
  if (!node || !node.style) {
2038
2130
  throw new Error("getComputedStyle要求传入一个节点 " + node)
2039
2131
  }
2040
- var ret, computed = getComputedStyle(node, null)
2132
+ var ret, computed = getComputedStyle(node)
2041
2133
  if (computed) {
2042
2134
  ret = name === "filter" ? computed.getPropertyValue(name) : computed[name]
2043
2135
  if (ret === "") {
@@ -2046,13 +2138,13 @@ cssHooks["@:get"] = function(node, name) {
2046
2138
  }
2047
2139
  return ret
2048
2140
  }
2049
- cssHooks["opacity:get"] = function(node) {
2141
+ cssHooks["opacity:get"] = function (node) {
2050
2142
  var ret = cssHooks["@:get"](node, "opacity")
2051
2143
  return ret === "" ? "1" : ret
2052
2144
  }
2053
2145
 
2054
- "top,left".replace(rword, function(name) {
2055
- cssHooks[name + ":get"] = function(node) {
2146
+ "top,left".replace(rword, function (name) {
2147
+ cssHooks[name + ":get"] = function (node) {
2056
2148
  var computed = cssHooks["@:get"](node, name)
2057
2149
  return /px$/.test(computed) ? computed :
2058
2150
  avalon(node).position()[name] + "px"
@@ -2086,12 +2178,12 @@ function showHidden(node, array) {
2086
2178
  }
2087
2179
  }
2088
2180
 
2089
- "Width,Height".replace(rword, function(name) {//fix 481
2181
+ "Width,Height".replace(rword, function (name) {//fix 481
2090
2182
  var method = name.toLowerCase(),
2091
2183
  clientProp = "client" + name,
2092
2184
  scrollProp = "scroll" + name,
2093
2185
  offsetProp = "offset" + name
2094
- cssHooks[method + ":get"] = function(node, which, override) {
2186
+ cssHooks[method + ":get"] = function (node, which, override) {
2095
2187
  var boxSizing = -4
2096
2188
  if (typeof override === "number") {
2097
2189
  boxSizing = override
@@ -2099,23 +2191,17 @@ function showHidden(node, array) {
2099
2191
  which = name === "Width" ? ["Left", "Right"] : ["Top", "Bottom"]
2100
2192
  var ret = node[offsetProp] // border-box 0
2101
2193
  if (boxSizing === 2) { // margin-box 2
2102
- return ret
2103
- + avalon.css(node, "margin" + which[0], true)
2104
- + avalon.css(node, "margin" + which[1], true)
2194
+ return ret + avalon.css(node, "margin" + which[0], true) + avalon.css(node, "margin" + which[1], true)
2105
2195
  }
2106
2196
  if (boxSizing < 0) { // padding-box -2
2107
- ret = ret
2108
- - avalon.css(node, "border" + which[0] + "Width", true)
2109
- - avalon.css(node, "border" + which[1] + "Width", true)
2197
+ ret = ret - avalon.css(node, "border" + which[0] + "Width", true) - avalon.css(node, "border" + which[1] + "Width", true)
2110
2198
  }
2111
2199
  if (boxSizing === -4) { // content-box -4
2112
- ret = ret
2113
- - avalon.css(node, "padding" + which[0], true)
2114
- - avalon.css(node, "padding" + which[1], true)
2200
+ ret = ret - avalon.css(node, "padding" + which[0], true) - avalon.css(node, "padding" + which[1], true)
2115
2201
  }
2116
2202
  return ret
2117
2203
  }
2118
- cssHooks[method + "&get"] = function(node) {
2204
+ cssHooks[method + "&get"] = function (node) {
2119
2205
  var hidden = [];
2120
2206
  showHidden(node, hidden);
2121
2207
  var val = cssHooks[method + ":get"](node)
@@ -2129,11 +2215,11 @@ function showHidden(node, array) {
2129
2215
  }
2130
2216
  return val;
2131
2217
  }
2132
- avalon.fn[method] = function(value) { //会忽视其display
2218
+ avalon.fn[method] = function (value) { //会忽视其display
2133
2219
  var node = this[0]
2134
2220
  if (arguments.length === 0) {
2135
2221
  if (node.setTimeout) { //取得窗口尺寸,IE9后可以用node.innerWidth /innerHeight代替
2136
- return node["inner" + name]
2222
+ return node["inner" + name]
2137
2223
  }
2138
2224
  if (node.nodeType === 9) { //取得页面尺寸
2139
2225
  var doc = node.documentElement
@@ -2147,33 +2233,33 @@ function showHidden(node, array) {
2147
2233
  return this.css(method, value)
2148
2234
  }
2149
2235
  }
2150
- avalon.fn["inner" + name] = function() {
2236
+ avalon.fn["inner" + name] = function () {
2151
2237
  return cssHooks[method + ":get"](this[0], void 0, -2)
2152
2238
  }
2153
- avalon.fn["outer" + name] = function(includeMargin) {
2239
+ avalon.fn["outer" + name] = function (includeMargin) {
2154
2240
  return cssHooks[method + ":get"](this[0], void 0, includeMargin === true ? 2 : 0)
2155
2241
  }
2156
2242
  })
2157
- avalon.fn.offset = function() { //取得距离页面左右角的坐标
2158
- var node = this[0], box = {
2159
- left: 0,
2160
- top: 0
2161
- }
2162
- if (!node || !node.tagName || !node.ownerDocument) {
2163
- return box
2164
- }
2165
- var doc = node.ownerDocument,
2166
- root = doc.documentElement,
2167
- win = doc.defaultView
2168
- if (!root.contains(node)) {
2169
- return box
2170
- }
2171
- if (node.getBoundingClientRect !== void 0) {
2172
- box = node.getBoundingClientRect()
2173
- }
2174
- return {
2175
- top: box.top + win.pageYOffset - root.clientTop,
2176
- left: box.left + win.pageXOffset - root.clientLeft
2243
+ avalon.fn.offset = function () { //取得距离页面左右角的坐标
2244
+ var node = this[0]
2245
+ try {
2246
+ var rect = node.getBoundingClientRect()
2247
+ // Make sure element is not hidden (display: none) or disconnected
2248
+ // https://github.com/jquery/jquery/pull/2043/files#r23981494
2249
+ if (rect.width || rect.height || node.getClientRects().length) {
2250
+ var doc = node.ownerDocument
2251
+ var root = doc.documentElement
2252
+ var win = doc.defaultView
2253
+ return {
2254
+ top: rect.top + win.pageYOffset - root.clientTop,
2255
+ left: rect.left + win.pageXOffset - root.clientLeft
2256
+ }
2257
+ }
2258
+ } catch (e) {
2259
+ return {
2260
+ left: 0,
2261
+ top: 0
2262
+ }
2177
2263
  }
2178
2264
  }
2179
2265
  //=============================val相关=======================
@@ -2183,7 +2269,7 @@ function getValType(el) {
2183
2269
  return ret === "input" && /checkbox|radio/.test(el.type) ? "checked" : ret
2184
2270
  }
2185
2271
  var valHooks = {
2186
- "select:get": function(node, value) {
2272
+ "select:get": function (node, value) {
2187
2273
  var option, options = node.options,
2188
2274
  index = node.selectedIndex,
2189
2275
  one = node.type === "select-one" || index < 0,
@@ -2206,7 +2292,7 @@ var valHooks = {
2206
2292
  }
2207
2293
  return values
2208
2294
  },
2209
- "select:set": function(node, values, optionSet) {
2295
+ "select:set": function (node, values, optionSet) {
2210
2296
  values = [].concat(values) //强制转换为数组
2211
2297
  for (var i = 0, el; el = node.options[i++]; ) {
2212
2298
  if ((el.selected = values.indexOf(el.value) > -1)) {
@@ -2224,28 +2310,26 @@ var valHooks = {
2224
2310
  **********************************************************************/
2225
2311
  var quote = JSON.stringify
2226
2312
 
2227
- var keywords =
2228
- // 关键字
2229
- "break,case,catch,continue,debugger,default,delete,do,else,false" +
2230
- ",finally,for,function,if,in,instanceof,new,null,return,switch,this" +
2231
- ",throw,true,try,typeof,var,void,while,with" +
2232
- // 保留字
2233
- ",abstract,boolean,byte,char,class,const,double,enum,export,extends" +
2234
- ",final,float,goto,implements,import,int,interface,long,native" +
2235
- ",package,private,protected,public,short,static,super,synchronized" +
2236
- ",throws,transient,volatile" +
2237
- // ECMA 5 - use strict
2238
- ",arguments,let,yield" + ",undefined"
2313
+ var keywords = [
2314
+ "break,case,catch,continue,debugger,default,delete,do,else,false",
2315
+ "finally,for,function,if,in,instanceof,new,null,return,switch,this",
2316
+ "throw,true,try,typeof,var,void,while,with", /* 关键字*/
2317
+ "abstract,boolean,byte,char,class,const,double,enum,export,extends",
2318
+ "final,float,goto,implements,import,int,interface,long,native",
2319
+ "package,private,protected,public,short,static,super,synchronized",
2320
+ "throws,transient,volatile", /*保留字*/
2321
+ "arguments,let,yield,undefined" /* ECMA 5 - use strict*/].join(",")
2239
2322
  var rrexpstr = /\/\*[\w\W]*?\*\/|\/\/[^\n]*\n|\/\/[^\n]*$|"(?:[^"\\]|\\[\w\W])*"|'(?:[^'\\]|\\[\w\W])*'|[\s\t\n]*\.[\s\t\n]*[$\w\.]+/g
2240
2323
  var rsplit = /[^\w$]+/g
2241
2324
  var rkeywords = new RegExp(["\\b" + keywords.replace(/,/g, '\\b|\\b') + "\\b"].join('|'), 'g')
2242
2325
  var rnumber = /\b\d[^,]*/g
2243
2326
  var rcomma = /^,+|,+$/g
2244
- var cacheVars = createCache(512)
2245
- var getVariables = function(code) {
2327
+ var cacheVars = new Cache(512)
2328
+ var getVariables = function (code) {
2246
2329
  var key = "," + code.trim()
2247
- if (cacheVars[key]) {
2248
- return cacheVars[key]
2330
+ var ret = cacheVars.get(key)
2331
+ if (ret) {
2332
+ return ret
2249
2333
  }
2250
2334
  var match = code
2251
2335
  .replace(rrexpstr, "")
@@ -2254,7 +2338,7 @@ var getVariables = function(code) {
2254
2338
  .replace(rnumber, "")
2255
2339
  .replace(rcomma, "")
2256
2340
  .split(/^$|,+/)
2257
- return cacheVars(key, uniqSet(match))
2341
+ return cacheVars.put(key, uniqSet(match))
2258
2342
  }
2259
2343
  /*添加赋值语句*/
2260
2344
 
@@ -2287,7 +2371,7 @@ function uniqSet(array) {
2287
2371
  return ret
2288
2372
  }
2289
2373
  //缓存求值函数,以便多次利用
2290
- var cacheExprs = createCache(128)
2374
+ var cacheExprs = new Cache(128)
2291
2375
  //取得求值函数及其传参
2292
2376
  var rduplex = /\w\[.*\]|\w\.\w/
2293
2377
  var rproxy = /(\$proxy\$[a-z]+)\d+$/
@@ -2299,16 +2383,16 @@ var rthimLeftParentheses = /"\s*\(/g
2299
2383
  function parseFilter(val, filters) {
2300
2384
  filters = filters
2301
2385
  .replace(rthimRightParentheses, "")//处理最后的小括号
2302
- .replace(rthimOtherParentheses, function() {//处理其他小括号
2386
+ .replace(rthimOtherParentheses, function () {//处理其他小括号
2303
2387
  return "],|"
2304
2388
  })
2305
- .replace(rquoteFilterName, function(a, b) { //处理|及它后面的过滤器的名字
2389
+ .replace(rquoteFilterName, function (a, b) { //处理|及它后面的过滤器的名字
2306
2390
  return "[" + quote(b)
2307
2391
  })
2308
- .replace(rpatchBracket, function() {
2392
+ .replace(rpatchBracket, function () {
2309
2393
  return '"],["'
2310
2394
  })
2311
- .replace(rthimLeftParentheses, function() {
2395
+ .replace(rthimLeftParentheses, function () {
2312
2396
  return '",'
2313
2397
  }) + "]"
2314
2398
  return "return avalon.filters.$filter(" + val + ", " + filters + ")"
@@ -2317,7 +2401,7 @@ function parseFilter(val, filters) {
2317
2401
  function parseExpr(code, scopes, data) {
2318
2402
  var dataType = data.type
2319
2403
  var filters = data.filters || ""
2320
- var exprId = scopes.map(function(el) {
2404
+ var exprId = scopes.map(function (el) {
2321
2405
  return String(el.$id).replace(rproxy, "$1")
2322
2406
  }) + code + dataType + filters
2323
2407
  var vars = getVariables(code).concat(),
@@ -2341,9 +2425,9 @@ function parseExpr(code, scopes, data) {
2341
2425
  }
2342
2426
  if (dataType !== "duplex" && (code.indexOf("||") > -1 || code.indexOf("&&") > -1)) {
2343
2427
  //https://github.com/RubyLouvre/avalon/issues/583
2344
- data.vars.forEach(function(v) {
2428
+ data.vars.forEach(function (v) {
2345
2429
  var reg = new RegExp("\\b" + v + "(?:\\.\\w+|\\[\\w+\\])+", "ig")
2346
- code = code.replace(reg, function(_) {
2430
+ code = code.replace(reg, function (_) {
2347
2431
  var c = _.charAt(v.length)
2348
2432
  var r = IEVersion ? code.slice(arguments[1] + _.length) : RegExp.rightContext
2349
2433
  var method = /^\s*\(/.test(r)
@@ -2370,7 +2454,7 @@ function parseExpr(code, scopes, data) {
2370
2454
  //---------------args----------------
2371
2455
  data.args = args
2372
2456
  //---------------cache----------------
2373
- var fn = cacheExprs[exprId] //直接从缓存,免得重复生成
2457
+ var fn = cacheExprs.get(exprId) //直接从缓存,免得重复生成
2374
2458
  if (fn) {
2375
2459
  data.evaluator = fn
2376
2460
  return
@@ -2394,7 +2478,7 @@ function parseExpr(code, scopes, data) {
2394
2478
  "= vvv;\n} "
2395
2479
  try {
2396
2480
  fn = Function.apply(noop, names.concat(_body))
2397
- data.evaluator = cacheExprs(exprId, fn)
2481
+ data.evaluator = cacheExprs.put(exprId, fn)
2398
2482
  } catch (e) {
2399
2483
  log("debug: parse error," + e.message)
2400
2484
  }
@@ -2416,11 +2500,11 @@ function parseExpr(code, scopes, data) {
2416
2500
  }
2417
2501
  try {
2418
2502
  fn = Function.apply(noop, names.concat("'use strict';\n" + prefix + code))
2419
- data.evaluator = cacheExprs(exprId, fn)
2503
+ data.evaluator = cacheExprs.put(exprId, fn)
2420
2504
  } catch (e) {
2421
2505
  log("debug: parse error," + e.message)
2422
2506
  } finally {
2423
- vars = textBuffer = names = null //释放内存
2507
+ vars = assigns = names = null //释放内存
2424
2508
  }
2425
2509
  }
2426
2510
 
@@ -2429,7 +2513,7 @@ function parseExpr(code, scopes, data) {
2429
2513
 
2430
2514
  function parseExprProxy(code, scopes, data, tokens, noregister) {
2431
2515
  if (Array.isArray(tokens)) {
2432
- code = tokens.map(function(el) {
2516
+ code = tokens.map(function (el) {
2433
2517
  return el.expr ? "(" + el.value + ")" : quote(el.value)
2434
2518
  }).join(" + ")
2435
2519
  }
@@ -2443,10 +2527,12 @@ function parseExprProxy(code, scopes, data, tokens, noregister) {
2443
2527
  }
2444
2528
  }
2445
2529
  avalon.parseExprProxy = parseExprProxy
2446
- var bools = "autofocus,autoplay,async,allowTransparency,checked,controls,declare,disabled,defer,defaultChecked,defaultSelected" +
2447
- "contentEditable,isMap,loop,multiple,noHref,noResize,noShade,open,readOnly,selected"
2530
+ var bools = ["autofocus,autoplay,async,allowTransparency,checked,controls",
2531
+ "declare,disabled,defer,defaultChecked,defaultSelected",
2532
+ "contentEditable,isMap,loop,multiple,noHref,noResize,noShade",
2533
+ "open,readOnly,selected"].join(",")
2448
2534
  var boolMap = {}
2449
- bools.replace(rword, function(name) {
2535
+ bools.replace(rword, function (name) {
2450
2536
  boolMap[name.toLowerCase()] = name
2451
2537
  })
2452
2538
 
@@ -2459,21 +2545,23 @@ var propMap = {//属性名映射
2459
2545
  "http-equiv": "httpEquiv"
2460
2546
  }
2461
2547
 
2462
- var anomaly = "accessKey,bgColor,cellPadding,cellSpacing,codeBase,codeType,colSpan," + "dateTime,defaultValue,frameBorder,longDesc,maxLength,marginWidth,marginHeight," + "rowSpan,tabIndex,useMap,vSpace,valueType,vAlign"
2463
- anomaly.replace(rword, function(name) {
2548
+ var anomaly = ["accessKey,bgColor,cellPadding,cellSpacing,codeBase,codeType,colSpan",
2549
+ "dateTime,defaultValue,frameBorder,longDesc,maxLength,marginWidth,marginHeight",
2550
+ "rowSpan,tabIndex,useMap,vSpace,valueType,vAlign"].join(",")
2551
+ anomaly.replace(rword, function (name) {
2464
2552
  propMap[name.toLowerCase()] = name
2465
2553
  })
2466
2554
 
2467
2555
  var rnoscripts = /<noscript.*?>(?:[\s\S]+?)<\/noscript>/img
2468
2556
  var rnoscriptText = /<noscript.*?>([\s\S]+?)<\/noscript>/im
2469
2557
 
2470
- var getXHR = function() {
2471
- return new (window.XMLHttpRequest || ActiveXObject)("Microsoft.XMLHTTP")
2558
+ var getXHR = function () {
2559
+ return new (window.XMLHttpRequest || ActiveXObject)("Microsoft.XMLHTTP")// jshint ignore:line
2472
2560
  }
2473
2561
 
2474
2562
  var cacheTmpls = avalon.templateCache = {}
2475
2563
 
2476
- bindingHandlers.attr = function(data, vmodels) {
2564
+ bindingHandlers.attr = function (data, vmodels) {
2477
2565
  var text = data.value.trim(),
2478
2566
  simple = true
2479
2567
  if (text.indexOf(openTag) > -1 && text.indexOf(closeTag) > 2) {
@@ -2487,7 +2575,10 @@ bindingHandlers.attr = function(data, vmodels) {
2487
2575
  var elem = data.element
2488
2576
  data.includeRendered = getBindingCallback(elem, "data-include-rendered", vmodels)
2489
2577
  data.includeLoaded = getBindingCallback(elem, "data-include-loaded", vmodels)
2490
- var outer = data.includeReplaced = !!avalon(elem).data("includeReplace")
2578
+ var outer = data.includeReplace = !!avalon(elem).data("includeReplace")
2579
+ if (avalon(elem).data("includeCache")) {
2580
+ data.templateCache = {}
2581
+ }
2491
2582
  data.startInclude = DOC.createComment("ms-include")
2492
2583
  data.endInclude = DOC.createComment("ms-include-end")
2493
2584
  if (outer) {
@@ -2503,7 +2594,7 @@ bindingHandlers.attr = function(data, vmodels) {
2503
2594
  parseExprProxy(text, vmodels, data, (simple ? 0 : scanExpr(data.value)))
2504
2595
  }
2505
2596
 
2506
- bindingExecutors.attr = function(val, elem, data) {
2597
+ bindingExecutors.attr = function (val, elem, data) {
2507
2598
  var method = data.type,
2508
2599
  attrName = data.param
2509
2600
  if (method === "css") {
@@ -2538,38 +2629,50 @@ bindingExecutors.attr = function(val, elem, data) {
2538
2629
  var vmodels = data.vmodels
2539
2630
  var rendered = data.includeRendered
2540
2631
  var loaded = data.includeLoaded
2541
- var replace = data.includeReplaced
2632
+ var replace = data.includeReplace
2542
2633
  var target = replace ? elem.parentNode : elem
2543
- var scanTemplate = function(text) {
2634
+ var scanTemplate = function (text) {
2544
2635
  if (loaded) {
2545
2636
  text = loaded.apply(target, [text].concat(vmodels))
2546
2637
  }
2547
2638
  if (rendered) {
2548
- checkScan(target, function() {
2639
+ checkScan(target, function () {
2549
2640
  rendered.call(target)
2550
2641
  }, NaN)
2551
2642
  }
2643
+ var lastID = data.includeLastID
2644
+ if (data.templateCache && lastID && lastID !== val) {
2645
+ var lastTemplate = data.templateCache[lastID]
2646
+ if (!lastTemplate) {
2647
+ lastTemplate = data.templateCache[lastID] = DOC.createElement("div")
2648
+ ifGroup.appendChild(lastTemplate)
2649
+ }
2650
+ }
2651
+ data.includeLastID = val
2552
2652
  while (true) {
2553
2653
  var node = data.startInclude.nextSibling
2554
2654
  if (node && node !== data.endInclude) {
2555
2655
  target.removeChild(node)
2656
+ if (lastTemplate)
2657
+ lastTemplate.appendChild(node)
2556
2658
  } else {
2557
2659
  break
2558
2660
  }
2559
2661
  }
2560
- var dom = avalon.parseHTML(text)
2662
+ var dom = getTemplateNodes(data, val, text)
2561
2663
  var nodes = avalon.slice(dom.childNodes)
2562
2664
  target.insertBefore(dom, data.endInclude)
2563
2665
  scanNodeArray(nodes, vmodels)
2564
2666
  }
2667
+
2565
2668
  if (data.param === "src") {
2566
2669
  if (cacheTmpls[val]) {
2567
- avalon.nextTick(function() {
2670
+ avalon.nextTick(function () {
2568
2671
  scanTemplate(cacheTmpls[val])
2569
2672
  })
2570
2673
  } else {
2571
2674
  var xhr = getXHR()
2572
- xhr.onreadystatechange = function() {
2675
+ xhr.onreadystatechange = function () {
2573
2676
  if (xhr.readyState === 4) {
2574
2677
  var s = xhr.status
2575
2678
  if (s >= 200 && s < 300 || s === 304 || s === 1223) {
@@ -2605,7 +2708,7 @@ bindingExecutors.attr = function(val, elem, data) {
2605
2708
  }
2606
2709
  }
2607
2710
  }
2608
- avalon.nextTick(function() {
2711
+ avalon.nextTick(function () {
2609
2712
  scanTemplate(el.fixIE78 || el.value || el.innerText || el.innerHTML)
2610
2713
  })
2611
2714
  }
@@ -2624,8 +2727,20 @@ bindingExecutors.attr = function(val, elem, data) {
2624
2727
  }
2625
2728
  }
2626
2729
 
2730
+ function getTemplateNodes(data, id, text) {
2731
+ var div = data.templateCache && data.templateCache[id]
2732
+ if (div) {
2733
+ var dom = DOC.createDocumentFragment(), firstChild
2734
+ while (firstChild = div.firstChild) {
2735
+ dom.appendChild(firstChild)
2736
+ }
2737
+ return dom
2738
+ }
2739
+ return avalon.parseHTML(text)
2740
+ }
2741
+
2627
2742
  //这几个指令都可以使用插值表达式,如ms-src="aaa/{{b}}/{{c}}.html"
2628
- "title,alt,src,value,css,include,href".replace(rword, function(name) {
2743
+ "title,alt,src,value,css,include,href".replace(rword, function (name) {
2629
2744
  bindingHandlers[name] = bindingHandlers.attr
2630
2745
  })
2631
2746
  //根据VM的属性值或表达式的值切换类名,ms-class="xxx yyy zzz:flag"
@@ -2733,7 +2848,7 @@ bindingExecutors.data = function(val, elem, data) {
2733
2848
  }
2734
2849
 
2735
2850
  //双工绑定
2736
- var duplexBinding = bindingHandlers.duplex = function(data, vmodels) {
2851
+ var duplexBinding = bindingHandlers.duplex = function (data, vmodels) {
2737
2852
  var elem = data.element,
2738
2853
  hasCast
2739
2854
  parseExprProxy(data.value, vmodels, data, 0, 1)
@@ -2748,7 +2863,7 @@ var duplexBinding = bindingHandlers.duplex = function(data, vmodels) {
2748
2863
  if (elem.msData) {
2749
2864
  elem.msData["ms-duplex"] = data.value
2750
2865
  }
2751
- data.param.replace(/\w+/g, function(name) {
2866
+ data.param.replace(/\w+/g, function (name) {
2752
2867
  if (/^(checkbox|radio)$/.test(elem.type) && /^(radio|checked)$/.test(name)) {
2753
2868
  if (name === "radio")
2754
2869
  log("ms-duplex-radio已经更名为ms-duplex-checked")
@@ -2771,14 +2886,14 @@ var duplexBinding = bindingHandlers.duplex = function(data, vmodels) {
2771
2886
  params.push("string")
2772
2887
  }
2773
2888
  data.param = params.join("-")
2774
- data.bound = function(type, callback) {
2889
+ data.bound = function (type, callback) {
2775
2890
  if (elem.addEventListener) {
2776
2891
  elem.addEventListener(type, callback, false)
2777
2892
  } else {
2778
2893
  elem.attachEvent("on" + type, callback)
2779
2894
  }
2780
2895
  var old = data.rollback
2781
- data.rollback = function() {
2896
+ data.rollback = function () {
2782
2897
  elem.avalonSetter = null
2783
2898
  avalon.unbind(elem, type, callback)
2784
2899
  old && old()
@@ -2800,32 +2915,44 @@ function fixNull(val) {
2800
2915
  }
2801
2916
  avalon.duplexHooks = {
2802
2917
  checked: {
2803
- get: function(val, data) {
2918
+ get: function (val, data) {
2804
2919
  return !data.element.oldValue
2805
2920
  }
2806
2921
  },
2807
2922
  string: {
2808
- get: function(val) { //同步到VM
2923
+ get: function (val) { //同步到VM
2809
2924
  return val
2810
2925
  },
2811
2926
  set: fixNull
2812
2927
  },
2813
2928
  "boolean": {
2814
- get: function(val) {
2929
+ get: function (val) {
2815
2930
  return val === "true"
2816
2931
  },
2817
2932
  set: fixNull
2818
2933
  },
2819
2934
  number: {
2820
- get: function(val) {
2821
- return isFinite(val) ? parseFloat(val) || 0 : val
2935
+ get: function (val, data) {
2936
+ var number = parseFloat(val)
2937
+ if (-val === -number) {
2938
+ return number
2939
+ }
2940
+ var arr = /strong|medium|weak/.exec(data.element.getAttribute("data-duplex-number")) || ["medium"]
2941
+ switch (arr[0]) {
2942
+ case "strong":
2943
+ return 0
2944
+ case "medium":
2945
+ return val === "" ? "" : 0
2946
+ case "weak":
2947
+ return val
2948
+ }
2822
2949
  },
2823
2950
  set: fixNull
2824
2951
  }
2825
2952
  }
2826
2953
 
2827
2954
  function pipe(val, data, action, e) {
2828
- data.param.replace(/\w+/g, function(name) {
2955
+ data.param.replace(/\w+/g, function (name) {
2829
2956
  var hook = avalon.duplexHooks[name]
2830
2957
  if (hook && typeof hook[action] === "function") {
2831
2958
  val = hook[action](val, data)
@@ -2835,18 +2962,8 @@ function pipe(val, data, action, e) {
2835
2962
  }
2836
2963
 
2837
2964
  var TimerID, ribbon = []
2838
- function W3CFire(el, name, detail) {
2839
- var event = DOC.createEvent("Events")
2840
- event.initEvent(name, true, true)
2841
- event.fireByAvalon = true//签名,标记事件是由avalon触发
2842
- //event.isTrusted = false 设置这个opera会报错
2843
- if (detail)
2844
- event.detail = detail
2845
- el.dispatchEvent(event)
2846
- }
2847
-
2848
2965
 
2849
- avalon.tick = function(fn) {
2966
+ avalon.tick = function (fn) {
2850
2967
  if (ribbon.push(fn) === 1) {
2851
2968
  TimerID = setInterval(ticker, 60)
2852
2969
  }
@@ -2865,15 +2982,18 @@ function ticker() {
2865
2982
  }
2866
2983
 
2867
2984
  var watchValueInTimer = noop
2868
- new function() {
2985
+ var rmsinput = /text|password|hidden/
2986
+ new function () {// jshint ignore:line
2869
2987
  try {//#272 IE9-IE11, firefox
2870
2988
  var setters = {}
2871
2989
  var aproto = HTMLInputElement.prototype
2872
2990
  var bproto = HTMLTextAreaElement.prototype
2873
- function newSetter(value) {
2991
+ function newSetter(value) {// jshint ignore:line
2874
2992
  if (avalon.contains(root, this)) {
2875
2993
  setters[this.tagName].call(this, value)
2876
- if (this.avalonSetter) {
2994
+ if (!rmsinput.test(this.type))
2995
+ return
2996
+ if (!this.msFocus && this.avalonSetter) {
2877
2997
  this.avalonSetter()
2878
2998
  }
2879
2999
  }
@@ -2891,12 +3011,12 @@ new function() {
2891
3011
  } catch (e) {
2892
3012
  watchValueInTimer = avalon.tick
2893
3013
  }
2894
- }
3014
+ }// jshint ignore:line
2895
3015
 
2896
3016
 
2897
3017
  //处理radio, checkbox, text, textarea, password
2898
- duplexBinding.INPUT = function(element, evaluator, data) {
2899
- var type = element.type,
3018
+ duplexBinding.INPUT = function (element, evaluator, data) {
3019
+ var $type = element.type,
2900
3020
  bound = data.bound,
2901
3021
  $elem = avalon(element),
2902
3022
  composing = false
@@ -2910,7 +3030,8 @@ duplexBinding.INPUT = function(element, evaluator, data) {
2910
3030
  composing = false
2911
3031
  }
2912
3032
  //当value变化时改变model的值
2913
- function updateVModel() {
3033
+
3034
+ var updateVModel = function () {
2914
3035
  if (composing)//处理中文输入法在minlengh下引发的BUG
2915
3036
  return
2916
3037
  var val = element.oldValue = element.value //防止递归调用形成死循环
@@ -2919,35 +3040,35 @@ duplexBinding.INPUT = function(element, evaluator, data) {
2919
3040
  evaluator(lastValue)
2920
3041
  callback.call(element, lastValue)
2921
3042
  if ($elem.data("duplex-focus")) {
2922
- avalon.nextTick(function() {
3043
+ avalon.nextTick(function () {
2923
3044
  element.focus()
2924
3045
  })
2925
3046
  }
2926
3047
  }
2927
3048
  }
2928
3049
  //当model变化时,它就会改变value的值
2929
- data.handler = function() {
3050
+ data.handler = function () {
2930
3051
  var val = data.pipe(evaluator(), data, "set") + ""
2931
3052
  if (val !== element.oldValue) {
2932
3053
  element.value = val
2933
3054
  }
2934
3055
  }
2935
- if (data.isChecked || element.type === "radio") {
2936
- updateVModel = function() {
3056
+ if (data.isChecked || $type === "radio") {
3057
+ updateVModel = function () {
2937
3058
  if ($elem.data("duplex-observe") !== false) {
2938
3059
  var lastValue = data.pipe(element.value, data, "get")
2939
3060
  evaluator(lastValue)
2940
3061
  callback.call(element, lastValue)
2941
3062
  }
2942
3063
  }
2943
- data.handler = function() {
3064
+ data.handler = function () {
2944
3065
  var val = evaluator()
2945
3066
  var checked = data.isChecked ? !!val : val + "" === element.value
2946
3067
  element.checked = element.oldValue = checked
2947
3068
  }
2948
3069
  bound("click", updateVModel)
2949
- } else if (type === "checkbox") {
2950
- updateVModel = function() {
3070
+ } else if ($type === "checkbox") {
3071
+ updateVModel = function () {
2951
3072
  if ($elem.data("duplex-observe") !== false) {
2952
3073
  var method = element.checked ? "ensure" : "remove"
2953
3074
  var array = evaluator()
@@ -2959,17 +3080,17 @@ duplexBinding.INPUT = function(element, evaluator, data) {
2959
3080
  callback.call(element, array)
2960
3081
  }
2961
3082
  }
2962
- data.handler = function() {
3083
+ data.handler = function () {
2963
3084
  var array = [].concat(evaluator()) //强制转换为数组
2964
3085
  element.checked = array.indexOf(data.pipe(element.value, data, "get")) > -1
2965
3086
  }
2966
3087
  bound("change", updateVModel)
2967
3088
  } else {
2968
- var events = element.getAttribute("data-duplex-event") || element.getAttribute("data-event") || "input"
3089
+ var events = element.getAttribute("data-duplex-event") || "input"
2969
3090
  if (element.attributes["data-event"]) {
2970
3091
  log("data-event指令已经废弃,请改用data-duplex-event")
2971
3092
  }
2972
- events.replace(rword, function(name) {
3093
+ events.replace(rword, function (name) {
2973
3094
  switch (name) {
2974
3095
  case "input":
2975
3096
  bound("input", updateVModel)
@@ -2984,21 +3105,27 @@ duplexBinding.INPUT = function(element, evaluator, data) {
2984
3105
  break
2985
3106
  }
2986
3107
  })
2987
- }
2988
-
2989
- if (/text|password/.test(element.type)) {
2990
- watchValueInTimer(function() {
2991
- if (root.contains(element)) {
2992
- if (element.value !== element.oldValue) {
2993
- updateVModel()
2994
- }
2995
- } else if (!element.msRetain) {
2996
- return false
2997
- }
3108
+ bound("focus", function () {
3109
+ element.msFocus = true
3110
+ })
3111
+ bound("blur", function () {
3112
+ element.msFocus = false
2998
3113
  })
3114
+ if (rmsinput.test($type)) {
3115
+ watchValueInTimer(function () {
3116
+ if (root.contains(element)) {
3117
+ if (!element.msFocus && element.oldValue !== element.value) {
3118
+ updateVModel()
3119
+ }
3120
+ } else if (!element.msRetain) {
3121
+ return false
3122
+ }
3123
+ })
3124
+ }
3125
+
3126
+ element.avalonSetter = updateVModel
2999
3127
  }
3000
3128
 
3001
- element.avalonSetter = updateVModel
3002
3129
  element.oldValue = element.value
3003
3130
  registerSubscriber(data)
3004
3131
  callback.call(element, element.value)
@@ -3188,11 +3315,10 @@ bindingHandlers.repeat = function(data, vmodels) {
3188
3315
  var xtype = avalon.type($repeat)
3189
3316
  if (xtype !== "object" && xtype !== "array") {
3190
3317
  freturn = true
3191
- avalon.log("warning:" + data.value + "对应类型不正确")
3318
+ avalon.log("warning:" + data.value + "只能是对象或数组")
3192
3319
  }
3193
3320
  } catch (e) {
3194
3321
  freturn = true
3195
- avalon.log("warning:" + data.value + "编译出错")
3196
3322
  }
3197
3323
 
3198
3324
  var arr = data.value.split(".") || []
@@ -3534,7 +3660,7 @@ function recycleProxies(proxies, type) {
3534
3660
  proxy.$events[i].forEach(function(data) {
3535
3661
  if (typeof data === "object")
3536
3662
  disposeData(data)
3537
- })
3663
+ })// jshint ignore:line
3538
3664
  proxy.$events[i].length = 0
3539
3665
  }
3540
3666
  }
@@ -3693,18 +3819,18 @@ var rsanitize = {
3693
3819
  var rsurrogate = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g
3694
3820
  var rnoalphanumeric = /([^\#-~| |!])/g;
3695
3821
 
3696
- function numberFormat(number, decimals, dec_point, thousands_sep) {
3822
+ function numberFormat(number, decimals, point, thousands) {
3697
3823
  //form http://phpjs.org/functions/number_format/
3698
3824
  //number 必需,要格式化的数字
3699
3825
  //decimals 可选,规定多少个小数位。
3700
- //dec_point 可选,规定用作小数点的字符串(默认为 . )。
3701
- //thousands_sep 可选,规定用作千位分隔符的字符串(默认为 , ),如果设置了该参数,那么所有其他参数都是必需的。
3826
+ //point 可选,规定用作小数点的字符串(默认为 . )。
3827
+ //thousands 可选,规定用作千位分隔符的字符串(默认为 , ),如果设置了该参数,那么所有其他参数都是必需的。
3702
3828
  number = (number + '')
3703
3829
  .replace(/[^0-9+\-Ee.]/g, '')
3704
3830
  var n = !isFinite(+number) ? 0 : +number,
3705
- prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
3706
- sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
3707
- dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
3831
+ prec = !isFinite(+decimals) ? 3 : Math.abs(decimals),
3832
+ sep = thousands || ",",
3833
+ dec = point || ".",
3708
3834
  s = '',
3709
3835
  toFixedFix = function(n, prec) {
3710
3836
  var k = Math.pow(10, prec)
@@ -3766,7 +3892,7 @@ var filters = avalon.filters = {
3766
3892
  if (reg) {
3767
3893
  a = a.replace(reg, function(s, name, value) {
3768
3894
  var quote = value.charAt(0)
3769
- return name + "=" + quote + "javascript:void(0)" + quote
3895
+ return name + "=" + quote + "javascript:void(0)" + quote// jshint ignore:line
3770
3896
  })
3771
3897
  }
3772
3898
  }
@@ -3791,9 +3917,7 @@ var filters = avalon.filters = {
3791
3917
  currency: function(amount, symbol, fractionSize) {
3792
3918
  return (symbol || "\uFFE5") + numberFormat(amount, isFinite(fractionSize) ? fractionSize : 2)
3793
3919
  },
3794
- number: function(number, fractionSize) {
3795
- return numberFormat(number, isFinite(fractionSize) ? fractionSize : 3)
3796
- }
3920
+ number: numberFormat
3797
3921
  }
3798
3922
  /*
3799
3923
  'yyyy': 4 digit representation of year (e.g. AD 1 => 0001, AD 2010 => 2010)
@@ -3828,7 +3952,7 @@ var filters = avalon.filters = {
3828
3952
  'mediumTime': equivalent to 'h:mm:ss a' for en_US locale (e.g. 12:05:08 pm)
3829
3953
  'shortTime': equivalent to 'h:mm a' for en_US locale (e.g. 12:05 pm)
3830
3954
  */
3831
- new function() {
3955
+ new function() {// jshint ignore:line
3832
3956
  function toInt(str) {
3833
3957
  return parseInt(str, 10) || 0
3834
3958
  }
@@ -4028,11 +4152,12 @@ new function() {
4028
4152
  }
4029
4153
  locate.SHORTMONTH = locate.MONTH
4030
4154
  filters.date.locate = locate
4031
- }
4155
+ }// jshint ignore:line
4032
4156
  /*********************************************************************
4033
4157
  * AMD加载器 *
4034
4158
  **********************************************************************/
4035
- //去掉getFullUrl 精简loadJS text!插件 baseUrl插件
4159
+ //https://www.devbridge.com/articles/understanding-amd-requirejs/
4160
+ //http://maxogden.com/nested-dependencies.html
4036
4161
  var modules = avalon.modules = {
4037
4162
  "domReady!": {
4038
4163
  exports: avalon,
@@ -4051,15 +4176,14 @@ var modules = avalon.modules = {
4051
4176
  // 4(execute) 其依赖也执行完毕, 值放到exports对象上,在这个阶段fireFactory方法会执行
4052
4177
  modules.exports = modules.avalon
4053
4178
 
4054
- new function() {
4179
+ new function () {// jshint ignore:line
4055
4180
  var loadings = [] //正在加载中的模块列表
4056
4181
  var factorys = [] //放置define方法的factory函数
4057
4182
  var rjsext = /\.js$/i
4058
- var name2url = {}
4059
4183
  function makeRequest(name, config) {
4060
- //1. 去掉资源前缀
4184
+ //1. 去掉资源前缀
4061
4185
  var res = "js"
4062
- name = name.replace(/^(\w+)\!/, function(a, b) {
4186
+ name = name.replace(/^(\w+)\!/, function (a, b) {
4063
4187
  res = b
4064
4188
  return ""
4065
4189
  })
@@ -4067,16 +4191,16 @@ new function() {
4067
4191
  log("debug: ready!已经被废弃,请使用domReady!")
4068
4192
  res = "domReady"
4069
4193
  }
4070
- //2. 去掉querystring, hash
4194
+ //2. 去掉querystring, hash
4071
4195
  var query = ""
4072
- name = name.replace(rquery, function(a) {
4196
+ name = name.replace(rquery, function (a) {
4073
4197
  query = a
4074
4198
  return ""
4075
4199
  })
4076
4200
  //3. 去掉扩展名
4077
4201
  var suffix = "." + res
4078
4202
  var ext = /js|css/.test(suffix) ? suffix : ""
4079
- name = name.replace(/\.[a-z0-9]+$/g, function(a) {
4203
+ name = name.replace(/\.[a-z0-9]+$/g, function (a) {
4080
4204
  if (a === suffix) {
4081
4205
  ext = a
4082
4206
  return ""
@@ -4101,8 +4225,7 @@ new function() {
4101
4225
  //1. 如果该模块已经发出请求,直接返回
4102
4226
  var module = modules[name]
4103
4227
  var urlNoQuery = name && req.urlNoQuery
4104
-
4105
- if (module && module.state >= 3) {
4228
+ if (module && module.state >= 1) {
4106
4229
  return name
4107
4230
  }
4108
4231
  module = modules[urlNoQuery]
@@ -4115,9 +4238,9 @@ new function() {
4115
4238
  id: urlNoQuery,
4116
4239
  state: 1 //send
4117
4240
  }
4118
- var wrap = function(obj) {
4241
+ var wrap = function (obj) {
4119
4242
  resources[res] = obj
4120
- obj.load(name, req, function(a) {
4243
+ obj.load(name, req, function (a) {
4121
4244
  if (arguments.length && a !== void 0) {
4122
4245
  module.exports = a
4123
4246
  }
@@ -4135,12 +4258,10 @@ new function() {
4135
4258
  return name ? urlNoQuery : res + "!"
4136
4259
  }
4137
4260
 
4138
-
4139
4261
  //核心API之一 require
4140
-
4141
4262
  var requireQueue = []
4142
4263
  var isUserFirstRequire = false
4143
- innerRequire = avalon.require = function(array, factory, parentUrl, defineConfig) {
4264
+ innerRequire = avalon.require = function (array, factory, parentUrl, defineConfig) {
4144
4265
  if (!isUserFirstRequire) {
4145
4266
  requireQueue.push(avalon.slice(arguments))
4146
4267
  if (arguments.length <= 2) {
@@ -4157,9 +4278,9 @@ new function() {
4157
4278
  avalon.error("require方法的第一个参数应为数组 " + array)
4158
4279
  }
4159
4280
  var deps = [] // 放置所有依赖项的完整路径
4160
- var uniq = {}
4161
- var id = parentUrl || "callback" + setTimeout("1")
4162
- defineConfig = defineConfig || {}
4281
+ var uniq = createMap()
4282
+ var id = parentUrl || "callback" + setTimeout("1")// jshint ignore:line
4283
+ defineConfig = defineConfig || createMap()
4163
4284
  defineConfig.baseUrl = kernel.baseUrl
4164
4285
  var isBuilt = !!defineConfig.built
4165
4286
  if (parentUrl) {
@@ -4167,10 +4288,10 @@ new function() {
4167
4288
  defineConfig.mapUrl = parentUrl.replace(rjsext, "")
4168
4289
  }
4169
4290
  if (isBuilt) {
4170
- var req = makeRequest(defineConfig.name, defineConfig)
4291
+ var req = makeRequest(defineConfig.defineName, defineConfig)
4171
4292
  id = req.urlNoQuery
4172
4293
  } else {
4173
- array.forEach(function(name) {
4294
+ array.forEach(function (name) {
4174
4295
  var req = makeRequest(name, defineConfig)
4175
4296
  var url = fireRequest(req) //加载资源,并返回该资源的完整地址
4176
4297
  if (url) {
@@ -4199,7 +4320,7 @@ new function() {
4199
4320
  }
4200
4321
 
4201
4322
  //核心API之二 require
4202
- innerRequire.define = function(name, deps, factory) { //模块名,依赖列表,模块本身
4323
+ innerRequire.define = function (name, deps, factory) { //模块名,依赖列表,模块本身
4203
4324
  if (typeof name !== "string") {
4204
4325
  factory = deps
4205
4326
  deps = name
@@ -4211,10 +4332,10 @@ new function() {
4211
4332
  }
4212
4333
  var config = {
4213
4334
  built: !isUserFirstRequire, //用r.js打包后,所有define会放到requirejs之前
4214
- name: name
4335
+ defineName: name
4215
4336
  }
4216
4337
  var args = [deps, factory, config]
4217
- factory.require = function(url) {
4338
+ factory.require = function (url) {
4218
4339
  args.splice(2, 0, url)
4219
4340
  if (modules[url]) {
4220
4341
  modules[url].state = 3 //loaded
@@ -4230,13 +4351,13 @@ new function() {
4230
4351
  delete factory.require //释放内存
4231
4352
  innerRequire.apply(null, args) //0,1,2 --> 1,2,0
4232
4353
  }
4233
- //根据标准,所有遵循W3C标准的浏览器,script标签会按标签的出现顺序执行。
4234
- //老的浏览器中,加载也是按顺序的:一个文件下载完成后,才开始下载下一个文件。
4235
- //较新的浏览器中(IE8+ 、FireFox3.5+ 、Chrome4+ 、Safari4+),为了减小请求时间以优化体验,
4236
- //下载可以是并行的,但是执行顺序还是按照标签出现的顺序。
4237
- //但如果script标签是动态插入的, 就未必按照先请求先执行的原则了,目测只有firefox遵守
4238
- //唯一比较一致的是,IE10+及其他标准浏览器,一旦开始解析脚本, 就会一直堵在那里,直接脚本解析完毕
4239
- //亦即,先进入loading阶段的script标签(模块)必然会先进入loaded阶段
4354
+ //根据标准,所有遵循W3C标准的浏览器,script标签会按标签的出现顺序执行。
4355
+ //老的浏览器中,加载也是按顺序的:一个文件下载完成后,才开始下载下一个文件。
4356
+ //较新的浏览器中(IE8+ 、FireFox3.5+ 、Chrome4+ 、Safari4+),为了减小请求时间以优化体验,
4357
+ //下载可以是并行的,但是执行顺序还是按照标签出现的顺序。
4358
+ //但如果script标签是动态插入的, 就未必按照先请求先执行的原则了,目测只有firefox遵守
4359
+ //唯一比较一致的是,IE10+及其他标准浏览器,一旦开始解析脚本, 就会一直堵在那里,直接脚本解析完毕
4360
+ //亦即,先进入loading阶段的script标签(模块)必然会先进入loaded阶段
4240
4361
  var url = config.built ? "unknown" : getCurrentScript()
4241
4362
  if (url) {
4242
4363
  var module = modules[url]
@@ -4250,30 +4371,30 @@ new function() {
4250
4371
  }
4251
4372
  //核心API之三 require.config(settings)
4252
4373
  innerRequire.config = kernel
4253
- //核心API之四 define.amd 标识其符合AMD规范
4374
+ //核心API之四 define.amd 标识其符合AMD规范
4254
4375
  innerRequire.define.amd = modules
4255
4376
 
4256
- //==========================对用户配置项进行再加工==========================
4257
- var allpaths = kernel["orig.paths"] = {}
4258
- var allmaps = kernel["orig.map"] = {}
4377
+ //==========================对用户配置项进行再加工==========================
4378
+ var allpaths = kernel["orig.paths"] = createMap()
4379
+ var allmaps = kernel["orig.map"] = createMap()
4259
4380
  var allpackages = kernel["packages"] = []
4260
- var allargs = kernel["orig.args"] = {}
4381
+ var allargs = kernel["orig.args"] = createMap()
4261
4382
  avalon.mix(plugins, {
4262
- paths: function(hash) {
4383
+ paths: function (hash) {
4263
4384
  avalon.mix(allpaths, hash)
4264
4385
  kernel.paths = makeIndexArray(allpaths)
4265
4386
  },
4266
- map: function(hash) {
4387
+ map: function (hash) {
4267
4388
  avalon.mix(allmaps, hash)
4268
4389
  var list = makeIndexArray(allmaps, 1, 1)
4269
- avalon.each(list, function(_, item) {
4390
+ avalon.each(list, function (_, item) {
4270
4391
  item.val = makeIndexArray(item.val)
4271
4392
  })
4272
4393
  kernel.map = list
4273
4394
  },
4274
- packages: function(array) {
4395
+ packages: function (array) {
4275
4396
  array = array.concat(allpackages)
4276
- var uniq = {}
4397
+ var uniq = createMap()
4277
4398
  var ret = []
4278
4399
  for (var i = 0, pkg; pkg = array[i++]; ) {
4279
4400
  pkg = typeof pkg === "string" ? {name: pkg} : pkg
@@ -4288,16 +4409,16 @@ new function() {
4288
4409
  }
4289
4410
  kernel.packages = ret.sort()
4290
4411
  },
4291
- urlArgs: function(hash) {
4412
+ urlArgs: function (hash) {
4292
4413
  if (typeof hash === "string") {
4293
4414
  hash = {"*": hash}
4294
4415
  }
4295
4416
  avalon.mix(allargs, hash)
4296
4417
  kernel.urlArgs = makeIndexArray(allargs, 1)
4297
4418
  },
4298
- baseUrl: function(url) {
4419
+ baseUrl: function (url) {
4299
4420
  if (!isAbsUrl(url)) {
4300
- var baseElement = head.querySelector("base")
4421
+ var baseElement = head.getElementsByTagName("base")[0]
4301
4422
  if (baseElement) {
4302
4423
  head.removeChild(baseElement)
4303
4424
  }
@@ -4311,7 +4432,7 @@ new function() {
4311
4432
  if (url.length > 3)
4312
4433
  kernel.baseUrl = url
4313
4434
  },
4314
- shim: function(obj) {
4435
+ shim: function (obj) {
4315
4436
  for (var i in obj) {
4316
4437
  var value = obj[i]
4317
4438
  if (Array.isArray(value)) {
@@ -4329,7 +4450,7 @@ new function() {
4329
4450
  })
4330
4451
 
4331
4452
 
4332
- //==============================内部方法=================================
4453
+ //==============================内部方法=================================
4333
4454
  function checkCycle(deps, nick) {
4334
4455
  //检测是否存在循环依赖
4335
4456
  for (var i = 0, id; id = deps[i++]; ) {
@@ -4344,7 +4465,7 @@ new function() {
4344
4465
  var id = trimQuery(node.src) //检测是否死链
4345
4466
  node.onload = node.onerror = null
4346
4467
  if (onError) {
4347
- setTimeout(function() {
4468
+ setTimeout(function () {
4348
4469
  head.removeChild(node)
4349
4470
  node = null // 处理旧式IE下的循环引用问题
4350
4471
  })
@@ -4362,10 +4483,6 @@ new function() {
4362
4483
  if (!deps)
4363
4484
  continue
4364
4485
  for (var j = 0, key; key = deps[j]; j++) {
4365
- var k = name2url[key]
4366
- if (k) {
4367
- key = deps[j] = k
4368
- }
4369
4486
  if (Object(modules[key]).state !== 4) {
4370
4487
  continue loop
4371
4488
  }
@@ -4378,11 +4495,12 @@ new function() {
4378
4495
  }
4379
4496
  }
4380
4497
  }
4498
+
4381
4499
  function loadJS(url, id, callback) {
4382
4500
  //通过script节点加载目标模块
4383
4501
  var node = DOC.createElement("script")
4384
4502
  node.className = subscribers //让getCurrentScript只处理类名为subscribers的script节点
4385
- node.onload = function() {
4503
+ node.onload = function () {
4386
4504
  var factory = factorys.pop()
4387
4505
  factory && factory.require(id)
4388
4506
  if (callback) {
@@ -4392,11 +4510,11 @@ new function() {
4392
4510
  id && loadings.push(id)
4393
4511
  checkDeps()
4394
4512
  }
4395
- node.onerror = function() {
4513
+ node.onerror = function () {
4396
4514
  checkFail(node, true)
4397
4515
  }
4398
4516
 
4399
- head.appendChild(node) //chrome下第二个参数不能为null
4517
+ head.insertBefore(node, head.firstChild) //chrome下第二个参数不能为null
4400
4518
  node.src = url //插入到head的第一个节点前,防止IE6下head标签没闭合前使用appendChild抛错
4401
4519
  log("debug: 正准备加载 " + url) //更重要的是IE6下可以收窄getCurrentScript的寻找范围
4402
4520
  }
@@ -4407,14 +4525,14 @@ new function() {
4407
4525
  load: noop
4408
4526
  },
4409
4527
  js: {
4410
- load: function(name, req, onLoad) {
4528
+ load: function (name, req, onLoad) {
4411
4529
  var url = req.url
4412
4530
  var id = req.urlNoQuery
4413
4531
  var shim = kernel.shim[name.replace(rjsext, "")]
4414
4532
  if (shim) { //shim机制
4415
- innerRequire(shim.deps || [], function() {
4533
+ innerRequire(shim.deps || [], function () {
4416
4534
  var args = avalon.slice(arguments)
4417
- loadJS(url, id, function() {
4535
+ loadJS(url, id, function () {
4418
4536
  onLoad(shim.exportsFn ? shim.exportsFn.apply(0, args) : void 0)
4419
4537
  })
4420
4538
  })
@@ -4424,25 +4542,23 @@ new function() {
4424
4542
  }
4425
4543
  },
4426
4544
  css: {
4427
- load: function(name, req, onLoad) {
4545
+ load: function (name, req, onLoad) {
4428
4546
  var url = req.url
4429
- var node = DOC.createElement("link")
4430
- node.rel = "stylesheet"
4431
- node.href = url
4432
- head.appendChild(node)
4547
+ head.insertAdjacentHTML("afterBegin", '<link rel="stylesheet" href="' + url + '">')
4433
4548
  log("debug: 已成功加载 " + url)
4434
4549
  onLoad()
4435
4550
  }
4436
4551
  },
4437
4552
  text: {
4438
- load: function(name, req, onLoad) {
4553
+ load: function (name, req, onLoad) {
4439
4554
  var url = req.url
4440
4555
  var xhr = getXHR()
4441
- xhr.onload = function() {
4556
+ xhr.onload = function () {
4442
4557
  var status = xhr.status;
4443
4558
  if (status > 399 && status < 600) {
4444
4559
  avalon.error(url + " 对应资源不存在或没有开启 CORS")
4445
4560
  } else {
4561
+ log("debug: 已成功加载 " + url)
4446
4562
  onLoad(xhr.responseText)
4447
4563
  }
4448
4564
  }
@@ -4468,6 +4584,7 @@ new function() {
4468
4584
  return /^(?:[a-z]+:)?\/\//i.test(String(path))
4469
4585
  }
4470
4586
 
4587
+
4471
4588
  function getCurrentScript() {
4472
4589
  // inspireb by https://github.com/samyk/jiagra/blob/master/jiagra.js
4473
4590
  var stack
@@ -4501,23 +4618,29 @@ new function() {
4501
4618
  }
4502
4619
  }
4503
4620
 
4504
-
4621
+ var rcallback = /^callback\d+$/
4505
4622
  function fireFactory(id, deps, factory) {
4506
4623
  var module = Object(modules[id])
4507
4624
  module.state = 4
4508
4625
  for (var i = 0, array = [], d; d = deps[i++]; ) {
4509
- d = name2url[d] || d
4510
4626
  if (d === "exports") {
4511
- var obj = module.exports || (module.exports = {})
4627
+ var obj = module.exports || (module.exports = createMap())
4512
4628
  array.push(obj)
4513
4629
  } else {
4514
4630
  array.push(modules[d].exports)
4515
4631
  }
4516
4632
  }
4517
- var ret = factory.apply(window, array)
4633
+ try {
4634
+ var ret = factory.apply(window, array)
4635
+ } catch (e) {
4636
+ log("执行[" + id + "]模块的factory抛错: " + e)
4637
+ }
4518
4638
  if (ret !== void 0) {
4519
4639
  module.exports = ret
4520
4640
  }
4641
+ if (rcallback.test(id)) {
4642
+ delete modules[id]
4643
+ }
4521
4644
  delete module.factory
4522
4645
  return ret
4523
4646
  }
@@ -4530,20 +4653,20 @@ new function() {
4530
4653
  var usePath = 0
4531
4654
  var baseUrl = this.baseUrl
4532
4655
  var rootUrl = this.parentUrl || baseUrl
4533
- eachIndexArray(id, kernel.paths, function(value, key) {
4656
+ eachIndexArray(id, kernel.paths, function (value, key) {
4534
4657
  url = url.replace(key, value)
4535
4658
  usePath = 1
4536
4659
  })
4537
4660
  //2. 是否命中packages配置项
4538
4661
  if (!usePath) {
4539
- eachIndexArray(id, kernel.packages, function(value, key, item) {
4662
+ eachIndexArray(id, kernel.packages, function (value, key, item) {
4540
4663
  url = url.replace(item.name, item.location)
4541
4664
  })
4542
4665
  }
4543
4666
  //3. 是否命中map配置项
4544
4667
  if (this.mapUrl) {
4545
- eachIndexArray(this.mapUrl, kernel.map, function(array) {
4546
- eachIndexArray(url, array, function(mdValue, mdKey) {
4668
+ eachIndexArray(this.mapUrl, kernel.map, function (array) {
4669
+ eachIndexArray(url, array, function (mdValue, mdKey) {
4547
4670
  url = url.replace(mdKey, mdValue)
4548
4671
  rootUrl = baseUrl
4549
4672
  })
@@ -4562,7 +4685,7 @@ new function() {
4562
4685
  var urlNoQuery = url + ext
4563
4686
  url = urlNoQuery + this.query
4564
4687
  //6. 处理urlArgs
4565
- eachIndexArray(id, kernel.urlArgs, function(value) {
4688
+ eachIndexArray(id, kernel.urlArgs, function (value) {
4566
4689
  url += (url.indexOf("?") === -1 ? "?" : "&") + value;
4567
4690
  })
4568
4691
  this.url = url
@@ -4581,7 +4704,7 @@ new function() {
4581
4704
  }
4582
4705
 
4583
4706
  function makeExports(value) {
4584
- return function() {
4707
+ return function () {
4585
4708
  var ret
4586
4709
  if (value.init) {
4587
4710
  ret = value.init.apply(window, arguments)
@@ -4594,17 +4717,17 @@ new function() {
4594
4717
  function hash2array(hash, useStar, part) {
4595
4718
  var array = [];
4596
4719
  for (var key in hash) {
4597
- if (hash.hasOwnProperty(key)) {
4598
- var item = {
4599
- name: key,
4600
- val: hash[key]
4601
- }
4602
- array.push(item)
4603
- item.reg = key === "*" && useStar ? /^/ : makeMatcher(key)
4604
- if (part && key !== "*") {
4605
- item.reg = new RegExp('\/' + key.replace(/^\//, "") + '(/|$)')
4606
- }
4720
+ // if (hash.hasOwnProperty(key)) {//hash是由createMap创建没有hasOwnProperty
4721
+ var item = {
4722
+ name: key,
4723
+ val: hash[key]
4607
4724
  }
4725
+ array.push(item)
4726
+ item.reg = key === "*" && useStar ? /^/ : makeMatcher(key)
4727
+ if (part && key !== "*") {
4728
+ item.reg = new RegExp('\/' + key.replace(/^\//, "") + '(/|$)')
4729
+ }
4730
+ // }
4608
4731
  }
4609
4732
  return array
4610
4733
  }
@@ -4618,7 +4741,7 @@ new function() {
4618
4741
  }
4619
4742
  }
4620
4743
  }
4621
- // 根据元素的name项进行数组字符数逆序的排序函数
4744
+ // 根据元素的name项进行数组字符数逆序的排序函数
4622
4745
  function descSorterByName(a, b) {
4623
4746
  var aaa = a.name
4624
4747
  var bbb = b.name
@@ -4657,7 +4780,7 @@ new function() {
4657
4780
  return value
4658
4781
  }
4659
4782
  var g = window
4660
- value.split(".").forEach(function(part) {
4783
+ value.split(".").forEach(function (part) {
4661
4784
  g = g[part]
4662
4785
  })
4663
4786
  return g
@@ -4674,40 +4797,41 @@ new function() {
4674
4797
  var loaderUrl = trimQuery(mainNode.src)
4675
4798
  kernel.baseUrl = loaderUrl.slice(0, loaderUrl.lastIndexOf("/") + 1)
4676
4799
  }
4677
- }
4800
+ }// jshint ignore:line
4678
4801
 
4679
4802
  /*********************************************************************
4680
4803
  * DOMReady *
4681
4804
  **********************************************************************/
4682
- var readyList = []
4683
- function fireReady() {
4805
+ var readyList = [], isReady
4806
+ var fireReady = function (fn) {
4807
+ isReady = true
4684
4808
  if (innerRequire) {
4685
4809
  modules["domReady!"].state = 4
4686
- innerRequire.checkDeps()//隋性函数,防止IE9二次调用_checkDeps
4810
+ innerRequire.checkDeps()
4811
+ }
4812
+ while (fn = readyList.shift()) {
4813
+ fn(avalon)
4687
4814
  }
4688
- readyList.forEach(function(a) {
4689
- a(avalon)
4690
- })
4691
- fireReady = noop //隋性函数,防止IE9二次调用_checkDeps
4692
4815
  }
4693
4816
 
4817
+
4694
4818
  if (DOC.readyState === "complete") {
4695
4819
  setTimeout(fireReady) //如果在domReady之外加载
4696
4820
  } else {
4697
4821
  DOC.addEventListener("DOMContentLoaded", fireReady)
4698
- window.addEventListener("load", fireReady)
4699
4822
  }
4700
- avalon.ready = function(fn) {
4701
- if (fireReady === noop) {
4702
- fn(avalon)
4703
- } else {
4823
+ window.addEventListener("load", fireReady)
4824
+ avalon.ready = function (fn) {
4825
+ if (!isReady) {
4704
4826
  readyList.push(fn)
4827
+ } else {
4828
+ fn(avalon)
4705
4829
  }
4706
4830
  }
4707
4831
  avalon.config({
4708
4832
  loader: true
4709
4833
  })
4710
- avalon.ready(function() {
4834
+ avalon.ready(function () {
4711
4835
  avalon.scan(DOC.body)
4712
4836
  })
4713
4837
 
@@ -4732,7 +4856,7 @@ avalon.ready(function() {
4732
4856
  var _avalon = window.avalon
4733
4857
  avalon.noConflict = function(deep) {
4734
4858
  if (deep && window.avalon === avalon) {
4735
- window.avalon = avalon
4859
+ window.avalon = _avalon
4736
4860
  }
4737
4861
  return avalon
4738
4862
  }