annotate dom/src/js/inject.js @ 29:acdcdf1a8d21

repository reorganized
author cin
date Tue, 26 Jun 2018 19:35:44 +0300
parents src/implab/dom/inject.js@c2a7d21175ce
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
1 define(["../Deferred", "../log/trace!"], function (Deferred, trace) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
2 function on(node, eventName, handler) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
3 // Add an event listener to a DOM node
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
4 node.addEventListener(eventName, handler, false);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
5
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
6 return function () {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
7 node.removeEventListener(eventName, handler, false);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
8 };
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
9 }
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
10
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
11 return {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
12 injectionPoint: document.head,
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
13 injectBefore: document.head.firstChild,
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
14
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
15 _map: {},
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
16
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
17 _inject: function (name, attr) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
18 var node = document.createElement(name),
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
19 d = new Deferred(),
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
20
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
21 cleanup = function () {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
22 noerr();
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
23 noload();
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
24 },
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
25
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
26 noload = on(node, "load", function () {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
27 d.resolve({
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
28 node: node
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
29 });
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
30 cleanup();
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
31 }, true),
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
32
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
33 noerr = on(node, "error", function (e) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
34 d.reject({
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
35 erorr: e,
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
36 node: node
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
37 });
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
38 cleanup();
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
39 }, true);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
40
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
41 for (var p in attr)
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
42 node[p] = attr[p];
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
43
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
44 this.injectionPoint.insertBefore(node, this.injectBefore);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
45 return d;
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
46 },
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
47
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
48 injectScript: function (url) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
49 var d = this._map[url];
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
50 if (!d) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
51 trace.log("js {0}", url);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
52 d = this._inject("script", {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
53 type: "text/javascript",
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
54 charset: "utf-8",
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
55 src: url
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
56 });
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
57 d.then(function () {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
58 trace.log("done {0}", url);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
59 }, function (e) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
60 trace.err([url, e]);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
61 });
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
62 this._map[url] = d;
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
63 }
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
64 return d;
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
65 },
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
66
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
67 injectStylesheet: function (url) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
68 var d = this._map[url];
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
69 if (!d) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
70 trace.log("css {0}", url);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
71 d = this._inject("link", {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
72 type: "text/css",
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
73 rel: "stylesheet",
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
74 href: url
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
75 });
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
76 d.then(function () {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
77 trace.log("done {0}", url);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
78 }, function (e) {
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
79 trace.error([url, e]);
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
80 });
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
81 this._map[url] = d;
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
82 }
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
83 return d;
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
84 }
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
85 };
c2a7d21175ce Added css injection plugin, implab/dom/css!<css-path>
cin
parents:
diff changeset
86 });