/
Fluid.js
1280 lines (1138 loc) · 49.9 KB
/
Fluid.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Fluid Infusion v1.4-SNAPSHOT
*
* Infusion is distributed under the Educational Community License 2.0 and new BSD licenses:
* http://wiki.fluidproject.org/display/fluid/Fluid+Licensing
*
* For information on copyright, see the individual Infusion source code files:
* https://source.fluidproject.org/svn/fluid/infusion/
*/
/*
Copyright 2007-2010 University of Cambridge
Copyright 2007-2010 University of Toronto
Copyright 2007-2009 University of California, Berkeley
Copyright 2010-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://source.fluidproject.org/svn/LICENSE.txt
*/
// Declare dependencies.
/*global jQuery, YAHOO, opera, window, console*/
// JSLint options
/*jslint white: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
var fluid_1_4 = fluid_1_4 || {};
var fluid = fluid || fluid_1_4;
(function ($, fluid) {
fluid.version = "Infusion 1.4-SNAPSHOT";
fluid.environment = {
fluid: fluid
};
var globalObject = window || {};
/**
* Causes an error message to be logged to the console and a real runtime error to be thrown.
*
* @param {String|Error} message the error message to log
*/
fluid.fail = function (message) {
fluid.setLogging(true);
fluid.log(message.message ? message.message : message);
//throw new Error(message);
message.fail(); // Intentionally cause a browser error by invoking a nonexistent function.
};
// Logging
var logging;
/** method to allow user to enable logging (off by default) */
fluid.setLogging = function (enabled) {
if (typeof enabled === "boolean") {
logging = enabled;
} else {
logging = false;
}
};
/** Log a message to a suitable environmental console. If the standard "console"
* stream is available, the message will be sent there - otherwise either the
* YAHOO logger or the Opera "postError" stream will be used. Logging must first
* be enabled with a call fo the fluid.setLogging(true) function.
*/
fluid.log = function (str) {
if (logging) {
str = fluid.renderTimestamp(new Date()) + ": " + str;
if (typeof(console) !== "undefined") {
if (console.debug) {
console.debug(str);
} else {
console.log(str);
}
}
else if (typeof(YAHOO) !== "undefined") {
YAHOO.log(str);
}
else if (typeof(opera) !== "undefined") {
opera.postError(str);
}
}
};
/**
* Wraps an object in a jQuery if it isn't already one. This function is useful since
* it ensures to wrap a null or otherwise falsy argument to itself, rather than the
* often unhelpful jQuery default of returning the overall document node.
*
* @param {Object} obj the object to wrap in a jQuery
*/
fluid.wrap = function (obj) {
return ((!obj || obj.jquery) ? obj : $(obj));
};
/**
* If obj is a jQuery, this function will return the first DOM element within it.
*
* @param {jQuery} obj the jQuery instance to unwrap into a pure DOM element
*/
fluid.unwrap = function (obj) {
return obj && obj.jquery && obj.length === 1 ? obj[0] : obj; // Unwrap the element if it's a jQuery.
};
// Functional programming utilities.
/** Return an empty container as the same type as the argument (either an
* array or hash */
fluid.freshContainer = function (tocopy) {
return fluid.isArrayable(tocopy) ? [] : {};
};
/** Performs a deep copy (clone) of its argument **/
fluid.copy = function (tocopy) {
if (fluid.isPrimitive(tocopy)) {
return tocopy;
}
return $.extend(true, fluid.freshContainer(tocopy), tocopy);
};
/** A basic utility that returns its argument unchanged */
fluid.identity = function (arg) {
return arg;
};
// Framework and instantiation functions.
/** Returns true if the argument is a primitive type **/
fluid.isPrimitive = function (value) {
var valueType = typeof(value);
return !value || valueType === "string" || valueType === "boolean" || valueType === "number" || valueType === "function";
};
/** Determines whether the supplied object can be treated as an array, by
* iterating an index towards its length. The test functions by detecting
* a property named "length" which is of type "number", but excluding objects
* which are themselves of primitive types (in particular functions and strings)
*/
fluid.isArrayable = function (totest) {
return totest && !fluid.isPrimitive(totest) && typeof(totest.length) === "number";
};
/** Corrected version of jQuery makearray that returns an empty array on undefined rather than crashing **/
fluid.makeArray = function (arg) {
if (arg === null || arg === undefined) {
return [];
}
else {
return $.makeArray(arg);
}
};
function transformInternal(source, togo, key, args) {
var transit = source[key];
for (var j = 0; j < args.length - 1; ++ j) {
transit = args[j + 1](transit, key);
}
togo[key] = transit;
}
/** Return a list or hash of objects, transformed by one or more functions. Similar to
* jQuery.map, only will accept an arbitrary list of transformation functions and also
* works on non-arrays.
* @param source {Array or Object} The initial container of objects to be transformed.
* @param fn1, fn2, etc. {Function} An arbitrary number of optional further arguments,
* all of type Function, accepting the signature (object, index), where object is the
* list member to be transformed, and index is its list index. Each function will be
* applied in turn to each list member, which will be replaced by the return value
* from the function.
* @return The finally transformed list, where each member has been replaced by the
* original member acted on by the function or functions.
*/
fluid.transform = function (source) {
var togo = fluid.freshContainer(source);
if (fluid.isArrayable(source)) {
for (var i = 0; i < source.length; ++ i) {
transformInternal(source, togo, i, arguments);
}
}
else {
for (var key in source) {
transformInternal(source, togo, key, arguments);
}
}
return togo;
};
/** Better jQuery.each which works on hashes as well as having the arguments
* the right way round.
* @param source {Arrayable or Object} The container to be iterated over
* @param func {Function} A function accepting (value, key) for each iterated
* object. This function may return a value to terminate the iteration
*/
fluid.each = function (source, func) {
if (fluid.isArrayable(source)) {
for (var i = 0; i < source.length; ++ i) {
func(source[i], i);
}
}
else {
for (var key in source) {
func(source[key], key);
}
}
};
/** Scan through a list or hash of objects, terminating on the first member which
* matches a predicate function.
* @param source {Arrayable or Object} The list or hash of objects to be searched.
* @param func {Function} A predicate function, acting on a member. A predicate which
* returns any value which is not <code>null</code> or <code>undefined</code> will terminate
* the search. The function accepts (object, index).
* @param deflt {Object} A value to be returned in the case no predicate function matches
* a list member. The default will be the natural value of <code>undefined</code>
* @return The first return value from the predicate function which is not <code>null</code>
* or <code>undefined</code>
*/
fluid.find = function (source, func, deflt) {
var disp;
if (fluid.isArrayable(source)) {
for (var i = 0; i < source.length; ++ i) {
disp = func(source[i], i);
if (disp !== undefined) {
return disp;
}
}
}
else {
for (var key in source) {
disp = func(source[key], key);
if (disp !== undefined) {
return disp;
}
}
}
return deflt;
};
/** Scan through a list of objects, "accumulating" a value over them
* (may be a straightforward "sum" or some other chained computation).
* @param list {Array} The list of objects to be accumulated over.
* @param fn {Function} An "accumulation function" accepting the signature (object, total, index) where
* object is the list member, total is the "running total" object (which is the return value from the previous function),
* and index is the index number.
* @param arg {Object} The initial value for the "running total" object.
* @return {Object} the final running total object as returned from the final invocation of the function on the last list member.
*/
fluid.accumulate = function (list, fn, arg) {
for (var i = 0; i < list.length; ++ i) {
arg = fn(list[i], arg, i);
}
return arg;
};
/** Can through a list of objects, removing those which match a predicate. Similar to
* jQuery.grep, only acts on the list in-place by removal, rather than by creating
* a new list by inclusion.
* @param source {Array|Object} The list of objects to be scanned over.
* @param fn {Function} A predicate function determining whether an element should be
* removed. This accepts the standard signature (object, index) and returns a "truthy"
* result in order to determine that the supplied object should be removed from the list.
* @return The list, transformed by the operation of removing the matched elements. The
* supplied list is modified by this operation.
*/
fluid.remove_if = function (source, fn) {
if (fluid.isArrayable(source)) {
for (var i = 0; i < source.length; ++i) {
if (fn(source[i], i)) {
source.splice(i, 1);
--i;
}
}
}
else {
for (var key in source) {
if (fn(source[key], key)) {
delete source[key];
}
}
}
return source;
};
/**
* Searches through the supplied object for the first value which matches the one supplied.
* @param obj {Object} the Object to be searched through
* @param value {Object} the value to be found. This will be compared against the object's
* member using === equality.
* @return {String} The first key whose value matches the one supplied, or <code>null</code> if no
* such key is found.
*/
fluid.keyForValue = function (obj, value) {
return fluid.find(obj, function (thisValue, key) {
if (value === thisValue) {
return key;
}
});
};
/**
* This method is now deprecated and will be removed in a future release of Infusion.
* See fluid.keyForValue instead.
*/
fluid.findKeyInObject = fluid.keyForValue;
/**
* Clears an object or array of its contents. For objects, each property is deleted.
*
* @param {Object|Array} target the target to be cleared
*/
fluid.clear = function (target) {
if (target instanceof Array) {
target.length = 0;
}
else {
for (var i in target) {
delete target[i];
}
}
};
// Model functions
fluid.model = {}; // cannot call registerNamespace yet since it depends on fluid.model
/** Another special "marker object" representing that a distinguished
* (probably context-dependent) value should be substituted.
*/
fluid.VALUE = {type: "fluid.marker", value: "VALUE"};
/** Another special "marker object" representing that no value is present (where
* signalling using the value "undefined" is not possible) */
fluid.NO_VALUE = {type: "fluid.marker", value: "NO_VALUE"};
/** Determine whether an object is any marker, or a particular marker - omit the
* 2nd argument to detect any marker
*/
fluid.isMarker = function (totest, type) {
if (!totest || typeof(totest) !== 'object' || totest.type !== "fluid.marker") {
return false;
}
if (!type) {
return true;
}
return totest.value === type || totest.value === type.value;
};
/** Copy a source "model" onto a target **/
fluid.model.copyModel = function (target, source) {
fluid.clear(target);
$.extend(true, target, source);
};
/** Parse an EL expression separated by periods (.) into its component segments.
* @param {String} EL The EL expression to be split
* @return {Array of String} the component path expressions.
* TODO: This needs to be upgraded to handle (the same) escaping rules (as RSF), so that
* path segments containing periods and backslashes etc. can be processed.
*/
fluid.model.parseEL = function (EL) {
return String(EL).split('.');
};
/** Compose an EL expression from two separate EL expressions. The returned
* expression will be the one that will navigate the first expression, and then
* the second, from the value reached by the first. Either prefix or suffix may be
* the empty string **/
fluid.model.composePath = function (prefix, suffix) {
return prefix === "" ? suffix : (suffix === "" ? prefix : prefix + "." + suffix);
};
/** Compose any number of path segments, none of which may be empty **/
fluid.model.composeSegments = function () {
return $.makeArray(arguments).join(".");
};
/** Standard strategies for resolving path segments **/
fluid.model.environmentStrategy = function (initEnvironment) {
return {
init: function () {
var environment = initEnvironment;
return function (root, segment, index) {
var togo;
if (environment && environment[segment]) {
togo = environment[segment];
}
environment = null;
return togo;
};
}
};
};
fluid.model.defaultCreatorStrategy = function (root, segment) {
if (root[segment] === undefined) {
root[segment] = {};
return root[segment];
}
};
fluid.model.defaultFetchStrategy = function (root, segment) {
return root[segment];
};
fluid.model.funcResolverStrategy = function (root, segment) {
if (root.resolvePathSegment) {
return root.resolvePathSegment(segment);
}
};
fluid.model.makeResolver = function (root, EL, config) {
var that = {
segs: fluid.model.parseEL(EL),
root: root,
index: 0,
strategies: fluid.transform(config, function (figel) {
return figel.init ? figel.init() : figel;
})
};
that.next = function () {
if (!that.root) {
return;
}
var accepted;
for (var i = 0; i < that.strategies.length; ++ i) {
var value = that.strategies[i](that.root, that.segs[that.index], that.index);
if (accepted === undefined) {
accepted = value;
}
}
if (accepted === fluid.NO_VALUE) {
accepted = undefined;
}
that.root = accepted;
++that.index;
};
that.step = function (limit) {
for (var i = 0; i < limit; ++ i) {
that.next();
}
that.last = that.segs[that.index];
};
return that;
};
fluid.model.defaultSetConfig = [fluid.model.funcResolverStrategy, fluid.model.defaultFetchStrategy, fluid.model.defaultCreatorStrategy];
fluid.model.getPenultimate = function (root, EL, config) {
config = config || fluid.model.defaultGetConfig;
var resolver = fluid.model.makeResolver(root, EL, config);
resolver.step(resolver.segs.length - 1);
return resolver;
};
fluid.set = function (root, EL, newValue, config) {
config = config || fluid.model.defaultSetConfig;
var resolver = fluid.model.getPenultimate(root, EL, config);
resolver.root[resolver.last] = newValue;
};
fluid.model.defaultGetConfig = [fluid.model.funcResolverStrategy, fluid.model.defaultFetchStrategy];
/** Evaluates an EL expression by fetching a dot-separated list of members
* recursively from a provided root.
* @param root The root data structure in which the EL expression is to be evaluated
* @param {string} EL The EL expression to be evaluated
* @param environment An optional "environment" which, if it contains any members
* at top level, will take priority over the root data structure.
* @return The fetched data value.
*/
fluid.get = function (root, EL, config) {
if (EL === "" || EL === null || EL === undefined) {
return root;
}
config = config || fluid.model.defaultGetConfig;
var resolver = fluid.model.makeResolver(root, EL, config);
resolver.step(resolver.segs.length);
return resolver.root;
};
// This backward compatibility will be maintained for a number of releases, probably until Fluid 2.0
fluid.model.setBeanValue = fluid.set;
fluid.model.getBeanValue = fluid.get;
fluid.getGlobalValue = function (path, env) {
if (path) {
env = env || fluid.environment;
var envFetcher = fluid.model.environmentStrategy(env);
return fluid.get(globalObject, path, [envFetcher].concat(fluid.model.defaultGetConfig));
}
};
/**
* Allows for the calling of a function from an EL expression "functionPath", with the arguments "args", scoped to an framework version "environment".
* @param {Object} functionPath - An EL expression
* @param {Object} args - An array of arguments to be applied to the function, specified in functionPath
* @param {Object} environment - (optional) The object to scope the functionPath to (typically the framework root for version control)
*/
fluid.invokeGlobalFunction = function (functionPath, args, environment) {
var func = fluid.getGlobalValue(functionPath, environment);
if (!func) {
fluid.fail("Error invoking global function: " + functionPath + " could not be located");
} else {
return func.apply(null, args);
}
};
/** Registers a new global function at a given path (currently assumes that
* it lies within the fluid namespace)
*/
fluid.registerGlobalFunction = function (functionPath, func, env) {
env = env || fluid.environment;
var envFetcher = fluid.model.environmentStrategy(env);
fluid.set(globalObject, functionPath, func, [envFetcher].concat(fluid.model.defaultSetConfig));
};
fluid.setGlobalValue = fluid.registerGlobalFunction;
/** Ensures that an entry in the global namespace exists **/
fluid.registerNamespace = function (naimspace, env) {
env = env || fluid.environment;
var existing = fluid.getGlobalValue(naimspace, env);
if (!existing) {
existing = {};
fluid.setGlobalValue(naimspace, existing, env);
}
return existing;
};
fluid.registerNamespace("fluid.event");
fluid.event.addListenerToFirer = function (firer, value, namespace) {
if (typeof(value) === "function") {
firer.addListener(value, namespace);
}
else if (value && typeof(value) === "object") {
firer.addListener(value.listener, namespace, value.predicate, value.priority);
}
};
/**
* Attaches the user's listeners to a set of events.
*
* @param {Object} events a collection of named event firers
* @param {Object} listeners optional listeners to add
*/
fluid.mergeListeners = function (events, listeners) {
fluid.each(listeners, function (value, key) {
var keydot = key.indexOf(".");
var namespace;
if (keydot !== -1) {
namespace = key.substring(keydot + 1);
key = key.substring(0, keydot);
}
if (!events[key]) {
events[key] = fluid.event.getEventFirer();
}
var firer = events[key];
if (fluid.isArrayable(value)) {
for (var i = 0; i < value.length; ++ i) {
fluid.event.addListenerToFirer(firer, value[i], namespace);
}
}
else {
fluid.event.addListenerToFirer(firer, value, namespace);
}
});
};
/**
* Sets up a component's declared events.
* Events are specified in the options object by name. There are three different types of events that can be
* specified:
* 1. an ordinary multicast event, specified by "null".
* 2. a unicast event, which allows only one listener to be registered
* 3. a preventable event
*
* @param {Object} that the component
* @param {Object} options the component's options structure, containing the declared event names and types
*/
fluid.instantiateFirers = function (that, options) {
that.events = {};
if (options.events) {
for (var event in options.events) {
var eventType = options.events[event];
that.events[event] = fluid.event.getEventFirer(eventType === "unicast", eventType === "preventable");
}
}
fluid.mergeListeners(that.events, options.listeners);
};
// stubs for two functions in FluidDebugging.js
fluid.dumpEl = fluid.identity;
fluid.renderTimestamp = fluid.identity;
/**
* Retreives and stores a component's default settings centrally.
* @param {boolean} (options) if true, manipulate a global option (for the head
* component) rather than instance options.
* @param {String} componentName the name of the component
* @param {Object} (optional) an container of key/value pairs to set
*
*/
var defaultsStore = {};
var globalDefaultsStore = {};
fluid.defaults = function () {
var offset = 0;
var store = defaultsStore;
if (typeof arguments[0] === "boolean") {
store = globalDefaultsStore;
offset = 1;
}
var componentName = arguments[offset];
var defaultsObject = arguments[offset + 1];
if (defaultsObject !== undefined) {
store[componentName] = defaultsObject;
return defaultsObject;
}
return store[componentName];
};
fluid.mergePolicyIs = function (policy, test) {
return typeof(policy) === "string" && policy.indexOf(test) !== -1;
};
function mergeImpl(policy, basePath, target, source, thisPolicy) {
if (typeof(thisPolicy) === "function") {
thisPolicy.apply(null, target, source);
return target;
}
if (fluid.mergePolicyIs(thisPolicy, "replace")) {
fluid.clear(target);
}
for (var name in source) {
var path = (basePath ? basePath + ".": "") + name;
var newPolicy = policy && typeof(policy) !== "string" ? policy[path] : policy;
var thisTarget = target[name];
var thisSource = source[name];
var primitiveTarget = fluid.isPrimitive(thisTarget);
if (thisSource !== undefined) {
if (thisSource !== null && typeof thisSource === 'object' &&
!thisSource.nodeType && !thisSource.jquery && thisSource !== fluid.VALUE &&
!fluid.mergePolicyIs(newPolicy, "preserve")) {
if (primitiveTarget) {
target[name] = thisTarget = thisSource instanceof Array ? [] : {};
}
mergeImpl(policy, path, thisTarget, thisSource, newPolicy);
}
else {
if (typeof(newPolicy) === "function") {
newPolicy.call(null, target, source, name);
}
else if (thisTarget === null || thisTarget === undefined || !fluid.mergePolicyIs(newPolicy, "reverse")) {
// TODO: When "grades" are implemented, grandfather in any paired applier to perform these operations
// NB: mergePolicy of "preserve" now creates dependency on DataBinding.js
target[name] = fluid.mergePolicyIs(newPolicy, "preserve") ? fluid.model.mergeModel(thisTarget, thisSource) : thisSource;
}
}
}
}
return target;
}
/** Merge a collection of options structures onto a target, following an optional policy.
* This function is typically called automatically, as a result of an invocation of
* <code>fluid.iniView</code>. The behaviour of this function is explained more fully on
* the page http://wiki.fluidproject.org/display/fluid/Options+Merging+for+Fluid+Components .
* @param policy {Object/String} A "policy object" specifiying the type of merge to be performed.
* If policy is of type {String} it should take on the value "reverse" or "replace" representing
* a static policy. If it is an
* Object, it should contain a mapping of EL paths onto these String values, representing a
* fine-grained policy. If it is an Object, the values may also themselves be EL paths
* representing that a default value is to be taken from that path.
* @param target {Object} The options structure which is to be modified by receiving the merge results.
* @param options1, options2, .... {Object} an arbitrary list of options structure which are to
* be merged "on top of" the <code>target</code>. These will not be modified.
*/
fluid.merge = function (policy, target) {
var path = "";
for (var i = 2; i < arguments.length; ++i) {
var source = arguments[i];
if (source !== null && source !== undefined) {
mergeImpl(policy, path, target, source, policy ? policy[""] : null);
}
}
if (policy && typeof(policy) !== "string") {
for (var key in policy) {
var elrh = policy[key];
if (typeof(elrh) === 'string' && elrh !== "replace") {
var oldValue = fluid.get(target, key);
if (oldValue === null || oldValue === undefined) {
var value = fluid.get(target, elrh);
fluid.set(target, key, value);
}
}
}
}
return target;
};
/**
* Merges the component's declared defaults, as obtained from fluid.defaults(),
* with the user's specified overrides.
*
* @param {Object} that the instance to attach the options to
* @param {String} componentName the unique "name" of the component, which will be used
* to fetch the default options from store. By recommendation, this should be the global
* name of the component's creator function.
* @param {Object} userOptions the user-specified configuration options for this component
*/
fluid.mergeComponentOptions = function (that, componentName, userOptions) {
var defaults = fluid.defaults(componentName);
if (fluid.expandOptions) {
defaults = fluid.expandOptions(fluid.copy(defaults), that);
}
that.options = fluid.merge(defaults ? defaults.mergePolicy: null, {}, defaults, userOptions);
};
/** A special "marker object" which is recognised as one of the arguments to
* fluid.initSubcomponents. This object is recognised by reference equality -
* where it is found, it is replaced in the actual argument position supplied
* to the specific subcomponent instance, with the particular options block
* for that instance attached to the overall "that" object.
*/
fluid.COMPONENT_OPTIONS = {type: "fluid.marker", value: "COMPONENT_OPTIONS"};
/** Construct a dummy or "placeholder" subcomponent, that optionally provides empty
* implementations for a set of methods.
*/
fluid.emptySubcomponent = function (options) {
var that = {};
options = $.makeArray(options);
var empty = function () {};
for (var i = 0; i < options.length; ++ i) {
that[options[i]] = empty;
}
return that;
};
/** Compute a "nickname" given a fully qualified typename, by returning the last path
* segment.
*/
fluid.computeNickName = function (typeName) {
var segs = fluid.model.parseEL(typeName);
return segs[segs.length - 1];
};
/**
* Creates a new "little component": a that-ist object with options merged into it by the framework.
* This method is a convenience for creating small objects that have options but don't require full
* View-like features such as the DOM Binder or events
*
* @param {Object} name the name of the little component to create
* @param {Object} options user-supplied options to merge with the defaults
*/
fluid.initLittleComponent = function (name, options) {
var that = {typeName: name, id: fluid.allocateGuid()};
// TODO: nickName must be available earlier than other merged options so that component may resolve to itself
that.nickName = options && options.nickName ? options.nickName: fluid.computeNickName(that.typeName);
fluid.mergeComponentOptions(that, name, options);
return that;
};
// The Model Events system.
var fluid_guid = 1;
/** Allocate an integer value that will be unique for this session **/
fluid.allocateGuid = function () {
return fluid_guid++;
};
fluid.event.identifyListener = function (listener) {
if (!listener.$$guid) {
listener.$$guid = fluid.allocateGuid();
}
return listener.$$guid;
};
fluid.event.mapPriority = function (priority) {
return (priority === null || priority === undefined ? 0 :
(priority === "last" ? -Number.MAX_VALUE :
(priority === "first" ? Number.MAX_VALUE : priority)));
};
fluid.event.listenerComparator = function (recA, recB) {
return recB.priority - recA.priority;
};
fluid.event.sortListeners = function (listeners) {
var togo = [];
fluid.each(listeners, function (listener) {
togo.push(listener);
});
return togo.sort(fluid.event.listenerComparator);
};
/** Construct an "event firer" object which can be used to register and deregister
* listeners, to which "events" can be fired. These events consist of an arbitrary
* function signature. General documentation on the Fluid events system is at
* http://wiki.fluidproject.org/display/fluid/The+Fluid+Event+System .
* @param {Boolean} unicast If <code>true</code>, this is a "unicast" event which may only accept
* a single listener.
* @param {Boolean} preventable If <code>true</code> the return value of each handler will
* be checked for <code>false</code> in which case further listeners will be shortcircuited, and this
* will be the return value of fire()
*/
fluid.event.getEventFirer = function (unicast, preventable) {
var listeners = {};
var sortedListeners = [];
function fireToListeners(listeners, args, wrapper) {
for (var i in listeners) {
var lisrec = listeners[i];
var listener = lisrec.listener;
if (lisrec.predicate && !lisrec.predicate(listener, args)) {
continue;
}
try {
var ret = (wrapper ? wrapper(listener) : listener).apply(null, args);
if (preventable && ret === false) {
return false;
}
}
catch (e) {
fluid.log("FireEvent received exception " + e.message + " e " + e + " firing to listener " + i);
throw (e);
}
}
}
return {
addListener: function (listener, namespace, predicate, priority) {
if (!listener) {
return;
}
if (unicast) {
namespace = "unicast";
}
if (!namespace) {
namespace = fluid.event.identifyListener(listener);
}
listeners[namespace] = {listener: listener, predicate: predicate, priority:
fluid.event.mapPriority(priority)};
sortedListeners = fluid.event.sortListeners(listeners);
},
removeListener: function (listener) {
if (typeof(listener) === 'string') {
delete listeners[listener];
}
else if (listener.$$guid) {
delete listeners[listener.$$guid];
}
sortedListeners = fluid.event.sortListeners(listeners);
},
// NB - this method exists currently solely for the convenience of the new,
// transactional changeApplier. As it exists it is hard to imagine the function
// being helpful to any other client. We need to get more experience on the kinds
// of listeners that are useful, and ultimately factor this method away.
fireToListeners: function (listeners, args, wrapper) {
return fireToListeners(listeners, args, wrapper);
},
fire: function () {
return fireToListeners(sortedListeners, arguments);
}
};
};
// **** VIEW-DEPENDENT DEFINITIONS BELOW HERE
/**
* Fetches a single container element and returns it as a jQuery.
*
* @param {String||jQuery||element} containerSpec an id string, a single-element jQuery, or a DOM element specifying a unique container
* @param {Boolean} fallible <code>true</code> if an empty container is to be reported as a valid condition
* @return a single-element jQuery of container
*/
fluid.container = function (containerSpec, fallible) {
var container = fluid.wrap(containerSpec);
if (fallible && !container || container.length === 0) {
return null;
}
// Throw an exception if we've got more or less than one element.
if (!container || !container.jquery || container.length !== 1) {
if (typeof(containerSpec) !== "string") {
containerSpec = container.selector;
}
var count = container.length !== undefined ? container.length : 0;
fluid.fail({
name: "NotOne",
message: count > 1 ? "More than one (" + count + ") container elements were "
: "No container element was found for selector " + containerSpec
});
}
return container;
};
/**
* Creates a new DOM Binder instance, used to locate elements in the DOM by name.
*
* @param {Object} container the root element in which to locate named elements
* @param {Object} selectors a collection of named jQuery selectors
*/
fluid.createDomBinder = function (container, selectors) {
var cache = {}, that = {};
function cacheKey(name, thisContainer) {
return fluid.allocateSimpleId(thisContainer) + "-" + name;
}
function record(name, thisContainer, result) {
cache[cacheKey(name, thisContainer)] = result;
}
that.locate = function (name, localContainer) {
var selector, thisContainer, togo;
selector = selectors[name];
thisContainer = localContainer ? localContainer: container;
if (!thisContainer) {
fluid.fail("DOM binder invoked for selector " + name + " without container");
}
if (!selector) {
return thisContainer;
}
if (typeof(selector) === "function") {
togo = $(selector.call(null, fluid.unwrap(thisContainer)));
} else {
togo = $(selector, thisContainer);
}
if (togo.get(0) === document) {
togo = [];
//fluid.fail("Selector " + name + " with value " + selectors[name] +
// " did not find any elements with container " + fluid.dumpEl(container));
}
if (!togo.selector) {
togo.selector = selector;
togo.context = thisContainer;
}
togo.selectorName = name;
record(name, thisContainer, togo);
return togo;
};
that.fastLocate = function (name, localContainer) {
var thisContainer = localContainer ? localContainer: container;
var key = cacheKey(name, thisContainer);
var togo = cache[key];
return togo ? togo : that.locate(name, localContainer);
};
that.clear = function () {
cache = {};
};
that.refresh = function (names, localContainer) {
var thisContainer = localContainer ? localContainer: container;
if (typeof names === "string") {
names = [names];
}
if (thisContainer.length === undefined) {
thisContainer = [thisContainer];
}
for (var i = 0; i < names.length; ++ i) {
for (var j = 0; j < thisContainer.length; ++ j) {
that.locate(names[i], thisContainer[j]);
}
}
};
that.resolvePathSegment = that.locate;
return that;
};
/** Expect that an output from the DOM binder has resulted in a non-empty set of