| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/has" |
| 5 |
], function(array, lang, has){ |
| 6 |
"use strict"; |
| 7 |
|
| 8 |
has.add("object-is-api", lang.isFunction(Object.is)); |
| 9 |
|
| 10 |
var arrayProto = Array.prototype, |
| 11 |
areSameValues = has("object-is-api") ? Object.is : function(lhs, rhs){ |
| 12 |
return lhs === rhs && (lhs !== 0 || 1 / lhs === 1 / rhs) || lhs !== lhs && rhs !== rhs; |
| 13 |
}; |
| 14 |
|
| 15 |
function watch(o, prop, callback){ |
| 16 |
var hWatch; |
| 17 |
|
| 18 |
if(o && lang.isFunction(o.watch)){ |
| 19 |
hWatch = o.watch(prop, function(name, old, current){ |
| 20 |
if(!areSameValues(old, current)){ |
| 21 |
callback(current, old); |
| 22 |
} |
| 23 |
}); |
| 24 |
}else{ |
| 25 |
console.log("Attempt to observe non-stateful " + o + " with " + prop + ". Observation not happening."); |
| 26 |
} |
| 27 |
|
| 28 |
return { |
| 29 |
remove: function(){ |
| 30 |
if(hWatch){ |
| 31 |
hWatch.remove(); |
| 32 |
} |
| 33 |
} |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
function getProps(list){ |
| 38 |
return array.map(list, function(p){ |
| 39 |
return p.each ? |
| 40 |
array.map(p.target, function(entry){ |
| 41 |
return entry.get ? entry.get(p.targetProp) : entry[p.targetProp]; |
| 42 |
}) : |
| 43 |
p.target.get ? p.target.get(p.targetProp) : p.target[p.targetProp]; |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
function removeHandles(handles) { |
| 48 |
for(var h = null; (h = handles.shift());){ |
| 49 |
h.remove(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return function(target, targetProp, compute ){ |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
function applyComputed(data){ |
| 82 |
var result, hasResult; |
| 83 |
|
| 84 |
try{ |
| 85 |
result = compute.apply(target, data); |
| 86 |
hasResult = true; |
| 87 |
}catch(e){ |
| 88 |
console.error("Error during computed property callback: " + (e && e.stack || e)); |
| 89 |
} |
| 90 |
|
| 91 |
if(hasResult){ |
| 92 |
if(lang.isFunction(target.set)){ |
| 93 |
target.set(targetProp, result); |
| 94 |
}else{ |
| 95 |
target[targetProp] = result; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if(target == null){ |
| 101 |
throw new Error("Computed property cannot be applied to null."); |
| 102 |
} |
| 103 |
if(targetProp === "*"){ |
| 104 |
throw new Error("Wildcard property cannot be used for computed properties."); |
| 105 |
} |
| 106 |
|
| 107 |
var deps = arrayProto.slice.call(arguments, 3), |
| 108 |
hDep = array.map(deps, function(dep, index){ |
| 109 |
function observeEntry(entry){ |
| 110 |
return watch(entry, dep.targetProp, function(){ |
| 111 |
applyComputed(getProps(deps)); |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
if(dep.targetProp === "*"){ |
| 116 |
throw new Error("Wildcard property cannot be used for computed properties."); |
| 117 |
}else if(dep.each){ |
| 118 |
var hArray, |
| 119 |
hEntry = array.map(dep.target, observeEntry); |
| 120 |
|
| 121 |
if(dep.target && lang.isFunction(dep.target.watchElements)){ |
| 122 |
hArray = dep.target.watchElements(function(idx, removals, adds){ |
| 123 |
removeHandles(arrayProto.splice.apply(hEntry, [idx, removals.length].concat(array.map(adds, observeEntry)))); |
| 124 |
applyComputed(getProps(deps)); |
| 125 |
}); |
| 126 |
}else{ |
| 127 |
console.log("Attempt to observe non-stateful-array " + dep.target + ". Observation not happening."); |
| 128 |
} |
| 129 |
|
| 130 |
return { |
| 131 |
remove: function(){ |
| 132 |
if(hArray){ |
| 133 |
hArray.remove(); |
| 134 |
} |
| 135 |
removeHandles(hEntry); |
| 136 |
} |
| 137 |
}; |
| 138 |
}else{ |
| 139 |
return watch(dep.target, dep.targetProp, function(current){ |
| 140 |
var list = []; |
| 141 |
arrayProto.push.apply(list, getProps(deps.slice(0, index))); |
| 142 |
list.push(current); |
| 143 |
arrayProto.push.apply(list, getProps(deps.slice(index + 1))); |
| 144 |
applyComputed(list); |
| 145 |
}); |
| 146 |
} |
| 147 |
}); |
| 148 |
|
| 149 |
applyComputed(getProps(deps)); |
| 150 |
return { |
| 151 |
remove: function(){ |
| 152 |
removeHandles(hDep); |
| 153 |
} |
| 154 |
}; |
| 155 |
}; |
| 156 |
}); |