Skip to content

Commit ef69a4a

Browse files
Prevent component upgrades by catching events.
1 parent 2666977 commit ef69a4a

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

src/mdlComponentHandler.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ componentHandler = (function() {
141141
return upgradedList.indexOf(jsClass) !== -1;
142142
}
143143

144+
/**
145+
* Create an event object.
146+
*
147+
* @param {string} eventType The type name of the event.
148+
* @param {boolean} bubbles Whether the event should bubble up the DOM.
149+
* @param {boolean} cancelable Whether the event can be canceled.
150+
* @returns {!Event}
151+
*/
152+
function createEvent_(eventType, bubbles, cancelable) {
153+
if ('CustomEvent' in window && typeof window.CustomEvent === 'function') {
154+
return new CustomEvent(eventType, {
155+
bubbles: bubbles,
156+
cancelable: cancelable
157+
});
158+
} else {
159+
var ev = document.createEvent('Events');
160+
ev.initEvent(eventType, bubbles, cancelable);
161+
return ev;
162+
}
163+
}
164+
144165
/**
145166
* Searches existing DOM for elements of our component type and upgrades them
146167
* if they have not already been upgraded.
@@ -185,6 +206,13 @@ componentHandler = (function() {
185206
if (!(typeof element === 'object' && element instanceof Element)) {
186207
throw new Error('Invalid argument provided to upgrade MDL element.');
187208
}
209+
// Allow upgrade to be canceled by canceling emitted event.
210+
var upgradingEv = createEvent_('mdl-componentupgrading', true, true);
211+
element.dispatchEvent(upgradingEv);
212+
if (upgradingEv.defaultPrevented) {
213+
return;
214+
}
215+
188216
var upgradedList = getUpgradedListOfElement_(element);
189217
var classesToUpgrade = [];
190218
// If jsClass is not provided scan the registered components to find the
@@ -227,16 +255,8 @@ componentHandler = (function() {
227255
'Unable to find a registered component for the given class.');
228256
}
229257

230-
var ev;
231-
if ('CustomEvent' in window && typeof window.CustomEvent === 'function') {
232-
ev = new CustomEvent('mdl-componentupgraded', {
233-
bubbles: true, cancelable: false
234-
});
235-
} else {
236-
ev = document.createEvent('Events');
237-
ev.initEvent('mdl-componentupgraded', true, true);
238-
}
239-
element.dispatchEvent(ev);
258+
var upgradedEv = createEvent_('mdl-componentupgraded', true, false);
259+
element.dispatchEvent(upgradedEv);
240260
}
241261
}
242262

@@ -358,15 +378,7 @@ componentHandler = (function() {
358378
upgrades.splice(componentPlace, 1);
359379
component.element_.setAttribute('data-upgraded', upgrades.join(','));
360380

361-
var ev;
362-
if ('CustomEvent' in window && typeof window.CustomEvent === 'function') {
363-
ev = new CustomEvent('mdl-componentdowngraded', {
364-
bubbles: true, cancelable: false
365-
});
366-
} else {
367-
ev = document.createEvent('Events');
368-
ev.initEvent('mdl-componentdowngraded', true, true);
369-
}
381+
var ev = createEvent_('mdl-componentdowngraded', true, false);
370382
component.element_.dispatchEvent(ev);
371383
}
372384
}

0 commit comments

Comments
 (0)