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