-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdownload.js
441 lines (380 loc) · 13.4 KB
/
download.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
/* eslint-env jquery, browser */
/*global Hash: false, JST: false, Model: false */
/*!
* jQuery UI DownloadBuilder client-side JavaScript file
* https://jqueryui.com/download/
*
* Copyright OpenJS Foundation and other contributors
* Released under the MIT license.
* https://jquery.org/license
*/
( function( $, Hash, JST, Model ) {
"use strict";
var dependencies, dependents, model,
componentsLoad = $.Deferred(),
downloadBuilder = $( "#download-builder" ),
themesLoad = $.Deferred(),
baseVars = downloadBuilder.data( "base-vars" ),
downloadJqueryuiHost = downloadBuilder.data( "download-jqueryui-host" ),
initialComponents = downloadBuilder.data( "initial-components" );
// Rewrite form action for testing on staging
if ( /^stage\./.test( location.host ) ) {
$( "#download-builder form" ).attr( "action", function( index, href ) {
return href.replace( /(download\.)/, "stage.$1" );
} );
downloadJqueryuiHost = downloadJqueryuiHost.replace( /(download\.)/, "stage.$1" );
}
function pluralize( count, singular, plural ) {
return count === 1 ? singular : plural;
}
function componentsFetch() {
return $.ajax( downloadJqueryuiHost + "/download/components/", {
dataType: "jsonp",
data: {
version: model.get( "version" )
}
} );
}
function renderServiceStatus( xhr ) {
var element = $( JST[ "service_status.html" ]( {
status: xhr.status,
statusText: xhr.statusText,
responseText: xhr.responseText
} ) );
$( "#service-status" ).html( element );
}
function themeFetch() {
var dfd = $.Deferred();
model.themeUrl( function( url ) {
$.ajax( url, {
dataType: "jsonp"
} ).done( dfd.resolve ).fail( dfd.reject );
} );
return dfd;
}
function allComponents() {
return $( "#download-builder .component-group-list input[type=checkbox]" );
}
function downloadOnOff() {
if ( !allComponents().filter( ":checked" ).length && $( "#theme" ).val() === "none" ) {
$( "#download-builder input[type=submit]" ).prop( "disabled", true ).addClass( "ui-state-disabled" );
} else {
$( "#download-builder input[type=submit]" ).prop( "disabled", false ).removeClass( "ui-state-disabled" );
}
}
function allGroup( referenceElement ) {
return $( referenceElement ).closest( ".component-group" ).find( ".component-group-list input[type=checkbox]" );
}
function _check( elem, value, options ) {
var modelUpdates = {},
depElem = $();
options = options || {};
elem.each( function() {
var elem = $( this ),
name = elem.attr( "name" );
// Handle dependencies
if ( value && !options.skipDependencies ) {
if ( dependencies[ name ] ) {
// Whenever a checkbox is activated, also activate all dependencies
depElem = depElem.add( dependencies[ name ] );
}
} else if ( dependents[ name ] && !options.skipDependencies ) {
// Whenever a checkbox is deactivated, also deactivate all dependents
depElem = depElem.add( dependents[ name ] );
}
elem.prop( "checked", value );
modelUpdates[ name ] = value;
} );
// Update dependencies
if ( depElem.length ) {
_check( depElem, value );
}
// Update toggle all
if ( value ) {
// Set group toggle all if all components of its group are checked
if ( !allGroup( elem ).filter( ":not(:checked)" ).length ) {
$( elem ).closest( ".component-group" ).find( ".toggle input[type=checkbox]" ).prop( "checked", true );
}
// Set toggle all if all components are checked
if ( !allComponents().filter( ":not(:checked)" ).length ) {
$( elem ).closest( ".components" ).prev().find( ".toggleAll input[type=checkbox]" ).prop( "checked", true );
}
} else {
// Unset group toggle all if no components of its group are checked
if ( !allGroup( elem ).filter( ":checked" ).length ) {
$( elem ).closest( ".component-group" ).find( ".toggle input[type=checkbox]" ).prop( "checked", false );
}
// Unset toggle all if no components are checked
if ( !allComponents().filter( ":checked" ).length ) {
$( elem ).closest( ".components" ).prev().find( ".toggleAll input[type=checkbox]" ).prop( "checked", false );
}
}
model.set( modelUpdates );
downloadOnOff();
}
function check( event, elem, value, options ) {
var consolidatedDependents, consolidatedNames;
// Uncheck validations
if ( !value ) {
consolidatedDependents = $();
consolidatedNames = [];
elem.each( function() {
var name = $( this ).attr( "name" );
if ( dependents[ name ] && dependents[ name ].filter( ":checked" ).not( elem ).length > 0 ) {
consolidatedNames.push( name );
consolidatedDependents = consolidatedDependents.add( dependents[ name ].filter( ":checked" ).not( elem ) );
}
} );
// Validate if uncheck is allowed when it has dependents
if ( consolidatedDependents.length > 0 ) {
event.preventDefault();
$( "<div>" )
.attr( "title", "Remove " + consolidatedNames.join( ", " ) + "?" )
.append(
$( "<p>" ).html(
"Are you sure you want to remove <b>" + consolidatedNames.join( ", " ) + "</b>? The following " + pluralize( consolidatedDependents.length, "component", "components" ) + " " + pluralize( consolidatedDependents.length, "depends", "depend" ) + " on it and will be removed: " + consolidatedDependents.map( function() {
return "<b>" + this.name + "</b>";
} ).toArray().join( ", " ) + "."
)
)
.dialog( {
modal: true,
buttons: {
"Remove": function() {
_check( elem, value, options );
$( this ).remove();
},
"Cancel": function() {
$( this ).remove();
}
}
} )
.dialog( "widget" ).addClass( "download-builder-dialog" );
} else {
_check( elem, value, options );
}
// Check validations (none)
} else {
_check( elem, value, options );
}
}
function drawToggleAll( className ) {
return $( "<label>" )
.addClass( className )
.text( " Toggle All" )
.prepend(
$( "<input type=checkbox>" )
.prop( "checked", true )
.addClass( "ui-widget-content" )
);
}
function initComponents( components ) {
var toggleAll;
dependencies = {};
dependents = {};
$( "#download-builder .components-area" ).html( JST[ "components.html" ]( components ) );
model.setOrderedComponents(
$( "#download-builder .components-area input[type=checkbox]" ).map( function() {
return this.name;
} )
);
// Initializes dependencies and dependents auxiliary variables.
$( "#download-builder input[type=checkbox]" ).each( function() {
var checkbox = $( this ),
thisDependencies = checkbox.data( "dependencies" ),
thisName = checkbox.attr( "name" );
if ( !thisName || !thisDependencies ) {
return;
}
// Adjust path prefixes to match names
var path = thisName.match( /^(.+)\// );
thisDependencies = thisDependencies.split( "," ).map( function( dependency ) {
if ( !path ) {
return dependency;
}
if ( /\.\.\//.test( dependency ) ) {
return dependency.replace( /^.+\//, "" );
}
return path[ 1 ] + "/" + dependency;
} );
dependencies[ thisName ] = $();
$.each( thisDependencies, function() {
var dependecy = this,
dependecyElem = $( ".components-area input[type=checkbox][name='" + this + "']" );
dependencies[ thisName ] = dependencies[ thisName ].add( dependecyElem );
if ( !dependents[ dependecy ] ) {
dependents[ dependecy ] = $();
}
dependents[ dependecy ] = dependents[ dependecy ].add( checkbox );
} );
} );
// Generating toggle all checkboxes
if ( !( toggleAll = $( "#download-builder .toggleAll" ) ).length ) {
toggleAll = drawToggleAll( "toggleAll" ).insertAfter( $( "#download-builder .components-area" ).prev().find( "h2" ) );
}
$( "#download-builder .component-group h3" ).after( drawToggleAll( "toggle" ) );
/* Remember components check/uncheck selection
- If a component is checked/unchecked, it should keep its check-state in a subsequent version-change or page-load;
- If a component is loaded in the page and there is no previous check-state for it, it should be checked unless it has an unchecked dependency;
*/
allComponents().each( function() {
var elem = $( this ),
name = elem.attr( "name" );
if ( model.has( name ) && model.get( name ) === false ) {
_check( elem, false );
}
} );
// Binds click handlers on components checkboxes
toggleAll.find( "input[type=checkbox]" ).on( "click", function( event ) {
check( event, allComponents(), $( this ).prop( "checked" ), {
skipDependencies: true
} );
} );
$( "#download-builder .components-area input[type=checkbox]" ).on( "click", function( event ) {
var target = $( event.target );
if ( target.parent().is( ".toggle" ) ) {
check( event, allGroup( this ), $( this ).prop( "checked" ) );
} else {
check( event, $( this ), $( this ).prop( "checked" ) );
}
} );
componentsLoad.resolve();
}
model = new Model.DownloadBuilder( {
baseVars: baseVars,
host: downloadJqueryuiHost
} );
model.defaults.version = $( "#download-builder [name=version][checked]" ).val();
model.on( "change", function( changed, created ) {
var versionElement;
if ( "folderName" in changed && !model.get( "folderName" ).length ) {
delete model.attributes.folderName;
delete changed.folderName;
}
if ( "scope" in changed && !model.get( "scope" ).length ) {
delete model.attributes.scope;
delete changed.scope;
}
themesLoad.done( function() {
var themeOption;
if ( "folderName" in changed ) {
$( "#theme-folder-name" ).val( model.get( "folderName" ) ).trigger( "change" );
}
if ( "scope" in changed ) {
$( "#scope" ).val( model.get( "scope" ) ).trigger( "change" );
}
if ( "themeParams" in changed ) {
themeOption = $( "#theme option[value=\"" + model.get( "themeParams" ) + "\"]" );
if ( !themeOption.filter( ":selected" ) ) {
themeOption.prop( "selected", true ).trigger( "change" );
}
}
model.themerollerUrl( function( url ) {
$( "#download-builder .download-builder-header a.themeroller-link" ).attr( "href", url );
} );
} );
componentsLoad.done( function() {
$.each( changed, function( attribute ) {
var value = model.get( attribute );
// If attribute is a component.
$( "#download-builder .component-group-list input[name=\"" + attribute + "\"]" ).each( function() {
// "false" -> false, TODO: this should be handled at History() level.
if ( value === "false" ) {
model.attributes[ attribute ] = value = false;
}
if ( value === false && $( this ).prop( "checked" ) !== value ) {
_check( $( this ), false );
}
// Ignore checked-components in the model
if ( value ) {
delete model.attributes[ attribute ];
}
} );
} );
} );
if ( "version" in changed ) {
versionElement = $( "#download-builder input[type=radio][name=version][value=\"" + model.get( "version" ) + "\"]" );
versionElement.trigger( "click" );
if ( created.version ) {
initComponents( initialComponents );
} else {
model.unsetOrderedComponents();
componentsFetch().done( function( components ) {
initComponents( components );
} ).fail( renderServiceStatus );
componentsLoad = $.Deferred();
}
}
model.querystring().done( function( querystring ) {
Hash.update( querystring, {
ignoreChange: true
} );
} );
} );
Hash.on( "change", function( hash ) {
model.parseHash( hash );
} );
// Binds click on version selection
$( "#download-builder [name=version]" ).on( "change", function() {
model.set( { version: $( this ).val() } );
} );
model.set( {
version: model.defaults.version
} );
Hash.init();
/* jqueryui.com site overrides for DB */
$( "#content" ).attr( "id", "download-builder-content" );
// Loads theme section.
themeFetch().done( function( theme ) {
$( "#download-builder .theme-area" ).html( JST[ "theme.html" ]( theme ) );
if ( !model.has( "themeParams" ) && !model.has( "zThemeParams" ) ) {
model.defaults.themeParams = $( "#theme option:nth-child(2)" ).val();
model.set( { themeParams: $( "#theme option:selected" ).val() } );
}
$( "#theme" ).on( "change", function() {
var selected = $( this ).find( "option:selected" ),
folderName = selected.text().toLowerCase().replace( " ", "-" );
model.defaults.folderName = folderName;
model.set( {
folderName: folderName,
themeParams: selected.val()
} );
downloadOnOff();
} );
$( "#download-builder .advanced-settings input" ).each( function() {
var content = $( this ).next().detach();
$( this ).tooltip( {
items: "*",
content: function() {
return content;
}
} );
} );
$( "#scope" ).on( "change", function() {
if ( !$( "#theme-folder-name" ).data( "edited" ) ) {
$( "#theme-folder-name" ).data( "suggestedEdit", true );
model.set( { folderName: $( this ).val().replace( /[ \/\\]/g, "-" ).replace( /[\.\#]/g, "" ) } );
}
model.set( { scope: $( this ).val() } );
} );
$( "#theme-folder-name" ).on( {
"blur": function() {
if ( $( this ).val() === "" ) {
$( "#theme-folder-name" ).removeData( "edited" );
}
},
"change": function() {
var val = $( this ).val(),
escapedVal = val.replace( /[ \.\#\/\\]/g, "-" );
$( this ).data( "edited", true );
$( "#theme-folder-name" ).removeData( "suggestedEdit" );
if ( escapedVal !== val ) {
model.set( { folderName: escapedVal } );
} else {
model.set( { folderName: $( this ).val() } );
}
}
} );
themesLoad.resolve();
} ).fail( renderServiceStatus );
} )( jQuery, Hash, JST, Model );