升级 avalon,修复 select 无法双向同步的 bug
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
http://weibo.com/jslouvre/
|
http://weibo.com/jslouvre/
|
||||||
|
|
||||||
Released under the MIT license
|
Released under the MIT license
|
||||||
avalon.shim.js 1.5.4 built in 2015.10.15
|
avalon.shim.js 1.5.4 built in 2015.10.18
|
||||||
support IE6+ and other browsers
|
support IE6+ and other browsers
|
||||||
==================================================*/
|
==================================================*/
|
||||||
(function(global, factory) {
|
(function(global, factory) {
|
||||||
@@ -3169,9 +3169,6 @@ function scanNodeArray(nodes, vmodels) {
|
|||||||
switch (node.nodeType) {
|
switch (node.nodeType) {
|
||||||
case 1:
|
case 1:
|
||||||
var elem = node, fn
|
var elem = node, fn
|
||||||
|
|
||||||
scanTag(node, vmodels) //扫描元素节点
|
|
||||||
|
|
||||||
if (!elem.msResolved && elem.parentNode && elem.parentNode.nodeType === 1) {
|
if (!elem.msResolved && elem.parentNode && elem.parentNode.nodeType === 1) {
|
||||||
var library = isWidget(elem)
|
var library = isWidget(elem)
|
||||||
if (library) {
|
if (library) {
|
||||||
@@ -3190,6 +3187,7 @@ function scanNodeArray(nodes, vmodels) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
scanTag(node, vmodels) //扫描元素节点
|
||||||
if (node.msHasEvent) {
|
if (node.msHasEvent) {
|
||||||
avalon.fireDom(node, "datasetchanged", {
|
avalon.fireDom(node, "datasetchanged", {
|
||||||
bubble: node.msHasEvent
|
bubble: node.msHasEvent
|
||||||
@@ -3424,6 +3422,7 @@ avalon.component = function (name, opts) {
|
|||||||
elem.msResolved = 1
|
elem.msResolved = 1
|
||||||
vmodel.$init(vmodel, elem)
|
vmodel.$init(vmodel, elem)
|
||||||
global.$init(vmodel, elem)
|
global.$init(vmodel, elem)
|
||||||
|
console.log("init")
|
||||||
var nodes = elem.childNodes
|
var nodes = elem.childNodes
|
||||||
//收集插入点
|
//收集插入点
|
||||||
var slots = {}, snode
|
var slots = {}, snode
|
||||||
@@ -3483,7 +3482,7 @@ avalon.component = function (name, opts) {
|
|||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log("dependencies "+dependencies)
|
||||||
if (dependencies === 0) {
|
if (dependencies === 0) {
|
||||||
var id1 = setTimeout(function () {
|
var id1 = setTimeout(function () {
|
||||||
clearTimeout(id1)
|
clearTimeout(id1)
|
||||||
@@ -3513,6 +3512,7 @@ avalon.component = function (name, opts) {
|
|||||||
scanTag(elem, [vmodel].concat(host.vmodels))
|
scanTag(elem, [vmodel].concat(host.vmodels))
|
||||||
|
|
||||||
avalon.vmodels[vmodel.$id] = vmodel
|
avalon.vmodels[vmodel.$id] = vmodel
|
||||||
|
avalon.log("添加组件VM: "+vmodel.$id)
|
||||||
if (!elem.childNodes.length) {
|
if (!elem.childNodes.length) {
|
||||||
avalon.fireDom(elem, "datasetchanged", {library: library, vm: vmodel, childReady: -1})
|
avalon.fireDom(elem, "datasetchanged", {library: library, vm: vmodel, childReady: -1})
|
||||||
} else {
|
} else {
|
||||||
@@ -3794,7 +3794,7 @@ avalon.directive("data", {
|
|||||||
//双工绑定
|
//双工绑定
|
||||||
var rduplexType = /^(?:checkbox|radio)$/
|
var rduplexType = /^(?:checkbox|radio)$/
|
||||||
var rduplexParam = /^(?:radio|checked)$/
|
var rduplexParam = /^(?:radio|checked)$/
|
||||||
var rnoduplexInput = /^(file|button|reset|submit|checkbox|radio)$/
|
var rnoduplexInput = /^(file|button|reset|submit|checkbox|radio|range)$/
|
||||||
var duplexBinding = avalon.directive("duplex", {
|
var duplexBinding = avalon.directive("duplex", {
|
||||||
priority: 2000,
|
priority: 2000,
|
||||||
init: function (binding, hasCast) {
|
init: function (binding, hasCast) {
|
||||||
@@ -3949,15 +3949,26 @@ var duplexBinding = avalon.directive("duplex", {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
binding.bound("datasetchanged", function (e) {
|
||||||
|
if (e.bubble === "selectDuplex") {
|
||||||
|
var value = binding._value
|
||||||
|
var curValue = Array.isArray(value) ? value.map(String) : value + ""
|
||||||
|
avalon(elem).val(curValue)
|
||||||
|
elem.oldValue = curValue + ""
|
||||||
|
binding.changed.call(elem, curValue)
|
||||||
|
}
|
||||||
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
if (binding.xtype === "input" && !rnoduplexInput.test(elem.type)) {
|
||||||
|
if (elem.type !== "hidden") {
|
||||||
binding.bound("focus", function () {
|
binding.bound("focus", function () {
|
||||||
elem.msFocus = true
|
elem.msFocus = true
|
||||||
})
|
})
|
||||||
binding.bound("blur", function () {
|
binding.bound("blur", function () {
|
||||||
elem.msFocus = false
|
elem.msFocus = false
|
||||||
})
|
})
|
||||||
if (binding.xtype === "input" && !rnoduplexInput.test(elem.type)) {
|
}
|
||||||
elem.avalonSetter = updateVModel //#765
|
elem.avalonSetter = updateVModel //#765
|
||||||
watchValueInTimer(function () {
|
watchValueInTimer(function () {
|
||||||
if (elem.contains(elem)) {
|
if (elem.contains(elem)) {
|
||||||
@@ -3987,7 +3998,18 @@ var duplexBinding = avalon.directive("duplex", {
|
|||||||
case "change":
|
case "change":
|
||||||
curValue = this.pipe(value, this, "set") //fix #673
|
curValue = this.pipe(value, this, "set") //fix #673
|
||||||
if (curValue !== this.oldValue) {
|
if (curValue !== this.oldValue) {
|
||||||
|
var fixCaret = false
|
||||||
|
if (elem.msFocus) {
|
||||||
|
var pos = getCaret(elem)
|
||||||
|
if (pos.start === pos.end) {
|
||||||
|
pos = pos.start
|
||||||
|
fixCaret = true
|
||||||
|
}
|
||||||
|
}
|
||||||
elem.value = this.oldValue = curValue
|
elem.value = this.oldValue = curValue
|
||||||
|
if (fixCaret) {
|
||||||
|
setCaret(element, pos, pos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case "radio":
|
case "radio":
|
||||||
@@ -4012,18 +4034,14 @@ var duplexBinding = avalon.directive("duplex", {
|
|||||||
case "select":
|
case "select":
|
||||||
//必须变成字符串后才能比较
|
//必须变成字符串后才能比较
|
||||||
binding._value = value
|
binding._value = value
|
||||||
|
if(!elem.msHasEvent){
|
||||||
elem.msHasEvent = "selectDuplex"
|
elem.msHasEvent = "selectDuplex"
|
||||||
//必须等到其孩子准备好才触发
|
//必须等到其孩子准备好才触发
|
||||||
avalon.bind(elem, "datasetchanged", function (e) {
|
}else{
|
||||||
if (e.bubble === "selectDuplex") {
|
avalon.fireDom(elem, "datasetchanged", {
|
||||||
var value = binding._value
|
bubble: elem.msHasEvent
|
||||||
var curValue = Array.isArray(value) ? value.map(String) : value + ""
|
|
||||||
avalon(elem).val(curValue)
|
|
||||||
elem.oldValue = curValue + ""
|
|
||||||
binding.changed.call(elem, curValue)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (binding.xtype !== "select") {
|
if (binding.xtype !== "select") {
|
||||||
@@ -4143,6 +4161,34 @@ new function () { // jshint ignore:line
|
|||||||
watchValueInTimer = avalon.tick
|
watchValueInTimer = avalon.tick
|
||||||
}
|
}
|
||||||
} // jshint ignore:line
|
} // jshint ignore:line
|
||||||
|
function getCaret(ctrl, start, end) {
|
||||||
|
if (ctrl.setSelectionRange) {
|
||||||
|
start = ctrl.selectionStart
|
||||||
|
end = ctrl.selectionEnd
|
||||||
|
} else if (document.selection && document.selection.createRange) {
|
||||||
|
var range = document.selection.createRange()
|
||||||
|
start = 0 - range.duplicate().moveStart('character', -100000)
|
||||||
|
end = start + range.text.length
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
start: start,
|
||||||
|
end: end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function setCaret(ctrl, begin, end) {
|
||||||
|
if (!ctrl.value || ctrl.readOnly)
|
||||||
|
return
|
||||||
|
if (ctrl.setSelectionRange) {
|
||||||
|
ctrl.selectionStart = begin
|
||||||
|
ctrl.selectionEnd = end
|
||||||
|
} else {
|
||||||
|
var range = ctrl.createTextRange()
|
||||||
|
range.collapse(true);
|
||||||
|
range.moveStart("character", begin)
|
||||||
|
range.moveEnd("character", end - begin)
|
||||||
|
range.select()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
avalon.directive("effect", {
|
avalon.directive("effect", {
|
||||||
priority: 5,
|
priority: 5,
|
||||||
@@ -4326,7 +4372,7 @@ function upperFirstChar(str) {
|
|||||||
}
|
}
|
||||||
var effectBuffer = new Buffer()
|
var effectBuffer = new Buffer()
|
||||||
function Effect() {
|
function Effect() {
|
||||||
}// 动画实例,做成类的形式,是为了共用所有原型方法
|
}// 动画实例,做成类的形式,是为了共用所有原型方法
|
||||||
|
|
||||||
Effect.prototype = {
|
Effect.prototype = {
|
||||||
contrustor: Effect,
|
contrustor: Effect,
|
||||||
@@ -4349,7 +4395,7 @@ Effect.prototype = {
|
|||||||
callEffectHook(me, "abort" + upperFirstChar(oppositeName))
|
callEffectHook(me, "abort" + upperFirstChar(oppositeName))
|
||||||
callEffectHook(me, "before" + upperFirstChar(name))
|
callEffectHook(me, "before" + upperFirstChar(name))
|
||||||
if (!isLeave)
|
if (!isLeave)
|
||||||
before(el) // 这里可能做插入DOM树的操作,因此必须在修改类名前执行
|
before(el) // 这里可能做插入DOM树的操作,因此必须在修改类名前执行
|
||||||
var cssCallback = function (cancel) {
|
var cssCallback = function (cancel) {
|
||||||
el.removeEventListener(me.cssEvent, me.cssCallback)
|
el.removeEventListener(me.cssEvent, me.cssCallback)
|
||||||
if (isLeave) {
|
if (isLeave) {
|
||||||
@@ -4576,7 +4622,7 @@ avalon.directive("if", {
|
|||||||
elem.required = false
|
elem.required = false
|
||||||
elem.setAttribute("_required", "true")
|
elem.setAttribute("_required", "true")
|
||||||
}
|
}
|
||||||
try {// 如果不支持querySelectorAll或:required,可以直接无视
|
try {// 如果不支持querySelectorAll或:required,可以直接无视
|
||||||
avalon.each(elem.querySelectorAll(":required"), function (el) {
|
avalon.each(elem.querySelectorAll(":required"), function (el) {
|
||||||
elem.required = false
|
elem.required = false
|
||||||
el.setAttribute("_required", "true")
|
el.setAttribute("_required", "true")
|
||||||
@@ -4733,7 +4779,7 @@ avalon.directive("include", {
|
|||||||
return nodesToFrag(nodes)
|
return nodesToFrag(nodes)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
before = function () {// 新添加元素的动画
|
before = function () {// 新添加元素的动画
|
||||||
target.insertBefore(fragment, binding.end)
|
target.insertBefore(fragment, binding.end)
|
||||||
scanNodeArray(nodes, vmodels)
|
scanNodeArray(nodes, vmodels)
|
||||||
}
|
}
|
||||||
@@ -4965,7 +5011,7 @@ avalon.directive("repeat", {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//重写proxy
|
//重写proxy
|
||||||
if (this.enterCount === 1) {// 防止多次进入,导致位置不对
|
if (this.enterCount === 1) {// 防止多次进入,导致位置不对
|
||||||
proxy.$active = false
|
proxy.$active = false
|
||||||
proxy.$oldIndex = proxy.$index
|
proxy.$oldIndex = proxy.$index
|
||||||
proxy.$active = true
|
proxy.$active = true
|
||||||
@@ -4978,7 +5024,7 @@ avalon.directive("repeat", {
|
|||||||
proxy.$last = i === length - 1
|
proxy.$last = i === length - 1
|
||||||
// proxy[param] = value[i]
|
// proxy[param] = value[i]
|
||||||
} else {
|
} else {
|
||||||
proxy.$val = toJson(value[keyOrId]) // 这里是处理vm.object = newObject的情况
|
proxy.$val = toJson(value[keyOrId]) // 这里是处理vm.object = newObject的情况
|
||||||
}
|
}
|
||||||
proxies.push(proxy)
|
proxies.push(proxy)
|
||||||
}
|
}
|
||||||
@@ -5029,7 +5075,7 @@ avalon.directive("repeat", {
|
|||||||
} else if (proxy.$index !== proxy.$oldIndex) {
|
} else if (proxy.$index !== proxy.$oldIndex) {
|
||||||
(function (proxy2, preElement) {
|
(function (proxy2, preElement) {
|
||||||
staggerIndex = mayStaggerAnimate(binding.effectEnterStagger, function () {
|
staggerIndex = mayStaggerAnimate(binding.effectEnterStagger, function () {
|
||||||
var curNode = removeItem(proxy2.$anchor)// 如果位置被挪动了
|
var curNode = removeItem(proxy2.$anchor)// 如果位置被挪动了
|
||||||
var inserted = avalon.slice(curNode.childNodes)
|
var inserted = avalon.slice(curNode.childNodes)
|
||||||
parent.insertBefore(curNode, preElement.nextSibling)
|
parent.insertBefore(curNode, preElement.nextSibling)
|
||||||
animateRepeat(inserted, 1, binding)
|
animateRepeat(inserted, 1, binding)
|
||||||
|
|||||||
Reference in New Issue
Block a user