OLD | NEW |
| (Empty) |
1 | |
2 /** | |
3 * `IronResizableBehavior` is a behavior that can be used in Polymer elements
to | |
4 * coordinate the flow of resize events between "resizers" (elements that cont
rol the | |
5 * size or hidden state of their children) and "resizables" (elements that nee
d to be | |
6 * notified when they are resized or un-hidden by their parents in order to ta
ke | |
7 * action on their new measurements). | |
8 * Elements that perform measurement should add the `IronResizableBehavior` be
havior to | |
9 * their element definition and listen for the `iron-resize` event on themselv
es. | |
10 * This event will be fired when they become showing after having been hidden, | |
11 * when they are resized explicitly by another resizable, or when the window h
as been | |
12 * resized. | |
13 * Note, the `iron-resize` event is non-bubbling. | |
14 * | |
15 * @polymerBehavior Polymer.IronResizableBehavior | |
16 * @demo demo/index.html | |
17 **/ | |
18 Polymer.IronResizableBehavior = { | |
19 properties: { | |
20 _parentResizable: { | |
21 type: Object, | |
22 observer: '_parentResizableChanged' | |
23 } | |
24 }, | |
25 | |
26 listeners: { | |
27 'iron-request-resize-notifications': '_onIronRequestResizeNotifications' | |
28 }, | |
29 | |
30 created: function() { | |
31 // We don't really need property effects on these, and also we want them | |
32 // to be created before the `_parentResizable` observer fires: | |
33 this._interestedResizables = []; | |
34 this._boundNotifyResize = this.notifyResize.bind(this); | |
35 }, | |
36 | |
37 attached: function() { | |
38 this.fire('iron-request-resize-notifications', null, { | |
39 node: this, | |
40 bubbles: true | |
41 }); | |
42 | |
43 if (!this._parentResizable) { | |
44 window.addEventListener('resize', this._boundNotifyResize); | |
45 this.notifyResize(); | |
46 } | |
47 }, | |
48 | |
49 detached: function() { | |
50 if (this._parentResizable) { | |
51 this._parentResizable.stopResizeNotificationsFor(this); | |
52 } else { | |
53 window.removeEventListener('resize', this._boundNotifyResize); | |
54 } | |
55 | |
56 this._parentResizable = null; | |
57 }, | |
58 | |
59 /** | |
60 * Can be called to manually notify a resizable and its descendant | |
61 * resizables of a resize change. | |
62 */ | |
63 notifyResize: function() { | |
64 if (!this.isAttached) { | |
65 return; | |
66 } | |
67 | |
68 this._interestedResizables.forEach(function(resizable) { | |
69 // TODO(cdata): Currently behaviors cannot define "abstract" methods.. | |
70 if (!this.resizerShouldNotify || this.resizerShouldNotify(resizable)) { | |
71 resizable.notifyResize(); | |
72 } | |
73 }, this); | |
74 | |
75 this.fire('iron-resize', null, { | |
76 node: this, | |
77 bubbles: false | |
78 }); | |
79 }, | |
80 | |
81 /** | |
82 * Used to assign the closest resizable ancestor to this resizable | |
83 * if the ancestor detects a request for notifications. | |
84 */ | |
85 assignParentResizable: function(parentResizable) { | |
86 this._parentResizable = parentResizable; | |
87 }, | |
88 | |
89 /** | |
90 * Used to remove a resizable descendant from the list of descendants | |
91 * that should be notified of a resize change. | |
92 */ | |
93 stopResizeNotificationsFor: function(target) { | |
94 var index = this._interestedResizables.indexOf(target); | |
95 | |
96 if (index > -1) { | |
97 this._interestedResizables.splice(index, 1); | |
98 } | |
99 }, | |
100 | |
101 // TODO(cdata): Currently behaviors cannot define "abstract" methods. | |
102 // resizerShouldNotify: function(el) { return true; }, | |
103 | |
104 _parentResizableChanged: function(parentResizable) { | |
105 if (parentResizable) { | |
106 window.removeEventListener('resize', this._boundNotifyResize); | |
107 } | |
108 }, | |
109 | |
110 _onIronRequestResizeNotifications: function(event) { | |
111 var target = event.path ? event.path[0] : event.target; | |
112 | |
113 if (target === this) { | |
114 return; | |
115 } | |
116 | |
117 if (this._interestedResizables.indexOf(target) === -1) { | |
118 this._interestedResizables.push(target); | |
119 } | |
120 | |
121 target.assignParentResizable(this); | |
122 | |
123 event.stopPropagation(); | |
124 } | |
125 }; | |
OLD | NEW |