forked from adopted-ember-addons/ember-paper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
676 lines (603 loc) · 16.6 KB
/
index.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
'use strict';
const path = require('path');
const resolve = require('resolve');
const version = require('./package.json').version;
const BroccoliMergeTrees = require('broccoli-merge-trees');
const writeFile = require('broccoli-file-creator');
const Funnel = require('broccoli-funnel');
const AngularScssFilter = require('./lib/angular-scss-filter');
const fastbootTransform = require('fastboot-transform');
/**
* Component dependencies, extracted from ember-bootstrap
* https://github.com/kaliber5/ember-bootstrap/blob/master/index.js
*/
const componentDependencies = {
'paper-autocomplete': {
styles: [
'components/autocomplete/autocomplete.scss',
'components/autocomplete/autocomplete-theme.scss'
],
dependencies: [
'paper-autocomplete/ebd-trigger',
'paper-autocomplete/eps-trigger',
'paper-autocomplete/ebd-content',
'paper-autocomplete/options',
'paper-autocomplete/no-matches-message',
'paper-autocomplete/highlight'
]
},
'paper-autocomplete/eps-trigger': {
dependencies: [
'paper-progress-linear',
'paper-input',
'paper-autocomplete/reset-button',
'paper-icon'
]
},
'paper-backdrop': {
styles: [
'components/backdrop/backdrop.scss',
'components/backdrop/backdrop-theme.scss'
]
},
'paper-button': {
styles: [
'components/button/button.scss',
'components/button/button-theme.scss'
]
},
'paper-card': {
styles: [
'components/card/card.scss',
'components/card/card-theme.scss'
],
dependencies: [
'paper-card-title',
'paper-card-content',
'paper-card-actions',
'paper-card-header',
'paper-card-image',
'paper-card-media'
]
},
'paper-card-actions': {
dependencies: [
'paper-card-icon-actions'
]
},
'paper-card-title': {
dependencies: [
'paper-card-title-text',
'paper-card-title-media'
]
},
'paper-card-title-text': {
dependencies: [
'paper-card-header-headline',
'paper-card-header-subhead'
]
},
'paper-card-header': {
dependencies: [
'paper-card-header-text',
'paper-card-avatar'
]
},
'paper-card-header-text': {
dependencies: [
'paper-card-header-title',
'paper-card-header-subhead'
]
},
'paper-checkbox': {
styles: [
'components/checkbox/checkbox.scss',
'components/checkbox/checkbox-theme.scss'
]
},
'paper-chips': {
styles: [
'components/chips/chips.scss',
'components/chips/chips-theme.scss'
],
dependencies: [
'paper-autocomplete',
'paper-icon'
]
},
'paper-contact-chips': {
dependencies: [
'paper-chips'
]
},
'paper-content': {
styles: [
'components/content/content.scss',
'components/content/content-theme.scss'
]
},
'paper-dialog': {
styles: [
'components/dialog/dialog.scss',
'components/dialog/dialog-theme.scss'
],
dependencies: [
'paper-dialog-actions',
'paper-backdrop',
'paper-dialog-container',
'paper-dialog-content',
'paper-dialog-inner'
]
},
'paper-divider': {
styles: [
'components/divider/divider.scss',
'components/divider/divider-theme.scss'
]
},
'paper-form': {
dependencies: [
'paper-input',
'paper-select',
'paper-autocomplete',
'paper-button'
]
},
'paper-grid-list': {
styles: [
'components/gridList/grid-list.scss'
],
dependencies: [
'paper-grid-tile'
]
},
'paper-grid-tile': {
dependencies: [
'paper-grid-tile-footer'
]
},
'paper-icon': {
styles: [
'components/icon/icon.scss',
'components/icon/icon-theme.scss'
]
},
'paper-input': {
styles: [
'components/input/input.scss',
'components/input/input-theme.scss'
]
},
'paper-item': {
dependencies: [
'paper-checkbox',
'paper-button',
'paper-switch',
'paper-radio-proxiable'
]
},
'paper-list': {
styles: [
'components/list/list.scss',
'components/list/list-theme.scss'
]
},
'paper-radio-proxiable': {
dependencies: [
'paper-radio-base'
]
},
'paper-menu': {
styles: [
'components/menu/menu.scss',
'components/menu/menu-theme.scss'
],
dependencies: [
'paper-menu/trigger',
'paper-menu/content'
]
},
'paper-menu/content': {
dependencies: [
'paper-backdrop',
'paper-menu/item'
]
},
'paper-menu/item': {
dependencies: [
'paper-button'
]
},
'paper-progress-circular': {
styles: [
'components/progressCircular/progress-circular.scss',
'components/progressCircular/progress-circular-theme.scss'
]
},
'paper-progress-linear': {
styles: [
'components/progressLinear/progress-linear.scss',
'components/progressLinear/progress-linear-theme.scss'
]
},
'paper-radio': {
dependencies: [
'paper-radio-base'
]
},
'paper-radio-base': {
styles: [
'components/radioButton/radio-button.scss',
'components/radioButton/radio-button-theme.scss'
]
},
'paper-radio-group': {
dependencies: [
'paper-radio'
]
},
'paper-select': {
styles: [
'components/select/select.scss',
'components/select/select-theme.scss'
],
dependencies: [
'paper-select/ebd-trigger',
'paper-select/eps-trigger',
'paper-select/ebd-content',
'paper-select/options',
'paper-select/search',
'paper-select/no-matches-message',
'paper-select/search-message',
]
},
'paper-select/options': {
dependencies: [
'paper-progress-circular',
'paper-select/option'
]
},
'paper-select/ebd-content': {
dependencies: [
'paper-backdrop'
]
},
'paper-sidenav': {
styles: [
'components/sidenav/sidenav.scss',
'components/sidenav/sidenav-theme.scss'
],
dependencies: [
'paper-backdrop',
'paper-sidenav-inner'
]
},
'paper-slider': {
styles: [
'components/slider/slider.scss',
'components/slider/slider-theme.scss'
]
},
'paper-speed-dial': {
styles: [
'components/fabSpeedDial/fabSpeedDial.scss'
],
dependencies: [
'paper-speed-dial-trigger',
'paper-speed-dial-actions'
]
},
'paper-subheader': {
styles: [
'components/subheader/subheader.scss',
'components/subheader/subheader-theme.scss'
]
},
'paper-speed-dial-actions': {
dependencies: [
'paper-speed-dial-actions-action'
]
},
'paper-switch': {
styles: [
'components/switch/switch.scss',
'components/switch/switch-theme.scss'
]
},
'paper-tabs': {
styles: [
'components/tabs/tabs.scss',
'components/tabs/tabs-theme.scss'
],
dependencies: [
'paper-tab',
'paper-ink-bar',
'paper-icon'
]
},
'paper-toast': {
styles: [
'components/toast/toast.scss',
'components/toast/toast-theme.scss'
],
dependencies: [
'paper-toast-inner',
'paper-toast-text'
]
},
'paper-toaster': {
dependencies: [
'paper-toast',
'paper-button'
]
},
'paper-toolbar': {
styles: [
'components/toolbar/toolbar.scss',
'components/toolbar/toolbar-theme.scss'
],
dependencies: [
'paper-toolbar-tools'
]
},
'paper-tooltip': {
styles: [
'components/tooltip/tooltip.scss',
'components/tooltip/tooltip-theme.scss'
],
dependencies: [
'paper-tooltip-inner'
]
}
};
module.exports = {
name: require('./package').name,
options: {
polyfills: {
'polyfill-nodelist-foreach': {
files: ['index.js'],
// compatibility from https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach
browsers: ['ie > 0', 'chrome < 52', 'ff < 50', 'opera < 38', 'safari < 10', 'edge < 16', 'android < 51', 'and_chr < 51', 'and_ff < 50', 'ios_saf < 10', 'Samsung < 5']
},
'classlist-polyfill': {
files: ['src/index.js'],
caniuse: 'classlist'
},
'element-closest': {
files: ['browser.js'],
caniuse: 'element-closest'
},
'matchmedia-polyfill': {
files: ['matchMedia.js'],
caniuse: 'matchmedia'
}
}
},
included() {
this._super.included.apply(this, arguments);
let app;
// If the addon has the _findHost() method (in ember-cli >= 2.7.0), we'll just
// use that.
if (typeof this._findHost === 'function') {
app = this._findHost();
} else {
// Otherwise, we'll use this implementation borrowed from the _findHost()
// method in ember-cli.
let current = this;
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
}
this.emberPaperOptions = Object.assign({}, app.options['ember-paper']);
app.import('vendor/ember-paper/register-version.js');
app.import('vendor/hammerjs/hammer.js');
app.import('vendor/propagating-hammerjs/propagating.js');
},
config() {
return {
'ember-paper': {
insertFontLinks: true
}
};
},
contentFor(type, config) {
if (type === 'head') {
if (config['ember-paper'].insertFontLinks) {
let whitelist = this.emberPaperOptions.whitelist || [];
let blacklist = this.emberPaperOptions.blacklist || [];
let links = '<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">';
let paperIconNotWhitelisted = whitelist.length && !whitelist.includes('paper-icon');
let paperIconBlacklisted = blacklist.length && blacklist.includes('paper-icon');
if (paperIconNotWhitelisted || paperIconBlacklisted) {
return links;
}
return `${links} <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">`;
}
} else if (type === 'body-footer') {
let response = null;
let emberPowerSelect = this.addons.filter(function(addon) {
return addon.name === 'ember-power-select';
})[0];
response = emberPowerSelect.contentFor(type, config);
if (config.environment !== 'test' && !config._emberPaperContentForInvoked) {
config._emberPaperContentForInvoked = true;
response = `
${response || ''}
<div id="paper-wormhole"></div>
<div id="paper-toast-fab-wormhole"></div>
`;
}
return response;
}
},
treeForVendor(tree) {
let trees = [];
let versionTree = writeFile(
'ember-paper/register-version.js',
`Ember.libraries.register('Ember Paper', '${version}');`
);
let hammerJs = fastbootTransform(new Funnel(this.pathBase('hammerjs'), {
files: ['hammer.js'],
destDir: 'hammerjs'
}));
let propagatingHammerJs = fastbootTransform(new Funnel(this.pathBase('propagating-hammerjs'), {
files: ['propagating.js'],
destDir: 'propagating-hammerjs'
}));
trees = trees.concat([hammerJs, propagatingHammerJs, versionTree]);
if (tree) {
trees.push(tree);
}
return new BroccoliMergeTrees(trees);
},
treeForStyles(tree) {
let coreScssFiles = [
// core styles
'core/style/mixins.scss',
'core/style/variables.scss',
'core/style/structure.scss',
'core/style/typography.scss',
'core/style/layout.scss',
'core/services/layout/layout.scss',
// TODO: Move to core, if we don't import menu, it breaks.
'components/menu/menu.scss',
'components/menu/menu-theme.scss',
// Need to find which components rely on this, otherwise, move to core.
'components/whiteframe/whiteframe.scss',
'components/panel/panel.scss',
'components/panel/panel-theme.scss'
];
let filteredScssFiles = this.addStyles(coreScssFiles) || coreScssFiles;
let angularScssFiles = new Funnel(this.pathBase('angular-material-styles'), {
files: filteredScssFiles,
srcDir: '/src',
destDir: 'angular-material',
annotation: 'AngularScssFunnel'
});
angularScssFiles = new AngularScssFilter(angularScssFiles);
let importer = writeFile(
'ember-paper-components.scss',
filteredScssFiles.map((path) => `@import './angular-material/${path}';`).join('\n')
);
let mergedTrees = new BroccoliMergeTrees([angularScssFiles, importer, tree], { overwrite: true });
return this._super.treeForStyles(mergedTrees);
},
/*
Rely on the `resolve` package to mimic node's resolve algorithm.
It finds the angular-material-source module in a manner that works for npm 2.x,
3.x, and yarn in both the addon itself and projects that depend on this addon.
This is an edge case b/c angular-material-source does not have a main
module we can require.resolve through node itself and similarily ember-cli
does not have such a hack for the same reason.
tl;dr - We want the non built scss files, and b/c this dep is only provided via
git, we use this hack. Please change it if you read this and know a better way.
*/
pathBase(packageName) {
return path.dirname(resolve.sync(`${packageName}/package.json`, { basedir: __dirname }));
},
treeForApp(tree) {
tree = this.filterComponents(tree);
return this._super.treeForApp.call(this, tree);
},
treeForAddon(tree) {
tree = this.filterComponents(tree);
return this._super.treeForAddon.call(this, tree);
},
treeForAddonTemplates(tree) {
tree = this.filterComponents(tree);
return this._super.treeForAddonTemplates.call(this, tree);
},
/**
* This function will push styles using whitelist and blacklist
* @param {Array} core - The core scss files
* @return {Array} - New array with styles appended
*/
addStyles(core = []) {
let whitelist = this.generateWhitelist(this.emberPaperOptions.whitelist);
let blacklist = this.emberPaperOptions.blacklist || [];
let styles = core.slice();
// add everything if no opts defined
if (whitelist.length === 0 && blacklist.length === 0) {
Object.keys(componentDependencies).forEach((key) => {
this.addComponentStyle(styles, componentDependencies[key]);
});
}
// build array from whitelist
if (whitelist.length && blacklist.length === 0) {
whitelist.forEach((component) => {
this.addComponentStyle(styles, componentDependencies[component]);
});
}
// add all but blacklisted
if (blacklist.length && whitelist.length === 0) {
Object.keys(componentDependencies).forEach((key) => {
if (!blacklist.includes(key)) {
this.addComponentStyle(styles, componentDependencies[key]);
}
});
}
return styles;
},
/**
* Validate if the object exists in componentDependencies and has any styles
* if so, add them to the arr
*
* @param {Array} arr - Styles array
* @param {Object} component - componentDependencies[key]
*/
addComponentStyle(arr, component) {
if (component && component.styles) {
component.styles.forEach((scss) => {
if (!arr.includes(scss)) {
arr.push(scss);
}
});
}
},
filterComponents(tree) {
let whitelist = this.generateWhitelist(this.emberPaperOptions.whitelist);
let blacklist = this.emberPaperOptions.blacklist || [];
// exit early if no opts defined
if (whitelist.length === 0 && blacklist.length === 0) {
return tree;
}
return new Funnel(tree, {
exclude: [(name) => this.excludeComponent(name, whitelist, blacklist)]
});
},
excludeComponent(name, whitelist, blacklist) {
let regex = /^(templates\/)?components\/(base\/)?/;
let isComponent = regex.test(name);
if (!isComponent) {
return false;
}
let baseName = name.replace(regex, '');
baseName = baseName.replace(/(\/component|\/template\b)/, '');
baseName = baseName.substring(0, baseName.lastIndexOf('.'));
let isWhitelisted = whitelist.indexOf(baseName) !== -1;
let isBlacklisted = blacklist.indexOf(baseName) !== -1;
if (whitelist.length === 0 && blacklist.length === 0) {
return false;
}
if (whitelist.length && blacklist.length === 0) {
return !isWhitelisted;
}
return isBlacklisted;
},
generateWhitelist(whitelist) {
let list = [];
if (!whitelist) {
return list;
}
function _addToWhitelist(item) {
if (list.indexOf(item) === -1) {
list.push(item);
if (componentDependencies[item] && componentDependencies[item].dependencies) {
componentDependencies[item].dependencies.forEach(_addToWhitelist);
}
}
}
whitelist.forEach(_addToWhitelist);
return list;
}
};