|
1 | 1 | "use strict";
|
2 | 2 | // This provides live binding for stache attributes.
|
3 |
| -var live = require('./core'); |
4 | 3 | var viewCallbacks = require('can-view-callbacks');
|
5 |
| -var domMutate = require('can-dom-mutate'); |
6 | 4 | var domMutateNode = require('can-dom-mutate/node');
|
7 | 5 | var canReflect = require('can-reflect');
|
8 |
| -var canReflectDeps = require('can-reflect-dependencies'); |
9 | 6 |
|
10 |
| -live.attrs = function(el, compute, scope, options) { |
| 7 | +var helpers = require('./helpers'); |
11 | 8 |
|
| 9 | +module.exports = function(el, compute, scope, options) { |
| 10 | + var handlerName = ""; |
12 | 11 | if (!canReflect.isObservableLike(compute)) {
|
13 | 12 | // Non-live case (`compute` was not a compute):
|
14 | 13 | // set all attributes on the element and don't
|
15 | 14 | // worry about setting up live binding since there
|
16 | 15 | // is not compute to bind on.
|
17 |
| - var attrs = live.getAttributeParts(compute); |
| 16 | + var attrs = helpers.getAttributeParts(compute); |
18 | 17 | for (var name in attrs) {
|
19 | 18 | domMutateNode.setAttribute.call(el, name, attrs[name]);
|
20 | 19 | }
|
21 | 20 | return;
|
22 | 21 | }
|
23 | 22 |
|
24 |
| - // last set of attributes |
25 |
| - var oldAttrs = {}; |
26 |
| - |
27 |
| - // set up a callback for handling changes when the compute |
28 |
| - // changes |
29 |
| - function liveAttrsUpdate(newVal) { |
30 |
| - var newAttrs = live.getAttributeParts(newVal), |
31 |
| - name; |
32 |
| - for (name in newAttrs) { |
33 |
| - var newValue = newAttrs[name], |
34 |
| - // `oldAttrs` was set on the last run of setAttrs in this context |
35 |
| - // (for this element and compute) |
36 |
| - oldValue = oldAttrs[name]; |
37 |
| - // Only fire a callback |
38 |
| - // if the value of the attribute has changed |
39 |
| - if (newValue !== oldValue) { |
40 |
| - // set on DOM attributes (dispatches an "attributes" event as well) |
41 |
| - domMutateNode.setAttribute.call(el, name, newValue); |
42 |
| - // get registered callback for attribute name and fire |
43 |
| - var callback = viewCallbacks.attr(name); |
44 |
| - if (callback) { |
45 |
| - callback(el, { |
46 |
| - attributeName: name, |
47 |
| - scope: scope, |
48 |
| - options: options |
49 |
| - }); |
50 |
| - } |
51 |
| - } |
52 |
| - // remove key found in new attrs from old attrs |
53 |
| - delete oldAttrs[name]; |
54 |
| - } |
55 |
| - // any attrs left at this point are not set on the element now, |
56 |
| - // so remove them. |
57 |
| - for (name in oldAttrs) { |
58 |
| - domMutateNode.removeAttribute.call(el, name); |
59 |
| - } |
60 |
| - oldAttrs = newAttrs; |
61 |
| - } |
62 |
| - |
63 | 23 | //!steal-remove-start
|
64 | 24 | if(process.env.NODE_ENV !== 'production') {
|
65 |
| - // register that the handler changes the parent element |
66 |
| - canReflect.assignSymbols(liveAttrsUpdate, { |
67 |
| - "can.getChangesDependencyRecord": function() { |
68 |
| - var s = new Set(); |
69 |
| - s.add(el); |
70 |
| - return { |
71 |
| - valueDependencies: s |
72 |
| - }; |
73 |
| - } |
74 |
| - }); |
75 |
| - |
76 |
| - Object.defineProperty(liveAttrsUpdate, "name", { |
77 |
| - value: "live.attrs update::"+canReflect.getName(compute), |
78 |
| - }); |
79 |
| - canReflectDeps.addMutatedBy(el, compute); |
| 25 | + handlerName = "live.attrs update::"+canReflect.getName(compute); |
80 | 26 | }
|
81 | 27 | //!steal-remove-end
|
82 | 28 |
|
83 |
| - // set attributes on any change to the compute |
84 |
| - canReflect.onValue(compute, liveAttrsUpdate,"domUI"); |
85 | 29 |
|
86 |
| - var removalDisposal; |
87 |
| - var teardownHandler = function() { |
88 |
| - canReflect.offValue(compute, liveAttrsUpdate,"domUI"); |
89 |
| - if (removalDisposal) { |
90 |
| - removalDisposal(); |
91 |
| - removalDisposal = undefined; |
92 |
| - } |
| 30 | + // last set of attributes |
| 31 | + var oldAttrs = {}; |
93 | 32 |
|
94 |
| - //!steal-remove-start |
95 |
| - if(process.env.NODE_ENV !== 'production') { |
96 |
| - canReflectDeps.deleteMutatedBy(el, compute); |
97 |
| - } |
98 |
| - //!steal-remove-end |
99 |
| - }; |
100 |
| - // unbind on element removal |
101 |
| - removalDisposal = domMutate.onNodeRemoval(el, function () { |
102 |
| - var doc = el.ownerDocument; |
103 |
| - var ownerNode = doc.contains ? doc : doc.documentElement; |
104 |
| - if (!ownerNode.contains(el)) { |
105 |
| - teardownHandler(); |
106 |
| - } |
107 |
| - }); |
108 | 33 |
|
109 |
| - // set up a current attribute set and assign to oldAttrs |
110 |
| - liveAttrsUpdate(canReflect.getValue(compute)); |
| 34 | + new helpers.ListenUntilRemovedAndInitialize(compute, |
| 35 | + function canViewLive_updateAttributes(newVal) { |
| 36 | + var newAttrs = helpers.getAttributeParts(newVal), |
| 37 | + name; |
| 38 | + for (name in newAttrs) { |
| 39 | + var newValue = newAttrs[name], |
| 40 | + // `oldAttrs` was set on the last run of setAttrs in this context |
| 41 | + // (for this element and compute) |
| 42 | + oldValue = oldAttrs[name]; |
| 43 | + // Only fire a callback |
| 44 | + // if the value of the attribute has changed |
| 45 | + if (newValue !== oldValue) { |
| 46 | + // set on DOM attributes (dispatches an "attributes" event as well) |
| 47 | + domMutateNode.setAttribute.call(el, name, newValue); |
| 48 | + // get registered callback for attribute name and fire |
| 49 | + var callback = viewCallbacks.attr(name); |
| 50 | + if (callback) { |
| 51 | + callback(el, { |
| 52 | + attributeName: name, |
| 53 | + scope: scope, |
| 54 | + options: options |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + // remove key found in new attrs from old attrs |
| 59 | + delete oldAttrs[name]; |
| 60 | + } |
| 61 | + // any attrs left at this point are not set on the element now, |
| 62 | + // so remove them. |
| 63 | + for (name in oldAttrs) { |
| 64 | + domMutateNode.removeAttribute.call(el, name); |
| 65 | + } |
| 66 | + oldAttrs = newAttrs; |
| 67 | + }, |
| 68 | + el, |
| 69 | + "dom", |
| 70 | + handlerName); |
| 71 | + |
111 | 72 | };
|
0 commit comments