-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlib-loader.js
More file actions
130 lines (105 loc) · 3.46 KB
/
lib-loader.js
File metadata and controls
130 lines (105 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Polymer = window.Polymer;
var CustomEvent = window.CustomEvent;
var libLoader = function () {
function libLoader() {
_classCallCheck(this, libLoader);
}
_createClass(libLoader, [{
key: 'beforeRegister',
// Element setup goes in beforeRegister instead of createdCallback.
value: function beforeRegister() {
this.is = 'lib-loader';
// Define the properties object in beforeRegister.
this.properties = {
/** Link of the library */
lib: {
type: String
},
/** <script id="" */
libUniqueId: {
type: String
},
/** True when the lib is ready */
libReady: {
type: Boolean,
value: false,
reflectToAttribute: true
}
};
}
// on ready try to insert the lib on the page
}, {
key: 'attached',
value: function attached() {
var _this = this;
if (!this.lib || !this.libUniqueId) {
console.error('<lib-loader> ERROR: Library or unique id not specified.');
return false;
}
this.addEventListener('lib-loaded', function (evt) {
_this.set('libReady', true);
});
this._insertLib(this.lib, this.libUniqueId);
}
}, {
key: 'detached',
value: function detached() {
this.removeLib();
}
/** Remove lib from the dom */
}, {
key: 'removeLib',
value: function removeLib() {
var lib = document.querySelector('#' + this.libUniqueId);
lib ? lib.remove() : null;
}
/** ===============
* Private methods
**/
/* Insert at the end of the body the js lib */
}, {
key: '_insertLib',
value: function _insertLib(link, type) {
var _this2 = this;
// Check for existent lib
if (document.querySelector('#' + type)) {
document.querySelector('lib-loader').addEventListener('lib-loaded', function (evt) {
_this2._onLoadLib(evt, type);
});
return false;
}
var src = document.createElement('script');
src.setAttribute('src', link);
src.id = type;
src.async = true;
src.onreadystatechange = src.onload = function (evt) {
_this2._onLoadLib(evt, type);
};
document.body.appendChild(src);
}
/** ===============
* Event listeners
**/
/* On lib loaded */
}, {
key: '_onLoadLib',
value: function _onLoadLib(evt, type) {
var _this3 = this;
setTimeout(function () {
_this3.dispatchEvent(new CustomEvent('lib-loaded'));
});
}
}, {
key: 'behaviors',
// Define behaviors with a getter.
get: function get() {
return [];
}
}]);
return libLoader;
}();
// Register the element using Polymer's constructor.
Polymer(libLoader);