Bug 1350646: Part 17 - Remove SDK DOM events modules. r=Mossop

MozReview-Commit-ID: C8gLoYwRLej

--HG--
extra : source : 7571b064a77e55a604d1dfefd86a6b1a57ea5c11
This commit is contained in:
Kris Maglione 2017-08-05 22:42:18 -07:00
Родитель 0d47f2a277
Коммит c8e4075eff
7 изменённых файлов: 0 добавлений и 363 удалений

Просмотреть файл

@ -30,11 +30,9 @@ modules = [
'sdk/addon/installer.js',
'sdk/addon/window.js',
'sdk/base64.js',
'sdk/browser/events.js',
'sdk/clipboard.js',
'sdk/console/plain-text.js',
'sdk/console/traceback.js',
'sdk/content/events.js',
'sdk/content/loader.js',
'sdk/content/thumbnail.js',
'sdk/core/disposable.js',
@ -44,12 +42,9 @@ modules = [
'sdk/core/promise.js',
'sdk/core/reference.js',
'sdk/deprecated/api-utils.js',
'sdk/deprecated/events/assembler.js',
'sdk/deprecated/unit-test-finder.js',
'sdk/deprecated/unit-test.js',
'sdk/deprecated/window-utils.js',
'sdk/dom/events-shimmed.js',
'sdk/dom/events.js',
'sdk/event/chrome.js',
'sdk/event/core.js',
'sdk/event/dom.js',
@ -78,7 +73,6 @@ modules = [
'sdk/self.js',
'sdk/system.js',
'sdk/system/environment.js',
'sdk/system/events-shimmed.js',
'sdk/system/events.js',
'sdk/system/globals.js',
'sdk/system/process.js',

Просмотреть файл

@ -1,20 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
module.metadata = {
"stability": "unstable"
};
const { events } = require("../window/events");
const { filter } = require("../event/utils");
const { isBrowser } = require("../window/utils");
// TODO: `isBrowser` detects weather window is a browser by checking
// `windowtype` attribute, which means that all 'open' events will be
// filtered out since document is not loaded yet. Maybe we can find a better
// implementation for `isBrowser`. Either way it's not really needed yet
// neither window tracker provides this event.
exports.events = filter(events, ({target}) => isBrowser(target));

Просмотреть файл

@ -1,57 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
module.metadata = {
"stability": "experimental"
};
const { Ci } = require("chrome");
lazyRequire(this, "../event/dom", "open");
const { observe } = require("../event/chrome");
const { filter, merge, map, expand } = require("../event/utils");
const { windows } = require("../window/utils");
const { events: windowEvents } = require("sdk/window/events");
// Note: Please note that even though pagehide event is included
// it's not observable reliably since it's not always triggered
// when closing tabs. Implementation can be imrpoved once that
// event will be necessary.
var TYPES = ["DOMContentLoaded", "load", "pageshow", "pagehide"];
var insert = observe("document-element-inserted");
var windowCreate = merge([
observe("content-document-global-created"),
observe("chrome-document-global-created")
]);
var create = map(windowCreate, function({target, data, type}) {
return { target: target.document, type: type, data: data }
});
function streamEventsFrom({document}) {
// Map supported event types to a streams of those events on the given
// `window` for the inserted document and than merge these streams into
// single form stream off all window state change events.
let stateChanges = TYPES.map(function(type) {
return open(document, type, { capture: true });
});
// Since load events on document occur for every loded resource
return filter(merge(stateChanges), function({target}) {
return target instanceof Ci.nsIDOMDocument
})
}
exports.streamEventsFrom = streamEventsFrom;
var opened = windows(null, { includePrivate: true });
var state = merge(opened.map(streamEventsFrom));
var futureReady = filter(windowEvents, ({type}) =>
type === "DOMContentLoaded");
var futureWindows = map(futureReady, ({target}) => target);
var futureState = expand(futureWindows, streamEventsFrom);
exports.events = merge([insert, create, state, futureState]);

Просмотреть файл

@ -1,54 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { Class } = require("../../core/heritage");
const { removeListener, on } = require("../../dom/events");
/**
* Event targets
* can be added / removed by calling `observe / ignore` methods. Composer should
* provide array of event types it wishes to handle as property
* `supportedEventsTypes` and function for handling all those events as
* `handleEvent` property.
*/
exports.DOMEventAssembler = Class({
/**
* Function that is supposed to handle all the supported events (that are
* present in the `supportedEventsTypes`) from all the observed
* `eventTargets`.
* @param {Event} event
* Event being dispatched.
*/
handleEvent() {
throw new TypeError("Instance of DOMEventAssembler must implement `handleEvent` method");
},
/**
* Array of supported event names.
* @type {String[]}
*/
get supportedEventsTypes() {
throw new TypeError("Instance of DOMEventAssembler must implement `handleEvent` field");
},
/**
* Adds `eventTarget` to the list of observed `eventTarget`s. Listeners for
* supported events will be registered on the given `eventTarget`.
* @param {EventTarget} eventTarget
*/
observe: function observe(eventTarget) {
this.supportedEventsTypes.forEach(function(eventType) {
on(eventTarget, eventType, this);
}, this);
},
/**
* Removes `eventTarget` from the list of observed `eventTarget`s. Listeners
* for all supported events will be unregistered from the given `eventTarget`.
* @param {EventTarget} eventTarget
*/
ignore: function ignore(eventTarget) {
this.supportedEventsTypes.forEach(function(eventType) {
removeListener(eventTarget, eventType, this);
}, this);
}
});

Просмотреть файл

@ -1,18 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
module.metadata = {
'stability': 'unstable'
};
const events = require('./events.js');
exports.emit = (element, type, obj) => events.emit(element, type, obj, true);
exports.on = (element, type, listener, capture) => events.on(element, type, listener, capture, true);
exports.once = (element, type, listener, capture) => events.once(element, type, listener, capture, true);
exports.removeListener = (element, type, listener, capture) => events.removeListener(element, type, listener, capture, true);
exports.removed = events.removed;
exports.when = (element, eventName, capture) => events.when(element, eventName, capture ? capture : false, true);

Просмотреть файл

@ -1,192 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
module.metadata = {
"stability": "unstable"
};
const { Cu } = require("chrome");
const { ShimWaiver } = Cu.import("resource://gre/modules/ShimWaiver.jsm");
// Utility function that returns copy of the given `text` with last character
// removed if it is `"s"`.
function singularify(text) {
return text[text.length - 1] === "s" ? text.substr(0, text.length - 1) : text;
}
// Utility function that takes event type, argument is passed to
// `document.createEvent` and returns name of the initializer method of the
// given event. Please note that there are some event types whose initializer
// methods can't be guessed by this function. For more details see following
// link: https://developer.mozilla.org/En/DOM/Document.createEvent
function getInitializerName(category) {
return "init" + singularify(category);
}
/**
* Registers an event `listener` on a given `element`, that will be called
* when events of specified `type` is dispatched on the `element`.
* @param {Element} element
* Dom element to register listener on.
* @param {String} type
* A string representing the
* [event type](https://developer.mozilla.org/en/DOM/event.type) to
* listen for.
* @param {Function} listener
* Function that is called whenever an event of the specified `type`
* occurs.
* @param {Boolean} capture
* If true, indicates that the user wishes to initiate capture. After
* initiating capture, all events of the specified type will be dispatched
* to the registered listener before being dispatched to any `EventTarget`s
* beneath it in the DOM tree. Events which are bubbling upward through
* the tree will not trigger a listener designated to use capture.
* See [DOM Level 3 Events](http://www.w3.org/TR/DOM-Level-3-Events/#event-flow)
* for a detailed explanation.
*/
function on(element, type, listener, capture, shimmed = false) {
// `capture` defaults to `false`.
capture = capture || false;
if (shimmed) {
element.addEventListener(type, listener, capture);
} else {
ShimWaiver.getProperty(element, "addEventListener")(type, listener, capture);
}
}
exports.on = on;
/**
* Registers an event `listener` on a given `element`, that will be called
* only once, next time event of specified `type` is dispatched on the
* `element`.
* @param {Element} element
* Dom element to register listener on.
* @param {String} type
* A string representing the
* [event type](https://developer.mozilla.org/en/DOM/event.type) to
* listen for.
* @param {Function} listener
* Function that is called whenever an event of the specified `type`
* occurs.
* @param {Boolean} capture
* If true, indicates that the user wishes to initiate capture. After
* initiating capture, all events of the specified type will be dispatched
* to the registered listener before being dispatched to any `EventTarget`s
* beneath it in the DOM tree. Events which are bubbling upward through
* the tree will not trigger a listener designated to use capture.
* See [DOM Level 3 Events](http://www.w3.org/TR/DOM-Level-3-Events/#event-flow)
* for a detailed explanation.
*/
function once(element, type, listener, capture, shimmed = false) {
on(element, type, function selfRemovableListener(event) {
removeListener(element, type, selfRemovableListener, capture, shimmed);
listener.apply(this, arguments);
}, capture, shimmed);
}
exports.once = once;
/**
* Unregisters an event `listener` on a given `element` for the events of the
* specified `type`.
*
* @param {Element} element
* Dom element to unregister listener from.
* @param {String} type
* A string representing the
* [event type](https://developer.mozilla.org/en/DOM/event.type) to
* listen for.
* @param {Function} listener
* Function that is called whenever an event of the specified `type`
* occurs.
* @param {Boolean} capture
* If true, indicates that the user wishes to initiate capture. After
* initiating capture, all events of the specified type will be dispatched
* to the registered listener before being dispatched to any `EventTarget`s
* beneath it in the DOM tree. Events which are bubbling upward through
* the tree will not trigger a listener designated to use capture.
* See [DOM Level 3 Events](http://www.w3.org/TR/DOM-Level-3-Events/#event-flow)
* for a detailed explanation.
*/
function removeListener(element, type, listener, capture, shimmed = false) {
if (shimmed) {
element.removeEventListener(type, listener, capture);
} else {
ShimWaiver.getProperty(element, "removeEventListener")(type, listener, capture);
}
}
exports.removeListener = removeListener;
/**
* Emits event of the specified `type` and `category` on the given `element`.
* Specified `settings` are used to initialize event before dispatching it.
* @param {Element} element
* Dom element to dispatch event on.
* @param {String} type
* A string representing the
* [event type](https://developer.mozilla.org/en/DOM/event.type).
* @param {Object} options
* Options object containing following properties:
* - `category`: String passed to the `document.createEvent`. Option is
* optional and defaults to "UIEvents".
* - `initializer`: If passed it will be used as name of the method used
* to initialize event. If omitted name will be generated from the
* `category` field by prefixing it with `"init"` and removing last
* character if it matches `"s"`.
* - `settings`: Array of settings that are forwarded to the event
* initializer after firs `type` argument.
* @see https://developer.mozilla.org/En/DOM/Document.createEvent
*/
function emit(element, type, { category, initializer, settings }, shimmed = false) {
category = category || "UIEvents";
initializer = initializer || getInitializerName(category);
let document = element.ownerDocument;
let event = document.createEvent(category);
event[initializer].apply(event, [type].concat(settings));
if (shimmed) {
element.dispatchEvent(event);
} else {
ShimWaiver.getProperty(element, "dispatchEvent")(event);
}
};
exports.emit = emit;
// Takes DOM `element` and returns promise which is resolved
// when given element is removed from it's parent node.
const removed = element => {
return new Promise(resolve => {
const { MutationObserver } = element.ownerGlobal;
const observer = new MutationObserver(mutations => {
for (let mutation of mutations) {
for (let node of mutation.removedNodes || []) {
if (node === element) {
observer.disconnect();
resolve(element);
}
}
}
});
observer.observe(element.parentNode, {childList: true});
});
};
exports.removed = removed;
const when = (element, eventName, capture=false, shimmed=false) => new Promise(resolve => {
const listener = event => {
if (shimmed) {
element.removeEventListener(eventName, listener, capture);
} else {
ShimWaiver.getProperty(element, "removeEventListener")(eventName, listener, capture);
}
resolve(event);
};
if (shimmed) {
element.addEventListener(eventName, listener, capture);
} else {
ShimWaiver.getProperty(element, "addEventListener")(eventName, listener, capture);
}
});
exports.when = when;

Просмотреть файл

@ -1,16 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
module.metadata = {
'stability': 'unstable'
};
const events = require('./events.js');
exports.emit = (type, event) => events.emit(type, event, true);
exports.on = (type, listener, strong) => events.on(type, listener, strong, true);
exports.once = (type, listener) => events.once(type, listener, true);
exports.off = (type, listener) => events.off(type, listener, true);