forked from DataTables/DataTablesSrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-loader-lib.js
352 lines (308 loc) · 8.76 KB
/
html-loader-lib.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* @summary DataTables testing suite, HTML and library loader
* @version 1.0.0
* @author SpryMedia Ltd (www.sprymedia.co.uk)
*
* This source file is free software, available under the following license:
* MIT license - http://datatables.net/license
*
* This source file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
*
* This loader is intended for use in the DataTables testing framework. I doubt
* it will be useful elsewhere since it is specifically tooled for the
* DataTables project's module structure, but it is MIT licensed should anyone
* feel they can get any benefit from it.
*
* The loader provides the global `dt` object which provides three methods:
*
* * `libs` - Load CSS and JS libraries. This method should be called only once
* per page. This takes a single object with the following parameters:
* * `framework` - Styling framework to load - for example `bootstrap` or
* `foundation`. Default is `datatables` if not provided.
* * `js` - Array of Javascript files to load - these are the key references
* to the files defined in the `client.libraries` object of the karma
* configuration object.
* * As the `js` array but in this case for CSS
*
* * `html` - Load HTML into a page from a given template. The `.html` extension
* should not be given.
*
* * `clean` - Clean up the HTML. The libraries loaded are retained, but the
* HTML is destroyed.
*
* The `dt.html` and `dt.libs` methods are async. This is done in Jasmine's
* testing environment by adding a test called `Load fixture` to the current
* spec.
*/
(function(window, jasmine) {
'use strict';
/**
* Sequentially load the CSS and JS files. When finished called the `done`
* method to tell Jasmine we are complete!
*/
function _runQueue(done, queue) {
var doc = window.document;
if (queue.length === 0) {
// Everything finally loaded
done();
} else {
var lib = queue.shift();
// CSS or JS?
if (lib.match(/js$/)) {
var script = doc.createElement('script');
script.onload = function() {
_runQueue(done, queue);
};
script.type = 'text/javascript';
script.src = lib;
doc.body.appendChild(script);
} else {
var link = doc.createElement('link');
link.onload = function() {
_runQueue(done, queue);
};
link.type = 'text/css';
link.href = lib;
link.rel = 'stylesheet';
doc.head.appendChild(link);
}
}
}
/**
* Convert the module name to a path to load
*/
function _pathResolver(type, path, framework) {
var config = window.__karma__.config;
var libs = config.libraries;
var lib = libs[path];
var urlBase = config.htmlLoader.builtRoot;
if (lib.pathName) {
// First party library
if (path === 'datatables') {
// DataTables is a special case...
if (type === 'css') {
return framework === 'dataTables'
? [urlBase + '/media/css/jquery.dataTables.css']
: [urlBase + '/media/css/dataTables.' + framework + '.css'];
} else {
return framework === 'dataTables'
? [urlBase + '/media/js/jquery.dataTables.js']
: [urlBase + '/media/js/jquery.dataTables.js', urlBase + '/media/js/dataTables.' + framework + '.js'];
}
} else {
if (type === 'css' && lib.css) {
return [urlBase + '/extensions/' + lib.pathName + '/css/' + lib.fileName + '.' + framework + '.css'];
} else if (type === 'js') {
var out = [urlBase + '/extensions/' + lib.pathName + '/js/dataTables.' + lib.fileName + '.js'];
if (lib.js && framework !== 'dataTables') {
out.push(urlBase + '/extensions/' + lib.pathName + '/js/' + lib.fileName + '.' + framework + '.js');
}
return out;
}
}
} else {
// Third party or additional
return lib[type] ? lib[type] : null;
}
return [];
}
/**
* Build a list of CSS and JS files that need to be loaded
*/
function _loadDeps(done, obj) {
var libraries = window.__karma__.config.libraries;
var doc = window.document;
var queue = [],
i,
ien;
// CSS
if (obj.css) {
for (i = 0, ien = obj.css.length; i < ien; i++) {
var resolved = _pathResolver('css', obj.css[i], obj.framework || 'dataTables');
queue = queue.concat(resolved);
}
}
// JS
if (obj.js) {
for (i = 0, ien = obj.js.length; i < ien; i++) {
var resolved = _pathResolver('js', obj.js[i], obj.framework || 'dataTables');
queue = queue.concat(resolved);
}
}
_runQueue(done, queue);
}
/**
* Get the HTML file and insert into the testing area
*/
function _loadHtml(done, file) {
var config = window.__karma__.config;
var url = config.htmlLoader.path + file + '.html';
// Create a container element
var doc = window.document;
var container = doc.createElement('div');
container.id = 'dt-test-loader-container';
doc.body.appendChild(container);
// Load the HTML needed
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
if (!xhr.responseText) {
console.error('Could not load file: ' + url);
}
container.innerHTML = xhr.responseText;
done();
});
xhr.open('GET', url);
xhr.send();
}
// Publicly exposed method
window.dt = {
libs: function(obj) {
window.it(
'Load libraries',
function(done) {
_loadDeps(done, obj);
},
5000
);
return window.dt;
},
html: function(extension, file) {
if (file === undefined) {
file = extension;
extension = undefined;
}
if (extension) {
file = '../../extensions/' + extension + '/test/html/' + file;
}
window.it(
'Load HTML: ' + file,
function(done) {
dt.clean();
_loadHtml(done, file);
},
5000
);
return window.dt;
},
clean: function() {
if ($) {
$('#dt-test-loader-container').remove();
} else {
var el = document.getElementById('dt-test-loader-container');
if (el) {
document.body.removeChild(el);
}
}
if ($ && $.fn.dataTableSettings && $.fn.dataTableSettings.length) {
$.fn.dataTableSettings.length = 0;
}
// Remove the detected browser settings so they can be recomputed
if ($ && $.fn.dataTable) {
$.fn.dataTable.__browser = undefined;
}
// Envelope display controller injects an element into the document body, which the
// above doesn't remove. So we remove it here
if ($) {
$('.DTED_Envelope_Background, .DTED_Envelope_Wrapper').remove();
}
// FixedHeader leaves it's header, plus reset scroll
if ($) {
$('table.fixedHeader-floating').remove();
$('html').scrollTop(0);
}
return window.dt;
},
container: function() {
return $('#dt-test-loader-container');
},
/*
* Below are common functions used througout the unit tests
*/
// check array for column visibility (default is visible)
areColumnsInvisible: function(colArray) {
let visiblity = $('#example')
.DataTable()
.columns()
.visible();
for (let i = 0; i < 6; i++) {
if (visiblity[i] == colArray.includes(i)) {
return false;
}
}
return true;
},
// check DOM for column's header, body, and footer
isColumnHBFExpected: function(column, header, body, footer = header) {
if (
$('#example thead th:eq(' + column + ')').text() == header &&
$('#example tbody tr:eq(' + column + ') td:eq(0)').text() == body &&
$('#example tfoot th:eq(' + column + ')').text() == footer
) {
return true;
}
return false;
},
// function to sleep for a bit
sleep: function(time) {
return new Promise(resolve => setTimeout(resolve, time));
},
// columns used during testing (used frequently)
_testColumns: [
{ data: 'name' },
{ data: 'position' },
{ data: 'office' },
{ data: 'age' },
{ data: 'start_date' },
{ data: 'salary' }
],
// makes a copy of the test columns so they can be modified
getTestColumns: function() {
return JSON.parse(JSON.stringify(this._testColumns));
},
// editor's config to set up the editor fields
_testEditorFields: [
{
label: 'Name:',
name: 'name'
},
{
label: 'Position:',
name: 'position'
},
{
label: 'Office:',
name: 'office'
},
{
label: 'Age:',
name: 'age'
},
{
label: 'Start date:',
name: 'start_date',
type: 'datetime'
},
{
label: 'Salary:',
name: 'salary'
}
],
// makes a copy of the test columns so they can be modified
getTestEditorColumns: function() {
return JSON.parse(JSON.stringify(this._testEditorFields));
},
// make a person object (as pain to do everytime in the test)
makePersonObject: function(name) {
return {
name: name,
position: 'BBB',
office: 'CCC',
age: '55',
start_date: '2018/03/04',
salary: '$12,345'
};
}
};
})(window, window.jasmine);