/
UIOptions.js
633 lines (571 loc) · 24.1 KB
/
UIOptions.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
/*
Copyright 2009 University of Toronto
Copyright 2010-2011 OCAD University
Copyright 2011 Lucendo Development Ltd.
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global fluid_1_5:true, jQuery*/
// JSLint options
/*jslint white: true, funcinvoke: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
var fluid_1_5 = fluid_1_5 || {};
(function ($, fluid) {
fluid.registerNamespace("fluid.uiOptions.inline");
/*********************
* UI Options Inline *
*********************/
/**
* An UI Options top-level component that reflects the collaboration between uiOptionsLoader
* and templateLoader. This component is the only UI Options component that is intended to be
* called by the outside world.
*
* @param {Object} options
*/
fluid.defaults("fluid.uiOptions.inline", {
gradeNames: ["fluid.viewComponent"],
mergePolicy: {
uiOptionsTransform: "noexpand",
derivedDefaults: "noexpand"
},
components: {
uiOptionsLoader: {
type: "fluid.uiOptions.loader"
},
templateLoader: {
priority: "first",
type: "fluid.uiOptions.templateLoader"
}
},
uiOptionsTransform: {
transformer: "fluid.uiOptions.mapOptions",
config: {
"*.templateLoader": "templateLoader",
"*.templateLoader.*.templatePath.options.value": "prefix",
"*.uiOptionsLoader": "uiOptionsLoader",
"*.uiOptionsLoader.container": "container",
"*.uiOptionsLoader.*.uiOptions": "uiOptions",
"*.uiOptionsLoader.*.uiOptions.*.textControls": "textControls",
"*.uiOptionsLoader.*.uiOptions.*.layoutControls": "layoutControls",
"*.uiOptionsLoader.*.uiOptions.*.linksControls": "linksControls",
"*.uiOptionsLoader.*.uiOptions.*.preview": "preview",
"*.uiOptionsLoader.*.uiOptions.*.preview.*.enhancer": "previewEnhancer"
}
},
derivedDefaults: {
uiOptions: {
options: {
components: {
settingsStore: "{uiEnhancer}.settingsStore"
},
listeners: {
onUIOptionsRefresh: "{uiEnhancer}.updateFromSettingsStore"
}
}
}
}
});
// A temporary function to automate the work of making a creator function for a UIOptions configuration.
// These creator functions accept literal options from the user (via direct function call) and
// apply a mapping transformation to them to make some deeply nested configuration more accessible.
// Unfortunately this operation conflicts with the workflow of the IoC system in constructing a component -
// so these UIOptions configurations are not suitable for use as IoC-driven subcomponents.
fluid.uiOptions.inline.makeCreator = function (componentName, processor) {
var creator = function (container, options) {
// make "container" one of the options so it can be munged by the uiOptions.mapOptions.
// This container is passed down to be used as uiOptionsLoader.container
var defaults = fluid.defaults(componentName);
options.container = container;
options = processor(options);
var mappedOptions = fluid.uiOptions.mapOptions(options, defaults.uiOptionsTransform.config, defaults.mergePolicy,
fluid.copy(defaults.derivedDefaults));
var that = fluid.initView(componentName, container, mappedOptions);
// This workflow copied from fluid.initView
fluid.diagnoseFailedView(componentName, that, fluid.defaults(componentName), [componentName, container, mappedOptions]);
fluid.initDependents(that);
return that;
};
// This workflow taken from framework fluid.makeComponent
var existing = fluid.getGlobalValue(componentName);
if (existing) {
$.extend(creator, existing);
}
fluid.setGlobalValue(componentName, creator);
};
/**
* @param {Object} inObject, the element on inObject is in the pair of key -> value
*/
fluid.uiOptions.sortByKeyLength = function (inObject) {
var keys = fluid.keys(inObject);
return keys.sort(fluid.compareStringLength(true));
};
fluid.uiOptions.mapOptionsRecord = function (options, sortedConfigKeys, config) {
var opRecs = [{}, {}, options || {}];
var appliers = fluid.transform(opRecs, function (opRec) {
return fluid.makeChangeApplier(opRec);
});
var toDelete = [];
fluid.each(sortedConfigKeys, function (origDest) {
var source = config[origDest];
var dest = fluid.uiOptions.expandShortPath(origDest);
var applier = appliers[origDest.charAt(0) === "!" ? 0 : 1];
// Process the user pass-in options
var value = fluid.get(options, source);
if (value) {
applier.requestChange(dest, value, "ADD");
toDelete.push({source: source, value: value});
}
});
fluid.each(toDelete, function (elem) {
appliers[2].requestChange(elem.source, elem.value, "DELETE");
});
return opRecs;
};
// TODO: This dreadful function will be absorbed into the framework for 1.5
/**
* @param {Object} options, top level options to be mapped
* @param {Array} config, a mapping between the target path on the IoC tree and the option name
* @param {Object} used in fluid.merge() to merge options and componentConfig
*/
fluid.uiOptions.mapOptions = function (options, config, mergePolicy, derivedDefaults) {
// Sort the config object by the length of the key in case an option and its child option
// are both configurable.
// For instance: "*.templateLoader" & "*.templateLoader.*.templatePath.options.value"
var sortedConfigKeys = fluid.uiOptions.sortByKeyLength(config);
var optrecs = fluid.uiOptions.mapOptionsRecord(options, sortedConfigKeys, config);
var devrecs = fluid.uiOptions.mapOptionsRecord(derivedDefaults, sortedConfigKeys, config);
var mergeOpts = [mergePolicy].concat(devrecs).concat(optrecs);
return fluid.merge.apply(null, mergeOpts);
};
fluid.uiOptions.expandShortPath = function (path) {
if (path.charAt(0) === "!") {
path = path.substring(1);
}
var strToreplaceFirst = "components";
var strToreplaceRest = "options.components";
// replace the beginning "*"
var newPath = (path.charAt(0) === "*") ? path.replace("*", strToreplaceFirst) : path;
// replace the rest "*"
newPath = newPath.replace(/\*/g, strToreplaceRest);
return newPath;
};
/******************************
* UI Options Template Loader *
******************************/
/**
* A configurable component that works in conjunction with or without the UI Options template path
* component (fluid.uiOptionsTemplatePath) to allow users to set either the location of their own
* templates or the templates that are relative to the path defined in the UI Options template path
* component.
*
* @param {Object} options
*/
fluid.defaults("fluid.uiOptions.templateLoader", {
gradeNames: ["fluid.eventedComponent", "autoInit"],
finalInitFunction: "fluid.uiOptions.templateLoader.resolveTemplates",
templates: {
uiOptions: "%prefix/FatPanelUIOptions.html",
textSizer: "%prefix/UIOptionsTemplate-textSizer.html",
textFont: "%prefix/UIOptionsTemplate-textFont.html",
lineSpacer: "%prefix/UIOptionsTemplate-lineSpacer.html",
contrast: "%prefix/UIOptionsTemplate-contrast.html",
layoutControls: "%prefix/UIOptionsTemplate-layout.html",
linksControls: "%prefix/UIOptionsTemplate-links.html"
},
// Unsupported, non-API option
components: {
templatePath: {
type: "fluid.uiOptions.templatePath"
}
},
invokers: {
transformURL: {
funcName: "fluid.stringTemplate",
args: [ "{arguments}.0", { "prefix/" : "{templateLoader}.templatePath.options.value"} ]
}
}
});
fluid.uiOptions.templateLoader.resolveTemplates = function (that) {
var mapped = fluid.transform(that.options.templates, that.transformURL);
that.resources = fluid.transform(mapped, function (url) {
return {url: url, forceCache: true};
});
};
/**************************************
* UI Options Template Path Specifier *
**************************************/
/**
* A configurable component that defines the relative path from the html to UI Options templates.
*
* @param {Object} options
*/
fluid.defaults("fluid.uiOptions.templatePath", {
gradeNames: ["fluid.littleComponent", "autoInit"],
value: "../html/"
});
/**************
* UI Options *
**************/
fluid.defaults("fluid.uiOptions.loader", {
gradeNames: ["fluid.viewComponent", "autoInit"],
resources: "{templateLoader}.resources",
finalInitFunction: "fluid.uiOptions.loader.finalInit",
events: {
// These two are events private to uiOptions
onUIOptionsTemplateReady: null, // templates are loaded - construct UIOptions itself
onUIOptionsComponentReady: null, // UIOptions is loaded - construct its subcomponents
// This is a public event which users outside the component can subscribe to - the argument
// supplied is UIOptions.loader itself
onReady: null
},
listeners: {
onUIOptionsComponentReady: {
listener: "{loader}.events.onReady",
args: ["{fluid.uiOptions.loader}", "{arguments}.0"],
priority: "last"
}
},
components: {
uiOptions: {
type: "fluid.uiOptions",
container: "{loader}.container",
createOnEvent: "onUIOptionsTemplateReady",
options: {
events: {
"onUIOptionsComponentReady": "{loader}.events.onUIOptionsComponentReady"
}
}
}
}
});
fluid.uiOptions.loader.finalInit = function (that) {
fluid.fetchResources(that.options.resources, function () {
that.events.onUIOptionsTemplateReady.fire();
});
};
// This function compensates for a framework deficiency that due to lack of gingerness, the "refreshView"
// function synthesized by rendererComponent is not available during listener registration which only
// occurs after component init functions have completed (http://issues.fluidproject.org/browse/FLUID-4334)
fluid.uiOptions.lateRefreshViewBinder = function (that) {
that.refreshView = function () {
that.renderer.refreshView();
};
};
fluid.uiOptions.defaultModelMerger = function (target, source) {
$.extend(true, target, source);
};
/**
* A component that works in conjunction with the UI Enhancer component and the Fluid Skinning System (FSS)
* to allow users to set personal user interface preferences. The UI Options component provides a user
* interface for setting and saving personal preferences, and the UI Enhancer component carries out the
* work of applying those preferences to the user interface.
*
* @param {Object} container
* @param {Object} options
*/
fluid.defaults("fluid.uiOptions", {
gradeNames: ["fluid.viewComponent", "autoInit"],
components: {
eventBinder: {
type: "fluid.uiOptions.eventBinder"
}
},
selectors: {
cancel: ".flc-uiOptions-cancel",
reset: ".flc-uiOptions-reset",
save: ".flc-uiOptions-save",
previewFrame : ".flc-uiOptions-preview-frame"
},
events: {
onSave: null,
onCancel: null,
onReset: null,
onAutoSave: null,
modelChanged: null,
onUIOptionsRefresh: null,
onUIOptionsMarkupReady: null,
onUIOptionsComponentReady: null,
contributeDefaultModel: null
},
listeners: {
onAutoSave: "{that}.save"
},
preInitFunction: "fluid.uiOptions.preInit",
finalInitFunction: "fluid.uiOptions.finalInit",
resources: {
template: "{templateLoader}.resources.uiOptions"
},
autoSave: false
});
// called once markup is applied to the document containing tab component roots
fluid.uiOptions.finishInit = function (that) {
var bindHandlers = function (that) {
var saveButton = that.locate("save");
if (saveButton.length > 0) {
saveButton.click(that.saveAndApply);
var form = fluid.findForm(saveButton);
$(form).submit(function () {
that.saveAndApply();
});
}
that.locate("reset").click(that.reset);
that.locate("cancel").click(that.cancel);
};
that.container.append(that.options.resources.template.resourceText);
bindHandlers(that);
// This creates subcomponents - we can find default model afterwards
that.events.onUIOptionsMarkupReady.fire(that);
that.defaultModel = {};
that.events.contributeDefaultModel.fire(that.defaultModel);
that.fetch();
that.events.onUIOptionsComponentReady.fire(that);
};
fluid.uiOptions.preInit = function (that) {
that.fetch = function () {
var initialModel = that.settingsStore.fetch();
initialModel = $.extend(true, {}, that.defaultModel, initialModel);
that.updateModel(initialModel);
that.events.onUIOptionsRefresh.fire();
};
/**
* Saves the current model and fires onSave
*/
that.save = function () {
that.events.onSave.fire(that.model.selections);
var savedSelections = fluid.copy(that.model.selections);
that.settingsStore.save(savedSelections);
};
that.saveAndApply = function () {
that.save();
that.events.onUIOptionsRefresh.fire();
};
/**
* Resets the selections to the integrator's defaults and fires onReset
*/
that.reset = function () {
that.updateModel(fluid.copy(that.defaultModel));
that.events.onReset.fire(that);
that.events.onUIOptionsRefresh.fire();
};
/**
* Resets the selections to the last saved selections and fires onCancel
*/
that.cancel = function () {
that.events.onCancel.fire();
that.fetch();
};
/**
* Updates the change applier and fires modelChanged on subcomponent fluid.uiOptions.controls
*
* @param {Object} newModel
* @param {Object} source
*/
that.updateModel = function (newModel) {
that.applier.requestChange("selections", newModel);
};
that.applier.modelChanged.addListener("selections", function (newModel, oldModel, changeRequest) {
that.events.modelChanged.fire(newModel, oldModel, changeRequest.source);
if (that.options.autoSave) {
that.events.onAutoSave.fire();
}
});
};
fluid.uiOptions.finalInit = function (that) {
fluid.fetchResources(that.options.resources, function () {
// This setTimeout is to ensure that fetching of resources is asynchronous,
// and so that component construction does not run ahead of subcomponents for FatPanel
// (FLUID-4453 - this may be a replacement for a branch removed for a FLUID-2248 fix)
setTimeout(function () {
fluid.uiOptions.finishInit(that);
}, 1);
});
};
/*********************************************************************************************************
* defaultSettingsPanels
*
* A collection of all the default UIO setting panels.
*********************************************************************************************************/
fluid.defaults("fluid.uiOptions.defaultSettingsPanels", {
gradeNames: ["fluid.uiOptions", "autoInit"],
selectors: {
textSizer: ".flc-uiOptions-text-sizer",
textFont: ".flc-uiOptions-text-font",
lineSpacer: ".flc-uiOptions-line-spacer",
contrast: ".flc-uiOptions-contrast",
textControls: ".flc-uiOptions-text-controls",
layoutControls: ".flc-uiOptions-layout-controls",
linksControls: ".flc-uiOptions-links-controls"
},
components: {
textSizer: {
type: "fluid.uiOptions.textSizer",
container: "{uiOptions}.dom.textSizer",
createOnEvent: "onUIOptionsMarkupReady",
options: {
rules: {
"selections.textSize": "value"
},
resources: {
template: "{templateLoader}.resources.textSizer"
}
}
},
lineSpacer: {
type: "fluid.uiOptions.lineSpacer",
container: "{uiOptions}.dom.lineSpacer",
createOnEvent: "onUIOptionsMarkupReady",
options: {
rules: {
"selections.lineSpacing": "value"
},
resources: {
template: "{templateLoader}.resources.lineSpacer"
}
}
},
textFont: {
type: "fluid.uiOptions.textFont",
container: "{uiOptions}.dom.textFont",
createOnEvent: "onUIOptionsMarkupReady",
options: {
classnameMap: "{uiEnhancer}.options.classnameMap",
rules: {
"selections.textFont": "value"
},
resources: {
template: "{templateLoader}.resources.textFont"
}
}
},
contrast: {
type: "fluid.uiOptions.contrast",
container: "{uiOptions}.dom.contrast",
createOnEvent: "onUIOptionsMarkupReady",
options: {
classnameMap: "{uiEnhancer}.options.classnameMap",
rules: {
"selections.theme": "value"
},
resources: {
template: "{templateLoader}.resources.contrast"
}
}
},
layoutControls: {
type: "fluid.uiOptions.layoutControls",
container: "{uiOptions}.dom.layoutControls",
createOnEvent: "onUIOptionsMarkupReady",
options: {
rules: {
"selections.toc": "toc",
"selections.layout": "layout"
},
resources: {
template: "{templateLoader}.resources.layoutControls"
}
}
},
linksControls: {
type: "fluid.uiOptions.linksControls",
container: "{uiOptions}.dom.linksControls",
createOnEvent: "onUIOptionsMarkupReady",
options: {
rules: {
"selections.links": "links",
"selections.inputsLarger": "inputsLarger"
},
resources: {
template: "{templateLoader}.resources.linksControls"
}
}
}
}
});
/******************************************************
* UI Options Event binder: *
* Binds events between UI Options and the UIEnhancer *
******************************************************/
fluid.defaults("fluid.uiOptions.eventBinder", {
gradeNames: ["fluid.eventedComponent", "autoInit"]
});
/**********************
* UI Options Preview *
**********************/
fluid.defaults("fluid.uiOptions.preview", {
gradeNames: ["fluid.viewComponent", "autoInit"],
components: {
enhancer: {
type: "fluid.uiEnhancer",
createOnEvent: "onReady",
options: {
settingsStore: {
type: "fluid.uiEnhancer.tempStore"
}
}
},
eventBinder: {
type: "fluid.uiOptions.preview.eventBinder",
createOnEvent: "onReady"
},
// TODO: This is a violation of containment, but we can't use up our allowance of demands
// blocks as a result of FLUID-4392
templateLoader: "{templateLoader}"
},
invokers: {
updateModel: {
funcName: "fluid.uiOptions.preview.updateModel",
args: [
"{preview}",
"{uiOptions}.model.selections"
]
}
},
finalInitFunction: "fluid.uiOptions.preview.finalInit",
events: {
onReady: null
},
templateUrl: "%prefix/UIOptionsPreview.html"
});
fluid.uiOptions.preview.updateModel = function (that, selections) {
/**
* SetTimeout is temp fix for http://issues.fluidproject.org/browse/FLUID-2248
*/
setTimeout(function () {
if (that.enhancer) {
that.enhancer.updateModel(selections);
}
}, 0);
};
fluid.uiOptions.preview.finalInit = function (that) {
var templateUrl = that.templateLoader.transformURL(that.options.templateUrl);
that.container.load(function () {
that.enhancerContainer = $("body", that.container.contents());
that.events.onReady.fire();
});
that.container.attr("src", templateUrl);
};
fluid.demands("fluid.uiEnhancer", "fluid.uiOptions.preview", {
funcName: "fluid.uiEnhancer",
args: [
"{preview}.enhancerContainer",
"{options}"
]
});
/***************************************************
* UI Options Event binder: *
* Binds events between UI Options and the Preview *
***************************************************/
fluid.defaults("fluid.uiOptions.preview.eventBinder", {
gradeNames: ["fluid.eventedComponent", "autoInit"]
});
fluid.demands("fluid.uiOptions.preview.eventBinder", ["fluid.uiOptions.preview", "fluid.uiOptions"], {
options: {
listeners: {
"{uiOptions}.events.modelChanged": "{preview}.updateModel"
}
}
});
})(jQuery, fluid_1_5);