avalon-rails 1.4.1.1.20150404164109 → 1.4.6.0.20150915133100

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.41 built in 2015.4.4
8
+ avalon.modern.js 1.46 built in 2015.9.11
9
9
  support IE10+ and other browsers
10
10
  ==================================================*/
11
11
  (function(global, factory) {
@@ -76,10 +76,10 @@ var aslice = ap.slice
76
76
  var Registry = {} //将函数曝光到此对象上,方便访问器收集依赖
77
77
  var W3C = window.dispatchEvent
78
78
  var root = DOC.documentElement
79
- var hyperspace = DOC.createDocumentFragment()
79
+ var avalonFragment = DOC.createDocumentFragment()
80
80
  var cinerator = DOC.createElement("div")
81
81
  var class2type = {}
82
- "Boolean Number String Function Array Date RegExp Object Error".replace(rword, function(name) {
82
+ "Boolean Number String Function Array Date RegExp Object Error".replace(rword, function (name) {
83
83
  class2type["[object " + name + "]"] = name.toLowerCase()
84
84
  })
85
85
 
@@ -101,30 +101,35 @@ function oneObject(array, val) {
101
101
  }
102
102
 
103
103
  //生成UUID http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
104
- var generateID = function(prefix) {
104
+ var generateID = function (prefix) {
105
105
  prefix = prefix || "avalon"
106
- return (prefix + Math.random() + Math.random()).replace(/0\./g, "")
106
+ return String(Math.random() + Math.random()).replace(/\d\.\d{4}/, prefix)
107
107
  }
108
108
  function IE() {
109
109
  if (window.VBArray) {
110
110
  var mode = document.documentMode
111
111
  return mode ? mode : window.XMLHttpRequest ? 7 : 6
112
112
  } else {
113
- return 0
113
+ return NaN
114
114
  }
115
115
  }
116
116
  var IEVersion = IE()
117
117
 
118
- avalon = function(el) { //创建jQuery式的无new 实例化结构
118
+ avalon = function (el) { //创建jQuery式的无new 实例化结构
119
119
  return new avalon.init(el)
120
120
  }
121
121
 
122
+ avalon.profile = function () {
123
+ if (window.console && avalon.config.profile) {
124
+ Function.apply.call(console.log, console, arguments)
125
+ }
126
+ }
127
+
122
128
  /*视浏览器情况采用最快的异步回调*/
123
- avalon.nextTick = new function() {// jshint ignore:line
129
+ avalon.nextTick = new function () {// jshint ignore:line
124
130
  var tickImmediate = window.setImmediate
125
131
  var tickObserver = window.MutationObserver
126
- var tickPost = W3C && window.postMessage
127
- if (tickImmediate) {
132
+ if (tickImmediate) {//IE10 \11 edage
128
133
  return tickImmediate.bind(window)
129
134
  }
130
135
 
@@ -137,32 +142,32 @@ avalon.nextTick = new function() {// jshint ignore:line
137
142
  queue = queue.slice(n)
138
143
  }
139
144
 
140
- if (tickObserver) {
145
+ if (tickObserver) {// 支持MutationObserver
141
146
  var node = document.createTextNode("avalon")
142
147
  new tickObserver(callback).observe(node, {characterData: true})// jshint ignore:line
143
- return function(fn) {
148
+ return function (fn) {
144
149
  queue.push(fn)
145
150
  node.data = Math.random()
146
151
  }
147
152
  }
148
153
 
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) {
154
+ if (window.VBArray) {
155
+ return function (fn) {
159
156
  queue.push(fn)
160
- window.postMessage('process-tick', '*')
157
+ var node = DOC.createElement("script")
158
+ node.onreadystatechange = function () {
159
+ callback() //在interactive阶段就触发
160
+ node.onreadystatechange = null
161
+ head.removeChild(node)
162
+ node = null
163
+ }
164
+ head.appendChild(node)
161
165
  }
162
166
  }
163
167
 
164
- return function(fn) {
165
- setTimeout(fn, 0)
168
+
169
+ return function (fn) {
170
+ setTimeout(fn, 4)
166
171
  }
167
172
  }// jshint ignore:line
168
173
  /*********************************************************************
@@ -263,7 +268,7 @@ function _number(a, len) { //用于模拟slice, splice的效果
263
268
  avalon.mix({
264
269
  rword: rword,
265
270
  subscribers: subscribers,
266
- version: 1.41,
271
+ version: 1.46,
267
272
  ui: {},
268
273
  log: log,
269
274
  slice: function(nodes, start, end) {
@@ -466,6 +471,7 @@ var Cache = new function() {// jshint ignore:line
466
471
  entry.newer =
467
472
  entry.older =
468
473
  this._keymap[entry.key] = void 0
474
+ delete this._keymap[entry.key] //#1029
469
475
  }
470
476
  }
471
477
  p.get = function(key) {
@@ -642,7 +648,6 @@ if (DOC.onmousewheel === void 0) {
642
648
  }
643
649
  }
644
650
  }
645
-
646
651
  /*********************************************************************
647
652
  * 配置系统 *
648
653
  **********************************************************************/
@@ -680,15 +685,15 @@ var plugins = {
680
685
  openTag = array[0]
681
686
  closeTag = array[1]
682
687
  if (openTag === closeTag) {
683
- throw new SyntaxError("openTag!==closeTag")
684
- } else if (array + "" === "<!--,-->") {
685
- kernel.commentInterpolate = true
688
+ throw new SyntaxError("openTag===closeTag")
686
689
  } else {
687
690
  var test = openTag + "test" + closeTag
688
691
  cinerator.innerHTML = test
689
692
  if (cinerator.innerHTML !== test && cinerator.innerHTML.indexOf("&lt;") > -1) {
690
693
  throw new SyntaxError("此定界符不合法")
691
694
  }
695
+ kernel.openTag = openTag
696
+ kernel.closeTag = closeTag
692
697
  cinerator.innerHTML = ""
693
698
  }
694
699
  var o = escapeRegExp(openTag),
@@ -777,8 +782,8 @@ var EventBus = {
777
782
  }
778
783
  //循环两个vmodel中的节点,查找匹配(向上匹配或者向下匹配)的节点并设置标识
779
784
  /* jshint ignore:start */
780
- Array.prototype.forEach.call(eventNodes, function (node) {
781
- Array.prototype.forEach.call(elements, function (element) {
785
+ ap.forEach.call(eventNodes, function (node) {
786
+ ap.forEach.call(elements, function (element) {
782
787
  var ok = special === "down" ? element.contains(node) : //向下捕获
783
788
  node.contains(element) //向上冒泡
784
789
  if (ok) {
@@ -792,7 +797,7 @@ var EventBus = {
792
797
  }
793
798
  var nodes = DOC.getElementsByTagName("*") //实现节点排序
794
799
  var alls = []
795
- Array.prototype.forEach.call(nodes, function (el) {
800
+ ap.forEach.call(nodes, function (el) {
796
801
  if (el._avalon) {
797
802
  alls.push(el._avalon)
798
803
  el._avalon = ""
@@ -821,12 +826,11 @@ var EventBus = {
821
826
  }
822
827
  }
823
828
  }
824
-
825
829
  /*********************************************************************
826
830
  * modelFactory *
827
831
  **********************************************************************/
828
832
  //avalon最核心的方法的两个方法之一(另一个是avalon.scan),返回一个ViewModel(VM)
829
- var VMODELS = avalon.vmodels = createMap() //所有vmodel都储存在这里
833
+ var VMODELS = avalon.vmodels = {} //所有vmodel都储存在这里
830
834
  avalon.define = function (id, factory) {
831
835
  var $id = id.$id || id
832
836
  if (!$id) {
@@ -852,194 +856,271 @@ avalon.define = function (id, factory) {
852
856
  }
853
857
 
854
858
  //一些不需要被监听的属性
855
- var $$skipArray = String("$id,$watch,$unwatch,$fire,$events,$model,$skipArray").match(rword)
856
-
857
- function isObservable(name, value, $skipArray) {
858
- if (isFunction(value) || value && value.nodeType) {
859
- return false
860
- }
861
- if ($skipArray.indexOf(name) !== -1) {
862
- return false
863
- }
864
- if ($$skipArray.indexOf(name) !== -1) {
865
- return false
866
- }
867
- var $special = $skipArray.$special
868
- if (name && name.charAt(0) === "$" && !$special[name]) {
869
- return false
870
- }
871
- return true
872
- }
873
- //ms-with,ms-each, ms-repeat绑定生成的代理对象储存池
874
- var midway = createMap()
875
- function getNewValue(accessor, name, value, $vmodel) {
876
- switch (accessor.type) {
877
- case 0://计算属性
878
- var getter = accessor.get
879
- var setter = accessor.set
880
- if (isFunction(setter)) {
881
- var $events = $vmodel.$events
882
- var lock = $events[name]
883
- $events[name] = [] //清空回调,防止内部冒泡而触发多次$fire
884
- setter.call($vmodel, value)
885
- $events[name] = lock
886
- }
887
- return getter.call($vmodel) //同步$model
888
- case 1://监控属性
889
- return value
890
- case 2://对象属性(包括数组与哈希)
891
- if (value !== $vmodel.$model[name]) {
892
- var svmodel = accessor.svmodel = objectFactory($vmodel, name, value, accessor.valueType)
893
- value = svmodel.$model //同步$model
894
- var fn = midway[svmodel.$id]
895
- fn && fn() //同步视图
896
- }
897
- return value
898
- }
899
- }
859
+ var $$skipArray = String("$id,$watch,$unwatch,$fire,$events,$model,$skipArray,$reinitialize").match(rword)
900
860
 
901
861
  function modelFactory(source, $special, $model) {
902
862
  if (Array.isArray(source)) {
903
863
  var arr = source.concat()
904
864
  source.length = 0
905
- var collection = Collection(source)// jshint ignore:line
865
+ var collection = arrayFactory(source)// jshint ignore:line
906
866
  collection.pushArray(arr)
907
867
  return collection
908
868
  }
909
- //0 null undefined || Node || VModel
869
+ //0 null undefined || Node || VModel(fix IE6-8 createWithProxy $val: val引发的BUG)
910
870
  if (!source || source.nodeType > 0 || (source.$id && source.$events)) {
911
871
  return source
912
872
  }
913
- if (!Array.isArray(source.$skipArray)) {
914
- source.$skipArray = []
915
- }
916
- source.$skipArray.$special = $special || createMap() //强制要监听的属性
873
+ var $skipArray = Array.isArray(source.$skipArray) ? source.$skipArray : []
874
+ $skipArray.$special = $special || createMap() //强制要监听的属性
917
875
  var $vmodel = {} //要返回的对象, 它在IE6-8下可能被偷龙转凤
918
876
  $model = $model || {} //vmodels.$model属性
919
877
  var $events = createMap() //vmodel.$events属性
920
- var watchedProperties = createMap() //监控属性
921
- var initCallbacks = [] //初始化才执行的函数
922
- for (var i in source) {
923
- (function (name, val) {
924
- $model[name] = val
925
- if (!isObservable(name, val, source.$skipArray)) {
926
- return //过滤所有非监控属性
927
- }
878
+ var accessors = createMap() //监控属性
879
+ var computed = []
880
+ $$skipArray.forEach(function (name) {
881
+ delete source[name]
882
+ })
883
+
884
+ var names = Object.keys(source)
885
+ /* jshint ignore:start */
886
+ names.forEach(function (name, accessor) {
887
+ var val = source[name]
888
+ $model[name] = val
889
+ if (isObservable(name, val, $skipArray)) {
928
890
  //总共产生三种accessor
929
891
  $events[name] = []
930
892
  var valueType = avalon.type(val)
931
- var accessor = function (newValue) {
932
- var name = accessor._name
933
- var $vmodel = this
934
- var $model = $vmodel.$model
935
- var oldValue = $model[name]
936
- var $events = $vmodel.$events
937
-
938
- if (arguments.length) {
939
- if (stopRepeatAssign) {
940
- return
941
- }
942
- //计算属性与对象属性需要重新计算newValue
943
- if (accessor.type !== 1) {
944
- newValue = getNewValue(accessor, name, newValue, $vmodel)
945
- if (!accessor.type)
946
- return
947
- }
948
- if (!isEqual(oldValue, newValue)) {
949
- $model[name] = newValue
950
- notifySubscribers($events[name]) //同步视图
951
- safeFire($vmodel, name, newValue, oldValue) //触发$watch回调
952
- }
953
- } else {
954
- if (accessor.type === 0) { //type 0 计算属性 1 监控属性 2 对象属性
955
- //计算属性不需要收集视图刷新函数,都是由其他监控属性代劳
956
- newValue = accessor.get.call($vmodel)
957
- if (oldValue !== newValue) {
958
- $model[name] = newValue
959
- //这里不用同步视图
960
- safeFire($vmodel, name, newValue, oldValue) //触发$watch回调
961
- }
962
- return newValue
963
- } else {
964
- collectSubscribers($events[name]) //收集视图函数
965
- return accessor.svmodel || oldValue
966
- }
967
- }
968
- }
969
893
  //总共产生三种accessor
970
894
  if (valueType === "object" && isFunction(val.get) && Object.keys(val).length <= 2) {
971
- //第1种为计算属性, 因变量,通过其他监控属性触发其改变
972
- accessor.set = val.set
973
- accessor.get = val.get
974
- accessor.type = 0
975
- initCallbacks.push(function () {
976
- var data = {
977
- evaluator: function () {
978
- data.type = Math.random(),
979
- data.element = null
980
- $model[name] = accessor.get.call($vmodel)
981
- },
982
- element: head,
983
- type: Math.random(),
984
- handler: noop,
985
- args: []
986
- }
987
- Registry[expose] = data
988
- accessor.call($vmodel)
989
- delete Registry[expose]
990
- })
895
+ accessor = makeComputedAccessor(name, val)
896
+ computed.push(accessor)
991
897
  } else if (rcomplexType.test(valueType)) {
992
- //第2种为对象属性,产生子VM与监控数组
993
- accessor.type = 2
994
- accessor.valueType = valueType
995
- initCallbacks.push(function () {
996
- var svmodel = modelFactory(val, 0, $model[name])
997
- accessor.svmodel = svmodel
998
- svmodel.$events[subscribers] = $events[name]
999
- })
898
+ // issue #940 解决$model层次依赖丢失 https://github.com/RubyLouvre/avalon/issues/940
899
+ accessor = makeComplexAccessor(name, val, valueType, $events[name], $model)
1000
900
  } else {
1001
- accessor.type = 1
1002
- //第3种为监控属性,对应简单的数据类型,自变量
901
+ accessor = makeSimpleAccessor(name, val)
1003
902
  }
1004
- accessor._name = name
1005
- watchedProperties[name] = accessor
1006
- })(i, source[i])// jshint ignore:line
1007
- }
1008
-
1009
- $$skipArray.forEach(function (name) {
1010
- delete source[name]
1011
- delete $model[name] //这些特殊属性不应该在$model中出现
903
+ accessors[name] = accessor
904
+ }
1012
905
  })
1013
-
1014
- $vmodel = Object.defineProperties($vmodel, descriptorFactory(watchedProperties), source) //生成一个空的ViewModel
1015
- for (var name in source) {
1016
- if (!watchedProperties[name]) {
906
+ /* jshint ignore:end */
907
+ $vmodel = Object.defineProperties($vmodel, descriptorFactory(accessors)) //生成一个空的ViewModel
908
+ for (var i = 0; i < names.length; i++) {
909
+ var name = names[i]
910
+ if (!accessors[name]) {
1017
911
  $vmodel[name] = source[name]
1018
912
  }
1019
913
  }
1020
914
  //添加$id, $model, $events, $watch, $unwatch, $fire
1021
- $vmodel.$id = generateID()
1022
- $vmodel.$model = $model
1023
- $vmodel.$events = $events
1024
- for (i in EventBus) {
1025
- $vmodel[i] = EventBus[i]
915
+ hideProperty($vmodel, "$id", generateID())
916
+ hideProperty($vmodel, "$model", $model)
917
+ hideProperty($vmodel, "$events", $events)
918
+ /* jshint ignore:start */
919
+ hideProperty($vmodel, "hasOwnProperty", function (name) {
920
+ return name in this.$model
921
+ })
922
+ /* jshint ignore:end */
923
+ for (var i in EventBus) {
924
+ hideProperty($vmodel, i, EventBus[i])
1026
925
  }
1027
926
 
1028
- Object.defineProperty($vmodel, "hasOwnProperty", {
1029
- value: function (name) {
1030
- return name in this.$model
1031
- },
1032
- writable: false,
927
+ $vmodel.$reinitialize = function () {
928
+ computed.forEach(function (accessor) {
929
+ delete accessor._value
930
+ delete accessor.oldArgs
931
+ accessor.digest = function () {
932
+ accessor.call($vmodel)
933
+ }
934
+ dependencyDetection.begin({
935
+ callback: function (vm, dependency) {//dependency为一个accessor
936
+ var name = dependency._name
937
+ if (dependency !== accessor) {
938
+ var list = vm.$events[name]
939
+ accessor.vm = $vmodel
940
+ injectDependency(list, accessor.digest)
941
+ }
942
+ }
943
+ })
944
+ try {
945
+ accessor.get.call($vmodel)
946
+ } finally {
947
+ dependencyDetection.end()
948
+ }
949
+ })
950
+ }
951
+ $vmodel.$reinitialize()
952
+ return $vmodel
953
+ }
954
+
955
+ function hideProperty(host, name, value) {
956
+ Object.defineProperty(host, name, {
957
+ value: value,
958
+ writable: true,
1033
959
  enumerable: false,
1034
960
  configurable: true
1035
961
  })
962
+ }
1036
963
 
1037
- initCallbacks.forEach(function (cb) { //收集依赖
1038
- cb()
1039
- })
1040
- return $vmodel
964
+ function keysVM(obj) {
965
+ var arr = Object.keys(obj)
966
+ for (var i = 0; i < $$skipArray.length; i++) {
967
+ var index = arr.indexOf($$skipArray[i])
968
+ if (index !== -1) {
969
+ arr.splice(index, 1)
970
+ }
971
+ }
972
+ return arr
973
+ }
974
+ //创建一个简单访问器
975
+ function makeSimpleAccessor(name, value) {
976
+ function accessor(value) {
977
+ var oldValue = accessor._value
978
+ if (arguments.length > 0) {
979
+ if (!stopRepeatAssign && !isEqual(value, oldValue)) {
980
+ accessor.updateValue(this, value)
981
+ accessor.notify(this, value, oldValue)
982
+ }
983
+ return this
984
+ } else {
985
+ dependencyDetection.collectDependency(this, accessor)
986
+ return oldValue
987
+ }
988
+ }
989
+ accessorFactory(accessor, name)
990
+ accessor._value = value
991
+ return accessor;
992
+ }
993
+
994
+ ///创建一个计算访问器
995
+ function makeComputedAccessor(name, options) {
996
+ function accessor(value) {//计算属性
997
+ var oldValue = accessor._value
998
+ var init = ("_value" in accessor)
999
+ if (arguments.length > 0) {
1000
+ if (stopRepeatAssign) {
1001
+ return this
1002
+ }
1003
+ if (typeof accessor.set === "function") {
1004
+ if (accessor.oldArgs !== value) {
1005
+ accessor.oldArgs = value
1006
+ var $events = this.$events
1007
+ var lock = $events[name]
1008
+ $events[name] = [] //清空回调,防止内部冒泡而触发多次$fire
1009
+ accessor.set.call(this, value)
1010
+ $events[name] = lock
1011
+ value = accessor.get.call(this)
1012
+ if (value !== oldValue) {
1013
+ accessor.updateValue(this, value)
1014
+ accessor.notify(this, value, oldValue) //触发$watch回调
1015
+ }
1016
+ }
1017
+ }
1018
+ return this
1019
+ } else {
1020
+ //将依赖于自己的高层访问器或视图刷新函数(以绑定对象形式)放到自己的订阅数组中
1021
+ //将自己注入到低层访问器的订阅数组中
1022
+ value = accessor.get.call(this)
1023
+ accessor.updateValue(this, value)
1024
+ if (init && oldValue !== value) {
1025
+ accessor.notify(this, value, oldValue) //触发$watch回调
1026
+ }
1027
+ return value
1028
+ }
1029
+ }
1030
+ accessor.set = options.set
1031
+ accessor.get = options.get
1032
+ accessorFactory(accessor, name)
1033
+ return accessor
1041
1034
  }
1042
1035
 
1036
+
1037
+ //创建一个复杂访问器
1038
+ function makeComplexAccessor(name, initValue, valueType, list, parentModel) {
1039
+ function accessor(value) {
1040
+ var oldValue = accessor._value
1041
+ var son = accessor._vmodel
1042
+ if (arguments.length > 0) {
1043
+ if (stopRepeatAssign) {
1044
+ return this
1045
+ }
1046
+ if (valueType === "array") {
1047
+ var a = son, b = value,
1048
+ an = a.length,
1049
+ bn = b.length
1050
+ a.$lock = true
1051
+ if (an > bn) {
1052
+ a.splice(bn, an - bn)
1053
+ } else if (bn > an) {
1054
+ a.push.apply(a, b.slice(an))
1055
+ }
1056
+ var n = Math.min(an, bn)
1057
+ for (var i = 0; i < n; i++) {
1058
+ a.set(i, b[i])
1059
+ }
1060
+ delete a.$lock
1061
+ a._fire("set")
1062
+ } else if (valueType === "object") {
1063
+ var observes = this.$events[name] || []
1064
+ var newObject = avalon.mix(true, {}, value)
1065
+ for (i in son) {
1066
+ if (son.hasOwnProperty(i) && ohasOwn.call(newObject, i)) {
1067
+ son[i] = newObject[i]
1068
+ }
1069
+ }
1070
+ son = accessor._vmodel = modelFactory(value)
1071
+ son.$events[subscribers] = observes
1072
+ if (observes.length) {
1073
+ observes.forEach(function (data) {
1074
+ if(!data.type) {
1075
+ return //防止模板先加载报错
1076
+ }
1077
+ if (data.rollback) {
1078
+ data.rollback() //还原 ms-with ms-on
1079
+ }
1080
+ bindingHandlers[data.type](data, data.vmodels)
1081
+ })
1082
+ }
1083
+ }
1084
+ accessor.updateValue(this, son.$model)
1085
+ accessor.notify(this, this._value, oldValue)
1086
+ return this
1087
+ } else {
1088
+ dependencyDetection.collectDependency(this, accessor)
1089
+ return son
1090
+ }
1091
+ }
1092
+ accessorFactory(accessor, name)
1093
+ if (Array.isArray(initValue)) {
1094
+ parentModel[name] = initValue
1095
+ } else {
1096
+ parentModel[name] = parentModel[name] || {}
1097
+ }
1098
+ var son = accessor._vmodel = modelFactory(initValue, 0, parentModel[name])
1099
+ son.$events[subscribers] = list
1100
+ return accessor
1101
+ }
1102
+
1103
+ function globalUpdateValue(vmodel, value) {
1104
+ vmodel.$model[this._name] = this._value = value
1105
+ }
1106
+ function globalUpdateModelValue(vmodel, value) {
1107
+ vmodel.$model[this._name] = value
1108
+ }
1109
+ function globalNotify(vmodel, value, oldValue) {
1110
+ var name = this._name
1111
+ var array = vmodel.$events[name] //刷新值
1112
+ if (array) {
1113
+ fireDependencies(array) //同步视图
1114
+ EventBus.$fire.call(vmodel, name, value, oldValue) //触发$watch回调
1115
+ }
1116
+ }
1117
+
1118
+ function accessorFactory(accessor, name) {
1119
+ accessor._name = name
1120
+ //同时更新_value与model
1121
+ accessor.updateValue = globalUpdateValue
1122
+ accessor.notify = globalNotify
1123
+ }
1043
1124
  //比较两个值是否相等
1044
1125
  var isEqual = Object.is || function (v1, v2) {
1045
1126
  if (v1 === 0 && v2 === 0) {
@@ -1051,14 +1132,22 @@ var isEqual = Object.is || function (v1, v2) {
1051
1132
  }
1052
1133
  }
1053
1134
 
1054
- function safeFire(a, b, c, d) {
1055
- if (a.$events) {
1056
- EventBus.$fire.call(a, b, c, d)
1135
+ function isObservable(name, value, $skipArray) {
1136
+ if (isFunction(value) || value && value.nodeType) {
1137
+ return false
1138
+ }
1139
+ if ($skipArray.indexOf(name) !== -1) {
1140
+ return false
1057
1141
  }
1142
+ var $special = $skipArray.$special
1143
+ if (name && name.charAt(0) === "$" && !$special[name]) {
1144
+ return false
1145
+ }
1146
+ return true
1058
1147
  }
1059
1148
 
1060
1149
  var descriptorFactory = function (obj) {
1061
- var descriptors = createMap()
1150
+ var descriptors = {}
1062
1151
  for (var i in obj) {
1063
1152
  descriptors[i] = {
1064
1153
  get: obj[i],
@@ -1070,50 +1159,12 @@ var descriptorFactory = function (obj) {
1070
1159
  return descriptors
1071
1160
  }
1072
1161
 
1073
- //应用于第2种accessor
1074
- function objectFactory(parent, name, value, valueType) {
1075
- //a为原来的VM, b为新数组或新对象
1076
- var son = parent[name]
1077
- if (valueType === "array") {
1078
- if (!Array.isArray(value) || son === value) {
1079
- return son //fix https://github.com/RubyLouvre/avalon/issues/261
1080
- }
1081
- son._.$unwatch()
1082
- son.clear()
1083
- son._.$watch()
1084
- son.pushArray(value.concat())
1085
- return son
1086
- } else {
1087
- var iterators = parent.$events[name]
1088
- var pool = son.$events.$withProxyPool
1089
- if (pool) {
1090
- recycleProxies(pool, "with")
1091
- son.$events.$withProxyPool = null
1092
- }
1093
- var ret = modelFactory(value)
1094
- ret.$events[subscribers] = iterators
1095
- midway[ret.$id] = function (data) {
1096
- while (data = iterators.shift()) {
1097
- (function (el) {
1098
- avalon.nextTick(function () {
1099
- var type = el.type
1100
- if (type && bindingHandlers[type]) { //#753
1101
- el.rollback && el.rollback() //还原 ms-with ms-on
1102
- bindingHandlers[type](el, el.vmodels)
1103
- }
1104
- })
1105
- })(data)// jshint ignore:line
1106
- }
1107
- delete midway[ret.$id]
1108
- }
1109
- return ret
1110
- }
1111
- }
1162
+
1112
1163
  /*********************************************************************
1113
1164
  * 监控数组(与ms-each, ms-repeat配合使用) *
1114
1165
  **********************************************************************/
1115
1166
 
1116
- function Collection(model) {
1167
+ function arrayFactory(model) {
1117
1168
  var array = []
1118
1169
  array.$id = generateID()
1119
1170
  array.$model = model //数据模型
@@ -1128,7 +1179,7 @@ function Collection(model) {
1128
1179
  for (var i in EventBus) {
1129
1180
  array[i] = EventBus[i]
1130
1181
  }
1131
- avalon.mix(array, CollectionPrototype)
1182
+ avalon.mix(array, arrayPrototype)
1132
1183
  return array
1133
1184
  }
1134
1185
 
@@ -1136,7 +1187,7 @@ function mutateArray(method, pos, n, index, method2, pos2, n2) {
1136
1187
  var oldLen = this.length, loop = 2
1137
1188
  while (--loop) {
1138
1189
  switch (method) {
1139
- case "add":
1190
+ case "add":
1140
1191
  /* jshint ignore:start */
1141
1192
  var array = this.$model.slice(pos, pos + n).map(function (el) {
1142
1193
  if (rcomplexType.test(avalon.type(el))) {
@@ -1170,10 +1221,10 @@ function mutateArray(method, pos, n, index, method2, pos2, n2) {
1170
1221
  }
1171
1222
 
1172
1223
  var _splice = ap.splice
1173
- var CollectionPrototype = {
1224
+ var arrayPrototype = {
1174
1225
  _splice: _splice,
1175
1226
  _fire: function (method, a, b) {
1176
- notifySubscribers(this.$events[subscribers], method, a, b)
1227
+ fireDependencies(this.$events[subscribers], method, a, b)
1177
1228
  },
1178
1229
  size: function () { //取得数组长度,这个函数可以同步视图,length不能
1179
1230
  return this._.length
@@ -1259,11 +1310,13 @@ var CollectionPrototype = {
1259
1310
  },
1260
1311
  removeAll: function (all) { //移除N个元素
1261
1312
  if (Array.isArray(all)) {
1262
- all.forEach(function (el) {
1263
- this.remove(el)
1264
- }, this)
1265
- } else if (typeof all === "function") {
1266
1313
  for (var i = this.length - 1; i >= 0; i--) {
1314
+ if (all.indexOf(this[i]) !== -1) {
1315
+ this.removeAt(i)
1316
+ }
1317
+ }
1318
+ } else if (typeof all === "function") {
1319
+ for ( i = this.length - 1; i >= 0; i--) {
1267
1320
  var el = this[i]
1268
1321
  if (all(el, i)) {
1269
1322
  this.removeAt(i)
@@ -1280,7 +1333,7 @@ var CollectionPrototype = {
1280
1333
  return this
1281
1334
  },
1282
1335
  set: function (index, val) {
1283
- if (index >= 0) {
1336
+ if (index < this.length && index > -1) {
1284
1337
  var valueType = avalon.type(val)
1285
1338
  if (val && val.$model) {
1286
1339
  val = val.$model
@@ -1303,6 +1356,15 @@ var CollectionPrototype = {
1303
1356
  return this
1304
1357
  }
1305
1358
  }
1359
+ //相当于原来bindingExecutors.repeat 的index分支
1360
+ function resetIndex(array, pos) {
1361
+ var last = array.length - 1
1362
+ for (var el; el = array[pos]; pos++) {
1363
+ el.$index = pos
1364
+ el.$first = pos === 0
1365
+ el.$last = pos === last
1366
+ }
1367
+ }
1306
1368
 
1307
1369
  function sortByIndex(array, indexes) {
1308
1370
  var map = {};
@@ -1319,7 +1381,7 @@ function sortByIndex(array, indexes) {
1319
1381
  }
1320
1382
 
1321
1383
  "sort,reverse".replace(rword, function (method) {
1322
- CollectionPrototype[method] = function () {
1384
+ arrayPrototype[method] = function () {
1323
1385
  var newArray = this.$model//这是要排序的新数组
1324
1386
  var oldArray = newArray.concat() //保持原来状态的旧数组
1325
1387
  var mask = Math.random()
@@ -1340,28 +1402,57 @@ function sortByIndex(array, indexes) {
1340
1402
  }
1341
1403
  if (hasSort) {
1342
1404
  sortByIndex(this, indexes)
1405
+ // sortByIndex(this.$proxy, indexes)
1343
1406
  this._fire("move", indexes)
1344
- this._fire("index", 0)
1407
+ this._fire("index", 0)
1345
1408
  }
1346
1409
  return this
1347
1410
  }
1348
1411
  })
1349
1412
 
1413
+
1350
1414
  /*********************************************************************
1351
1415
  * 依赖调度系统 *
1352
1416
  **********************************************************************/
1417
+ //检测两个对象间的依赖关系
1418
+ var dependencyDetection = (function () {
1419
+ var outerFrames = []
1420
+ var currentFrame
1421
+ return {
1422
+ begin: function (accessorObject) {
1423
+ //accessorObject为一个拥有callback的对象
1424
+ outerFrames.push(currentFrame)
1425
+ currentFrame = accessorObject
1426
+ },
1427
+ end: function () {
1428
+ currentFrame = outerFrames.pop()
1429
+ },
1430
+ collectDependency: function (vmodel, accessor) {
1431
+ if (currentFrame) {
1432
+ //被dependencyDetection.begin调用
1433
+ currentFrame.callback(vmodel, accessor);
1434
+ }
1435
+ }
1436
+ };
1437
+ })()
1438
+ //将绑定对象注入到其依赖项的订阅数组中
1353
1439
  var ronduplex = /^(duplex|on)$/
1354
-
1355
- function registerSubscriber(data) {
1356
- Registry[expose] = data //暴光此函数,方便collectSubscribers收集
1357
- avalon.openComputedCollect = true
1358
- var fn = data.evaluator
1359
- if (fn) { //如果是求值函数
1440
+ avalon.injectBinding = function (data) {
1441
+ var valueFn = data.evaluator
1442
+ if (valueFn) { //如果是求值函数
1443
+ dependencyDetection.begin({
1444
+ callback: function (vmodel, dependency) {
1445
+ injectDependency(vmodel.$events[dependency._name], data)
1446
+ }
1447
+ })
1360
1448
  try {
1361
- var c = ronduplex.test(data.type) ? data : fn.apply(0, data.args)
1362
- data.handler(c, data.element, data)
1449
+ var value = ronduplex.test(data.type) ? data : valueFn.apply(0, data.args)
1450
+ if(value === void 0){
1451
+ delete data.evaluator
1452
+ }
1453
+ data.handler(value, data.element, data)
1363
1454
  } catch (e) {
1364
- //log("warning:exception throwed in [registerSubscriber] " + e)
1455
+ log("warning:exception throwed in [avalon.injectBinding] " , e)
1365
1456
  delete data.evaluator
1366
1457
  var node = data.element
1367
1458
  if (node.nodeType === 3) {
@@ -1369,126 +1460,160 @@ function registerSubscriber(data) {
1369
1460
  if (kernel.commentInterpolate) {
1370
1461
  parent.replaceChild(DOC.createComment(data.value), node)
1371
1462
  } else {
1372
- node.data = openTag + data.value + closeTag
1463
+ node.data = openTag + (data.oneTime ? "::" : "") + data.value + closeTag
1373
1464
  }
1374
1465
  }
1466
+ } finally {
1467
+ dependencyDetection.end()
1375
1468
  }
1376
1469
  }
1377
- avalon.openComputedCollect = false
1378
- delete Registry[expose]
1379
1470
  }
1380
1471
 
1381
- function collectSubscribers(list) { //收集依赖于这个访问器的订阅者
1382
- var data = Registry[expose]
1383
- if (list && data && avalon.Array.ensure(list, data) && data.element) { //只有数组不存在此元素才push进去
1384
- addSubscribers(data, list)
1472
+ //将依赖项(比它高层的访问器或构建视图刷新函数的绑定对象)注入到订阅者数组
1473
+ function injectDependency(list, data) {
1474
+ if (data.oneTime)
1475
+ return
1476
+ if (list && avalon.Array.ensure(list, data) && data.element) {
1477
+ injectDisposeQueue(data, list)
1478
+ if (new Date() - beginTime > 444 ) {
1479
+ rejectDisposeQueue()
1480
+ }
1385
1481
  }
1386
1482
  }
1387
1483
 
1388
-
1389
- function addSubscribers(data, list) {
1390
- data.$uuid = data.$uuid || generateID()
1391
- list.$uuid = list.$uuid || generateID()
1392
- var obj = {
1393
- data: data,
1394
- list: list,
1395
- $$uuid: data.$uuid + list.$uuid
1396
- }
1397
- if (!$$subscribers[obj.$$uuid]) {
1398
- $$subscribers[obj.$$uuid] = 1
1399
- $$subscribers.push(obj)
1484
+ //通知依赖于这个访问器的订阅者更新自身
1485
+ function fireDependencies(list) {
1486
+ if (list && list.length) {
1487
+ if (new Date() - beginTime > 444 && typeof list[0] === "object") {
1488
+ rejectDisposeQueue()
1489
+ }
1490
+ var args = aslice.call(arguments, 1)
1491
+ for (var i = list.length, fn; fn = list[--i]; ) {
1492
+ var el = fn.element
1493
+ if (el && el.parentNode) {
1494
+ try {
1495
+ var valueFn = fn.evaluator
1496
+ if (fn.$repeat) {
1497
+ fn.handler.apply(fn, args) //处理监控数组的方法
1498
+ }else if("$repeat" in fn || !valueFn ){//如果没有eval,先eval
1499
+ bindingHandlers[fn.type](fn, fn.vmodels)
1500
+ } else if (fn.type !== "on") { //事件绑定只能由用户触发,不能由程序触发
1501
+ var value = valueFn.apply(0, fn.args || [])
1502
+ fn.handler(value, el, fn)
1503
+ }
1504
+ } catch (e) {
1505
+ console.log(e)
1506
+ }
1507
+ }
1508
+ }
1400
1509
  }
1401
1510
  }
1402
-
1403
- function disposeData(data) {
1404
- data.element = null
1405
- data.rollback && data.rollback()
1406
- for (var key in data) {
1407
- data[key] = null
1511
+ /*********************************************************************
1512
+ * 定时GC回收机制 *
1513
+ **********************************************************************/
1514
+ var disposeCount = 0
1515
+ var disposeQueue = avalon.$$subscribers = []
1516
+ var beginTime = new Date()
1517
+ var oldInfo = {}
1518
+ //var uuid2Node = {}
1519
+ function getUid(elem, makeID) { //IE9+,标准浏览器
1520
+ if (!elem.uuid && !makeID) {
1521
+ elem.uuid = ++disposeCount
1408
1522
  }
1523
+ return elem.uuid
1409
1524
  }
1410
1525
 
1411
- function isRemove(el) {
1412
- try {//IE下,如果文本节点脱离DOM树,访问parentNode会报错
1413
- if (!el.parentNode) {
1414
- return true
1526
+ //添加到回收列队中
1527
+ function injectDisposeQueue(data, list) {
1528
+ var elem = data.element
1529
+ if (!data.uuid) {
1530
+ if (elem.nodeType !== 1) {
1531
+ data.uuid = data.type + (data.pos || 0) + "-" + getUid(elem.parentNode)
1532
+ } else {
1533
+ data.uuid = data.name + "-" + getUid(elem)
1415
1534
  }
1416
- } catch (e) {
1417
- return true
1418
1535
  }
1419
- return el.msRetain ? 0 : (el.nodeType === 1 ? typeof el.sourceIndex === "number" ?
1420
- el.sourceIndex === 0 : !root.contains(el) : !avalon.contains(root, el))
1536
+ var lists = data.lists || (data.lists = [])
1537
+ avalon.Array.ensure(lists, list)
1538
+ list.$uuid = list.$uuid || generateID()
1539
+ if (!disposeQueue[data.uuid]) {
1540
+ disposeQueue[data.uuid] = 1
1541
+ disposeQueue.push(data)
1542
+ }
1421
1543
  }
1422
- var $$subscribers = avalon.$$subscribers = []
1423
- var beginTime = new Date()
1424
- var oldInfo = {}
1425
- function removeSubscribers() {
1426
- var i = $$subscribers.length
1544
+
1545
+ function rejectDisposeQueue(data) {
1546
+ if (avalon.optimize)
1547
+ return
1548
+ var i = disposeQueue.length
1427
1549
  var n = i
1428
- var k = 0
1429
- var obj
1430
- var types = []
1550
+ var allTypes = []
1551
+ var iffishTypes = {}
1431
1552
  var newInfo = {}
1432
- var needTest = {}
1433
- while (obj = $$subscribers[--i]) {
1434
- var data = obj.data
1553
+ //对页面上所有绑定对象进行分门别类, 只检测个数发生变化的类型
1554
+ while (data = disposeQueue[--i]) {
1435
1555
  var type = data.type
1436
1556
  if (newInfo[type]) {
1437
1557
  newInfo[type]++
1438
1558
  } else {
1439
1559
  newInfo[type] = 1
1440
- types.push(type)
1560
+ allTypes.push(type)
1441
1561
  }
1442
1562
  }
1443
1563
  var diff = false
1444
- types.forEach(function(type) {
1564
+ allTypes.forEach(function (type) {
1445
1565
  if (oldInfo[type] !== newInfo[type]) {
1446
- needTest[type] = 1
1566
+ iffishTypes[type] = 1
1447
1567
  diff = true
1448
1568
  }
1449
1569
  })
1450
1570
  i = n
1451
- //avalon.log("需要检测的个数 " + i)
1452
1571
  if (diff) {
1453
- //avalon.log("有需要移除的元素")
1454
- while (obj = $$subscribers[--i]) {
1455
- data = obj.data
1456
- if (data.element === void 0)
1572
+ while (data = disposeQueue[--i]) {
1573
+ if (data.element === null) {
1574
+ disposeQueue.splice(i, 1)
1457
1575
  continue
1458
- if (needTest[data.type] && isRemove(data.element)) { //如果它没有在DOM树
1459
- k++
1460
- $$subscribers.splice(i, 1)
1461
- delete $$subscribers[obj.$$uuid]
1462
- avalon.Array.remove(obj.list, data)
1463
- //log("debug: remove " + data.type)
1576
+ }
1577
+ if (iffishTypes[data.type] && shouldDispose(data.element)) { //如果它没有在DOM树
1578
+ disposeQueue.splice(i, 1)
1579
+ delete disposeQueue[data.uuid]
1580
+ //delete uuid2Node[data.element.uuid]
1581
+ var lists = data.lists
1582
+ for (var k = 0, list; list = lists[k++]; ) {
1583
+ avalon.Array.remove(lists, list)
1584
+ avalon.Array.remove(list, data)
1585
+ }
1464
1586
  disposeData(data)
1465
- obj.data = obj.list = null
1466
1587
  }
1467
1588
  }
1468
1589
  }
1469
1590
  oldInfo = newInfo
1470
- // avalon.log("已经移除的个数 " + k)
1471
1591
  beginTime = new Date()
1472
1592
  }
1473
1593
 
1474
- function notifySubscribers(list) { //通知依赖于这个访问器的订阅者更新自身
1475
- if (list && list.length) {
1476
- if (new Date() - beginTime > 444 && typeof list[0] === "object") {
1477
- removeSubscribers()
1478
- }
1479
- var args = aslice.call(arguments, 1)
1480
- for (var i = list.length, fn; fn = list[--i]; ) {
1481
- var el = fn.element
1482
- if (el && el.parentNode) {
1483
- if (fn.$repeat) {
1484
- fn.handler.apply(fn, args) //处理监控数组的方法
1485
- } else if (fn.type !== "on") { //事件绑定只能由用户触发,不能由程序触发
1486
- var fun = fn.evaluator || noop
1487
- fn.handler(fun.apply(0, fn.args || []), el, fn)
1488
- }
1489
- }
1594
+ function disposeData(data) {
1595
+ delete disposeQueue[data.uuid] // 先清除,不然无法回收了
1596
+ data.element = null
1597
+ data.rollback && data.rollback()
1598
+ for (var key in data) {
1599
+ data[key] = null
1600
+ }
1601
+ }
1602
+
1603
+ function shouldDispose(el) {
1604
+ try {//IE下,如果文本节点脱离DOM树,访问parentNode会报错
1605
+ var fireError = el.parentNode.nodeType
1606
+ } catch (e) {
1607
+ return true
1608
+ }
1609
+ if (el.ifRemove) {
1610
+ // 如果节点被放到ifGroup,才移除
1611
+ if (!root.contains(el.ifRemove) && (ifGroup === el.parentNode)) {
1612
+ el.parentNode && el.parentNode.removeChild(el)
1613
+ return true
1490
1614
  }
1491
1615
  }
1616
+ return el.msRetain ? 0 : (el.nodeType === 1 ? !root.contains(el) : !avalon.contains(root, el))
1492
1617
  }
1493
1618
 
1494
1619
  /************************************************************************
@@ -1521,7 +1646,7 @@ var scriptTypes = oneObject(["", "text/javascript", "text/ecmascript", "applicat
1521
1646
  var script = DOC.createElement("script")
1522
1647
  var rhtml = /<|&#?\w+;/
1523
1648
  avalon.parseHTML = function(html) {
1524
- var fragment = hyperspace.cloneNode(false)
1649
+ var fragment = avalonFragment.cloneNode(false)
1525
1650
  if (typeof html !== "string" ) {
1526
1651
  return fragment
1527
1652
  }
@@ -1569,351 +1694,30 @@ avalon.clearHTML = function(node) {
1569
1694
  }
1570
1695
 
1571
1696
  /*********************************************************************
1572
- * 扫描系统 *
1697
+ * avalon的原型方法定义区 *
1573
1698
  **********************************************************************/
1574
1699
 
1575
- avalon.scan = function(elem, vmodel, group) {
1576
- elem = elem || root
1577
- var vmodels = vmodel ? [].concat(vmodel) : []
1578
- scanTag(elem, vmodels)
1700
+ function hyphen(target) {
1701
+ //转换为连字符线风格
1702
+ return target.replace(/([a-z\d])([A-Z]+)/g, "$1-$2").toLowerCase()
1579
1703
  }
1580
1704
 
1581
- //http://www.w3.org/TR/html5/syntax.html#void-elements
1582
- var stopScan = oneObject("area,base,basefont,br,col,command,embed,hr,img,input,link,meta,param,source,track,wbr,noscript,script,style,textarea".toUpperCase())
1583
-
1584
- function checkScan(elem, callback, innerHTML) {
1585
- var id = setTimeout(function() {
1586
- var currHTML = elem.innerHTML
1587
- clearTimeout(id)
1588
- if (currHTML === innerHTML) {
1589
- callback()
1590
- } else {
1591
- checkScan(elem, callback, currHTML)
1592
- }
1705
+ function camelize(target) {
1706
+ //转换为驼峰风格
1707
+ if (target.indexOf("-") < 0 && target.indexOf("_") < 0) {
1708
+ return target //提前判断,提高getStyle等的效率
1709
+ }
1710
+ return target.replace(/[-_][^-_]/g, function(match) {
1711
+ return match.charAt(1).toUpperCase()
1593
1712
  })
1594
1713
  }
1595
1714
 
1596
-
1597
- function createSignalTower(elem, vmodel) {
1598
- var id = elem.getAttribute("avalonctrl") || vmodel.$id
1599
- elem.setAttribute("avalonctrl", id)
1600
- vmodel.$events.expr = elem.tagName + '[avalonctrl="' + id + '"]'
1601
- }
1602
-
1603
- var getBindingCallback = function(elem, name, vmodels) {
1604
- var callback = elem.getAttribute(name)
1605
- if (callback) {
1606
- for (var i = 0, vm; vm = vmodels[i++]; ) {
1607
- if (vm.hasOwnProperty(callback) && typeof vm[callback] === "function") {
1608
- return vm[callback]
1609
- }
1610
- }
1611
- }
1612
- }
1613
-
1614
- function executeBindings(bindings, vmodels) {
1615
- for (var i = 0, data; data = bindings[i++]; ) {
1616
- data.vmodels = vmodels
1617
- bindingHandlers[data.type](data, vmodels)
1618
- if (data.evaluator && data.element && data.element.nodeType === 1) { //移除数据绑定,防止被二次解析
1619
- //chrome使用removeAttributeNode移除不存在的特性节点时会报错 https://github.com/RubyLouvre/avalon/issues/99
1620
- data.element.removeAttribute(data.name)
1621
- }
1622
- }
1623
- bindings.length = 0
1624
- }
1625
-
1626
- //https://github.com/RubyLouvre/avalon/issues/636
1627
- var mergeTextNodes = IEVersion && window.MutationObserver ? function (elem) {
1628
- var node = elem.firstChild, text
1629
- while (node) {
1630
- var aaa = node.nextSibling
1631
- if (node.nodeType === 3) {
1632
- if (text) {
1633
- text.nodeValue += node.nodeValue
1634
- elem.removeChild(node)
1635
- } else {
1636
- text = node
1637
- }
1638
- } else {
1639
- text = null
1640
- }
1641
- node = aaa
1642
- }
1643
- } : 0
1644
-
1645
- var rmsAttr = /ms-(\w+)-?(.*)/
1646
- var priorityMap = {
1647
- "if": 10,
1648
- "repeat": 90,
1649
- "data": 100,
1650
- "widget": 110,
1651
- "each": 1400,
1652
- "with": 1500,
1653
- "duplex": 2000,
1654
- "on": 3000
1655
- }
1656
-
1657
- var events = oneObject("animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit")
1658
- var obsoleteAttrs = oneObject("value,title,alt,checked,selected,disabled,readonly,enabled")
1659
- function bindingSorter(a, b) {
1660
- return a.priority - b.priority
1661
- }
1662
-
1663
- function scanAttr(elem, vmodels) {
1664
- //防止setAttribute, removeAttribute时 attributes自动被同步,导致for循环出错
1665
- var attributes = elem.hasAttributes() ? avalon.slice(elem.attributes) : []
1666
- var bindings = [],
1667
- msData = createMap(),
1668
- match
1669
- for (var i = 0, attr; attr = attributes[i++]; ) {
1670
- if (attr.specified) {
1671
- if (match = attr.name.match(rmsAttr)) {
1672
- //如果是以指定前缀命名的
1673
- var type = match[1]
1674
- var param = match[2] || ""
1675
- var value = attr.value
1676
- var name = attr.name
1677
- msData[name] = value
1678
- if (events[type]) {
1679
- param = type
1680
- type = "on"
1681
- } else if (obsoleteAttrs[type]) {
1682
- log("warning!请改用ms-attr-" + type + "代替ms-" + type + "!")
1683
- if (type === "enabled") {//吃掉ms-enabled绑定,用ms-disabled代替
1684
- log("warning!ms-enabled或ms-attr-enabled已经被废弃")
1685
- type = "disabled"
1686
- value = "!(" + value + ")"
1687
- }
1688
- param = type
1689
- type = "attr"
1690
- elem.removeAttribute(name)
1691
- name = "ms-attr-" + param
1692
- elem.setAttribute(name, value)
1693
- match = [name]
1694
- msData[name] = value
1695
- }
1696
- if (typeof bindingHandlers[type] === "function") {
1697
- var binding = {
1698
- type: type,
1699
- param: param,
1700
- element: elem,
1701
- name: match[0],
1702
- value: value,
1703
- priority: type in priorityMap ? priorityMap[type] : type.charCodeAt(0) * 10 + (Number(param) || 0)
1704
- }
1705
- if (type === "html" || type === "text") {
1706
- var token = getToken(value)
1707
- avalon.mix(binding, token)
1708
- binding.filters = binding.filters.replace(rhasHtml, function () {
1709
- binding.type = "html"
1710
- binding.group = 1
1711
- return ""
1712
- })// jshint ignore:line
1713
- }
1714
- if (name === "ms-if-loop") {
1715
- binding.priority += 100
1716
- }
1717
- if (vmodels.length) {
1718
- bindings.push(binding)
1719
- if (type === "widget") {
1720
- elem.msData = elem.msData || msData
1721
- }
1722
- }
1723
- }
1724
- }
1725
- }
1726
- }
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
- }
1735
- }
1736
- bindings.sort(bindingSorter)
1737
- var scanNode = true
1738
- for (i = 0; binding = bindings[i]; i++) {
1739
- type = binding.type
1740
- if (rnoscanAttrBinding.test(type)) {
1741
- return executeBindings(bindings.slice(0, i + 1), vmodels)
1742
- } else if (scanNode) {
1743
- scanNode = !rnoscanNodeBinding.test(type)
1744
- }
1745
- }
1746
- executeBindings(bindings, vmodels)
1747
- if (scanNode && !stopScan[elem.tagName] && rbind.test(elem.innerHTML + elem.textContent)) {
1748
- mergeTextNodes && mergeTextNodes(elem)
1749
- scanNodeList(elem, vmodels) //扫描子孙元素
1750
- }
1751
- }
1752
-
1753
- var rnoscanAttrBinding = /^if|widget|repeat$/
1754
- var rnoscanNodeBinding = /^each|with|html|include$/
1755
- function scanNodeList(parent, vmodels) {
1756
- var node = parent.firstChild
1757
- while (node) {
1758
- var nextNode = node.nextSibling
1759
- scanNode(node, node.nodeType, vmodels)
1760
- node = nextNode
1761
- }
1762
- }
1763
-
1764
- function scanNodeArray(nodes, vmodels) {
1765
- for (var i = 0, node; node = nodes[i++]; ) {
1766
- scanNode(node, node.nodeType, vmodels)
1767
- }
1768
- }
1769
- function scanNode(node, nodeType, vmodels) {
1770
- if (nodeType === 1) {
1771
- scanTag(node, vmodels) //扫描元素节点
1772
- } else if (nodeType === 3 && rexpr.test(node.data)){
1773
- scanText(node, vmodels) //扫描文本节点
1774
- } else if (kernel.commentInterpolate && nodeType === 8 && !rexpr.test(node.nodeValue)) {
1775
- scanText(node, vmodels) //扫描注释节点
1776
- }
1777
- }
1778
- function scanTag(elem, vmodels, node) {
1779
- //扫描顺序 ms-skip(0) --> ms-important(1) --> ms-controller(2) --> ms-if(10) --> ms-repeat(100)
1780
- //--> ms-if-loop(110) --> ms-attr(970) ...--> ms-each(1400)-->ms-with(1500)--〉ms-duplex(2000)垫后
1781
- var a = elem.getAttribute("ms-skip")
1782
- var b = elem.getAttributeNode("ms-important")
1783
- var c = elem.getAttributeNode("ms-controller")
1784
- if (typeof a === "string") {
1785
- return
1786
- } else if (node = b || c) {
1787
- var newVmodel = avalon.vmodels[node.value]
1788
- if (!newVmodel) {
1789
- return
1790
- }
1791
- //ms-important不包含父VM,ms-controller相反
1792
- vmodels = node === b ? [newVmodel] : [newVmodel].concat(vmodels)
1793
- elem.removeAttribute(node.name) //removeAttributeNode不会刷新[ms-controller]样式规则
1794
- elem.classList.remove(node.name)
1795
- createSignalTower(elem, newVmodel)
1796
- }
1797
- scanAttr(elem, vmodels) //扫描特性节点
1798
- }
1799
- var rhasHtml = /\|\s*html\s*/,
1800
- r11a = /\|\|/g,
1801
- rlt = /&lt;/g,
1802
- rgt = /&gt;/g
1803
- rstringLiteral = /(['"])(\\\1|.)+?\1/g
1804
- function getToken(value) {
1805
- if (value.indexOf("|") > 0) {
1806
- var scapegoat = value.replace( rstringLiteral, function(_){
1807
- return Array(_.length+1).join("1")// jshint ignore:line
1808
- })
1809
- var index = scapegoat.replace(r11a, "\u1122\u3344").indexOf("|") //干掉所有短路或
1810
- if (index > -1) {
1811
- return {
1812
- filters: value.slice(index),
1813
- value: value.slice(0, index),
1814
- expr: true
1815
- }
1816
- }
1817
- }
1818
- return {
1819
- value: value,
1820
- filters: "",
1821
- expr: true
1822
- }
1823
- }
1824
-
1825
- function scanExpr(str) {
1826
- var tokens = [],
1827
- value, start = 0,
1828
- stop
1829
- do {
1830
- stop = str.indexOf(openTag, start)
1831
- if (stop === -1) {
1832
- break
1833
- }
1834
- value = str.slice(start, stop)
1835
- if (value) { // {{ 左边的文本
1836
- tokens.push({
1837
- value: value,
1838
- filters: "",
1839
- expr: false
1840
- })
1841
- }
1842
- start = stop + openTag.length
1843
- stop = str.indexOf(closeTag, start)
1844
- if (stop === -1) {
1845
- break
1846
- }
1847
- value = str.slice(start, stop)
1848
- if (value) { //处理{{ }}插值表达式
1849
- tokens.push(getToken(value))
1850
- }
1851
- start = stop + closeTag.length
1852
- } while (1)
1853
- value = str.slice(start)
1854
- if (value) { //}} 右边的文本
1855
- tokens.push({
1856
- value: value,
1857
- expr: false,
1858
- filters: ""
1859
- })
1860
- }
1861
- return tokens
1862
- }
1863
-
1864
- function scanText(textNode, vmodels) {
1865
- var bindings = []
1866
- if (textNode.nodeType === 8) {
1867
- var token = getToken(textNode.nodeValue)
1868
- var tokens = [token]
1869
- } else {
1870
- tokens = scanExpr(textNode.data)
1871
- }
1872
- if (tokens.length) {
1873
- for (var i = 0; token = tokens[i++]; ) {
1874
- var node = DOC.createTextNode(token.value) //将文本转换为文本节点,并替换原来的文本节点
1875
- if (token.expr) {
1876
- token.type = "text"
1877
- token.element = node
1878
- token.filters = token.filters.replace(rhasHtml, function() {
1879
- token.type = "html"
1880
- token.group = 1
1881
- return ""
1882
- })// jshint ignore:line
1883
- bindings.push(token) //收集带有插值表达式的文本
1884
- }
1885
- hyperspace.appendChild(node)
1886
- }
1887
- textNode.parentNode.replaceChild(hyperspace, textNode)
1888
- if (bindings.length)
1889
- executeBindings(bindings, vmodels)
1890
- }
1891
- }
1892
-
1893
-
1894
- /*********************************************************************
1895
- * avalon的原型方法定义区 *
1896
- **********************************************************************/
1897
- function hyphen(target) {
1898
- //转换为连字符线风格
1899
- return target.replace(/([a-z\d])([A-Z]+)/g, "$1-$2").toLowerCase()
1900
- }
1901
- function camelize(target) {
1902
- //转换为驼峰风格
1903
- if (target.indexOf("-") < 0 && target.indexOf("_") < 0) {
1904
- return target //提前判断,提高getStyle等的效率
1905
- }
1906
- return target.replace(/[-_][^-_]/g, function (match) {
1907
- return match.charAt(1).toUpperCase()
1908
- })
1909
- }
1910
-
1911
- "add,remove".replace(rword, function (method) {
1912
- avalon.fn[method + "Class"] = function (cls) {
1715
+ "add,remove".replace(rword, function(method) {
1716
+ avalon.fn[method + "Class"] = function(cls) {
1913
1717
  var el = this[0]
1914
1718
  //https://developer.mozilla.org/zh-CN/docs/Mozilla/Firefox/Releases/26
1915
1719
  if (cls && typeof cls === "string" && el && el.nodeType === 1) {
1916
- cls.replace(/\S+/g, function (c) {
1720
+ cls.replace(/\S+/g, function(c) {
1917
1721
  el.classList[method](c)
1918
1722
  })
1919
1723
  }
@@ -1922,13 +1726,13 @@ function camelize(target) {
1922
1726
  })
1923
1727
 
1924
1728
  avalon.fn.mix({
1925
- hasClass: function (cls) {
1729
+ hasClass: function(cls) {
1926
1730
  var el = this[0] || {} //IE10+, chrome8+, firefox3.6+, safari5.1+,opera11.5+支持classList,chrome24+,firefox26+支持classList2.0
1927
1731
  return el.nodeType === 1 && el.classList.contains(cls)
1928
1732
  },
1929
- toggleClass: function (value, stateVal) {
1733
+ toggleClass: function(value, stateVal) {
1930
1734
  var className, i = 0
1931
- var classNames = value.split(/\s+/)
1735
+ var classNames = String(value).split(/\s+/)
1932
1736
  var isBool = typeof stateVal === "boolean"
1933
1737
  while ((className = classNames[i++])) {
1934
1738
  var state = isBool ? stateVal : !this.hasClass(className)
@@ -1936,7 +1740,7 @@ avalon.fn.mix({
1936
1740
  }
1937
1741
  return this
1938
1742
  },
1939
- attr: function (name, value) {
1743
+ attr: function(name, value) {
1940
1744
  if (arguments.length === 2) {
1941
1745
  this[0].setAttribute(name, value)
1942
1746
  return this
@@ -1944,7 +1748,7 @@ avalon.fn.mix({
1944
1748
  return this[0].getAttribute(name)
1945
1749
  }
1946
1750
  },
1947
- data: function (name, value) {
1751
+ data: function(name, value) {
1948
1752
  name = "data-" + hyphen(name || "")
1949
1753
  switch (arguments.length) {
1950
1754
  case 2:
@@ -1955,7 +1759,7 @@ avalon.fn.mix({
1955
1759
  return parseData(val)
1956
1760
  case 0:
1957
1761
  var ret = {}
1958
- ap.forEach.call(this[0].attributes, function (attr) {
1762
+ ap.forEach.call(this[0].attributes, function(attr) {
1959
1763
  if (attr) {
1960
1764
  name = attr.name
1961
1765
  if (!name.indexOf("data-")) {
@@ -1967,12 +1771,12 @@ avalon.fn.mix({
1967
1771
  return ret
1968
1772
  }
1969
1773
  },
1970
- removeData: function (name) {
1774
+ removeData: function(name) {
1971
1775
  name = "data-" + hyphen(name)
1972
1776
  this[0].removeAttribute(name)
1973
1777
  return this
1974
1778
  },
1975
- css: function (name, value) {
1779
+ css: function(name, value) {
1976
1780
  if (avalon.isPlainObject(name)) {
1977
1781
  for (var i in name) {
1978
1782
  avalon.css(this, i, name[i])
@@ -1982,13 +1786,13 @@ avalon.fn.mix({
1982
1786
  }
1983
1787
  return ret !== void 0 ? ret : this
1984
1788
  },
1985
- position: function () {
1789
+ position: function() {
1986
1790
  var offsetParent, offset,
1987
- elem = this[0],
1988
- parentOffset = {
1989
- top: 0,
1990
- left: 0
1991
- };
1791
+ elem = this[0],
1792
+ parentOffset = {
1793
+ top: 0,
1794
+ left: 0
1795
+ };
1992
1796
  if (!elem) {
1993
1797
  return
1994
1798
  }
@@ -2002,31 +1806,34 @@ avalon.fn.mix({
2002
1806
  }
2003
1807
  parentOffset.top += avalon.css(offsetParent[0], "borderTopWidth", true)
2004
1808
  parentOffset.left += avalon.css(offsetParent[0], "borderLeftWidth", true)
1809
+ // Subtract offsetParent scroll positions
1810
+ parentOffset.top -= offsetParent.scrollTop()
1811
+ parentOffset.left -= offsetParent.scrollLeft()
2005
1812
  }
2006
1813
  return {
2007
1814
  top: offset.top - parentOffset.top - avalon.css(elem, "marginTop", true),
2008
1815
  left: offset.left - parentOffset.left - avalon.css(elem, "marginLeft", true)
2009
1816
  }
2010
1817
  },
2011
- offsetParent: function () {
1818
+ offsetParent: function() {
2012
1819
  var offsetParent = this[0].offsetParent
2013
1820
  while (offsetParent && avalon.css(offsetParent, "position") === "static") {
2014
1821
  offsetParent = offsetParent.offsetParent;
2015
1822
  }
2016
1823
  return avalon(offsetParent || root)
2017
1824
  },
2018
- bind: function (type, fn, phase) {
1825
+ bind: function(type, fn, phase) {
2019
1826
  if (this[0]) { //此方法不会链
2020
1827
  return avalon.bind(this[0], type, fn, phase)
2021
1828
  }
2022
1829
  },
2023
- unbind: function (type, fn, phase) {
1830
+ unbind: function(type, fn, phase) {
2024
1831
  if (this[0]) {
2025
1832
  avalon.unbind(this[0], type, fn, phase)
2026
1833
  }
2027
1834
  return this
2028
1835
  },
2029
- val: function (value) {
1836
+ val: function(value) {
2030
1837
  var node = this[0]
2031
1838
  if (node && node.nodeType === 1) {
2032
1839
  var get = arguments.length === 0
@@ -2045,7 +1852,8 @@ avalon.fn.mix({
2045
1852
  })
2046
1853
 
2047
1854
  if (root.dataset) {
2048
- avalon.fn.data = function (name, val) {
1855
+ avalon.fn.data = function(name, val) {
1856
+ name = name && camelize(name)
2049
1857
  var dataset = this[0].dataset
2050
1858
  switch (arguments.length) {
2051
1859
  case 2:
@@ -2071,19 +1879,18 @@ function parseData(data) {
2071
1879
  if (typeof data === "object")
2072
1880
  return data
2073
1881
  data = data === "true" ? true :
2074
- data === "false" ? false :
2075
- data === "null" ? null : +data + "" === data ? +data : rbrace.test(data) ? JSON.parse(data) : data
2076
- } catch (e) {
2077
- }
1882
+ data === "false" ? false :
1883
+ data === "null" ? null : +data + "" === data ? +data : rbrace.test(data) ? JSON.parse(data) : data
1884
+ } catch (e) {}
2078
1885
  return data
2079
1886
  }
2080
1887
  avalon.each({
2081
1888
  scrollLeft: "pageXOffset",
2082
1889
  scrollTop: "pageYOffset"
2083
- }, function (method, prop) {
2084
- avalon.fn[method] = function (val) {
1890
+ }, function(method, prop) {
1891
+ avalon.fn[method] = function(val) {
2085
1892
  var node = this[0] || {}, win = getWindow(node),
2086
- top = method === "scrollTop"
1893
+ top = method === "scrollTop"
2087
1894
  if (!arguments.length) {
2088
1895
  return win ? win[prop] : node[method]
2089
1896
  } else {
@@ -2102,13 +1909,13 @@ function getWindow(node) {
2102
1909
 
2103
1910
  //=============================css相关==================================
2104
1911
  var cssHooks = avalon.cssHooks = createMap()
2105
- var prefixes = ["", "-webkit-", "-moz-", "-ms-"]//去掉opera-15的支持
1912
+ var prefixes = ["", "-webkit-", "-moz-", "-ms-"] //去掉opera-15的支持
2106
1913
  var cssMap = {
2107
1914
  "float": "cssFloat"
2108
1915
  }
2109
- avalon.cssNumber = oneObject("columnCount,order,fillOpacity,fontWeight,lineHeight,opacity,orphans,widows,zIndex,zoom")
1916
+ avalon.cssNumber = oneObject("animationIterationCount,columnCount,order,flex,flexGrow,flexShrink,fillOpacity,fontWeight,lineHeight,opacity,orphans,widows,zIndex,zoom")
2110
1917
 
2111
- avalon.cssName = function (name, host, camelCase) {
1918
+ avalon.cssName = function(name, host, camelCase) {
2112
1919
  if (cssMap[name]) {
2113
1920
  return cssMap[name]
2114
1921
  }
@@ -2121,33 +1928,33 @@ avalon.cssName = function (name, host, camelCase) {
2121
1928
  }
2122
1929
  return null
2123
1930
  }
2124
- cssHooks["@:set"] = function (node, name, value) {
1931
+ cssHooks["@:set"] = function(node, name, value) {
2125
1932
  node.style[name] = value
2126
1933
  }
2127
1934
 
2128
- cssHooks["@:get"] = function (node, name) {
1935
+ cssHooks["@:get"] = function(node, name) {
2129
1936
  if (!node || !node.style) {
2130
1937
  throw new Error("getComputedStyle要求传入一个节点 " + node)
2131
1938
  }
2132
1939
  var ret, computed = getComputedStyle(node)
2133
- if (computed) {
2134
- ret = name === "filter" ? computed.getPropertyValue(name) : computed[name]
2135
- if (ret === "") {
2136
- ret = node.style[name] //其他浏览器需要我们手动取内联样式
1940
+ if (computed) {
1941
+ ret = name === "filter" ? computed.getPropertyValue(name) : computed[name]
1942
+ if (ret === "") {
1943
+ ret = node.style[name] //其他浏览器需要我们手动取内联样式
1944
+ }
2137
1945
  }
2138
- }
2139
1946
  return ret
2140
1947
  }
2141
- cssHooks["opacity:get"] = function (node) {
1948
+ cssHooks["opacity:get"] = function(node) {
2142
1949
  var ret = cssHooks["@:get"](node, "opacity")
2143
1950
  return ret === "" ? "1" : ret
2144
1951
  }
2145
1952
 
2146
- "top,left".replace(rword, function (name) {
2147
- cssHooks[name + ":get"] = function (node) {
1953
+ "top,left".replace(rword, function(name) {
1954
+ cssHooks[name + ":get"] = function(node) {
2148
1955
  var computed = cssHooks["@:get"](node, name)
2149
1956
  return /px$/.test(computed) ? computed :
2150
- avalon(node).position()[name] + "px"
1957
+ avalon(node).position()[name] + "px"
2151
1958
  }
2152
1959
  })
2153
1960
  var cssShow = {
@@ -2157,125 +1964,125 @@ var cssShow = {
2157
1964
  }
2158
1965
  var rdisplayswap = /^(none|table(?!-c[ea]).+)/
2159
1966
 
2160
- function showHidden(node, array) {
2161
- //http://www.cnblogs.com/rubylouvre/archive/2012/10/27/2742529.html
2162
- if (node.offsetWidth <= 0) { //opera.offsetWidth可能小于0
2163
- var styles = getComputedStyle(node, null)
2164
- if (rdisplayswap.test(styles["display"])) {
2165
- var obj = {
2166
- node: node
1967
+ function showHidden(node, array) {
1968
+ //http://www.cnblogs.com/rubylouvre/archive/2012/10/27/2742529.html
1969
+ if (node.offsetWidth <= 0) { //opera.offsetWidth可能小于0
1970
+ var styles = getComputedStyle(node, null)
1971
+ if (rdisplayswap.test(styles["display"])) {
1972
+ var obj = {
1973
+ node: node
1974
+ }
1975
+ for (var name in cssShow) {
1976
+ obj[name] = styles[name]
1977
+ node.style[name] = cssShow[name]
1978
+ }
1979
+ array.push(obj)
2167
1980
  }
2168
- for (var name in cssShow) {
2169
- obj[name] = styles[name]
2170
- node.style[name] = cssShow[name]
1981
+ var parent = node.parentNode
1982
+ if (parent && parent.nodeType === 1) {
1983
+ showHidden(parent, array)
2171
1984
  }
2172
- array.push(obj)
2173
- }
2174
- var parent = node.parentNode
2175
- if (parent && parent.nodeType === 1) {
2176
- showHidden(parent, array)
2177
1985
  }
2178
1986
  }
2179
- }
2180
1987
 
2181
- "Width,Height".replace(rword, function (name) {//fix 481
2182
- var method = name.toLowerCase(),
1988
+ "Width,Height".replace(rword, function(name) { //fix 481
1989
+ var method = name.toLowerCase(),
2183
1990
  clientProp = "client" + name,
2184
1991
  scrollProp = "scroll" + name,
2185
1992
  offsetProp = "offset" + name
2186
- cssHooks[method + ":get"] = function (node, which, override) {
2187
- var boxSizing = -4
2188
- if (typeof override === "number") {
2189
- boxSizing = override
1993
+ cssHooks[method + ":get"] = function(node, which, override) {
1994
+ var boxSizing = -4
1995
+ if (typeof override === "number") {
1996
+ boxSizing = override
1997
+ }
1998
+ which = name === "Width" ? ["Left", "Right"] : ["Top", "Bottom"]
1999
+ var ret = node[offsetProp] // border-box 0
2000
+ if (boxSizing === 2) { // margin-box 2
2001
+ return ret + avalon.css(node, "margin" + which[0], true) + avalon.css(node, "margin" + which[1], true)
2002
+ }
2003
+ if (boxSizing < 0) { // padding-box -2
2004
+ ret = ret - avalon.css(node, "border" + which[0] + "Width", true) - avalon.css(node, "border" + which[1] + "Width", true)
2005
+ }
2006
+ if (boxSizing === -4) { // content-box -4
2007
+ ret = ret - avalon.css(node, "padding" + which[0], true) - avalon.css(node, "padding" + which[1], true)
2008
+ }
2009
+ return ret
2010
+ }
2011
+ cssHooks[method + "&get"] = function(node) {
2012
+ var hidden = [];
2013
+ showHidden(node, hidden);
2014
+ var val = cssHooks[method + ":get"](node)
2015
+ for (var i = 0, obj; obj = hidden[i++];) {
2016
+ node = obj.node
2017
+ for (var n in obj) {
2018
+ if (typeof obj[n] === "string") {
2019
+ node.style[n] = obj[n]
2020
+ }
2021
+ }
2022
+ }
2023
+ return val;
2190
2024
  }
2191
- which = name === "Width" ? ["Left", "Right"] : ["Top", "Bottom"]
2192
- var ret = node[offsetProp] // border-box 0
2193
- if (boxSizing === 2) { // margin-box 2
2194
- return ret + avalon.css(node, "margin" + which[0], true) + avalon.css(node, "margin" + which[1], true)
2025
+ avalon.fn[method] = function(value) { //会忽视其display
2026
+ var node = this[0]
2027
+ if (arguments.length === 0) {
2028
+ if (node.setTimeout) { //取得窗口尺寸,IE9后可以用node.innerWidth /innerHeight代替
2029
+ return node["inner" + name]
2030
+ }
2031
+ if (node.nodeType === 9) { //取得页面尺寸
2032
+ var doc = node.documentElement
2033
+ //FF chrome html.scrollHeight< body.scrollHeight
2034
+ //IE 标准模式 : html.scrollHeight> body.scrollHeight
2035
+ //IE 怪异模式 : html.scrollHeight 最大等于可视窗口多一点?
2036
+ return Math.max(node.body[scrollProp], doc[scrollProp], node.body[offsetProp], doc[offsetProp], doc[clientProp])
2037
+ }
2038
+ return cssHooks[method + "&get"](node)
2039
+ } else {
2040
+ return this.css(method, value)
2041
+ }
2195
2042
  }
2196
- if (boxSizing < 0) { // padding-box -2
2197
- ret = ret - avalon.css(node, "border" + which[0] + "Width", true) - avalon.css(node, "border" + which[1] + "Width", true)
2043
+ avalon.fn["inner" + name] = function() {
2044
+ return cssHooks[method + ":get"](this[0], void 0, -2)
2198
2045
  }
2199
- if (boxSizing === -4) { // content-box -4
2200
- ret = ret - avalon.css(node, "padding" + which[0], true) - avalon.css(node, "padding" + which[1], true)
2046
+ avalon.fn["outer" + name] = function(includeMargin) {
2047
+ return cssHooks[method + ":get"](this[0], void 0, includeMargin === true ? 2 : 0)
2201
2048
  }
2202
- return ret
2203
- }
2204
- cssHooks[method + "&get"] = function (node) {
2205
- var hidden = [];
2206
- showHidden(node, hidden);
2207
- var val = cssHooks[method + ":get"](node)
2208
- for (var i = 0, obj; obj = hidden[i++]; ) {
2209
- node = obj.node
2210
- for (var n in obj) {
2211
- if (typeof obj[n] === "string") {
2212
- node.style[n] = obj[n]
2049
+ })
2050
+ avalon.fn.offset = function() { //取得距离页面左右角的坐标
2051
+ var node = this[0]
2052
+ try {
2053
+ var rect = node.getBoundingClientRect()
2054
+ // Make sure element is not hidden (display: none) or disconnected
2055
+ // https://github.com/jquery/jquery/pull/2043/files#r23981494
2056
+ if (rect.width || rect.height || node.getClientRects().length) {
2057
+ var doc = node.ownerDocument
2058
+ var root = doc.documentElement
2059
+ var win = doc.defaultView
2060
+ return {
2061
+ top: rect.top + win.pageYOffset - root.clientTop,
2062
+ left: rect.left + win.pageXOffset - root.clientLeft
2213
2063
  }
2214
2064
  }
2215
- }
2216
- return val;
2217
- }
2218
- avalon.fn[method] = function (value) { //会忽视其display
2219
- var node = this[0]
2220
- if (arguments.length === 0) {
2221
- if (node.setTimeout) { //取得窗口尺寸,IE9后可以用node.innerWidth /innerHeight代替
2222
- return node["inner" + name]
2223
- }
2224
- if (node.nodeType === 9) { //取得页面尺寸
2225
- var doc = node.documentElement
2226
- //FF chrome html.scrollHeight< body.scrollHeight
2227
- //IE 标准模式 : html.scrollHeight> body.scrollHeight
2228
- //IE 怪异模式 : html.scrollHeight 最大等于可视窗口多一点?
2229
- return Math.max(node.body[scrollProp], doc[scrollProp], node.body[offsetProp], doc[offsetProp], doc[clientProp])
2230
- }
2231
- return cssHooks[method + "&get"](node)
2232
- } else {
2233
- return this.css(method, value)
2234
- }
2235
- }
2236
- avalon.fn["inner" + name] = function () {
2237
- return cssHooks[method + ":get"](this[0], void 0, -2)
2238
- }
2239
- avalon.fn["outer" + name] = function (includeMargin) {
2240
- return cssHooks[method + ":get"](this[0], void 0, includeMargin === true ? 2 : 0)
2241
- }
2242
- })
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
2065
+ } catch (e) {
2253
2066
  return {
2254
- top: rect.top + win.pageYOffset - root.clientTop,
2255
- left: rect.left + win.pageXOffset - root.clientLeft
2067
+ left: 0,
2068
+ top: 0
2256
2069
  }
2257
2070
  }
2258
- } catch (e) {
2259
- return {
2260
- left: 0,
2261
- top: 0
2262
- }
2263
2071
  }
2264
- }
2265
- //=============================val相关=======================
2072
+ //=============================val相关=======================
2266
2073
 
2267
- function getValType(el) {
2268
- var ret = el.tagName.toLowerCase()
2269
- return ret === "input" && /checkbox|radio/.test(el.type) ? "checked" : ret
2270
- }
2074
+ function getValType(elem) {
2075
+ var ret = elem.tagName.toLowerCase()
2076
+ return ret === "input" && /checkbox|radio/.test(elem.type) ? "checked" : ret
2077
+ }
2271
2078
  var valHooks = {
2272
- "select:get": function (node, value) {
2079
+ "select:get": function(node, value) {
2273
2080
  var option, options = node.options,
2274
- index = node.selectedIndex,
2275
- one = node.type === "select-one" || index < 0,
2276
- values = one ? null : [],
2277
- max = one ? index + 1 : options.length,
2278
- i = index < 0 ? max : one ? index : 0
2081
+ index = node.selectedIndex,
2082
+ one = node.type === "select-one" || index < 0,
2083
+ values = one ? null : [],
2084
+ max = one ? index + 1 : options.length,
2085
+ i = index < 0 ? max : one ? index : 0
2279
2086
  for (; i < max; i++) {
2280
2087
  option = options[i]
2281
2088
  //旧式IE在reset后不会改变selected,需要改用i === index判定
@@ -2292,9 +2099,9 @@ var valHooks = {
2292
2099
  }
2293
2100
  return values
2294
2101
  },
2295
- "select:set": function (node, values, optionSet) {
2102
+ "select:set": function(node, values, optionSet) {
2296
2103
  values = [].concat(values) //强制转换为数组
2297
- for (var i = 0, el; el = node.options[i++]; ) {
2104
+ for (var i = 0, el; el = node.options[i++];) {
2298
2105
  if ((el.selected = values.indexOf(el.value) > -1)) {
2299
2106
  optionSet = true
2300
2107
  }
@@ -2304,7 +2111,6 @@ var valHooks = {
2304
2111
  }
2305
2112
  }
2306
2113
  }
2307
-
2308
2114
  /*********************************************************************
2309
2115
  * 编译系统 *
2310
2116
  **********************************************************************/
@@ -2324,10 +2130,10 @@ var rsplit = /[^\w$]+/g
2324
2130
  var rkeywords = new RegExp(["\\b" + keywords.replace(/,/g, '\\b|\\b') + "\\b"].join('|'), 'g')
2325
2131
  var rnumber = /\b\d[^,]*/g
2326
2132
  var rcomma = /^,+|,+$/g
2327
- var cacheVars = new Cache(512)
2133
+ var variablePool = new Cache(512)
2328
2134
  var getVariables = function (code) {
2329
2135
  var key = "," + code.trim()
2330
- var ret = cacheVars.get(key)
2136
+ var ret = variablePool.get(key)
2331
2137
  if (ret) {
2332
2138
  return ret
2333
2139
  }
@@ -2338,7 +2144,7 @@ var getVariables = function (code) {
2338
2144
  .replace(rnumber, "")
2339
2145
  .replace(rcomma, "")
2340
2146
  .split(/^$|,+/)
2341
- return cacheVars.put(key, uniqSet(match))
2147
+ return variablePool.put(key, uniqSet(match))
2342
2148
  }
2343
2149
  /*添加赋值语句*/
2344
2150
 
@@ -2371,7 +2177,7 @@ function uniqSet(array) {
2371
2177
  return ret
2372
2178
  }
2373
2179
  //缓存求值函数,以便多次利用
2374
- var cacheExprs = new Cache(128)
2180
+ var evaluatorPool = new Cache(128)
2375
2181
  //取得求值函数及其传参
2376
2182
  var rduplex = /\w\[.*\]|\w\.\w/
2377
2183
  var rproxy = /(\$proxy\$[a-z]+)\d+$/
@@ -2395,7 +2201,7 @@ function parseFilter(val, filters) {
2395
2201
  .replace(rthimLeftParentheses, function () {
2396
2202
  return '",'
2397
2203
  }) + "]"
2398
- return "return avalon.filters.$filter(" + val + ", " + filters + ")"
2204
+ return "return this.filters.$filter(" + val + ", " + filters + ")"
2399
2205
  }
2400
2206
 
2401
2207
  function parseExpr(code, scopes, data) {
@@ -2427,9 +2233,11 @@ function parseExpr(code, scopes, data) {
2427
2233
  //https://github.com/RubyLouvre/avalon/issues/583
2428
2234
  data.vars.forEach(function (v) {
2429
2235
  var reg = new RegExp("\\b" + v + "(?:\\.\\w+|\\[\\w+\\])+", "ig")
2430
- code = code.replace(reg, function (_) {
2236
+ code = code.replace(reg, function (_, cap) {
2431
2237
  var c = _.charAt(v.length)
2432
- var r = IEVersion ? code.slice(arguments[1] + _.length) : RegExp.rightContext
2238
+ //var r = IEVersion ? code.slice(arguments[1] + _.length) : RegExp.rightContext
2239
+ //https://github.com/RubyLouvre/avalon/issues/966
2240
+ var r = code.slice(cap + _.length)
2433
2241
  var method = /^\s*\(/.test(r)
2434
2242
  if (c === "." || c === "[" || method) {//比如v为aa,我们只匹配aa.bb,aa[cc],不匹配aaa.xxx
2435
2243
  var name = "var" + String(Math.random()).replace(/^0\./, "")
@@ -2454,7 +2262,8 @@ function parseExpr(code, scopes, data) {
2454
2262
  //---------------args----------------
2455
2263
  data.args = args
2456
2264
  //---------------cache----------------
2457
- var fn = cacheExprs.get(exprId) //直接从缓存,免得重复生成
2265
+ delete data.vars
2266
+ var fn = evaluatorPool.get(exprId) //直接从缓存,免得重复生成
2458
2267
  if (fn) {
2459
2268
  data.evaluator = fn
2460
2269
  return
@@ -2469,6 +2278,16 @@ function parseExpr(code, scopes, data) {
2469
2278
  }
2470
2279
  code = "\nvar ret" + expose + " = " + code + ";\r\n"
2471
2280
  code += parseFilter("ret" + expose, filters)
2281
+ try {
2282
+ fn = Function.apply(noop, names.concat("'use strict';\n" + prefix + code))
2283
+ data.evaluator = evaluatorPool.put(exprId, function() {
2284
+ return fn.apply(avalon, arguments)//确保可以在编译代码中使用this获取avalon对象
2285
+ })
2286
+ } catch (e) {
2287
+ log("debug: parse error," + e.message)
2288
+ }
2289
+ vars = assigns = names = null //释放内存
2290
+ return
2472
2291
  } else if (dataType === "duplex") { //双工绑定
2473
2292
  var _body = "'use strict';\nreturn function(vvv){\n\t" +
2474
2293
  prefix +
@@ -2478,10 +2297,11 @@ function parseExpr(code, scopes, data) {
2478
2297
  "= vvv;\n} "
2479
2298
  try {
2480
2299
  fn = Function.apply(noop, names.concat(_body))
2481
- data.evaluator = cacheExprs.put(exprId, fn)
2300
+ data.evaluator = evaluatorPool.put(exprId, fn)
2482
2301
  } catch (e) {
2483
2302
  log("debug: parse error," + e.message)
2484
2303
  }
2304
+ vars = assigns = names = null //释放内存
2485
2305
  return
2486
2306
  } else if (dataType === "on") { //事件绑定
2487
2307
  if (code.indexOf("(") === -1) {
@@ -2500,37 +2320,377 @@ function parseExpr(code, scopes, data) {
2500
2320
  }
2501
2321
  try {
2502
2322
  fn = Function.apply(noop, names.concat("'use strict';\n" + prefix + code))
2503
- data.evaluator = cacheExprs.put(exprId, fn)
2323
+ data.evaluator = evaluatorPool.put(exprId, fn)
2504
2324
  } catch (e) {
2505
2325
  log("debug: parse error," + e.message)
2506
- } finally {
2507
- vars = assigns = names = null //释放内存
2508
2326
  }
2327
+ vars = assigns = names = null //释放内存
2509
2328
  }
2510
-
2511
-
2512
- //parseExpr的智能引用代理
2513
-
2514
- function parseExprProxy(code, scopes, data, tokens, noregister) {
2515
- if (Array.isArray(tokens)) {
2516
- code = tokens.map(function (el) {
2329
+ function stringifyExpr(code) {
2330
+ var hasExpr = rexpr.test(code) //比如ms-class="width{{w}}"的情况
2331
+ if (hasExpr) {
2332
+ var array = scanExpr(code)
2333
+ if (array.length === 1) {
2334
+ return array[0].value
2335
+ }
2336
+ return array.map(function (el) {
2517
2337
  return el.expr ? "(" + el.value + ")" : quote(el.value)
2518
2338
  }).join(" + ")
2339
+ } else {
2340
+ return code
2519
2341
  }
2342
+ }
2343
+ //parseExpr的智能引用代理
2344
+
2345
+ function parseExprProxy(code, scopes, data, noRegister) {
2346
+ code = code || "" //code 可能未定义
2520
2347
  parseExpr(code, scopes, data)
2521
- if (data.evaluator && !noregister) {
2348
+ if (data.evaluator && !noRegister) {
2522
2349
  data.handler = bindingExecutors[data.handlerName || data.type]
2523
2350
  //方便调试
2524
2351
  //这里非常重要,我们通过判定视图刷新函数的element是否在DOM树决定
2525
2352
  //将它移出订阅者列表
2526
- registerSubscriber(data)
2353
+ avalon.injectBinding(data)
2354
+ }
2355
+ }
2356
+ avalon.parseExprProxy = parseExprProxy
2357
+ /*********************************************************************
2358
+ * 扫描系统 *
2359
+ **********************************************************************/
2360
+
2361
+ avalon.scan = function(elem, vmodel) {
2362
+ elem = elem || root
2363
+ var vmodels = vmodel ? [].concat(vmodel) : []
2364
+ scanTag(elem, vmodels)
2365
+ }
2366
+
2367
+ //http://www.w3.org/TR/html5/syntax.html#void-elements
2368
+ var stopScan = oneObject("area,base,basefont,br,col,command,embed,hr,img,input,link,meta,param,source,track,wbr,noscript,script,style,textarea".toUpperCase())
2369
+
2370
+ function checkScan(elem, callback, innerHTML) {
2371
+ var id = setTimeout(function() {
2372
+ var currHTML = elem.innerHTML
2373
+ clearTimeout(id)
2374
+ if (currHTML === innerHTML) {
2375
+ callback()
2376
+ } else {
2377
+ checkScan(elem, callback, currHTML)
2378
+ }
2379
+ })
2380
+ }
2381
+
2382
+
2383
+ function createSignalTower(elem, vmodel) {
2384
+ var id = elem.getAttribute("avalonctrl") || vmodel.$id
2385
+ elem.setAttribute("avalonctrl", id)
2386
+ vmodel.$events.expr = elem.tagName + '[avalonctrl="' + id + '"]'
2387
+ }
2388
+
2389
+ var getBindingCallback = function(elem, name, vmodels) {
2390
+ var callback = elem.getAttribute(name)
2391
+ if (callback) {
2392
+ for (var i = 0, vm; vm = vmodels[i++]; ) {
2393
+ if (vm.hasOwnProperty(callback) && typeof vm[callback] === "function") {
2394
+ return vm[callback]
2395
+ }
2396
+ }
2397
+ }
2398
+ }
2399
+
2400
+ function executeBindings(bindings, vmodels) {
2401
+ for (var i = 0, data; data = bindings[i++]; ) {
2402
+ data.vmodels = vmodels
2403
+ bindingHandlers[data.type](data, vmodels)
2404
+ if (data.evaluator && data.element && data.element.nodeType === 1) { //移除数据绑定,防止被二次解析
2405
+ //chrome使用removeAttributeNode移除不存在的特性节点时会报错 https://github.com/RubyLouvre/avalon/issues/99
2406
+ data.element.removeAttribute(data.name)
2407
+ }
2408
+ }
2409
+ bindings.length = 0
2410
+ }
2411
+
2412
+ //https://github.com/RubyLouvre/avalon/issues/636
2413
+ var mergeTextNodes = IEVersion && window.MutationObserver ? function (elem) {
2414
+ var node = elem.firstChild, text
2415
+ while (node) {
2416
+ var aaa = node.nextSibling
2417
+ if (node.nodeType === 3) {
2418
+ if (text) {
2419
+ text.nodeValue += node.nodeValue
2420
+ elem.removeChild(node)
2421
+ } else {
2422
+ text = node
2423
+ }
2424
+ } else {
2425
+ text = null
2426
+ }
2427
+ node = aaa
2428
+ }
2429
+ } : 0
2430
+ var roneTime = /^\s*::/
2431
+ var rmsAttr = /ms-(\w+)-?(.*)/
2432
+ var priorityMap = {
2433
+ "if": 10,
2434
+ "repeat": 90,
2435
+ "data": 100,
2436
+ "widget": 110,
2437
+ "each": 1400,
2438
+ "with": 1500,
2439
+ "duplex": 2000,
2440
+ "on": 3000
2441
+ }
2442
+
2443
+ var events = oneObject("animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit")
2444
+ var obsoleteAttrs = oneObject("value,title,alt,checked,selected,disabled,readonly,enabled")
2445
+ function bindingSorter(a, b) {
2446
+ return a.priority - b.priority
2447
+ }
2448
+
2449
+ function scanAttr(elem, vmodels, match) {
2450
+ var scanNode = true
2451
+ if (vmodels.length) {
2452
+ var attributes = elem.attributes
2453
+ var bindings = []
2454
+ var fixAttrs = []
2455
+ var uniq = {}
2456
+ var msData = createMap()
2457
+ for (var i = 0, attr; attr = attributes[i++]; ) {
2458
+ if (attr.specified) {
2459
+ if (match = attr.name.match(rmsAttr)) {
2460
+ //如果是以指定前缀命名的
2461
+ var type = match[1]
2462
+ var param = match[2] || ""
2463
+ var value = attr.value
2464
+ var name = attr.name
2465
+ if (uniq[name]) {//IE8下ms-repeat,ms-with BUG
2466
+ continue
2467
+ }
2468
+ uniq[name] = 1
2469
+ if (events[type]) {
2470
+ param = type
2471
+ type = "on"
2472
+ } else if (obsoleteAttrs[type]) {
2473
+ if (type === "enabled") {//吃掉ms-enabled绑定,用ms-disabled代替
2474
+ log("warning!ms-enabled或ms-attr-enabled已经被废弃")
2475
+ type = "disabled"
2476
+ value = "!(" + value + ")"
2477
+ }
2478
+ param = type
2479
+ type = "attr"
2480
+ name = "ms-" + type + "-" + param
2481
+ fixAttrs.push([attr.name, name, value])
2482
+ }
2483
+ msData[name] = value
2484
+ if (typeof bindingHandlers[type] === "function") {
2485
+ var newValue = value.replace(roneTime, "")
2486
+ var oneTime = value !== newValue
2487
+ var binding = {
2488
+ type: type,
2489
+ param: param,
2490
+ element: elem,
2491
+ name: name,
2492
+ value: newValue,
2493
+ oneTime: oneTime,
2494
+ priority: (priorityMap[type] || type.charCodeAt(0) * 10) + (Number(param.replace(/\D/g, "")) || 0)
2495
+ }
2496
+ if (type === "html" || type === "text") {
2497
+ var token = getToken(value)
2498
+ avalon.mix(binding, token)
2499
+ binding.filters = binding.filters.replace(rhasHtml, function () {
2500
+ binding.type = "html"
2501
+ binding.group = 1
2502
+ return ""
2503
+ })// jshint ignore:line
2504
+ } else if (type === "duplex") {
2505
+ var hasDuplex = name
2506
+ } else if (name === "ms-if-loop") {
2507
+ binding.priority += 100
2508
+ }
2509
+ bindings.push(binding)
2510
+ if (type === "widget") {
2511
+ elem.msData = elem.msData || msData
2512
+ }
2513
+ }
2514
+ }
2515
+ }
2516
+ }
2517
+ if (bindings.length) {
2518
+ bindings.sort(bindingSorter)
2519
+ fixAttrs.forEach(function (arr) {
2520
+ log("warning!请改用" + arr[1] + "代替" + arr[0] + "!")
2521
+ elem.removeAttribute(arr[0])
2522
+ elem.setAttribute(arr[1], arr[2])
2523
+ })
2524
+ var control = elem.type
2525
+ if (control && hasDuplex) {
2526
+ if (msData["ms-attr-value"] && elem.type === "text") {
2527
+ log("warning!" + control + "控件不能同时定义ms-attr-value与" + hasDuplex)
2528
+ }
2529
+ }
2530
+
2531
+ for (i = 0; binding = bindings[i]; i++) {
2532
+ type = binding.type
2533
+ if (rnoscanAttrBinding.test(type)) {
2534
+ return executeBindings(bindings.slice(0, i + 1), vmodels)
2535
+ } else if (scanNode) {
2536
+ scanNode = !rnoscanNodeBinding.test(type)
2537
+ }
2538
+ }
2539
+ executeBindings(bindings, vmodels)
2540
+ }
2541
+ }
2542
+ if (scanNode && !stopScan[elem.tagName] && rbind.test(elem.innerHTML + elem.textContent)) {
2543
+ mergeTextNodes && mergeTextNodes(elem)
2544
+ scanNodeList(elem, vmodels) //扫描子孙元素
2545
+ }
2546
+ }
2547
+
2548
+ var rnoscanAttrBinding = /^if|widget|repeat$/
2549
+ var rnoscanNodeBinding = /^each|with|html|include$/
2550
+ function scanNodeList(parent, vmodels) {
2551
+ var nodes = avalon.slice(parent.childNodes)
2552
+ scanNodeArray(nodes, vmodels)
2553
+ }
2554
+
2555
+ function scanNodeArray(nodes, vmodels) {
2556
+ for (var i = 0, node; node = nodes[i++];) {
2557
+ switch (node.nodeType) {
2558
+ case 1:
2559
+ scanTag(node, vmodels) //扫描元素节点
2560
+ if (node.msCallback) {
2561
+ node.msCallback()
2562
+ node.msCallback = void 0
2563
+ }
2564
+ break
2565
+ case 3:
2566
+ if(rexpr.test(node.nodeValue)){
2567
+ scanText(node, vmodels, i) //扫描文本节点
2568
+ }
2569
+ break
2570
+ }
2571
+ }
2572
+ }
2573
+
2574
+
2575
+ function scanTag(elem, vmodels, node) {
2576
+ //扫描顺序 ms-skip(0) --> ms-important(1) --> ms-controller(2) --> ms-if(10) --> ms-repeat(100)
2577
+ //--> ms-if-loop(110) --> ms-attr(970) ...--> ms-each(1400)-->ms-with(1500)--〉ms-duplex(2000)垫后
2578
+ var a = elem.getAttribute("ms-skip")
2579
+ var b = elem.getAttributeNode("ms-important")
2580
+ var c = elem.getAttributeNode("ms-controller")
2581
+ if (typeof a === "string") {
2582
+ return
2583
+ } else if (node = b || c) {
2584
+ var newVmodel = avalon.vmodels[node.value]
2585
+ if (!newVmodel) {
2586
+ return
2587
+ }
2588
+ //ms-important不包含父VM,ms-controller相反
2589
+ vmodels = node === b ? [newVmodel] : [newVmodel].concat(vmodels)
2590
+ elem.removeAttribute(node.name) //removeAttributeNode不会刷新[ms-controller]样式规则
2591
+ elem.classList.remove(node.name)
2592
+ createSignalTower(elem, newVmodel)
2593
+ }
2594
+ scanAttr(elem, vmodels) //扫描特性节点
2595
+ }
2596
+ var rhasHtml = /\|\s*html(?:\b|$)/,
2597
+ r11a = /\|\|/g,
2598
+ rlt = /&lt;/g,
2599
+ rgt = /&gt;/g,
2600
+ rstringLiteral = /(['"])(\\\1|.)+?\1/g
2601
+ function getToken(value) {
2602
+ if (value.indexOf("|") > 0) {
2603
+ var scapegoat = value.replace(rstringLiteral, function (_) {
2604
+ return Array(_.length + 1).join("1")// jshint ignore:line
2605
+ })
2606
+ var index = scapegoat.replace(r11a, "\u1122\u3344").indexOf("|") //干掉所有短路或
2607
+ if (index > -1) {
2608
+ return {
2609
+ filters: value.slice(index),
2610
+ value: value.slice(0, index),
2611
+ expr: true
2612
+ }
2613
+ }
2614
+ }
2615
+ return {
2616
+ value: value,
2617
+ filters: "",
2618
+ expr: true
2619
+ }
2620
+ }
2621
+
2622
+ function scanExpr(str) {
2623
+ var tokens = [],
2624
+ value, start = 0,
2625
+ stop
2626
+ do {
2627
+ stop = str.indexOf(openTag, start)
2628
+ if (stop === -1) {
2629
+ break
2630
+ }
2631
+ value = str.slice(start, stop)
2632
+ if (value) { // {{ 左边的文本
2633
+ tokens.push({
2634
+ value: value,
2635
+ filters: "",
2636
+ expr: false
2637
+ })
2638
+ }
2639
+ start = stop + openTag.length
2640
+ stop = str.indexOf(closeTag, start)
2641
+ if (stop === -1) {
2642
+ break
2643
+ }
2644
+ value = str.slice(start, stop)
2645
+ if (value) { //处理{{ }}插值表达式
2646
+ tokens.push(getToken(value, start))
2647
+ }
2648
+ start = stop + closeTag.length
2649
+ } while (1)
2650
+ value = str.slice(start)
2651
+ if (value) { //}} 右边的文本
2652
+ tokens.push({
2653
+ value: value,
2654
+ expr: false,
2655
+ filters: ""
2656
+ })
2657
+ }
2658
+ return tokens
2659
+ }
2660
+
2661
+ function scanText(textNode, vmodels, index) {
2662
+ var bindings = []
2663
+ tokens = scanExpr(textNode.data)
2664
+ if (tokens.length) {
2665
+ for (var i = 0; token = tokens[i++]; ) {
2666
+ var node = DOC.createTextNode(token.value) //将文本转换为文本节点,并替换原来的文本节点
2667
+ if (token.expr) {
2668
+ token.value = token.value.replace(roneTime, function () {
2669
+ token.oneTime = true
2670
+ return ""
2671
+ })// jshint ignore:line
2672
+ token.type = "text"
2673
+ token.element = node
2674
+ token.filters = token.filters.replace(rhasHtml, function (a, b,c) {
2675
+ token.type = "html"
2676
+ return ""
2677
+ })// jshint ignore:line
2678
+ token.pos = index * 1000 + i
2679
+ bindings.push(token) //收集带有插值表达式的文本
2680
+ }
2681
+ avalonFragment.appendChild(node)
2682
+ }
2683
+ textNode.parentNode.replaceChild(avalonFragment, textNode)
2684
+ if (bindings.length)
2685
+ executeBindings(bindings, vmodels)
2527
2686
  }
2528
2687
  }
2529
- avalon.parseExprProxy = parseExprProxy
2688
+
2530
2689
  var bools = ["autofocus,autoplay,async,allowTransparency,checked,controls",
2531
2690
  "declare,disabled,defer,defaultChecked,defaultSelected",
2532
2691
  "contentEditable,isMap,loop,multiple,noHref,noResize,noShade",
2533
- "open,readOnly,selected"].join(",")
2692
+ "open,readOnly,selected"
2693
+ ].join(",")
2534
2694
  var boolMap = {}
2535
2695
  bools.replace(rword, function (name) {
2536
2696
  boolMap[name.toLowerCase()] = name
@@ -2547,7 +2707,8 @@ var propMap = {//属性名映射
2547
2707
 
2548
2708
  var anomaly = ["accessKey,bgColor,cellPadding,cellSpacing,codeBase,codeType,colSpan",
2549
2709
  "dateTime,defaultValue,frameBorder,longDesc,maxLength,marginWidth,marginHeight",
2550
- "rowSpan,tabIndex,useMap,vSpace,valueType,vAlign"].join(",")
2710
+ "rowSpan,tabIndex,useMap,vSpace,valueType,vAlign"
2711
+ ].join(",")
2551
2712
  anomaly.replace(rword, function (name) {
2552
2713
  propMap[name.toLowerCase()] = name
2553
2714
  })
@@ -2556,21 +2717,13 @@ var rnoscripts = /<noscript.*?>(?:[\s\S]+?)<\/noscript>/img
2556
2717
  var rnoscriptText = /<noscript.*?>([\s\S]+?)<\/noscript>/im
2557
2718
 
2558
2719
  var getXHR = function () {
2559
- return new (window.XMLHttpRequest || ActiveXObject)("Microsoft.XMLHTTP")// jshint ignore:line
2720
+ return new (window.XMLHttpRequest || ActiveXObject)("Microsoft.XMLHTTP") // jshint ignore:line
2560
2721
  }
2561
2722
 
2562
- var cacheTmpls = avalon.templateCache = {}
2723
+ var templatePool = avalon.templateCache = {}
2563
2724
 
2564
2725
  bindingHandlers.attr = function (data, vmodels) {
2565
- var text = data.value.trim(),
2566
- simple = true
2567
- if (text.indexOf(openTag) > -1 && text.indexOf(closeTag) > 2) {
2568
- simple = false
2569
- if (rexpr.test(text) && RegExp.rightContext === "" && RegExp.leftContext === "") {
2570
- simple = true
2571
- text = RegExp.$1
2572
- }
2573
- }
2726
+ var value = stringifyExpr(data.value.trim())
2574
2727
  if (data.type === "include") {
2575
2728
  var elem = data.element
2576
2729
  data.includeRendered = getBindingCallback(elem, "data-include-rendered", vmodels)
@@ -2591,7 +2744,7 @@ bindingHandlers.attr = function (data, vmodels) {
2591
2744
  }
2592
2745
  }
2593
2746
  data.handlerName = "attr" //handleName用于处理多种绑定共用同一种bindingExecutor的情况
2594
- parseExprProxy(text, vmodels, data, (simple ? 0 : scanExpr(data.value)))
2747
+ parseExprProxy(value, vmodels, data)
2595
2748
  }
2596
2749
 
2597
2750
  bindingExecutors.attr = function (val, elem, data) {
@@ -2600,28 +2753,29 @@ bindingExecutors.attr = function (val, elem, data) {
2600
2753
  if (method === "css") {
2601
2754
  avalon(elem).css(attrName, val)
2602
2755
  } else if (method === "attr") {
2756
+
2603
2757
  // ms-attr-class="xxx" vm.xxx="aaa bbb ccc"将元素的className设置为aaa bbb ccc
2604
2758
  // ms-attr-class="xxx" vm.xxx=false 清空元素的所有类名
2605
2759
  // ms-attr-name="yyy" vm.yyy="ooo" 为元素设置name属性
2606
- if (boolMap[attrName]) {
2607
- var bool = boolMap[attrName]
2608
- if (typeof elem[bool] === "boolean") {
2609
- // IE6-11不支持动态设置fieldset的disabled属性,IE11下样式是生效了,但无法阻止用户对其底下的input元素进行设值……
2610
- return elem[bool] = !!val
2611
- }
2612
- }
2613
2760
  var toRemove = (val === false) || (val === null) || (val === void 0)
2614
2761
 
2615
2762
  if (!W3C && propMap[attrName]) { //旧式IE下需要进行名字映射
2616
2763
  attrName = propMap[attrName]
2617
2764
  }
2765
+ var bool = boolMap[attrName]
2766
+ if (typeof elem[bool] === "boolean") {
2767
+ elem[bool] = !!val //布尔属性必须使用el.xxx = true|false方式设值
2768
+ if (!val) { //如果为false, IE全系列下相当于setAttribute(xxx,''),会影响到样式,需要进一步处理
2769
+ toRemove = true
2770
+ }
2771
+ }
2618
2772
  if (toRemove) {
2619
2773
  return elem.removeAttribute(attrName)
2620
2774
  }
2621
2775
  //SVG只能使用setAttribute(xxx, yyy), VML只能使用elem.xxx = yyy ,HTML的固有属性必须elem.xxx = yyy
2622
2776
  var isInnate = rsvg.test(elem) ? false : (DOC.namespaces && isVML(elem)) ? true : attrName in elem.cloneNode(false)
2623
2777
  if (isInnate) {
2624
- elem[attrName] = val
2778
+ elem[attrName] = val + ""
2625
2779
  } else {
2626
2780
  elem.setAttribute(attrName, val)
2627
2781
  }
@@ -2633,7 +2787,9 @@ bindingExecutors.attr = function (val, elem, data) {
2633
2787
  var target = replace ? elem.parentNode : elem
2634
2788
  var scanTemplate = function (text) {
2635
2789
  if (loaded) {
2636
- text = loaded.apply(target, [text].concat(vmodels))
2790
+ var newText = loaded.apply(target, [text].concat(vmodels))
2791
+ if (typeof newText === "string")
2792
+ text = newText
2637
2793
  }
2638
2794
  if (rendered) {
2639
2795
  checkScan(target, function () {
@@ -2666,20 +2822,27 @@ bindingExecutors.attr = function (val, elem, data) {
2666
2822
  }
2667
2823
 
2668
2824
  if (data.param === "src") {
2669
- if (cacheTmpls[val]) {
2825
+ if (typeof templatePool[val] === "string") {
2670
2826
  avalon.nextTick(function () {
2671
- scanTemplate(cacheTmpls[val])
2827
+ scanTemplate(templatePool[val])
2672
2828
  })
2829
+ } else if (Array.isArray(templatePool[val])) { //#805 防止在循环绑定中发出许多相同的请求
2830
+ templatePool[val].push(scanTemplate)
2673
2831
  } else {
2674
2832
  var xhr = getXHR()
2675
2833
  xhr.onreadystatechange = function () {
2676
2834
  if (xhr.readyState === 4) {
2677
2835
  var s = xhr.status
2678
2836
  if (s >= 200 && s < 300 || s === 304 || s === 1223) {
2679
- scanTemplate(cacheTmpls[val] = xhr.responseText)
2837
+ var text = xhr.responseText
2838
+ for (var f = 0, fn; fn = templatePool[val][f++]; ) {
2839
+ fn(text)
2840
+ }
2841
+ templatePool[val] = text
2680
2842
  }
2681
2843
  }
2682
2844
  }
2845
+ templatePool[val] = [scanTemplate]
2683
2846
  xhr.open("GET", val, true)
2684
2847
  if ("withCredentials" in xhr) {
2685
2848
  xhr.withCredentials = true
@@ -2730,13 +2893,14 @@ bindingExecutors.attr = function (val, elem, data) {
2730
2893
  function getTemplateNodes(data, id, text) {
2731
2894
  var div = data.templateCache && data.templateCache[id]
2732
2895
  if (div) {
2733
- var dom = DOC.createDocumentFragment(), firstChild
2896
+ var dom = DOC.createDocumentFragment(),
2897
+ firstChild
2734
2898
  while (firstChild = div.firstChild) {
2735
2899
  dom.appendChild(firstChild)
2736
2900
  }
2737
2901
  return dom
2738
2902
  }
2739
- return avalon.parseHTML(text)
2903
+ return avalon.parseHTML(text)
2740
2904
  }
2741
2905
 
2742
2906
  //这几个指令都可以使用插值表达式,如ms-src="aaa/{{b}}/{{c}}.html"
@@ -2745,92 +2909,85 @@ function getTemplateNodes(data, id, text) {
2745
2909
  })
2746
2910
  //根据VM的属性值或表达式的值切换类名,ms-class="xxx yyy zzz:flag"
2747
2911
  //http://www.cnblogs.com/rubylouvre/archive/2012/12/17/2818540.html
2748
- bindingHandlers["class"] = function(data, vmodels) {
2749
- var oldStyle = data.param,
2750
- text = data.value,
2912
+ bindingHandlers["class"] = function (binding, vmodels) {
2913
+ var oldStyle = binding.param,
2914
+ text = binding.value,
2751
2915
  rightExpr
2752
- data.handlerName = "class"
2916
+ binding.handlerName = "class"
2753
2917
  if (!oldStyle || isFinite(oldStyle)) {
2754
- data.param = "" //去掉数字
2755
- var noExpr = text.replace(rexprg, function(a) {
2918
+ binding.param = "" //去掉数字
2919
+ var colonIndex = text.replace(rexprg, function (a) {
2756
2920
  return a.replace(/./g, "0")
2757
- //return Math.pow(10, a.length - 1) //将插值表达式插入10的N-1次方来占位
2758
- })
2759
- var colonIndex = noExpr.indexOf(":") //取得第一个冒号的位置
2921
+ }).indexOf(":") //取得第一个冒号的位置
2760
2922
  if (colonIndex === -1) { // 比如 ms-class="aaa bbb ccc" 的情况
2761
2923
  var className = text
2762
- } else { // 比如 ms-class-1="ui-state-active:checked" 的情况
2924
+ rightExpr = true
2925
+ } else { // 比如 ms-class-1="ui-state-active:checked" 的情况
2763
2926
  className = text.slice(0, colonIndex)
2764
2927
  rightExpr = text.slice(colonIndex + 1)
2765
- parseExpr(rightExpr, vmodels, data) //决定是添加还是删除
2766
- if (!data.evaluator) {
2767
- log("debug: ms-class '" + (rightExpr || "").trim() + "' 不存在于VM中")
2768
- return false
2769
- } else {
2770
- data._evaluator = data.evaluator
2771
- data._args = data.args
2772
- }
2773
2928
  }
2774
- var hasExpr = rexpr.test(className) //比如ms-class="width{{w}}"的情况
2775
- if (!hasExpr) {
2776
- data.immobileClass = className
2929
+ if (!rexpr.test(text)) {
2930
+ className = quote(className)
2931
+ } else {
2932
+ className = stringifyExpr(className)
2777
2933
  }
2778
- parseExprProxy("", vmodels, data, (hasExpr ? scanExpr(className) : 0))
2934
+ binding.expr = "[" + className + "," + rightExpr + "]"
2779
2935
  } else {
2780
- data.immobileClass = data.oldStyle = data.param
2781
- parseExprProxy(text, vmodels, data)
2936
+ binding.expr = '[' + quote(oldStyle) + "," + text + "]"
2937
+ binding.oldStyle = oldStyle
2938
+ }
2939
+ var method = binding.type
2940
+ if (method === "hover" || method === "active") { //确保只绑定一次
2941
+ if (!binding.hasBindEvent) {
2942
+ var elem = binding.element
2943
+ var $elem = avalon(elem)
2944
+ var activate = "mouseenter" //在移出移入时切换类名
2945
+ var abandon = "mouseleave"
2946
+ if (method === "active") { //在聚焦失焦中切换类名
2947
+ elem.tabIndex = elem.tabIndex || -1
2948
+ activate = "mousedown"
2949
+ abandon = "mouseup"
2950
+ var fn0 = $elem.bind("mouseleave", function () {
2951
+ binding.toggleClass && $elem.removeClass(binding.newClass)
2952
+ })
2953
+ }
2954
+ }
2955
+
2956
+ var fn1 = $elem.bind(activate, function () {
2957
+ binding.toggleClass && $elem.addClass(binding.newClass)
2958
+ })
2959
+ var fn2 = $elem.bind(abandon, function () {
2960
+ binding.toggleClass && $elem.removeClass(binding.newClass)
2961
+ })
2962
+ binding.rollback = function () {
2963
+ $elem.unbind("mouseleave", fn0)
2964
+ $elem.unbind(activate, fn1)
2965
+ $elem.unbind(abandon, fn2)
2966
+ }
2967
+ binding.hasBindEvent = true
2782
2968
  }
2969
+ parseExprProxy(binding.expr, vmodels, binding)
2783
2970
  }
2784
2971
 
2785
- bindingExecutors ["class"] = function(val, elem, data) {
2786
- var $elem = avalon(elem),
2787
- method = data.type
2788
- if (method === "class" && data.oldStyle) { //如果是旧风格
2789
- $elem.toggleClass(data.oldStyle, !!val)
2790
- } else {
2791
- //如果存在冒号就有求值函数
2792
- data.toggleClass = data._evaluator ? !!data._evaluator.apply(elem, data._args) : true
2793
- data.newClass = data.immobileClass || val
2794
- if (data.oldClass && data.newClass !== data.oldClass) {
2795
- $elem.removeClass(data.oldClass)
2796
- }
2797
- data.oldClass = data.newClass
2798
- switch (method) {
2799
- case "class":
2800
- $elem.toggleClass(data.newClass, data.toggleClass)
2801
- break
2802
- case "hover":
2803
- case "active":
2804
- if (!data.hasBindEvent) { //确保只绑定一次
2805
- var activate = "mouseenter" //在移出移入时切换类名
2806
- var abandon = "mouseleave"
2807
- if (method === "active") { //在聚焦失焦中切换类名
2808
- elem.tabIndex = elem.tabIndex || -1
2809
- activate = "mousedown"
2810
- abandon = "mouseup"
2811
- var fn0 = $elem.bind("mouseleave", function() {
2812
- data.toggleClass && $elem.removeClass(data.newClass)
2813
- })
2814
- }
2815
- var fn1 = $elem.bind(activate, function() {
2816
- data.toggleClass && $elem.addClass(data.newClass)
2817
- })
2818
- var fn2 = $elem.bind(abandon, function() {
2819
- data.toggleClass && $elem.removeClass(data.newClass)
2820
- })
2821
- data.rollback = function() {
2822
- $elem.unbind("mouseleave", fn0)
2823
- $elem.unbind(activate, fn1)
2824
- $elem.unbind(abandon, fn2)
2825
- }
2826
- data.hasBindEvent = true
2827
- }
2828
- break;
2972
+ bindingExecutors["class"] = function (arr, elem, binding) {
2973
+ var $elem = avalon(elem)
2974
+ binding.newClass = arr[0]
2975
+ binding.toggleClass = !!arr[1]
2976
+ if (binding.oldClass && binding.newClass !== binding.oldClass) {
2977
+ $elem.removeClass(binding.oldClass)
2978
+ }
2979
+ binding.oldClass = binding.newClass
2980
+ if (binding.type === "class") {
2981
+ if (binding.oldStyle) {
2982
+ $elem.toggleClass(binding.oldStyle, !!arr[1])
2983
+ } else {
2984
+ $elem.toggleClass(binding.newClass, binding.toggleClass)
2829
2985
  }
2830
2986
  }
2987
+
2831
2988
  }
2832
2989
 
2833
- "hover,active".replace(rword, function(method) {
2990
+ "hover,active".replace(rword, function (method) {
2834
2991
  bindingHandlers[method] = bindingHandlers["class"]
2835
2992
  })
2836
2993
  //ms-controller绑定已经在scanTag 方法中实现
@@ -2839,21 +2996,20 @@ bindingExecutors ["class"] = function(val, elem, data) {
2839
2996
 
2840
2997
  // bindingHandlers.data 定义在if.js
2841
2998
  bindingExecutors.data = function(val, elem, data) {
2842
- var key = "data-" + data.param
2843
- if (val && typeof val === "object") {
2844
- elem[key] = val
2845
- } else {
2846
- elem.setAttribute(key, String(val))
2847
- }
2999
+ var key = "data-" + data.param
3000
+ if (val && typeof val === "object") {
3001
+ elem[key] = val
3002
+ } else {
3003
+ elem.setAttribute(key, String(val))
3004
+ }
2848
3005
  }
2849
-
2850
3006
  //双工绑定
2851
- var duplexBinding = bindingHandlers.duplex = function (data, vmodels) {
3007
+ var duplexBinding = bindingHandlers.duplex = function(data, vmodels) {
2852
3008
  var elem = data.element,
2853
- hasCast
2854
- parseExprProxy(data.value, vmodels, data, 0, 1)
3009
+ hasCast
3010
+ parseExprProxy(data.value, vmodels, data, 1)
2855
3011
 
2856
- data.changed = getBindingCallback(elem, "data-duplex-changed", vmodels) || noop
3012
+ data.changed = getBindingCallback(elem, "data-duplex-changed", vmodels) || noop
2857
3013
  if (data.evaluator && data.args) {
2858
3014
  var params = []
2859
3015
  var casting = oneObject("string,number,boolean,checked")
@@ -2863,7 +3019,7 @@ var duplexBinding = bindingHandlers.duplex = function (data, vmodels) {
2863
3019
  if (elem.msData) {
2864
3020
  elem.msData["ms-duplex"] = data.value
2865
3021
  }
2866
- data.param.replace(/\w+/g, function (name) {
3022
+ data.param.replace(/\w+/g, function(name) {
2867
3023
  if (/^(checkbox|radio)$/.test(elem.type) && /^(radio|checked)$/.test(name)) {
2868
3024
  if (name === "radio")
2869
3025
  log("ms-duplex-radio已经更名为ms-duplex-checked")
@@ -2886,14 +3042,14 @@ var duplexBinding = bindingHandlers.duplex = function (data, vmodels) {
2886
3042
  params.push("string")
2887
3043
  }
2888
3044
  data.param = params.join("-")
2889
- data.bound = function (type, callback) {
3045
+ data.bound = function(type, callback) {
2890
3046
  if (elem.addEventListener) {
2891
3047
  elem.addEventListener(type, callback, false)
2892
3048
  } else {
2893
3049
  elem.attachEvent("on" + type, callback)
2894
3050
  }
2895
3051
  var old = data.rollback
2896
- data.rollback = function () {
3052
+ data.rollback = function() {
2897
3053
  elem.avalonSetter = null
2898
3054
  avalon.unbind(elem, type, callback)
2899
3055
  old && old()
@@ -2910,29 +3066,30 @@ var duplexBinding = bindingHandlers.duplex = function (data, vmodels) {
2910
3066
  }
2911
3067
  }
2912
3068
  //不存在 bindingExecutors.duplex
2913
- function fixNull(val) {
2914
- return val == null ? "" : val
2915
- }
3069
+
3070
+ function fixNull(val) {
3071
+ return val == null ? "" : val
3072
+ }
2916
3073
  avalon.duplexHooks = {
2917
3074
  checked: {
2918
- get: function (val, data) {
3075
+ get: function(val, data) {
2919
3076
  return !data.element.oldValue
2920
3077
  }
2921
3078
  },
2922
3079
  string: {
2923
- get: function (val) { //同步到VM
3080
+ get: function(val) { //同步到VM
2924
3081
  return val
2925
3082
  },
2926
3083
  set: fixNull
2927
3084
  },
2928
3085
  "boolean": {
2929
- get: function (val) {
3086
+ get: function(val) {
2930
3087
  return val === "true"
2931
3088
  },
2932
3089
  set: fixNull
2933
3090
  },
2934
3091
  number: {
2935
- get: function (val, data) {
3092
+ get: function(val, data) {
2936
3093
  var number = parseFloat(val)
2937
3094
  if (-val === -number) {
2938
3095
  return number
@@ -2952,7 +3109,7 @@ avalon.duplexHooks = {
2952
3109
  }
2953
3110
 
2954
3111
  function pipe(val, data, action, e) {
2955
- data.param.replace(/\w+/g, function (name) {
3112
+ data.param.replace(/\w+/g, function(name) {
2956
3113
  var hook = avalon.duplexHooks[name]
2957
3114
  if (hook && typeof hook[action] === "function") {
2958
3115
  val = hook[action](val, data)
@@ -2963,44 +3120,41 @@ function pipe(val, data, action, e) {
2963
3120
 
2964
3121
  var TimerID, ribbon = []
2965
3122
 
2966
- avalon.tick = function (fn) {
2967
- if (ribbon.push(fn) === 1) {
2968
- TimerID = setInterval(ticker, 60)
3123
+ avalon.tick = function(fn) {
3124
+ if (ribbon.push(fn) === 1) {
3125
+ TimerID = setInterval(ticker, 60)
3126
+ }
2969
3127
  }
2970
- }
2971
3128
 
2972
- function ticker() {
2973
- for (var n = ribbon.length - 1; n >= 0; n--) {
2974
- var el = ribbon[n]
2975
- if (el() === false) {
2976
- ribbon.splice(n, 1)
3129
+ function ticker() {
3130
+ for (var n = ribbon.length - 1; n >= 0; n--) {
3131
+ var el = ribbon[n]
3132
+ if (el() === false) {
3133
+ ribbon.splice(n, 1)
3134
+ }
3135
+ }
3136
+ if (!ribbon.length) {
3137
+ clearInterval(TimerID)
2977
3138
  }
2978
3139
  }
2979
- if (!ribbon.length) {
2980
- clearInterval(TimerID)
2981
- }
2982
- }
2983
3140
 
2984
3141
  var watchValueInTimer = noop
2985
3142
  var rmsinput = /text|password|hidden/
2986
- new function () {// jshint ignore:line
2987
- try {//#272 IE9-IE11, firefox
3143
+ new function() { // jshint ignore:line
3144
+ try { //#272 IE9-IE11, firefox
2988
3145
  var setters = {}
2989
3146
  var aproto = HTMLInputElement.prototype
2990
3147
  var bproto = HTMLTextAreaElement.prototype
2991
- function newSetter(value) {// jshint ignore:line
2992
- if (avalon.contains(root, this)) {
3148
+ function newSetter(value) { // jshint ignore:line
2993
3149
  setters[this.tagName].call(this, value)
2994
- if (!rmsinput.test(this.type))
2995
- return
2996
- if (!this.msFocus && this.avalonSetter) {
3150
+ if (rmsinput.test(this.type) && !this.msFocus && this.avalonSetter) {
2997
3151
  this.avalonSetter()
2998
3152
  }
2999
- }
3000
3153
  }
3001
3154
  var inputProto = HTMLInputElement.prototype
3002
3155
  Object.getOwnPropertyNames(inputProto) //故意引发IE6-8等浏览器报错
3003
3156
  setters["INPUT"] = Object.getOwnPropertyDescriptor(aproto, "value").set
3157
+
3004
3158
  Object.defineProperty(aproto, "value", {
3005
3159
  set: newSetter
3006
3160
  })
@@ -3009,67 +3163,66 @@ new function () {// jshint ignore:line
3009
3163
  set: newSetter
3010
3164
  })
3011
3165
  } catch (e) {
3166
+ //在chrome 43中 ms-duplex终于不需要使用定时器实现双向绑定了
3167
+ // http://updates.html5rocks.com/2015/04/DOM-attributes-now-on-the-prototype
3168
+ // https://docs.google.com/document/d/1jwA8mtClwxI-QJuHT7872Z0pxpZz8PBkf2bGAbsUtqs/edit?pli=1
3012
3169
  watchValueInTimer = avalon.tick
3013
3170
  }
3014
- }// jshint ignore:line
3015
-
3016
-
3171
+ } // jshint ignore:line
3017
3172
  //处理radio, checkbox, text, textarea, password
3018
- duplexBinding.INPUT = function (element, evaluator, data) {
3173
+ duplexBinding.INPUT = function(element, evaluator, data) {
3019
3174
  var $type = element.type,
3020
- bound = data.bound,
3021
- $elem = avalon(element),
3022
- composing = false
3023
- function callback(value) {
3024
- data.changed.call(this, value, data)
3025
- }
3026
- function compositionStart() {
3027
- composing = true
3028
- }
3029
- function compositionEnd() {
3175
+ bound = data.bound,
3176
+ $elem = avalon(element),
3030
3177
  composing = false
3031
- }
3032
- //当value变化时改变model的值
3033
3178
 
3034
- var updateVModel = function () {
3035
- if (composing)//处理中文输入法在minlengh下引发的BUG
3179
+ function callback(value) {
3180
+ data.changed.call(this, value, data)
3181
+ }
3182
+
3183
+ function compositionStart() {
3184
+ composing = true
3185
+ }
3186
+
3187
+ function compositionEnd() {
3188
+ composing = false
3189
+ }
3190
+ //当value变化时改变model的值
3191
+
3192
+ var updateVModel = function() {
3193
+ var val = element.value //防止递归调用形成死循环
3194
+ if (composing || val === element.oldValue) //处理中文输入法在minlengh下引发的BUG
3036
3195
  return
3037
- var val = element.oldValue = element.value //防止递归调用形成死循环
3038
3196
  var lastValue = data.pipe(val, data, "get")
3039
- if ($elem.data("duplex-observe") !== false) {
3197
+ if ($elem.data("duplexObserve") !== false) {
3040
3198
  evaluator(lastValue)
3041
3199
  callback.call(element, lastValue)
3042
- if ($elem.data("duplex-focus")) {
3043
- avalon.nextTick(function () {
3044
- element.focus()
3045
- })
3046
- }
3047
3200
  }
3048
3201
  }
3049
3202
  //当model变化时,它就会改变value的值
3050
- data.handler = function () {
3203
+ data.handler = function() {
3051
3204
  var val = data.pipe(evaluator(), data, "set") + ""
3052
3205
  if (val !== element.oldValue) {
3053
- element.value = val
3206
+ element.value = element.oldValue = val
3054
3207
  }
3055
3208
  }
3056
3209
  if (data.isChecked || $type === "radio") {
3057
- updateVModel = function () {
3058
- if ($elem.data("duplex-observe") !== false) {
3210
+ updateVModel = function() {
3211
+ if ($elem.data("duplexObserve") !== false) {
3059
3212
  var lastValue = data.pipe(element.value, data, "get")
3060
3213
  evaluator(lastValue)
3061
3214
  callback.call(element, lastValue)
3062
3215
  }
3063
3216
  }
3064
- data.handler = function () {
3217
+ data.handler = function() {
3065
3218
  var val = evaluator()
3066
- var checked = data.isChecked ? !!val : val + "" === element.value
3219
+ var checked = data.isChecked ? !! val : val + "" === element.value
3067
3220
  element.checked = element.oldValue = checked
3068
3221
  }
3069
3222
  bound("click", updateVModel)
3070
3223
  } else if ($type === "checkbox") {
3071
- updateVModel = function () {
3072
- if ($elem.data("duplex-observe") !== false) {
3224
+ updateVModel = function() {
3225
+ if ($elem.data("duplexObserve") !== false) {
3073
3226
  var method = element.checked ? "ensure" : "remove"
3074
3227
  var array = evaluator()
3075
3228
  if (!Array.isArray(array)) {
@@ -3080,7 +3233,7 @@ duplexBinding.INPUT = function (element, evaluator, data) {
3080
3233
  callback.call(element, array)
3081
3234
  }
3082
3235
  }
3083
- data.handler = function () {
3236
+ data.handler = function() {
3084
3237
  var array = [].concat(evaluator()) //强制转换为数组
3085
3238
  element.checked = array.indexOf(data.pipe(element.value, data, "get")) > -1
3086
3239
  }
@@ -3090,7 +3243,7 @@ duplexBinding.INPUT = function (element, evaluator, data) {
3090
3243
  if (element.attributes["data-event"]) {
3091
3244
  log("data-event指令已经废弃,请改用data-duplex-event")
3092
3245
  }
3093
- events.replace(rword, function (name) {
3246
+ events.replace(rword, function(name) {
3094
3247
  switch (name) {
3095
3248
  case "input":
3096
3249
  bound("input", updateVModel)
@@ -3105,14 +3258,14 @@ duplexBinding.INPUT = function (element, evaluator, data) {
3105
3258
  break
3106
3259
  }
3107
3260
  })
3108
- bound("focus", function () {
3261
+ bound("focus", function() {
3109
3262
  element.msFocus = true
3110
3263
  })
3111
- bound("blur", function () {
3264
+ bound("blur", function() {
3112
3265
  element.msFocus = false
3113
3266
  })
3114
3267
  if (rmsinput.test($type)) {
3115
- watchValueInTimer(function () {
3268
+ watchValueInTimer(function() {
3116
3269
  if (root.contains(element)) {
3117
3270
  if (!element.msFocus && element.oldValue !== element.value) {
3118
3271
  updateVModel()
@@ -3127,28 +3280,29 @@ duplexBinding.INPUT = function (element, evaluator, data) {
3127
3280
  }
3128
3281
 
3129
3282
  element.oldValue = element.value
3130
- registerSubscriber(data)
3283
+ avalon.injectBinding(data)
3131
3284
  callback.call(element, element.value)
3132
3285
  }
3133
3286
  duplexBinding.TEXTAREA = duplexBinding.INPUT
3134
3287
  duplexBinding.SELECT = function(element, evaluator, data) {
3135
3288
  var $elem = avalon(element)
3136
- function updateVModel() {
3137
- if ($elem.data("duplex-observe") !== false) {
3138
- var val = $elem.val() //字符串或字符串数组
3139
- if (Array.isArray(val)) {
3140
- val = val.map(function(v) {
3141
- return data.pipe(v, data, "get")
3142
- })
3143
- } else {
3144
- val = data.pipe(val, data, "get")
3145
- }
3146
- if (val + "" !== element.oldValue) {
3147
- evaluator(val)
3289
+
3290
+ function updateVModel() {
3291
+ if ($elem.data("duplexObserve") !== false) {
3292
+ var val = $elem.val() //字符串或字符串数组
3293
+ if (Array.isArray(val)) {
3294
+ val = val.map(function(v) {
3295
+ return data.pipe(v, data, "get")
3296
+ })
3297
+ } else {
3298
+ val = data.pipe(val, data, "get")
3299
+ }
3300
+ if (val + "" !== element.oldValue) {
3301
+ evaluator(val)
3302
+ }
3303
+ data.changed.call(element, val, data)
3148
3304
  }
3149
- data.changed.call(element, val, data)
3150
3305
  }
3151
- }
3152
3306
  data.handler = function() {
3153
3307
  var val = evaluator()
3154
3308
  val = val && val.$model || val
@@ -3169,74 +3323,77 @@ duplexBinding.SELECT = function(element, evaluator, data) {
3169
3323
  }
3170
3324
  }
3171
3325
  data.bound("change", updateVModel)
3172
- checkScan(element, function() {
3173
- registerSubscriber(data)
3326
+ element.msCallback = function() {
3327
+ avalon.injectBinding(data)
3174
3328
  data.changed.call(element, evaluator(), data)
3175
- }, NaN)
3329
+ }
3176
3330
  }
3177
-
3178
-
3179
3331
  // bindingHandlers.html 定义在if.js
3180
- bindingExecutors.html = function(val, elem, data) {
3181
- val = val == null ? "" : val
3182
- var isHtmlFilter = "group" in data
3332
+ bindingExecutors.html = function (val, elem, data) {
3333
+ var isHtmlFilter = elem.nodeType !== 1
3183
3334
  var parent = isHtmlFilter ? elem.parentNode : elem
3184
3335
  if (!parent)
3185
3336
  return
3186
- if (val.nodeType === 11) { //将val转换为文档碎片
3187
- var fragment = val
3337
+ val = val == null ? "" : val
3338
+ if (data.oldText !== val) {
3339
+ data.oldText = val
3340
+ } else {
3341
+ return
3342
+ }
3343
+ if (elem.nodeType === 3) {
3344
+ var signature = generateID("html")
3345
+ parent.insertBefore(DOC.createComment(signature), elem)
3346
+ data.element = DOC.createComment(signature + ":end")
3347
+ parent.replaceChild(data.element, elem)
3348
+ elem = data.element
3349
+ }
3350
+ if (typeof val !== "object") {//string, number, boolean
3351
+ var fragment = avalon.parseHTML(String(val))
3352
+ } else if (val.nodeType === 11) { //将val转换为文档碎片
3353
+ fragment = val
3188
3354
  } else if (val.nodeType === 1 || val.item) {
3189
- var nodes = val.nodeType === 1 ? val.childNodes : val.item ? val : []
3190
- fragment = hyperspace.cloneNode(true)
3355
+ var nodes = val.nodeType === 1 ? val.childNodes : val.item
3356
+ fragment = avalonFragment.cloneNode(true)
3191
3357
  while (nodes[0]) {
3192
3358
  fragment.appendChild(nodes[0])
3193
3359
  }
3194
- } else {
3195
- fragment = avalon.parseHTML(val)
3196
3360
  }
3361
+
3362
+ nodes = avalon.slice(fragment.childNodes)
3197
3363
  //插入占位符, 如果是过滤器,需要有节制地移除指定的数量,如果是html指令,直接清空
3198
- var comment = DOC.createComment("ms-html")
3199
3364
  if (isHtmlFilter) {
3200
- parent.insertBefore(comment, elem)
3201
- var n = data.group, i = 1
3202
- while (i < n) {
3203
- var node = elem.nextSibling
3204
- if (node) {
3365
+ var endValue = elem.nodeValue.slice(0, -4)
3366
+ while (true) {
3367
+ var node = elem.previousSibling
3368
+ if (!node || node.nodeType === 8 && node.nodeValue === endValue) {
3369
+ break
3370
+ } else {
3205
3371
  parent.removeChild(node)
3206
- i++
3207
3372
  }
3208
3373
  }
3209
- parent.removeChild(elem)
3210
- data.element = comment //防止被CG
3374
+ parent.insertBefore(fragment, elem)
3211
3375
  } else {
3212
- avalon.clearHTML(parent).appendChild(comment)
3213
- }
3214
- if (isHtmlFilter) {
3215
- data.group = fragment.childNodes.length || 1
3216
- }
3217
- nodes = avalon.slice(fragment.childNodes)
3218
- if (nodes[0]) {
3219
- if (comment.parentNode)
3220
- comment.parentNode.replaceChild(fragment, comment)
3221
- if (isHtmlFilter) {
3222
- data.element = nodes[0]
3223
- }
3376
+ avalon.clearHTML(elem).appendChild(fragment)
3224
3377
  }
3225
3378
  scanNodeArray(nodes, data.vmodels)
3226
3379
  }
3227
-
3228
3380
  bindingHandlers["if"] =
3229
- bindingHandlers.data =
3230
- bindingHandlers.text =
3231
- bindingHandlers.html =
3232
- function(data, vmodels) {
3233
- parseExprProxy(data.value, vmodels, data)
3234
- }
3381
+ bindingHandlers.data =
3382
+ bindingHandlers.text =
3383
+ bindingHandlers.html =
3384
+ function(data, vmodels) {
3385
+ parseExprProxy(data.value, vmodels, data)
3386
+ }
3235
3387
 
3236
3388
  bindingExecutors["if"] = function(val, elem, data) {
3389
+ try {
3390
+ if(!elem.parentNode) return
3391
+ } catch(e) {return}
3237
3392
  if (val) { //插回DOM树
3238
3393
  if (elem.nodeType === 8) {
3239
3394
  elem.parentNode.replaceChild(data.template, elem)
3395
+ elem.ifRemove = null
3396
+ // animate.enter(data.template, elem.parentNode)
3240
3397
  elem = data.element = data.template //这时可能为null
3241
3398
  }
3242
3399
  if (elem.getAttribute(data.name)) {
@@ -3248,6 +3405,8 @@ bindingExecutors["if"] = function(val, elem, data) {
3248
3405
  if (elem.nodeType === 1) {
3249
3406
  var node = data.element = DOC.createComment("ms-if")
3250
3407
  elem.parentNode.replaceChild(node, elem)
3408
+ elem.ifRemove = node
3409
+ // animate.leave(elem, node.parentNode, node)
3251
3410
  data.template = elem //元素节点
3252
3411
  ifGroup.appendChild(elem)
3253
3412
  data.rollback = function() {
@@ -3258,8 +3417,6 @@ bindingExecutors["if"] = function(val, elem, data) {
3258
3417
  }
3259
3418
  }
3260
3419
  }
3261
-
3262
-
3263
3420
  //ms-important绑定已经在scanTag 方法中实现
3264
3421
  //ms-include绑定已由ms-attr绑定实现
3265
3422
 
@@ -3303,11 +3460,9 @@ bindingExecutors.on = function(callback, elem, data) {
3303
3460
  }
3304
3461
  }
3305
3462
  }
3306
-
3307
-
3308
- bindingHandlers.repeat = function(data, vmodels) {
3463
+ bindingHandlers.repeat = function (data, vmodels) {
3309
3464
  var type = data.type
3310
- parseExprProxy(data.value, vmodels, data, 0, 1)
3465
+ parseExprProxy(data.value, vmodels, data, 1)
3311
3466
  data.proxies = []
3312
3467
  var freturn = false
3313
3468
  try {
@@ -3316,11 +3471,12 @@ bindingHandlers.repeat = function(data, vmodels) {
3316
3471
  if (xtype !== "object" && xtype !== "array") {
3317
3472
  freturn = true
3318
3473
  avalon.log("warning:" + data.value + "只能是对象或数组")
3474
+ } else {
3475
+ data.xtype = xtype
3319
3476
  }
3320
3477
  } catch (e) {
3321
3478
  freturn = true
3322
3479
  }
3323
-
3324
3480
  var arr = data.value.split(".") || []
3325
3481
  if (arr.length > 1) {
3326
3482
  arr.pop()
@@ -3334,41 +3490,43 @@ bindingHandlers.repeat = function(data, vmodels) {
3334
3490
  }
3335
3491
  }
3336
3492
  }
3493
+
3337
3494
  var elem = data.element
3338
- elem.removeAttribute(data.name)
3339
-
3340
- data.sortedCallback = getBindingCallback(elem, "data-with-sorted", vmodels)
3341
- data.renderedCallback = getBindingCallback(elem, "data-" + type + "-rendered", vmodels)
3342
- var signature = generateID(type)
3343
- var comment = data.element = DOC.createComment(signature + ":end")
3344
- data.clone = DOC.createComment(signature)
3345
- hyperspace.appendChild(comment)
3346
-
3347
- if (type === "each" || type === "with") {
3348
- data.template = elem.innerHTML.trim()
3349
- avalon.clearHTML(elem).appendChild(comment)
3350
- } else {
3351
- data.template = elem.outerHTML.trim()
3352
- elem.parentNode.replaceChild(comment, elem)
3353
- }
3354
- data.template = avalon.parseHTML(data.template)
3355
- data.rollback = function() {
3356
- var elem = data.element
3357
- if (!elem)
3358
- return
3359
- bindingExecutors.repeat.call(data, "clear")
3360
- var parentNode = elem.parentNode
3361
- var content = data.template
3362
- var target = content.firstChild
3363
- parentNode.replaceChild(content, elem)
3364
- var start = data.$stamp
3365
- start && start.parentNode && start.parentNode.removeChild(start)
3366
- target = data.element = data.type === "repeat" ? target : parentNode
3495
+ if (elem.nodeType === 1) {
3496
+ elem.removeAttribute(data.name)
3497
+ data.sortedCallback = getBindingCallback(elem, "data-with-sorted", vmodels)
3498
+ data.renderedCallback = getBindingCallback(elem, "data-" + type + "-rendered", vmodels)
3499
+ var signature = generateID(type)
3500
+ var start = DOC.createComment(signature)
3501
+ var end = DOC.createComment(signature + ":end")
3502
+ data.signature = signature
3503
+ data.template = avalonFragment.cloneNode(false)
3504
+ if (type === "repeat") {
3505
+ var parent = elem.parentNode
3506
+ parent.replaceChild(end, elem)
3507
+ parent.insertBefore(start, end)
3508
+ data.template.appendChild(elem)
3509
+ } else {
3510
+ while (elem.firstChild) {
3511
+ data.template.appendChild(elem.firstChild)
3512
+ }
3513
+ elem.appendChild(start)
3514
+ elem.appendChild(end)
3515
+ }
3516
+ data.element = end
3517
+ data.handler = bindingExecutors.repeat
3518
+ data.rollback = function () {
3519
+ var elem = data.element
3520
+ if (!elem)
3521
+ return
3522
+ data.handler("clear")
3523
+ }
3367
3524
  }
3525
+
3368
3526
  if (freturn) {
3369
3527
  return
3370
3528
  }
3371
- data.handler = bindingExecutors.repeat
3529
+
3372
3530
  data.$outer = {}
3373
3531
  var check0 = "$key"
3374
3532
  var check1 = "$val"
@@ -3376,6 +3534,7 @@ bindingHandlers.repeat = function(data, vmodels) {
3376
3534
  check0 = "$first"
3377
3535
  check1 = "$last"
3378
3536
  }
3537
+
3379
3538
  for (i = 0; v = vmodels[i++]; ) {
3380
3539
  if (v.hasOwnProperty(check0) && v.hasOwnProperty(check1)) {
3381
3540
  data.$outer = v
@@ -3384,81 +3543,101 @@ bindingHandlers.repeat = function(data, vmodels) {
3384
3543
  }
3385
3544
  var $events = $repeat.$events
3386
3545
  var $list = ($events || {})[subscribers]
3387
- if ($list && avalon.Array.ensure($list, data)) {
3388
- addSubscribers(data, $list)
3389
- }
3546
+ injectDependency($list, data)
3390
3547
  if (xtype === "object") {
3391
- data.$with = true
3392
- var pool = !$events ? {} : $events.$withProxyPool || ($events.$withProxyPool = {})
3393
- data.handler("append", $repeat, pool)
3548
+ data.handler("append")
3394
3549
  } else if ($repeat.length) {
3395
3550
  data.handler("add", 0, $repeat.length)
3396
3551
  }
3397
3552
  }
3398
3553
 
3399
- bindingExecutors.repeat = function(method, pos, el) {
3554
+ bindingExecutors.repeat = function (method, pos, el) {
3555
+ var data = this
3556
+ if (!method && data.xtype) {
3557
+ var old = data.$repeat
3558
+ var neo = data.evaluator.apply(0, data.args || [])
3559
+
3560
+ if (data.xtype === "array") {
3561
+ if (old.length === neo.length) {
3562
+ return
3563
+ }
3564
+ method = "add"
3565
+ pos = 0
3566
+ data.$repeat = neo
3567
+ el = neo.length
3568
+ } else {
3569
+ if (keysVM(old).join(";;") === keysVM(neo).join(";;")) {
3570
+ return
3571
+ }
3572
+ method = "append"
3573
+ data.$repeat = neo
3574
+ }
3575
+ }
3400
3576
  if (method) {
3401
- var data = this
3577
+ var start, fragment
3402
3578
  var end = data.element
3579
+ var comments = getComments(data)
3403
3580
  var parent = end.parentNode
3404
3581
  var proxies = data.proxies
3405
- var transation = hyperspace.cloneNode(false)
3582
+ var transation = avalonFragment.cloneNode(false)
3406
3583
  switch (method) {
3407
- case "add": //在pos位置后添加el数组(pos为数字,el为数组)
3584
+ case "add": //在pos位置后添加el数组(pos为插入位置,el为要插入的个数)
3408
3585
  var n = pos + el
3409
- var array = data.$repeat
3410
- var last = array.length - 1
3411
- var fragments = [], fragment
3412
- var start = locateNode(data, pos)
3586
+ var fragments = []
3413
3587
  for (var i = pos; i < n; i++) {
3414
3588
  var proxy = eachProxyAgent(i, data)
3415
3589
  proxies.splice(i, 0, proxy)
3416
3590
  shimController(data, transation, proxy, fragments)
3417
3591
  }
3418
- parent.insertBefore(transation, start)
3592
+ parent.insertBefore(transation, comments[pos] || end)
3419
3593
  for (i = 0; fragment = fragments[i++]; ) {
3420
3594
  scanNodeArray(fragment.nodes, fragment.vmodels)
3421
3595
  fragment.nodes = fragment.vmodels = null
3422
3596
  }
3597
+
3423
3598
  break
3424
3599
  case "del": //将pos后的el个元素删掉(pos, el都是数字)
3425
- start = proxies[pos].$stamp
3426
- end = locateNode(data, pos + el)
3427
- sweepNodes(start, end)
3600
+ sweepNodes(comments[pos], comments[pos + el] || end)
3428
3601
  var removed = proxies.splice(pos, el)
3429
3602
  recycleProxies(removed, "each")
3430
3603
  break
3431
3604
  case "clear":
3432
- var check = data.$stamp || proxies[0]
3433
- if (check) {
3434
- start = check.$stamp || check
3605
+ start = comments[0]
3606
+ if (start) {
3435
3607
  sweepNodes(start, end)
3608
+ if (data.xtype === "object") {
3609
+ parent.insertBefore(start, end)
3610
+ }else{
3611
+ recycleProxies(proxies, "each")
3612
+ }
3436
3613
  }
3437
- recycleProxies(proxies, "each")
3438
3614
  break
3439
3615
  case "move":
3440
- start = proxies[0].$stamp
3441
- var signature = start.nodeValue
3442
- var rooms = []
3443
- var room = [], node
3444
- sweepNodes(start, end, function() {
3445
- room.unshift(this)
3446
- if (this.nodeValue === signature) {
3447
- rooms.unshift(room)
3448
- room = []
3449
- }
3450
- })
3451
- sortByIndex(proxies, pos)
3452
- sortByIndex(rooms, pos)
3453
- while (room = rooms.shift()) {
3454
- while (node = room.shift()) {
3455
- transation.appendChild(node)
3616
+ start = comments[0]
3617
+ if (start) {
3618
+ var signature = start.nodeValue
3619
+ var rooms = []
3620
+ var room = [],
3621
+ node
3622
+ sweepNodes(start, end, function () {
3623
+ room.unshift(this)
3624
+ if (this.nodeValue === signature) {
3625
+ rooms.unshift(room)
3626
+ room = []
3627
+ }
3628
+ })
3629
+ sortByIndex(rooms, pos)
3630
+ sortByIndex(proxies, pos)
3631
+ while (room = rooms.shift()) {
3632
+ while (node = room.shift()) {
3633
+ transation.appendChild(node)
3634
+ }
3456
3635
  }
3636
+ parent.insertBefore(transation, end)
3457
3637
  }
3458
- parent.insertBefore(transation, end)
3459
3638
  break
3460
3639
  case "index": //将proxies中的第pos个起的所有元素重新索引
3461
- last = proxies.length - 1
3640
+ var last = proxies.length - 1
3462
3641
  for (; el = proxies[pos]; pos++) {
3463
3642
  el.$index = pos
3464
3643
  el.$first = pos === 0
@@ -3468,15 +3647,23 @@ bindingExecutors.repeat = function(method, pos, el) {
3468
3647
  case "set": //将proxies中的第pos个元素的VM设置为el(pos为数字,el任意)
3469
3648
  proxy = proxies[pos]
3470
3649
  if (proxy) {
3471
- notifySubscribers(proxy.$events.$index)
3650
+ fireDependencies(proxy.$events[data.param || "el"])
3472
3651
  }
3473
- return
3474
- case "append": //将pos的键值对从el中取出(pos为一个普通对象,el为预先生成好的代理VM对象池)
3475
- var pool = el
3652
+ break
3653
+ case "append":
3654
+ var object = data.$repeat //原来第2参数, 被循环对象
3655
+ var pool = Array.isArray(proxies) ||!proxies ? {}: proxies //代理对象组成的hash
3656
+ data.proxies = pool
3476
3657
  var keys = []
3477
3658
  fragments = []
3478
- for (var key in pos) { //得到所有键名
3479
- if (pos.hasOwnProperty(key) && key !== "hasOwnProperty") {
3659
+ for (var key in pool) {
3660
+ if (!object.hasOwnProperty(key)) {
3661
+ proxyRecycler(pool[key], withProxyPool) //去掉之前的代理VM
3662
+ delete(pool[key])
3663
+ }
3664
+ }
3665
+ for (key in object) { //得到所有键名
3666
+ if (object.hasOwnProperty(key) && key !== "hasOwnProperty") {
3480
3667
  keys.push(key)
3481
3668
  }
3482
3669
  }
@@ -3488,14 +3675,11 @@ bindingExecutors.repeat = function(method, pos, el) {
3488
3675
  }
3489
3676
  for (i = 0; key = keys[i++]; ) {
3490
3677
  if (key !== "hasOwnProperty") {
3491
- if (!pool[key]) {
3492
- pool[key] = withProxyAgent(key, data)
3493
- }
3678
+ pool[key] = withProxyAgent(pool[key], key, data)
3494
3679
  shimController(data, transation, pool[key], fragments)
3495
3680
  }
3496
3681
  }
3497
- var comment = data.$stamp = data.clone
3498
- parent.insertBefore(comment, end)
3682
+
3499
3683
  parent.insertBefore(transation, end)
3500
3684
  for (i = 0; fragment = fragments[i++]; ) {
3501
3685
  scanNodeArray(fragment.nodes, fragment.vmodels)
@@ -3503,29 +3687,26 @@ bindingExecutors.repeat = function(method, pos, el) {
3503
3687
  }
3504
3688
  break
3505
3689
  }
3690
+ if (!data.$repeat || data.$repeat.hasOwnProperty("$lock")) //IE6-8 VBScript对象会报错, 有时候data.$repeat不存在
3691
+ return
3506
3692
  if (method === "clear")
3507
3693
  method = "del"
3508
3694
  var callback = data.renderedCallback || noop,
3509
3695
  args = arguments
3510
- checkScan(parent, function() {
3511
- callback.apply(parent, args)
3512
- if (parent.oldValue && parent.tagName === "SELECT") { //fix #503
3513
- avalon(parent).val(parent.oldValue.split(","))
3514
- }
3515
- }, NaN)
3696
+ if (parent.oldValue && parent.tagName === "SELECT") { //fix #503
3697
+ avalon(parent).val(parent.oldValue.split(","))
3698
+ }
3699
+ callback.apply(parent, args)
3516
3700
  }
3517
3701
  }
3518
-
3519
- "with,each".replace(rword, function(name) {
3702
+ "with,each".replace(rword, function (name) {
3520
3703
  bindingHandlers[name] = bindingHandlers.repeat
3521
3704
  })
3522
3705
 
3523
3706
  function shimController(data, transation, proxy, fragments) {
3524
3707
  var content = data.template.cloneNode(true)
3525
3708
  var nodes = avalon.slice(content.childNodes)
3526
- if (proxy.$stamp) {
3527
- content.insertBefore(proxy.$stamp, content.firstChild)
3528
- }
3709
+ content.insertBefore(DOC.createComment(data.signature), content.firstChild)
3529
3710
  transation.appendChild(content)
3530
3711
  var nv = [proxy].concat(data.vmodels)
3531
3712
  var fragment = {
@@ -3535,11 +3716,21 @@ function shimController(data, transation, proxy, fragments) {
3535
3716
  fragments.push(fragment)
3536
3717
  }
3537
3718
 
3538
- function locateNode(data, pos) {
3539
- var proxy = data.proxies[pos]
3540
- return proxy ? proxy.$stamp : data.element
3719
+ function getComments(data) {
3720
+ var ret = []
3721
+ var nodes = data.element.parentNode.childNodes
3722
+ for (var i = 0, node; node = nodes[i++]; ) {
3723
+ if (node.nodeValue === data.signature) {
3724
+ ret.push(node)
3725
+ } else if (node.nodeValue === data.signature + ":end") {
3726
+ break
3727
+ }
3728
+ }
3729
+ return ret
3541
3730
  }
3542
3731
 
3732
+
3733
+ //移除掉start与end之间的节点(保留end)
3543
3734
  function sweepNodes(start, end, callback) {
3544
3735
  while (true) {
3545
3736
  var node = end.previousSibling
@@ -3556,24 +3747,89 @@ function sweepNodes(start, end, callback) {
3556
3747
  // 为ms-each,ms-with, ms-repeat会创建一个代理VM,
3557
3748
  // 通过它们保持一个下上文,让用户能调用$index,$first,$last,$remove,$key,$val,$outer等属性与方法
3558
3749
  // 所有代理VM的产生,消费,收集,存放通过xxxProxyFactory,xxxProxyAgent, recycleProxies,xxxProxyPool实现
3559
- var eachProxyPool = []
3560
3750
  var withProxyPool = []
3751
+ function withProxyFactory() {
3752
+ var proxy = modelFactory({
3753
+ $key: "",
3754
+ $outer: {},
3755
+ $host: {},
3756
+ $val: {
3757
+ get: function () {
3758
+ return this.$host[this.$key]
3759
+ },
3760
+ set: function (val) {
3761
+ this.$host[this.$key] = val
3762
+ }
3763
+ }
3764
+ }, {
3765
+ $val: 1
3766
+ })
3767
+ proxy.$id = generateID("$proxy$with")
3768
+ return proxy
3769
+ }
3770
+
3771
+ function withProxyAgent(proxy, key, data) {
3772
+ proxy = proxy || withProxyPool.pop()
3773
+ if (!proxy) {
3774
+ proxy = withProxyFactory()
3775
+ } else {
3776
+ proxy.$reinitialize()
3777
+ }
3778
+ var host = data.$repeat
3779
+ proxy.$key = key
3780
+
3781
+ proxy.$host = host
3782
+ proxy.$outer = data.$outer
3783
+ if (host.$events) {
3784
+ proxy.$events.$val = host.$events[key]
3785
+ } else {
3786
+ proxy.$events = {}
3787
+ }
3788
+ return proxy
3789
+ }
3790
+
3791
+
3792
+ function recycleProxies(proxies) {
3793
+ eachProxyRecycler(proxies)
3794
+ }
3795
+ function eachProxyRecycler(proxies) {
3796
+ proxies.forEach(function (proxy) {
3797
+ proxyRecycler(proxy, eachProxyPool)
3798
+ })
3799
+ proxies.length = 0
3800
+ }
3801
+
3802
+
3803
+ var eachProxyPool = []
3561
3804
  function eachProxyFactory(name) {
3562
3805
  var source = {
3563
3806
  $host: [],
3564
3807
  $outer: {},
3565
- $stamp: 1,
3566
3808
  $index: 0,
3567
3809
  $first: false,
3568
3810
  $last: false,
3569
3811
  $remove: avalon.noop
3570
3812
  }
3571
3813
  source[name] = {
3572
- get: function() {
3573
- return this.$host[this.$index]
3814
+ get: function () {
3815
+ var e = this.$events
3816
+ var array = e.$index
3817
+ e.$index = e[name] //#817 通过$index为el收集依赖
3818
+ try {
3819
+ return this.$host[this.$index]
3820
+ } finally {
3821
+ e.$index = array
3822
+ }
3574
3823
  },
3575
- set: function(val) {
3576
- this.$host.set(this.$index, val)
3824
+ set: function (val) {
3825
+ try {
3826
+ var e = this.$events
3827
+ var array = e.$index
3828
+ e.$index = []
3829
+ this.$host.set(this.$index, val)
3830
+ } finally {
3831
+ e.$index = array
3832
+ }
3577
3833
  }
3578
3834
  }
3579
3835
  var second = {
@@ -3582,14 +3838,13 @@ function eachProxyFactory(name) {
3582
3838
  $index: 1
3583
3839
  }
3584
3840
  var proxy = modelFactory(source, second)
3585
- var e = proxy.$events
3586
- e[name] = e.$first = e.$last = e.$index
3587
3841
  proxy.$id = generateID("$proxy$each")
3588
3842
  return proxy
3589
3843
  }
3590
3844
 
3591
3845
  function eachProxyAgent(index, data) {
3592
- var param = data.param || "el", proxy
3846
+ var param = data.param || "el",
3847
+ proxy
3593
3848
  for (var i = 0, n = eachProxyPool.length; i < n; i++) {
3594
3849
  var candidate = eachProxyPool[i]
3595
3850
  if (candidate && candidate.hasOwnProperty(param)) {
@@ -3607,93 +3862,45 @@ function eachProxyAgent(index, data) {
3607
3862
  proxy.$last = index === last
3608
3863
  proxy.$host = host
3609
3864
  proxy.$outer = data.$outer
3610
- proxy.$stamp = data.clone.cloneNode(false)
3611
- proxy.$remove = function() {
3865
+ proxy.$remove = function () {
3612
3866
  return host.removeAt(proxy.$index)
3613
3867
  }
3614
3868
  return proxy
3615
3869
  }
3616
3870
 
3617
- function withProxyFactory() {
3618
- var proxy = modelFactory({
3619
- $key: "",
3620
- $outer: {},
3621
- $host: {},
3622
- $val: {
3623
- get: function() {
3624
- return this.$host[this.$key]
3625
- },
3626
- set: function(val) {
3627
- this.$host[this.$key] = val
3628
- }
3629
- }
3630
- }, {
3631
- $val: 1
3632
- })
3633
- proxy.$id = generateID("$proxy$with")
3634
- return proxy
3635
- }
3636
3871
 
3637
- function withProxyAgent(key, data) {
3638
- var proxy = withProxyPool.pop()
3639
- if (!proxy) {
3640
- proxy = withProxyFactory()
3872
+ function proxyRecycler(proxy, proxyPool) {
3873
+ for (var i in proxy.$events) {
3874
+ var arr = proxy.$events[i]
3875
+ if (Array.isArray(arr)) {
3876
+ arr.forEach(function (data) {
3877
+ if (typeof data === "object")
3878
+ disposeData(data)
3879
+ })// jshint ignore:line
3880
+ arr.length = 0
3881
+ }
3641
3882
  }
3642
- var host = data.$repeat
3643
- proxy.$key = key
3644
- proxy.$host = host
3645
- proxy.$outer = data.$outer
3646
- if (host.$events) {
3647
- proxy.$events.$val = host.$events[key]
3648
- } else {
3649
- proxy.$events = {}
3883
+ proxy.$host = proxy.$outer = {}
3884
+ if (proxyPool.unshift(proxy) > kernel.maxRepeatSize) {
3885
+ proxyPool.pop()
3650
3886
  }
3651
- return proxy
3652
3887
  }
3653
3888
 
3654
- function recycleProxies(proxies, type) {
3655
- var proxyPool = type === "each" ? eachProxyPool : withProxyPool
3656
- avalon.each(proxies, function(key, proxy) {
3657
- if (proxy.$events) {
3658
- for (var i in proxy.$events) {
3659
- if (Array.isArray(proxy.$events[i])) {
3660
- proxy.$events[i].forEach(function(data) {
3661
- if (typeof data === "object")
3662
- disposeData(data)
3663
- })// jshint ignore:line
3664
- proxy.$events[i].length = 0
3665
- }
3666
- }
3667
- proxy.$host = proxy.$outer = {}
3668
- if (proxyPool.unshift(proxy) > kernel.maxRepeatSize) {
3669
- proxyPool.pop()
3670
- }
3671
- }
3672
- })
3673
- if (type === "each")
3674
- proxies.length = 0
3675
- }
3676
-
3677
-
3678
-
3679
-
3680
3889
  /*********************************************************************
3681
3890
  * 各种指令 *
3682
3891
  **********************************************************************/
3683
3892
  //ms-skip绑定已经在scanTag 方法中实现
3684
3893
  // bindingHandlers.text 定义在if.js
3685
3894
  bindingExecutors.text = function(val, elem) {
3686
- val = val == null ? "" : val //不在页面上显示undefined null
3687
- if (elem.nodeType === 3) { //绑定在文本节点上
3688
- try { //IE对游离于DOM树外的节点赋值会报错
3689
- elem.data = val
3690
- } catch (e) {
3691
- }
3692
- } else { //绑定在特性节点上
3693
- elem.textContent = val
3694
- }
3895
+ val = val == null ? "" : val //不在页面上显示undefined null
3896
+ if (elem.nodeType === 3) { //绑定在文本节点上
3897
+ try { //IE对游离于DOM树外的节点赋值会报错
3898
+ elem.data = val
3899
+ } catch (e) {}
3900
+ } else { //绑定在特性节点上
3901
+ elem.textContent = val
3902
+ }
3695
3903
  }
3696
-
3697
3904
  function parseDisplay(nodeName, val) {
3698
3905
  //用于取得此类标签的默认display值
3699
3906
  var key = "_" + nodeName
@@ -3713,42 +3920,33 @@ function parseDisplay(nodeName, val) {
3713
3920
 
3714
3921
  avalon.parseDisplay = parseDisplay
3715
3922
 
3716
- bindingHandlers.visible = function(data, vmodels) {
3717
- var elem = avalon(data.element)
3718
- var display = elem.css("display")
3719
- if (display === "none") {
3720
- var style = elem[0].style
3721
- var has = /visibility/i.test(style.cssText)
3722
- var visible = elem.css("visibility")
3723
- style.display = ""
3724
- style.visibility = "hidden"
3725
- display = elem.css("display")
3726
- if (display === "none") {
3727
- display = parseDisplay(elem[0].nodeName)
3728
- }
3729
- style.visibility = has ? visible : ""
3730
- }
3731
- data.display = display
3923
+ bindingHandlers.visible = function (data, vmodels) {
3732
3924
  parseExprProxy(data.value, vmodels, data)
3733
3925
  }
3734
3926
 
3735
- bindingExecutors.visible = function(val, elem, data) {
3736
- elem.style.display = val ? data.display : "none"
3927
+ bindingExecutors.visible = function (val, elem, binding) {
3928
+ if (val) {
3929
+ elem.style.display = binding.display || ""
3930
+ if (avalon(elem).css("display") === "none") {
3931
+ elem.style.display = binding.display = parseDisplay(elem.nodeName)
3932
+ }
3933
+ } else {
3934
+ elem.style.display = "none"
3935
+ }
3737
3936
  }
3738
-
3739
3937
  bindingHandlers.widget = function(data, vmodels) {
3740
3938
  var args = data.value.match(rword)
3741
3939
  var elem = data.element
3742
3940
  var widget = args[0]
3743
3941
  var id = args[1]
3744
- if (!id || id === "$") {//没有定义或为$时,取组件名+随机数
3942
+ if (!id || id === "$") { //没有定义或为$时,取组件名+随机数
3745
3943
  id = generateID(widget)
3746
3944
  }
3747
- var optName = args[2] || widget//没有定义,取组件名
3945
+ var optName = args[2] || widget //没有定义,取组件名
3748
3946
  var constructor = avalon.ui[widget]
3749
3947
  if (typeof constructor === "function") { //ms-widget="tabs,tabsAAA,optname"
3750
3948
  vmodels = elem.vmodels || vmodels
3751
- for (var i = 0, v; v = vmodels[i++]; ) {
3949
+ for (var i = 0, v; v = vmodels[i++];) {
3752
3950
  if (v.hasOwnProperty(optName) && typeof v[optName] === "object") {
3753
3951
  var vmOptions = v[optName]
3754
3952
  vmOptions = vmOptions.$model || vmOptions
@@ -3758,6 +3956,7 @@ bindingHandlers.widget = function(data, vmodels) {
3758
3956
  if (vmOptions) {
3759
3957
  var wid = vmOptions[widget + "Id"]
3760
3958
  if (typeof wid === "string") {
3959
+ log("warning!不再支持" + widget + "Id")
3761
3960
  id = wid
3762
3961
  }
3763
3962
  }
@@ -3773,27 +3972,26 @@ bindingHandlers.widget = function(data, vmodels) {
3773
3972
  if (vmodel.$id) {
3774
3973
  avalon.vmodels[id] = vmodel
3775
3974
  createSignalTower(elem, vmodel)
3776
- if (vmodel.hasOwnProperty("$init")) {
3975
+ try {
3777
3976
  vmodel.$init(function() {
3778
3977
  avalon.scan(elem, [vmodel].concat(vmodels))
3779
3978
  if (typeof options.onInit === "function") {
3780
3979
  options.onInit.call(elem, vmodel, options, vmodels)
3781
3980
  }
3782
3981
  })
3783
- }
3982
+ } catch (e) {}
3784
3983
  data.rollback = function() {
3785
3984
  try {
3786
- vmodel.widgetElement = null
3787
3985
  vmodel.$remove()
3788
- } catch (e) {
3789
- }
3986
+ vmodel.widgetElement = null // 放到$remove后边
3987
+ } catch (e) {}
3790
3988
  elem.msData = {}
3791
3989
  delete avalon.vmodels[vmodel.$id]
3792
3990
  }
3793
- addSubscribers(data, widgetList)
3991
+ injectDisposeQueue(data, widgetList)
3794
3992
  if (window.chrome) {
3795
3993
  elem.addEventListener("DOMNodeRemovedFromDocument", function() {
3796
- setTimeout(removeSubscribers)
3994
+ setTimeout(rejectDisposeQueue)
3797
3995
  })
3798
3996
  }
3799
3997
  } else {
@@ -3863,7 +4061,7 @@ var filters = avalon.filters = {
3863
4061
  truncate: function(str, length, truncation) {
3864
4062
  //length,新字符串长度,truncation,新字符串的结尾的字段,返回新字符串
3865
4063
  length = length || 30
3866
- truncation = truncation === void(0) ? "..." : truncation
4064
+ truncation = typeof truncation === "string" ? truncation : "..."
3867
4065
  return str.length > length ? str.slice(0, length - truncation.length) + truncation : String(str)
3868
4066
  },
3869
4067
  $filter: function(val) {
@@ -4400,7 +4598,7 @@ new function () {// jshint ignore:line
4400
4598
  pkg = typeof pkg === "string" ? {name: pkg} : pkg
4401
4599
  var name = pkg.name
4402
4600
  if (!uniq[name]) {
4403
- var url = pkg.location ? pkg.location : joinPath(name, pkg.main || "main")
4601
+ var url = joinPath(pkg.location || name, pkg.main || "main")
4404
4602
  url = url.replace(rjsext, "")
4405
4603
  ret.push(pkg)
4406
4604
  uniq[name] = pkg.location = url
@@ -4562,7 +4760,9 @@ new function () {// jshint ignore:line
4562
4760
  onLoad(xhr.responseText)
4563
4761
  }
4564
4762
  }
4565
- xhr.open("GET", url, true)
4763
+ var time = "_=" + (new Date() - 0)
4764
+ var _url = url.indexOf("?") === -1 ? url + "?" + time : url + "&" + time
4765
+ xhr.open("GET", _url, true)
4566
4766
  if ("withCredentials" in xhr) {//这是处理跨域
4567
4767
  xhr.withCredentials = true
4568
4768
  }
@@ -4633,7 +4833,7 @@ new function () {// jshint ignore:line
4633
4833
  try {
4634
4834
  var ret = factory.apply(window, array)
4635
4835
  } catch (e) {
4636
- log("执行[" + id + "]模块的factory抛错: " + e)
4836
+ log("执行[" + id + "]模块的factory抛错: ", e)
4637
4837
  }
4638
4838
  if (ret !== void 0) {
4639
4839
  module.exports = ret
@@ -4684,6 +4884,10 @@ new function () {// jshint ignore:line
4684
4884
  //5. 还原扩展名,query
4685
4885
  var urlNoQuery = url + ext
4686
4886
  url = urlNoQuery + this.query
4887
+ urlNoQuery = url.replace(rquery, function (a) {
4888
+ this.query = a
4889
+ return ""
4890
+ })
4687
4891
  //6. 处理urlArgs
4688
4892
  eachIndexArray(id, kernel.urlArgs, function (value) {
4689
4893
  url += (url.indexOf("?") === -1 ? "?" : "&") + value;
@@ -4865,6 +5069,7 @@ avalon.ready(function () {
4865
5069
  if (noGlobal === void 0) {
4866
5070
  window.avalon = avalon
4867
5071
  }
5072
+
4868
5073
  return avalon
4869
5074
 
4870
5075
  }));