| OLD | NEW |
| 1 | 1 |
| 2 /** | 2 /** |
| 3 * `IronResizableBehavior` is a behavior that can be used in Polymer elements
to | 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 | 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 | 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 | 6 * notified when they are resized or un-hidden by their parents in order to ta
ke |
| 7 * action on their new measurements). | 7 * action on their new measurements). |
| 8 * Elements that perform measurement should add the `IronResizableBehavior` be
havior to | 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. | 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, | 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 | 11 * when they are resized explicitly by another resizable, or when the window h
as been |
| 12 * resized. | 12 * resized. |
| 13 * Note, the `iron-resize` event is non-bubbling. | 13 * Note, the `iron-resize` event is non-bubbling. |
| 14 * | 14 * |
| 15 * @polymerBehavior Polymer.IronResizableBehavior | 15 * @polymerBehavior Polymer.IronResizableBehavior |
| 16 * @demo demo/index.html | 16 * @demo demo/index.html |
| 17 **/ | 17 **/ |
| 18 Polymer.IronResizableBehavior = { | 18 Polymer.IronResizableBehavior = { |
| 19 properties: { | 19 properties: { |
| 20 /** |
| 21 * The closest ancestor element that implements `IronResizableBehavior`. |
| 22 */ |
| 20 _parentResizable: { | 23 _parentResizable: { |
| 21 type: Object, | 24 type: Object, |
| 22 observer: '_parentResizableChanged' | 25 observer: '_parentResizableChanged' |
| 26 }, |
| 27 |
| 28 /** |
| 29 * True if this element is currently notifying its descedant elements of |
| 30 * resize. |
| 31 */ |
| 32 _notifyingDescendant: { |
| 33 type: Boolean, |
| 34 value: false |
| 23 } | 35 } |
| 24 }, | 36 }, |
| 25 | 37 |
| 26 listeners: { | 38 listeners: { |
| 27 'iron-request-resize-notifications': '_onIronRequestResizeNotifications' | 39 'iron-request-resize-notifications': '_onIronRequestResizeNotifications' |
| 28 }, | 40 }, |
| 29 | 41 |
| 30 created: function() { | 42 created: function() { |
| 31 // We don't really need property effects on these, and also we want them | 43 // We don't really need property effects on these, and also we want them |
| 32 // to be created before the `_parentResizable` observer fires: | 44 // to be created before the `_parentResizable` observer fires: |
| 33 this._interestedResizables = []; | 45 this._interestedResizables = []; |
| 34 this._boundNotifyResize = this.notifyResize.bind(this); | 46 this._boundNotifyResize = this.notifyResize.bind(this); |
| 35 }, | 47 }, |
| 36 | 48 |
| 37 attached: function() { | 49 attached: function() { |
| 38 this.fire('iron-request-resize-notifications', null, { | 50 this.fire('iron-request-resize-notifications', null, { |
| 39 node: this, | 51 node: this, |
| 40 bubbles: true | 52 bubbles: true, |
| 53 cancelable: true |
| 41 }); | 54 }); |
| 42 | 55 |
| 43 if (!this._parentResizable) { | 56 if (!this._parentResizable) { |
| 44 window.addEventListener('resize', this._boundNotifyResize); | 57 window.addEventListener('resize', this._boundNotifyResize); |
| 45 this.notifyResize(); | 58 this.notifyResize(); |
| 46 } | 59 } |
| 47 }, | 60 }, |
| 48 | 61 |
| 49 detached: function() { | 62 detached: function() { |
| 50 if (this._parentResizable) { | 63 if (this._parentResizable) { |
| 51 this._parentResizable.stopResizeNotificationsFor(this); | 64 this._parentResizable.stopResizeNotificationsFor(this); |
| 52 } else { | 65 } else { |
| 53 window.removeEventListener('resize', this._boundNotifyResize); | 66 window.removeEventListener('resize', this._boundNotifyResize); |
| 54 } | 67 } |
| 55 | 68 |
| 56 this._parentResizable = null; | 69 this._parentResizable = null; |
| 57 }, | 70 }, |
| 58 | 71 |
| 59 /** | 72 /** |
| 60 * Can be called to manually notify a resizable and its descendant | 73 * Can be called to manually notify a resizable and its descendant |
| 61 * resizables of a resize change. | 74 * resizables of a resize change. |
| 62 */ | 75 */ |
| 63 notifyResize: function() { | 76 notifyResize: function() { |
| 64 if (!this.isAttached) { | 77 if (!this.isAttached) { |
| 65 return; | 78 return; |
| 66 } | 79 } |
| 67 | 80 |
| 68 this._interestedResizables.forEach(function(resizable) { | 81 this._interestedResizables.forEach(function(resizable) { |
| 69 // TODO(cdata): Currently behaviors cannot define "abstract" methods.. | 82 if (this.resizerShouldNotify(resizable)) { |
| 70 if (!this.resizerShouldNotify || this.resizerShouldNotify(resizable)) { | 83 this._notifyDescendant(resizable); |
| 71 resizable.notifyResize(); | |
| 72 } | 84 } |
| 73 }, this); | 85 }, this); |
| 74 | 86 |
| 75 this.fire('iron-resize', null, { | 87 this._fireResize(); |
| 76 node: this, | |
| 77 bubbles: false | |
| 78 }); | |
| 79 }, | 88 }, |
| 80 | 89 |
| 81 /** | 90 /** |
| 82 * Used to assign the closest resizable ancestor to this resizable | 91 * Used to assign the closest resizable ancestor to this resizable |
| 83 * if the ancestor detects a request for notifications. | 92 * if the ancestor detects a request for notifications. |
| 84 */ | 93 */ |
| 85 assignParentResizable: function(parentResizable) { | 94 assignParentResizable: function(parentResizable) { |
| 86 this._parentResizable = parentResizable; | 95 this._parentResizable = parentResizable; |
| 87 }, | 96 }, |
| 88 | 97 |
| 89 /** | 98 /** |
| 90 * Used to remove a resizable descendant from the list of descendants | 99 * Used to remove a resizable descendant from the list of descendants |
| 91 * that should be notified of a resize change. | 100 * that should be notified of a resize change. |
| 92 */ | 101 */ |
| 93 stopResizeNotificationsFor: function(target) { | 102 stopResizeNotificationsFor: function(target) { |
| 94 var index = this._interestedResizables.indexOf(target); | 103 var index = this._interestedResizables.indexOf(target); |
| 95 | 104 |
| 96 if (index > -1) { | 105 if (index > -1) { |
| 97 this._interestedResizables.splice(index, 1); | 106 this._interestedResizables.splice(index, 1); |
| 107 this.unlisten(target, 'iron-resize', '_onDescendantIronResize'); |
| 98 } | 108 } |
| 99 }, | 109 }, |
| 100 | 110 |
| 101 // TODO(cdata): Currently behaviors cannot define "abstract" methods. | 111 /** |
| 102 // resizerShouldNotify: function(el) { return true; }, | 112 * This method can be overridden to filter nested elements that should or |
| 113 * should not be notified by the current element. Return true if an element |
| 114 * should be notified, or false if it should not be notified. |
| 115 * |
| 116 * @param {HTMLElement} element A candidate descendant element that |
| 117 * implements `IronResizableBehavior`. |
| 118 * @return {boolean} True if the `element` should be notified of resize. |
| 119 */ |
| 120 resizerShouldNotify: function(element) { return true; }, |
| 103 | 121 |
| 104 _parentResizableChanged: function(parentResizable) { | 122 _onDescendantIronResize: function(event) { |
| 105 if (parentResizable) { | 123 if (this._notifyingDescendant) { |
| 106 window.removeEventListener('resize', this._boundNotifyResize); | 124 event.stopPropagation(); |
| 125 return; |
| 107 } | 126 } |
| 127 |
| 128 // NOTE(cdata): In ShadowDOM, event retargetting makes echoing of the |
| 129 // otherwise non-bubbling event "just work." We do it manually here for |
| 130 // the case where Polymer is not using shadow roots for whatever reason: |
| 131 if (!Polymer.Settings.useShadow) { |
| 132 this._fireResize(); |
| 133 } |
| 134 }, |
| 135 |
| 136 _fireResize: function() { |
| 137 this.fire('iron-resize', null, { |
| 138 node: this, |
| 139 bubbles: false |
| 140 }); |
| 108 }, | 141 }, |
| 109 | 142 |
| 110 _onIronRequestResizeNotifications: function(event) { | 143 _onIronRequestResizeNotifications: function(event) { |
| 111 var target = event.path ? event.path[0] : event.target; | 144 var target = event.path ? event.path[0] : event.target; |
| 112 | 145 |
| 113 if (target === this) { | 146 if (target === this) { |
| 114 return; | 147 return; |
| 115 } | 148 } |
| 116 | 149 |
| 117 if (this._interestedResizables.indexOf(target) === -1) { | 150 if (this._interestedResizables.indexOf(target) === -1) { |
| 118 this._interestedResizables.push(target); | 151 this._interestedResizables.push(target); |
| 152 this.listen(target, 'iron-resize', '_onDescendantIronResize'); |
| 119 } | 153 } |
| 120 | 154 |
| 121 target.assignParentResizable(this); | 155 target.assignParentResizable(this); |
| 156 this._notifyDescendant(target); |
| 122 | 157 |
| 123 event.stopPropagation(); | 158 event.stopPropagation(); |
| 159 }, |
| 160 |
| 161 _parentResizableChanged: function(parentResizable) { |
| 162 if (parentResizable) { |
| 163 window.removeEventListener('resize', this._boundNotifyResize); |
| 164 } |
| 165 }, |
| 166 |
| 167 _notifyDescendant: function(descendant) { |
| 168 // NOTE(cdata): In IE10, attached is fired on children first, so it's |
| 169 // important not to notify them if the parent is not attached yet (or |
| 170 // else they will get redundantly notified when the parent attaches). |
| 171 if (!this.isAttached) { |
| 172 return; |
| 173 } |
| 174 |
| 175 this._notifyingDescendant = true; |
| 176 descendant.notifyResize(); |
| 177 this._notifyingDescendant = false; |
| 124 } | 178 } |
| 125 }; | 179 }; |
| OLD | NEW |