-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmodel.js
486 lines (430 loc) · 13.8 KB
/
model.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
/* eslint-env jquery, browser */
/*global EventEmitter: false, LZMA: false, QueryString: false */
/*!
* jQuery UI helper JavaScript file for DownloadBuilder and ThemeRoller models
* https://jqueryui.com/download/
*
* Copyright OpenJS Foundation and other contributors
* Released under the MIT license.
* https://jquery.org/license
*/
( function( exports, $, EventEmitter, LZMA, QueryString ) {
"use strict";
var Model, DownloadBuilderModel, ThemeRollerModel,
lzma = new LZMA( $( "[data-lzma-worker]" ).data( "lzma-worker" ) );
// Encodes an Array of booleans [ true, false, ... ] into a string sequence "10...".
function booleansEncode( array ) {
var string = "";
$.each( array, function( i, val ) {
string += val ? "1" : "0";
} );
return string;
}
// Decodes the string sequence "10..." back into an Array of booleans [ true, false, ... ].
function booleansDecode( string ) {
var array = [];
$.each( string.split( "" ), function( i, val ) {
array.push( val === "0" ? false : true );
} );
return array;
}
function omit( obj, keys ) {
var key,
copy = {};
for ( key in obj ) {
if ( $.inArray( key, keys ) === -1 ) {
copy[ key ] = obj[ key ];
}
}
return copy;
}
function pick( obj, keys ) {
var copy = {};
$.each( keys, function( i, key ) {
if ( key in obj ) {
copy[ key ] = obj[ key ];
}
} );
return copy;
}
function unzip( zipped, callback ) {
var data,
intoDec = function( hex ) {
var dec = parseInt( hex, 16 );
if ( dec >= 128 ) {
dec = dec - 256;
}
return dec;
};
// Split string into an array of hexes
data = [];
while ( zipped.length ) {
data.push( zipped.slice( -2 ) );
zipped = zipped.slice( 0, -2 );
}
data = data.reverse();
lzma.decompress( $.map( data, intoDec ), function( unzipped ) {
callback( JSON.parse( unzipped ) );
} );
}
function zip( obj, callback ) {
var data = JSON.stringify( obj ),
intoHex = function( byteFF ) {
var hex;
if ( byteFF < 0 ) {
byteFF = byteFF + 256;
}
hex = byteFF.toString( 16 );
// Add leading zero.
if ( hex.length === 1 ) {
hex = "0" + hex;
}
return hex;
};
lzma.compress( data, 0, function( zipped ) {
callback( $.map( zipped, intoHex ).join( "" ) );
} );
}
function zParam( paramName, attributes, callback ) {
if ( $.isEmptyObject( attributes ) ) {
callback( attributes );
} else {
zip( attributes, function( zipped ) {
var shorten,
original = QueryString.encode( attributes ).length,
shortenAttributes = {};
shortenAttributes[ paramName ] = zipped;
shorten = QueryString.encode( shortenAttributes ).length;
callback( shorten < original ? shortenAttributes : attributes );
} );
}
}
/**
* Model
*/
Model = function() {
this.attributes = {};
this.emitter = new EventEmitter();
this.on = $.proxy( this.emitter.on, this.emitter );
};
Model.prototype = {
set: function( attributes ) {
var self = this,
changed = false,
changedAttributes = {},
createdAttributes = {};
$.each( attributes, function( name ) {
if ( self.attributes[ name ] !== attributes[ name ] ) {
changedAttributes[ name ] = changed = true;
if ( !( name in self.attributes ) ) {
createdAttributes[ name ] = true;
}
self.attributes[ name ] = attributes[ name ];
}
} );
if ( changed ) {
this.emitter.trigger( "change:before", [ changedAttributes, createdAttributes ] );
this.emitter.trigger( "change", [ changedAttributes, createdAttributes ] );
}
return this;
},
get: function( name ) {
return this.attributes[ name ];
},
has: function( name ) {
return name in this.attributes;
},
/**
* querystring( [options] )
* Returns a Deferred with the model querystring when done.
* - options.concurrencyDelay; TimeoutID that should be cleared on this request. It's used to avoid concurrency of a certain group of calls/requests.
* - options.omit: Array of attributes to omit. Use pick or omit. Default: omit none;
* - options.pick: Array of attributes to pick. Use pick or omit. Default: pick all attributes;
* - options.shorten: A boolean whether to shorten the querystring. Default: true.
*/
querystring: function( options ) {
var self = this,
attributes = this.attributes,
dfd = $.Deferred(),
concurrencyDelay;
options = options || {};
if ( options.pick ) {
attributes = pick( attributes, options.pick );
} else if ( options.omit ) {
attributes = omit( attributes, options.omit );
}
concurrencyDelay = options.concurrencyDelay || this.querystringDelay;
if ( "shorten" in options && !options.shorten ) {
dfd.resolve( QueryString.encode( this._relevantAttributes( attributes ) ) );
} else {
if ( concurrencyDelay ) {
clearTimeout( concurrencyDelay );
}
// This is an expensive computation, so avoiding two consecutive calls
this.querystringDelay = setTimeout( function() {
self._shorten.call( self, self._relevantAttributes.call( self, attributes ), function( shortened ) {
dfd.resolve( QueryString.encode( shortened ) );
} );
}, 200 );
}
return dfd;
}
};
/**
* DownloadBuilder Model
*/
DownloadBuilderModel = function( obj ) {
if ( typeof obj !== "object" ) {
throw new Error( "parameter required" );
}
Model.call( this );
this.defaults = {};
this.baseVars = obj.baseVars;
this.host = obj.host;
this.orderedComponentsDfd = $.Deferred();
this.themeParamsUnzipping = $.Deferred().resolve();
this.on( "change:before", $.proxy( this._change, this ) );
};
$.extend( DownloadBuilderModel.prototype, Model.prototype, {
_change: function( changed ) {
var self = this;
if ( "components" in changed ) {
this.orderedComponentsDfd.done( function() {
var booleansArray, hash;
delete changed.components;
booleansArray = booleansDecode( self.get.call( self, "components" ) );
delete self.attributes.components;
hash = {};
$.each( self.orderedComponents, function( i, component ) {
hash[ component ] = booleansArray[ i ];
} );
self.set.call( self, hash );
} );
}
if ( "zThemeParams" in changed ) {
this.themeParamsUnzipping = $.Deferred();
delete changed.zThemeParams;
unzip( this.get( "zThemeParams" ), function( unzipped ) {
// Make sure there's no zThemeParams attribute in unzipped object, due to former bug #171 fixed by 1633bad.
delete unzipped.zThemeParams;
delete self.attributes.zThemeParams;
self.set.call( self, {
themeParams: QueryString.encode( unzipped )
} );
self.themeParamsUnzipping.resolve();
} );
}
},
_relevantAttributes: function( attributes ) {
var defaults = this.defaults,
irrelevantAttributes = [];
$.each( defaults, function( varName ) {
if ( attributes[ varName ] === defaults[ varName ] ) {
irrelevantAttributes.push( varName );
}
} );
// Exception rule: if any component is set, make sure version is shown.
if ( $.inArray( "version", irrelevantAttributes ) !== -1 && !$.isEmptyObject( omit( attributes, [ "folderName", "scope", "themeParams", "version" ] ) ) ) {
irrelevantAttributes.splice( $.inArray( "version", irrelevantAttributes ), 1 );
}
if ( irrelevantAttributes.length ) {
return omit( attributes, irrelevantAttributes );
} else {
return attributes;
}
},
_shorten: function( attributes, callback ) {
var self = this,
shortened = pick( attributes, [ "folderName", "scope", "version" ] ),
df1 = $.Deferred(),
df2 = $.Deferred();
// themeParams / zThemeParams
this.themeParamsUnzipping.done( function() {
if ( "themeParams" in attributes && attributes.themeParams !== "none" ) {
zParam( "zThemeParams", QueryString.decode( attributes.themeParams ), function( zThemeParams ) {
$.extend( shortened, zThemeParams );
df1.resolve();
} );
} else {
if ( "themeParams" in attributes ) {
shortened.themeParams = attributes.themeParams;
}
df1.resolve();
}
} );
// components
if ( !this.orderedComponents && this.get( "components" ) ) {
// We have unprocessed components, so use it as shortened.
shortened.components = this.get( "components" );
df2.resolve();
} else if ( $.isEmptyObject( omit( attributes, [ "folderName", "scope", "themeParams", "version" ] ) ) ) {
df2.resolve();
} else {
this.orderedComponentsDfd.done( function() {
var booleansArray = $.map( self.orderedComponents, function( component ) {
// Each component is true by default.
return !( component in attributes ) ? true : attributes[ component ];
} );
shortened.components = booleansEncode( booleansArray );
df2.resolve();
} );
}
$.when( df1, df2 ).done( function() {
callback( shortened );
} );
},
setOrderedComponents: function( orderedComponents ) {
this.orderedComponents = orderedComponents;
this.orderedComponentsDfd.resolve();
},
parseHash: function( hash ) {
this.set( QueryString.decode( hash ) );
},
unsetOrderedComponents: function() {
delete this.orderedComponents;
this.orderedComponentsDfd = $.Deferred();
},
url: function( callback ) {
var self = this;
this.themeParamsUnzipping.done( function() {
self.querystring.call( self, {
concurrencyDelay: self._urlQuerystringDelay
} ).done( function( querystring ) {
callback( "/download/" + ( querystring.length ? "?" + querystring : "" ) );
} );
} );
},
themerollerUrl: function( callback ) {
var self = this, themeParams;
this.themeParamsUnzipping.done( function() {
themeParams = ( self.has.call( self, "themeParams" ) && self.get.call( self, "themeParams" ) !== "none" ? QueryString.decode( self.get.call( self, "themeParams" ) ) : {} );
// 1: Skip folderName, because it will be updated based on theme selection anyway.
self.querystring.call( self, {
concurrencyDelay: self._themerollerUrlQuerystringDelay,
omit: [ "folderName" /* 1 */, "themeParams" ]
} ).done( function( querystring ) {
var attributes = themeParams,
themeRollerModel = new ThemeRollerModel( {
baseVars: self.baseVars,
host: self.host
} );
if ( querystring.length ) {
attributes.downloadParams = querystring;
}
themeRollerModel.set( attributes );
themeRollerModel.url( callback );
} );
} );
},
themeUrl: function( callback ) {
var self = this;
this.themeParamsUnzipping.done( function() {
self.querystring.call( self, {
concurrencyDelay: self._themeUrlQuerystringDelay,
pick: [ "themeParams" ],
shorten: false
} ).done( function( querystring ) {
callback( self.host + "/download/theme" + ( querystring.length ? "?" + querystring : "" ) );
} );
} );
}
} );
/**
* ThemeRoller Model
*/
// TODO cache zThemeParams
ThemeRollerModel = function( obj ) {
if ( typeof obj !== "object" ) {
throw new Error( "parameter required" );
}
Model.call( this );
this.baseVars = obj.baseVars;
this.host = obj.host;
this.on( "change:before", $.proxy( this._change, this ) );
};
$.extend( ThemeRollerModel.prototype, Model.prototype, {
_change: function( changed ) {
var self = this;
if ( "zThemeParams" in changed ) {
delete changed.zThemeParams;
unzip( this.get( "zThemeParams" ), function( unzipped ) {
delete self.attributes.zThemeParams;
self.set.call( self, unzipped );
} );
}
},
_relevantAttributes: function( attributes ) {
var i,
isBaseVars = true;
// If theme is baseVars, omit it in the querystring.
for ( i in this.baseVars ) {
if ( attributes[ i ] !== this.baseVars[ i ] ) {
isBaseVars = false;
break;
}
}
if ( isBaseVars ) {
return omit( attributes,
$.map( this.baseVars, function( value, varName ) {
return varName;
} )
);
} else {
return attributes;
}
},
_shorten: function( attributes, callback ) {
var shortened = pick( attributes, [ "downloadParams" ] ),
df1 = $.Deferred();
zParam( "zThemeParams", omit( attributes, [ "downloadParams" ] ), function( zThemeParams ) {
$.extend( shortened, zThemeParams );
df1.resolve();
} );
df1.done( function() {
callback( shortened );
} );
},
parseHash: function( hash ) {
this.set( QueryString.decode( hash ) );
},
url: function( callback ) {
this.querystring( {
concurrencyDelay: this._urlQuerystringDelay
} ).done( function( querystring ) {
callback( "/themeroller/" + ( querystring.length ? "?" + querystring : "" ) );
} );
},
downloadUrl: function( callback, zThemeParams ) {
var downloadBuilderModel, querystring, themeParams,
attributes = "downloadParams" in this.attributes ? QueryString.decode( this.attributes.downloadParams ) : {};
if ( zThemeParams ) {
attributes.zThemeParams = zThemeParams;
querystring = QueryString.encode( attributes );
callback( "/download/" + ( querystring.length ? "?" + querystring : "" ) );
} else {
themeParams = QueryString.encode( omit( this.attributes, [ "downloadParams" ] ) );
if ( themeParams.length ) {
attributes.themeParams = themeParams;
}
downloadBuilderModel = new DownloadBuilderModel( {
host: this.host
} );
downloadBuilderModel.set( attributes ).url( function( url ) {
callback( url );
} );
}
},
parsethemeUrl: function() {
var attributes = omit( this.attributes, [ "downloadParams", "zThemeParams" ] ),
downloadParams = ( "downloadParams" in this.attributes ? QueryString.decode( this.attributes.downloadParams ) : {} );
if ( downloadParams.version ) {
attributes.version = downloadParams.version;
}
return this.host + "/themeroller/parsetheme.css?" + QueryString.encode( attributes );
}
} );
exports.Model = {
DownloadBuilder: DownloadBuilderModel,
ThemeRoller: ThemeRollerModel
};
} )( this, jQuery, EventEmitter, LZMA, QueryString );