This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
forked from justin-lau/ember-intl-tel-input
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
197 lines (163 loc) · 5.7 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
/* eslint-env node */
'use strict';
const Funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
const defaultsDeep = require('lodash.defaultsdeep');
const SassCompiler = require('broccoli-sass-source-maps');
const createFile = require('broccoli-file-creator');
const chalk = require('chalk');
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const log = (message) => {
return chalk.dim('[ember-intl-tel-input]') + ': ' + message;
};
const warn = (message) => {
return log(chalk.bgYellow(chalk.bold("WARN")) + ' ' + message);
};
const defaultOptions = {
includeUtilsScript: false,
utilsScript: 'assets/intl-tel-input/js/utils.js',
exportFlagsImages: true,
cssVariables: {
hoverColor: null,
greyText: null,
greyBorder: null,
flagHeight: null,
flagWidth: null,
flagPadding: null,
borderWidth: null,
arrowHeight: null,
arrowWidth: null,
triangleBorder: null,
arrowPadding: null,
arrowColor: null,
inputPadding: null,
selectedFlagWidth: null,
selectedFlagArrowWidth: null,
selectedFlagDialCodeWidth: null,
selectedFlagArrowDialCodeWidth: null,
flagsImagePath: '"images/"',
flagsImageName: null,
flagsImageExtension: null,
mobilePopupMargin: null,
}
};
let variablesToExport = ['includeUtilsScript', 'utilsScript'];
module.exports = {
name: 'ember-intl-tel-input',
included(app, parentAddon) {
this._super.included.apply(this, arguments);
let target = (parentAddon || app);
let isTesting = EmberApp.env() === 'test';
let options = target.options['ember-intl-tel-input'];
this._options = defaultsDeep(options, defaultOptions);
if (this._options.exportFlagsImages) {
if (this._options.cssVariables.flagsImageName || this._options.cssVariables.flagsImageExtension) {
this.ui.writeLine(warn(`${chalk.bold("cssVariables.flagsImageName")} and ${chalk.bold("cssVariables.flagsImageExtension")}
options will be ignored due ${chalk.bold("exportFlagsImages")} is ${chalk.green("true")}`));
}
this._options.cssVariables.flagsImageName = null;
this._options.cssVariables.flagsImageExtension = null;
}
// Remove initial '.' of flagsImageExtension
if (this._options.cssVariables.flagsImageExtension && this._options.cssVariables.flagsImageExtension.charAt(0) === '.') {
this._options.cssVariables.flagsImageExtension = this._options.cssVariables.flagsImageExtension.substr(1);
}
this._scssString = this.createScssStringWithVariables(this._options.cssVariables);
this._shouldBuildCss = this._scssString !== null;
if (isTesting) {
variablesToExport = [];
}
this._jsOptionsString = this.createJsOptionsString(this._options);
this._jsOptionsFilename = 'ember-intl-tel-input/variables.js';
if (this._options.includeUtilsScript) {
let outputFile = this._options.utilsScript
if (isTesting) {
outputFile = undefined;
}
target.import('vendor/intl-tel-input/js/utils.js', { outputFile });
}
target.import(`vendor/${this._jsOptionsFilename}`);
target.import({
development: 'vendor/intl-tel-input/js/intlTelInput.js',
production: 'vendor/intl-tel-input/js/intlTelInput.min.js'
});
target.import('vendor/intl-tel-input/css/intlTelInput.css');
},
createScssStringWithVariables(cssVariables) {
let fileString = '';
let defaults = true;
for (let variable in cssVariables) {
let value = cssVariables[variable];
if (value && value !== null) {
defaults = false;
fileString += `$${variable}: ${value};\n`;
}
}
if (defaults) {
return null;
} else {
fileString += '\n@import "intlTelInput";\n';
return fileString;
}
},
createJsOptionsString(options) {
let fileString = 'emberIntlTelInputConfig={';
let remove = false;
variablesToExport.forEach((variable) => {
let option = options[variable];
if (option) {
remove = true;
fileString += `${variable}:${JSON.stringify(option)},`;
}
});
if (remove) {
fileString = fileString.substr(0, fileString.length - 1);
}
fileString += '};'
return fileString;
},
treeForVendor(vendorTree) {
let trees = [];
if (vendorTree) {
trees.push(vendorTree);
}
let intlTelInputJsTree = new Funnel('node_modules/intl-tel-input/build', {
destDir: 'intl-tel-input',
include: [/js\/.*\.js$/],
});
trees.push(intlTelInputJsTree);
trees.push(createFile(this._jsOptionsFilename, this._jsOptionsString));
let intlTelInputCssTree;
if (this._shouldBuildCss) {
let scssBaseFilename = '/styles/base.scss';
let intlTelInputScssSrcTrees = [
new Funnel('node_modules/intl-tel-input/src/css'),
createFile(scssBaseFilename, this._scssString)
];
intlTelInputCssTree = new SassCompiler(intlTelInputScssSrcTrees, scssBaseFilename, 'intl-tel-input/css/intlTelInput.css');
} else {
intlTelInputCssTree = new Funnel('node_modules/intl-tel-input/build', {
destDir: 'intl-tel-input',
include: [/css\/.*\.css$/],
exclude: ['demo.css']
});
}
trees.push(intlTelInputCssTree);
return mergeTrees(trees);
},
treeForPublic(publicTree) {
let trees = [];
if (publicTree) {
trees.push(publicTree);
}
if (this._options.exportFlagsImages) {
let flagsImagePath = `assets/${this._options.cssVariables.flagsImagePath || '../img/'}`;
flagsImagePath = flagsImagePath.replace(/"|'/g, '');
let imagesTree = new Funnel('node_modules/intl-tel-input/build/img', {
destDir: flagsImagePath
});
trees.push(imagesTree);
}
return mergeTrees(trees);
}
};