Skip to content

Instantly share code, notes, and snippets.

@christianvuerings
Created December 16, 2011 18:10
Show Gist options
  • Save christianvuerings/1487197 to your computer and use it in GitHub Desktop.
Save christianvuerings/1487197 to your computer and use it in GitHub Desktop.
Fluid Infusion 1.4 within Sakai OAE
/* 3akai_Infusion.js
* custom build for Sakai
* built with: ant -lib lib/rhino customBuild -Dinclude="reorderer" -Dexclude="jQuery, jQueryUICore, jQueryUIWidgets, jQueryTooltipPlugin" -Djsfilename="3akai_Infusion.js" -DnoMinify="true"
*/
/*
Copyright 2007-2010 University of Cambridge
Copyright 2007-2009 University of Toronto
Copyright 2007-2009 University of California, Berkeley
Copyright 2010-2011 Lucendo Development Ltd.
Copyright 2010 OCAD University
Copyright 2011 Charly Molter
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 console, window, fluid:true, fluid_1_4:true, jQuery, opera, YAHOO*/
// JSLint options
/*jslint white: true, funcinvoke: true, continue: true, jslintok: 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;
require(["jquery", "jquery-ui"], function(jQuery) {
(function ($, fluid) {
fluid.version = "Infusion 1.4";
fluid.environment = {
fluid: fluid
};
var globalObject = window || {};
var softFailure = [true];
// This function will be patched from FluidIoC.js in order to describe complex activities
fluid.describeActivity = function () {
return [];
};
/**
* 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
* @param ... Additional arguments
*/
fluid.fail = function (message /*, ... */) { // jslint:ok - whitespace in arg list
fluid.setLogging(true);
fluid.log.apply(null, ["ASSERTION FAILED: "].concat(fluid.makeArray(arguments)).concat(fluid.describeActivity()));
if (softFailure[0]) {
throw new Error(message);
} else {
message.fail(); // Intentionally cause a browser error by invoking a nonexistent function.
}
};
fluid.pushSoftFailure = function (condition) {
if (typeof (condition) === "boolean") {
softFailure.unshift(condition);
} else if (condition === -1) {
softFailure.shift();
}
};
fluid.notrycatch = false;
// A wrapper for the try/catch/finally language feature, to aid debugging on environments
// such as IE, where any try will destroy stack information for errors
fluid.tryCatch = function (tryfun, catchfun, finallyfun) {
finallyfun = finallyfun || fluid.identity;
if (fluid.notrycatch) {
var togo = tryfun();
finallyfun();
return togo;
} else {
try {
return tryfun();
} catch (e) {
if (catchfun) {
catchfun(e);
} else {
throw (e);
}
} finally {
finallyfun();
}
}
};
// TODO: rescued from kettleCouchDB.js - clean up in time
fluid.expect = function (name, members, target) {
fluid.transform(fluid.makeArray(members), function (key) {
if (typeof target[key] === "undefined") {
fluid.fail(name + " missing required parameter " + key);
}
});
};
// Logging
var logging;
/** Returns whether logging is enabled **/
fluid.isLogging = function () {
return logging;
};
/** method to allow user to enable logging (off by default) */
fluid.setLogging = function (enabled) {
if (typeof enabled === "boolean") {
logging = enabled;
} else {
logging = false;
}
};
// On some dodgy environments (notably IE9 and recent alphas of Firebug 1.8),
// console.log/debug are incomplete function objects and need to be operated via
// this trick: http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function
fluid.applyHostFunction = function (obj, func, args) {
if (func.apply) {
func.apply(obj, args);
} else {
var applier = Function.prototype.bind.call(func, obj);
applier.apply(obj, args);
}
};
/** 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 to the fluid.setLogging(true) function.
*/
fluid.log = function (message /*, ... */) { // jslint:ok - whitespace in arg list
if (logging) {
var arg0 = fluid.renderTimestamp(new Date()) + ": ";
var args = [arg0].concat(fluid.makeArray(arguments));
var str = args.join("");
if (typeof (console) !== "undefined") {
if (console.debug) {
fluid.applyHostFunction(console, console.debug, args);
} else if (typeof (console.log) === "function") {
fluid.applyHostFunction(console, console.log, args);
} else {
console.log(str); // this branch executes on old IE, fully synthetic console.log
}
} 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.
/** 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 value other than null or undefined **/
fluid.isValue = function (value) {
return value !== undefined && value !== null;
};
/** 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";
};
fluid.isDOMNode = function (obj) {
// This could be more sound, but messy:
// http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object
return obj && typeof (obj.nodeType) === "number";
};
/** 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";
};
/** 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);
};
/** 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>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>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;
};
/** Accepts an object to be filtered, and a list of keys. Either all keys not present in
* the list are removed, or only keys present in the list are returned.
* @param toFilter {Array|Object} The object to be filtered - this will be modified by the operation
* @param keys {Array of String} The list of keys to operate with
* @param exclude {boolean} If <code>true</code>, the keys listed are removed rather than included
* @return the filtered object (the same object that was supplied as <code>toFilter</code>
*/
fluid.filterKeys = function (toFilter, keys, exclude) {
return fluid.remove_if($.extend({}, toFilter), function (value, key) {
return exclude ^ ($.inArray(key, keys) === -1);
});
};
/** A convenience wrapper for <code>fluid.filterKeys</code> with the parameter <code>exclude</code> set to <code>true</code>
* Returns the supplied object with listed keys removed */
fluid.censorKeys = function (toCensor, keys) {
return fluid.filterKeys(toCensor, keys, true);
};
/** Return the keys in the supplied object as an array **/
fluid.keys = function (obj) {
var togo = [];
fluid.each(obj, function (value, key) {
togo.push(key);
});
return togo;
};
/**
* Searches through the supplied object, and returns <code>true</code> if the supplied value
* can be found
*/
fluid.contains = function (obj, value) {
return obj ? fluid.find(obj, function (thisValue, key) {
if (value === thisValue) {
return true;
}
}) : undefined;
};
/**
* 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;
/** Converts an array into an object whose keys are the elements of the array, each with the value "true"
*/
fluid.arrayToHash = function (array) {
var togo = {};
fluid.each(array, function (el) {
togo[el] = true;
});
return togo;
};
/**
* 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 (fluid.isArrayable(target)) {
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"};
/** A marker indicating that a value requires to be expanded after component construction begins **/
fluid.EXPAND = {type: "fluid.marker", value: "EXPAND"};
/** A marker indicating that a value requires to be expanded immediately**/
fluid.EXPAND_NOW = {type: "fluid.marker", value: "EXPAND_NOW"};
/** 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 === type;
};
/** 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, and be harmonised
* with the more complex implementations in fluid.pathUtil(data binding).
*/
fluid.model.parseEL = function (EL) {
return EL === "" ? [] : 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(".");
};
/** Helpful alias for old-style API **/
fluid.path = fluid.model.composeSegments;
fluid.composePath = fluid.model.composePath;
/** 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 segment === "" ? root : root[segment];
};
fluid.model.funcResolverStrategy = function (root, segment) {
if (root.resolvePathSegment) {
return root.resolvePathSegment(segment);
}
};
// unsupported, NON-API function
fluid.model.applyStrategy = function (strategy, root, segment, index) {
if (typeof (strategy) === "function") {
return strategy(root, segment, index);
} else if (strategy && strategy.next) {
return strategy.next(root, segment, index);
}
};
// unsupported, NON-API function
fluid.model.initStrategy = function (baseStrategy, index, oldStrategies) {
return baseStrategy.init ? baseStrategy.init(oldStrategies ? oldStrategies[index] : undefined) : baseStrategy;
};
// unsupported, NON-API function
fluid.model.makeTrundler = function (root, config, oldStrategies) {
var that = {
root: root,
strategies: fluid.isArrayable(config) ? config :
fluid.transform(config.strategies, function (strategy, index) {
return fluid.model.initStrategy(strategy, index, oldStrategies);
})
};
that.trundle = function (EL, uncess) {
uncess = uncess || 0;
var newThat = fluid.model.makeTrundler(that.root, config, that.strategies);
newThat.segs = fluid.model.parseEL(EL);
newThat.index = 0;
newThat.step(newThat.segs.length - uncess);
return newThat;
};
that.next = function () {
if (!that.root) {
return;
}
var accepted;
for (var i = 0; i < that.strategies.length; ++i) {
var value = fluid.model.applyStrategy(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 = {
strategies: [fluid.model.funcResolverStrategy, fluid.model.defaultFetchStrategy, fluid.model.defaultCreatorStrategy]
};
// unsupported, NON-API function
// core trundling recursion point
fluid.model.trundleImpl = function (trundler, EL, config, uncess) {
if (typeof (EL) === "string") {
trundler = trundler.trundle(EL, uncess);
} else {
var key = EL.type || "default";
var resolver = config.resolvers[key];
if (!resolver) {
fluid.fail("Unable to find resolver of type " + key);
}
trundler = resolver(EL, trundler) || {};
if (EL.path && trundler.trundle && trundler.root !== undefined) {
trundler = fluid.model.trundleImpl(trundler, EL.path, config, uncess);
}
}
return trundler;
};
// unsupported, NON-API function
// entry point for initially unbased trundling
fluid.model.trundle = function (root, EL, config, uncess) {
EL = EL || "";
config = config || fluid.model.defaultGetConfig;
var trundler = fluid.model.makeTrundler(root, config);
return fluid.model.trundleImpl(trundler, EL, config, uncess);
};
fluid.model.getPenultimate = function (root, EL, config) {
return fluid.model.trundle(root, EL, config, 1);
};
fluid.set = function (root, EL, newValue, config) {
config = config || fluid.model.defaultSetConfig;
var trundler = fluid.model.getPenultimate(root, EL, config);
trundler.root[trundler.last] = newValue;
};
fluid.model.defaultGetConfig = {
strategies: [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) {
return fluid.model.trundle(root, EL, config).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, {strategies: [envFetcher].concat(fluid.model.defaultGetConfig.strategies)});
}
};
/**
* 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, {strategies: [envFetcher].concat(fluid.model.defaultSetConfig.strategies)});
};
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;
};
// stubs for two functions in FluidDebugging.js
fluid.dumpEl = fluid.identity;
fluid.renderTimestamp = fluid.identity;
/*** The Model Events system. ***/
fluid.registerNamespace("fluid.event");
fluid.generateUniquePrefix = function () {
return (Math.floor(Math.random() * 1e12)).toString(36) + "-";
};
var fluid_prefix = fluid.generateUniquePrefix();
var fluid_guid = 1;
/** Allocate an string value that will be very likely unique within this (browser) process **/
fluid.allocateGuid = function () {
return fluid_prefix + (fluid_guid++);
};
fluid.event.identifyListener = function (listener) {
if (typeof (listener) === "string") {
return listener;
}
if (!listener.$$guid) {
listener.$$guid = fluid.allocateGuid();
}
return listener.$$guid;
};
// unsupported, NON-API function
fluid.event.mapPriority = function (priority, count) {
return (priority === null || priority === undefined ? -count :
(priority === "last" ? -Number.MAX_VALUE :
(priority === "first" ? Number.MAX_VALUE : priority)));
};
// unsupported, NON-API function
fluid.event.listenerComparator = function (recA, recB) {
return recB.priority - recA.priority;
};
// unsupported, NON-API function
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 (typeof (listener) === "string") {
var listenerFunc = fluid.getGlobalValue(listener);
if (!listenerFunc) {
fluid.fail("Unable to look up name " + listener + " as a global function");
} else {
listener = lisrec.listener = listenerFunc;
}
}
if (lisrec.predicate && !lisrec.predicate(listener, args)) {
continue;
}
var value = fluid.tryCatch(function () {
var ret = (wrapper ? wrapper(listener) : listener).apply(null, args);
if (preventable && ret === false) {
return false;
}
if (unicast) {
return ret;
}
}, function (e) { // jslint:ok - function within a loop, only invoked synchronously
fluid.log("FireEvent received exception " + e.message + " e " + e + " firing to listener " + i);
throw (e);
}); // jslint:ok - function within loop
if (value !== undefined) {
return value;
}
}
}
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.length)};
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);
}
};
};
// unsupported, NON-API function
fluid.event.addListenerToFirer = function (firer, value, namespace) {
if (fluid.isArrayable(value)) {
for (var i = 0; i < value.length; ++i) {
fluid.event.addListenerToFirer(firer, value[i], namespace);
}
} else if (typeof (value) === "function" || typeof (value) === "string") {
firer.addListener(value, namespace);
} else if (value && typeof (value) === "object") {
firer.addListener(value.listener, namespace || value.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 (that, events, listeners) {
fluid.each(listeners, function (value, key) {
var firer, namespace;
if (key.charAt(0) === "{") {
if (!fluid.expandOptions) {
fluid.fail("fluid.expandOptions could not be loaded - please include FluidIoC.js in order to operate IoC-driven event with descriptor " +
key);
}
firer = fluid.expandOptions(key, that);
} else {
var keydot = key.indexOf(".");
if (keydot !== -1) {
namespace = key.substring(keydot + 1);
key = key.substring(0, keydot);
}
if (!events[key]) {
fluid.fail("Listener registered for event " + key + " which is not defined for this component");
events[key] = fluid.event.getEventFirer();
}
firer = events[key];
}
fluid.event.addListenerToFirer(firer, value, namespace);
});
};
function initEvents(that, events, pass) {
fluid.each(events, function (eventSpec, eventKey) {
var isIoCEvent = eventSpec && (typeof (eventSpec) !== "string" || eventSpec.charAt(0) === "{");
var event;
if (isIoCEvent && pass === "IoC") {
if (!fluid.event.resolveEvent) {
fluid.fail("fluid.event.resolveEvent could not be loaded - please include FluidIoC.js in order to operate IoC-driven event with descriptor ",
eventSpec);
} else {
event = fluid.event.resolveEvent(that, eventKey, eventSpec);
}
} else if (pass === "flat") {
event = fluid.event.getEventFirer(eventSpec === "unicast", eventSpec === "preventable");
}
if (event) {
that.events[eventKey] = event;
}
});
}
/**
* 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 = {};
// TODO: manual 2-phase instantiation since we have no GINGER WORLD
initEvents(that, options.events, "flat");
initEvents(that, options.events, "IoC");
// TODO: manually expand these late so that members attached to ourselves with preInitFunction can be detected
var listeners = fluid.expandOptions ? fluid.expandOptions(options.listeners, that) : options.listeners;
fluid.mergeListeners(that, that.events, listeners);
};
fluid.mergeListenersPolicy = function (target, source) {
var togo = target || {};
fluid.each(source, function (listeners, key) {
togo[key] = fluid.makeArray(source[key]).concat(fluid.makeArray(listeners));
});
return togo;
};
/*** DEFAULTS AND OPTIONS MERGING SYSTEM ***/
var defaultsStore = {};
var resolveGradesImpl = function (gs, gradeNames) {
gradeNames = fluid.makeArray(gradeNames);
fluid.each(gradeNames, function (gradeName) {
var options = fluid.rawDefaults(gradeName) || {};
gs.gradeHash[gradeName] = true;
gs.gradeChain.push(gradeName);
gs.optionsChain.push(options);
fluid.each(options.gradeNames, function (parent) {
if (!gs.gradeHash[parent]) {
resolveGradesImpl(gs, parent);
}
});
});
return gs;
};
// unsupported, NON-API function
fluid.resolveGradeStructure = function (gradeNames) {
var gradeStruct = {
gradeChain: [],
gradeHash: {},
optionsChain: []
};
return resolveGradesImpl(gradeStruct, gradeNames);
};
fluid.lifecycleFunctions = {
preInitFunction: true,
postInitFunction: true,
finalInitFunction: true
};
// unsupported, NON-API function
fluid.mergeLifecycleFunction = function (target, source) {
fluid.event.addListenerToFirer(target, source);
return target;
};
fluid.rootMergePolicy = fluid.transform(fluid.lifecycleFunctions, function () {
return fluid.mergeLifecycleFunction;
});
// unsupported, NON-API function
fluid.makeLifecycleFirers = function () {
return fluid.transform(fluid.lifecycleFunctions, function () {
return fluid.event.getEventFirer();
});
};
// unsupported, NON-API function
fluid.resolveGrade = function (defaults, gradeNames) {
var mergeArgs = [defaults];
if (gradeNames) {
var gradeStruct = fluid.resolveGradeStructure(gradeNames);
mergeArgs = gradeStruct.optionsChain.reverse().concat(mergeArgs).concat({gradeNames: gradeStruct.gradeChain});
}
mergeArgs = [fluid.rootMergePolicy, fluid.makeLifecycleFirers()].concat(mergeArgs);
var mergedDefaults = fluid.merge.apply(null, mergeArgs);
return mergedDefaults;
};
// unsupported, NON-API function
fluid.resolveGradedOptions = function (componentName) {
var defaults = fluid.rawDefaults(componentName);
if (!defaults) {
return defaults;
} else {
return fluid.resolveGrade(defaults, defaults.gradeNames);
}
};
// unsupported, NON-API function
fluid.rawDefaults = function (componentName, options) {
if (options === undefined) {
return defaultsStore[componentName];
} else {
defaultsStore[componentName] = options;
}
};
fluid.hasGrade = function (options, gradeName) {
return !options || !options.gradeNames ? false : fluid.contains(options.gradeNames, gradeName);
};
/**
* Retrieves 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. NB - the use of "global options"
* is deprecated and will be removed from the framework in release 1.5
* @param {String} componentName the name of the component
* @param {Object} (optional) an container of key/value pairs to set
*/
fluid.defaults = function () {
var offset = 0;
if (typeof arguments[0] === "boolean") {
offset = 1;
}
var componentName = (offset === 0 ? "" : "*.global-") + arguments[offset];
var options = arguments[offset + 1];
if (options === undefined) {
return fluid.resolveGradedOptions(componentName);
} else {
if (options && options.options) {
fluid.fail("Probable error in options structure with option named \"options\" - perhaps you meant to write these options at top level in fluid.defaults?");
}
fluid.rawDefaults(componentName, options);
if (fluid.hasGrade(options, "autoInit")) {
fluid.makeComponent(componentName, fluid.resolveGradedOptions(componentName));
}
}
};
fluid.makeComponent = function (componentName, options) {
if (!options.initFunction || !options.gradeNames) {
fluid.fail("Cannot autoInit component " + componentName + " which does not have an initFunction and gradeName defined");
}
var creator = function () {
return fluid.initComponent(componentName, arguments);
};
var existing = fluid.getGlobalValue(componentName);
if (existing) {
$.extend(creator, existing);
}
fluid.setGlobalValue(componentName, creator);
};
fluid.makeComponents = function (components, env) {
fluid.each(components, function (value, key) {
var options = {
gradeNames: fluid.makeArray(value).concat(["autoInit"])
};
fluid.defaults(key, options);
});
};
// The base system grade definitions
fluid.defaults("fluid.littleComponent", {
initFunction: "fluid.initLittleComponent",
argumentMap: {
options: 0
}
});
fluid.defaults("fluid.eventedComponent", {
gradeNames: ["fluid.littleComponent"],
mergePolicy: {
listeners: "fluid.mergeListenersPolicy"
}
});
fluid.preInitModelComponent = function (that) {
that.model = that.options.model || {};
that.applier = that.options.applier || fluid.makeChangeApplier(that.model, that.options.changeApplierOptions);
};
fluid.defaults("fluid.modelComponent", {
gradeNames: ["fluid.littleComponent"],
preInitFunction: {
namespace: "preInitModelComponent",
listener: "fluid.preInitModelComponent"
},
mergePolicy: {
model: "preserve",
applier: "nomerge"
}
});
fluid.defaults("fluid.viewComponent", {
gradeNames: ["fluid.littleComponent", "fluid.modelComponent", "fluid.eventedComponent"],
initFunction: "fluid.initView",
argumentMap: {
container: 0,
options: 1
}
});
// unsupported, NON-API function
fluid.guardCircularity = function (seenIds, source, message1, message2) {
if (source && source.id) {
if (!seenIds[source.id]) {
seenIds[source.id] = source;
} else if (seenIds[source.id] === source) {
fluid.fail("Circularity in options " + message1 + " - component with typename " + source.typeName + " and id " + source.id
+ " has already been seen" + message2);
}
}
};
fluid.mergePolicyIs = function (policy, test) {
return typeof (policy) === "string" && $.inArray(test, policy.split(/\s*,\s*/)) !== -1;
};
function mergeImpl(policy, basePath, target, source, thisPolicy, rec) {
if (typeof (thisPolicy) === "function") {
thisPolicy.call(null, target, source);
return target;
}
if (fluid.mergePolicyIs(thisPolicy, "replace")) {
fluid.clear(target);
}
fluid.guardCircularity(rec.seenIds, source, "merging", " when evaluating path " + basePath + " - please protect components from merging using the \"nomerge\" merge policy");
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" &&
!fluid.isDOMNode(thisSource) && !thisSource.jquery && thisSource !== fluid.VALUE &&
!fluid.mergePolicyIs(newPolicy, "preserve") && !fluid.mergePolicyIs(newPolicy, "nomerge") && !fluid.mergePolicyIs(newPolicy, "noexpand")) {
if (primitiveTarget) {
target[name] = thisTarget = fluid.freshContainer(thisSource)
}
mergeImpl(policy, path, thisTarget, thisSource, newPolicy, rec);
} else {
if (typeof (newPolicy) === "function") {
target[name] = newPolicy.call(null, thisTarget, thisSource, name);
} else if (!fluid.isValue(thisTarget) || !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.isValue(thisTarget) && 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.initLittleComponent</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, {seenIds: {}});
}
}
if (policy && typeof (policy) !== "string") {
for (var key in policy) {
var elrh = policy[key];
if (typeof (elrh) === "string" && elrh !== "replace" && elrh !== "preserve") {
var oldValue = fluid.get(target, key);
if (oldValue === null || oldValue === undefined) {
var value = fluid.get(target, elrh);
fluid.set(target, key, value);
}
}
}
}
return target;
};
// unsupported, NON-API function
fluid.transformOptions = function (mergeArgs, transRec) {
fluid.expect("Options transformation record", ["transformer", "config"], transRec);
var transFunc = fluid.getGlobalValue(transRec.transformer);
var togo = fluid.transform(mergeArgs, function (value, key) {
return key === 0 ? value : transFunc.call(null, value, transRec.config);
});
return togo;
};
// unsupporter, NON-API function
fluid.lastTransformationRecord = function (extraArgs) {
for (var i = extraArgs.length - 1; i >= 0; --i) {
if (extraArgs[i] && extraArgs[i].transformOptions) {
return extraArgs[i].transformOptions;
}
}
};
/**
* 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
*/
// unsupported, NON-API function
fluid.mergeComponentOptions = function (that, componentName, userOptions, localOptions) {
var defaults = fluid.defaults(componentName);
var mergePolicy = $.extend({}, fluid.rootMergePolicy, defaults ? defaults.mergePolicy : {});
var defaultGrades = defaults && defaults.gradeNames;
var mergeArgs;
if (!defaultGrades) {
defaults = fluid.censorKeys(defaults, fluid.keys(fluid.lifecycleFunctions));
mergeArgs = [mergePolicy, localOptions];
} else {
mergeArgs = [mergePolicy];
}
var extraArgs;
if (fluid.expandComponentOptions) {
extraArgs = fluid.expandComponentOptions(defaults, userOptions, that);
} else {
extraArgs = [defaults, userOptions];
}
var transRec = fluid.lastTransformationRecord(extraArgs);
if (transRec) {
extraArgs = fluid.transformOptions(extraArgs, transRec);
}
mergeArgs = mergeArgs.concat(extraArgs);
that.options = fluid.merge.apply(null, mergeArgs);
};
// The Fluid Component System proper
/** 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.
* NOTE: The use of this marker has been deprecated as of the Fluid 1.4 release in
* favour of the contextual EL path "{options}" - it will be removed in a future
* release of the framework.
*/
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];
};
/** Create a "type tag" component with no state but simply a type name and id. The most
* minimal form of Fluid component */
fluid.typeTag = function (name) {
return name ? {
typeName: name,
id: fluid.allocateGuid()
} : null;
};
/** A combined "component and grade name" which allows type tags to be declaratively constructed
* from options material */
fluid.typeFount = function (options) {
var that = fluid.initLittleComponent("fluid.typeFount", options);
return fluid.typeTag(that.options.targetTypeName);
};
/**
* 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
*/
// NOTE: the 3rd argument localOptions is NOT to be advertised as part of the stable API, it is present
// just to allow backward compatibility whilst grade specifications are not mandatory
fluid.initLittleComponent = function (name, options, localOptions) {
var that = fluid.typeTag(name);
// 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);
localOptions = localOptions || {gradeNames: "fluid.littleComponent"};
localOptions = fluid.resolveGrade({}, localOptions.gradeNames);
fluid.mergeComponentOptions(that, name, options, localOptions);
that.options.preInitFunction.fire(that);
if (fluid.hasGrade(that.options, "fluid.eventedComponent")) {
fluid.instantiateFirers(that, that.options);
}
if (!fluid.hasGrade(that.options, "autoInit")) {
fluid.clearLifecycleFunctions(that.options);
}
return that;
};
fluid.clearLifecycleFunctions = function (options) {
fluid.each(fluid.lifecycleFunctions, function (value, key) {
delete options[key];
});
delete options.initFunction;
};
// unsupported, NON-API function
// NOTE: this function represents a temporary strategy until we have more integrated IoC debugging.
// It preserves the current framework behaviour for the 1.4 release, but provides a more informative
// diagnostic - in fact, it is perfectly acceptable for a component's creator to return no value and
// the failure is really in assumptions in fluid.initComponent. Revisit this issue for 1.5
fluid.diagnoseFailedView = function(componentName, that, options, args) {
if (!that && fluid.hasGrade(options, "fluid.viewComponent")) {
var container = fluid.wrap(args[1]);
var message1 = "Instantiation of autoInit component with type " + componentName + " failed, since "
if (container.length === 0) {
fluid.fail(message1 + "selector \"", args[1], "\" did not match any markup in the document");
}
else {
fluid.fail(message1 + " component creator function did not return a value");
}
}
};
fluid.initComponent = function (componentName, initArgs) {
var options = fluid.defaults(componentName);
if (!options.gradeNames) {
fluid.fail("Cannot initialise component " + componentName + " which has no gradeName registered");
}
var args = [componentName].concat(fluid.makeArray(initArgs)); // TODO: support different initFunction variants
var that = fluid.invokeGlobalFunction(options.initFunction, args);
fluid.diagnoseFailedView(componentName, that, options, args);
that.options.postInitFunction.fire(that);
if (fluid.initDependents) {
fluid.initDependents(that);
}
that.options.finalInitFunction.fire(that);
fluid.clearLifecycleFunctions(that.options);
return that.options.returnedPath ? fluid.get(that, that.options.returnedPath) : that;
};
// unsupported, NON-API function
fluid.initSubcomponentImpl = function (that, entry, args) {
var togo;
if (typeof (entry) !== "function") {
var entryType = typeof (entry) === "string" ? entry : entry.type;
var globDef = fluid.defaults(true, entryType);
fluid.merge("reverse", that.options, globDef);
togo = entryType === "fluid.emptySubcomponent" ?
fluid.emptySubcomponent(entry.options) :
fluid.invokeGlobalFunction(entryType, args);
} else {
togo = entry.apply(null, args);
}
var returnedOptions = togo ? togo.returnedOptions : null;
if (returnedOptions) {
fluid.merge(that.options.mergePolicy, that.options, returnedOptions);
if (returnedOptions.listeners) {
fluid.mergeListeners(that, that.events, returnedOptions.listeners);
}
}
return togo;
};
/** Initialise all the "subcomponents" which are configured to be attached to
* the supplied top-level component, which share a particular "class name".
* @param {Component} that The top-level component for which sub-components are
* to be instantiated. It contains specifications for these subcomponents in its
* <code>options</code> structure.
* @param {String} className The "class name" or "category" for the subcomponents to
* be instantiated. A class name specifies an overall "function" for a class of
* subcomponents and represents a category which accept the same signature of
* instantiation arguments.
* @param {Array of Object} args The instantiation arguments to be passed to each
* constructed subcomponent. These will typically be members derived from the
* top-level <code>that</code> or perhaps globally discovered from elsewhere. One
* of these arguments may be <code>fluid.COMPONENT_OPTIONS</code> in which case this
* placeholder argument will be replaced by instance-specific options configured
* into the member of the top-level <code>options</code> structure named for the
* <code>className</code>
* @return {Array of Object} The instantiated subcomponents, one for each member
* of <code>that.options[className]</code>.
*/
fluid.initSubcomponents = function (that, className, args) {
var entry = that.options[className];
if (!entry) {
return;
}
var entries = $.makeArray(entry);
var optindex = -1;
var togo = [];
args = $.makeArray(args);
for (var i = 0; i < args.length; ++i) {
if (args[i] === fluid.COMPONENT_OPTIONS) {
optindex = i;
}
}
for (i = 0; i < entries.length; ++i) {
entry = entries[i];
if (optindex !== -1) {
args[optindex] = entry.options;
}
togo[i] = fluid.initSubcomponentImpl(that, entry, args);
}
return togo;
};
fluid.initSubcomponent = function (that, className, args) {
return fluid.initSubcomponents(that, className, args)[0];
};
// **** VIEW-DEPENDENT DEFINITIONS BELOW HERE
fluid.checkTryCatchParameter = function () {
var location = window.location || { search: "", protocol: "file:" };
var GETParams = location.search.slice(1).split('&');
return fluid.contains(GETParams, "notrycatch");
};
fluid.notrycatch = fluid.checkTryCatchParameter();
/**
* 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((count > 1 ? "More than one (" + count + ") container elements were"
: "No container element was") + " found for selector " + containerSpec);
}
if (!fluid.isDOMNode(container[0])) {
fluid.fail("fluid.container was supplied a non-jQueryable element");
}
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 jQuery selector query has resulted in a non-empty set of
* results. If none are found, this function will fail with a diagnostic message,
* with the supplied message prepended.
*/
fluid.expectFilledSelector = function (result, message) {
if (result && result.length === 0 && result.jquery) {
fluid.fail(message + ": selector \"" + result.selector + "\" with name " + result.selectorName +
" returned no results in context " + fluid.dumpEl(result.context));
}
};
/**
* The central initialiation method called as the first act of every Fluid
* component. This function automatically merges user options with defaults,
* attaches a DOM Binder to the instance, and configures events.
*
* @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 {jQueryable} container A specifier for the single root "container node" in the
* DOM which will house all the markup for this component.
* @param {Object} userOptions The configuration options for this component.
*/
// 4th argument is NOT SUPPORTED, see comments for initLittleComponent
fluid.initView = function (componentName, container, userOptions, localOptions) {
fluid.expectFilledSelector(container, "Error instantiating component with name \"" + componentName);
container = fluid.container(container, true);
if (!container) {
return null;
}
var that = fluid.initLittleComponent(componentName, userOptions, localOptions || {gradeNames: ["fluid.viewComponent"]});
that.container = container;
fluid.initDomBinder(that);
return that;
};
/**
* Creates a new DOM Binder instance for the specified component and mixes it in.
*
* @param {Object} that the component instance to attach the new DOM Binder to
*/
fluid.initDomBinder = function (that) {
that.dom = fluid.createDomBinder(that.container, that.options.selectors);
that.locate = that.dom.locate;
};
// DOM Utilities.
/**
* Finds the nearest ancestor of the element that passes the test
* @param {Element} element DOM element
* @param {Function} test A function which takes an element as a parameter and return true or false for some test
*/
fluid.findAncestor = function (element, test) {
element = fluid.unwrap(element);
while (element) {
if (test(element)) {
return element;
}
element = element.parentNode;
}
};
/**
* Returns a jQuery object given the id of a DOM node. In the case the element
* is not found, will return an empty list.
*/
fluid.jById = function (id, dokkument) {
dokkument = dokkument && dokkument.nodeType === 9 ? dokkument : document;
var element = fluid.byId(id, dokkument);
var togo = element ? $(element) : [];
togo.selector = "#" + id;
togo.context = dokkument;
return togo;
};
/**
* Returns an DOM element quickly, given an id
*
* @param {Object} id the id of the DOM node to find
* @param {Document} dokkument the document in which it is to be found (if left empty, use the current document)
* @return The DOM element with this id, or null, if none exists in the document.
*/
fluid.byId = function (id, dokkument) {
dokkument = dokkument && dokkument.nodeType === 9 ? dokkument : document;
var el = dokkument.getElementById(id);
if (el) {
// Use element id property here rather than attribute, to work around FLUID-3953
if (el.id !== id) {
fluid.fail("Problem in document structure - picked up element " +
fluid.dumpEl(el) + " for id " + id +
" without this id - most likely the element has a name which conflicts with this id");
}
return el;
} else {
return null;
}
};
/**
* Returns the id attribute from a jQuery or pure DOM element.
*
* @param {jQuery||Element} element the element to return the id attribute for
*/
fluid.getId = function (element) {
return fluid.unwrap(element).id;
};
/**
* Allocate an id to the supplied element if it has none already, by a simple
* scheme resulting in ids "fluid-id-nnnn" where nnnn is an increasing integer.
*/
fluid.allocateSimpleId = function (element) {
var simpleId = "fluid-id-" + fluid.allocateGuid();
if (!element) {
return simpleId;
}
element = fluid.unwrap(element);
if (!element.id) {
element.id = simpleId;
}
return element.id;
};
// Message resolution and templating
/**
* Converts a string to a regexp with the specified flags given in parameters
* @param {String} a string that has to be turned into a regular expression
* @param {String} the flags to provide to the reg exp
*/
fluid.stringToRegExp = function (str, flags) {
return new RegExp(str.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"), flags);
};
/**
* Simple string template system.
* Takes a template string containing tokens in the form of "%value".
* Returns a new string with the tokens replaced by the specified values.
* Keys and values can be of any data type that can be coerced into a string. Arrays will work here as well.
*
* @param {String} template a string (can be HTML) that contains tokens embedded into it
* @param {object} values a collection of token keys and values
*/
fluid.stringTemplate = function (template, values) {
var newString = template;
for (var key in values) {
var re = fluid.stringToRegExp("%" + key, "g");
newString = newString.replace(re, values[key]);
}
return newString;
};
fluid.messageResolver = function (options) {
var that = fluid.initLittleComponent("fluid.messageResolver", options);
that.messageBase = that.options.parseFunc(that.options.messageBase);
that.lookup = function (messagecodes) {
var resolved = fluid.messageResolver.resolveOne(that.messageBase, messagecodes);
if (resolved === undefined) {
return fluid.find(that.options.parents, function (parent) {
return parent.lookup(messagecodes);
});
} else {
return {template: resolved, resolveFunc: that.options.resolveFunc};
}
};
that.resolve = function (messagecodes, args) {
if (!messagecodes) {
return "[No messagecodes provided]";
}
messagecodes = fluid.makeArray(messagecodes);
var looked = that.lookup(messagecodes);
return looked ? looked.resolveFunc(looked.template, args) :
"[Message string for key " + messagecodes[0] + " not found]";
};
return that;
};
fluid.defaults("fluid.messageResolver", {
mergePolicy: {
messageBase: "preserve"
},
resolveFunc: fluid.stringTemplate,
parseFunc: fluid.identity,
messageBase: {},
parents: []
});
fluid.messageResolver.resolveOne = function (messageBase, messagecodes) {
for (var i = 0; i < messagecodes.length; ++i) {
var code = messagecodes[i];
var message = messageBase[code];
if (message !== undefined) {
return message;
}
}
};
/** Converts a data structure consisting of a mapping of keys to message strings,
* into a "messageLocator" function which maps an array of message codes, to be
* tried in sequence until a key is found, and an array of substitution arguments,
* into a substituted message string.
*/
fluid.messageLocator = function (messageBase, resolveFunc) {
var resolver = fluid.messageResolver({messageBase: messageBase, resolveFunc: resolveFunc});
return function (messagecodes, args) {
return resolver.resolve(messagecodes, args);
};
};
})(jQuery, fluid_1_4);
/*
Copyright 2007-2010 University of Cambridge
Copyright 2007-2009 University of Toronto
Copyright 2010-2011 Lucendo Development Ltd.
Copyright 2010 OCAD University
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
*/
/** This file contains functions which depend on the presence of a DOM document
* but which do not depend on the contents of Fluid.js **/
// Declare dependencies
/*global fluid_1_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
// Private constants.
var NAMESPACE_KEY = "fluid-scoped-data";
/**
* Gets stored state from the jQuery instance's data map.
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.getScopedData = function(target, key) {
var data = $(target).data(NAMESPACE_KEY);
return data ? data[key] : undefined;
};
/**
* Stores state in the jQuery instance's data map. Unlike jQuery's version,
* accepts multiple-element jQueries.
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.setScopedData = function(target, key, value) {
$(target).each(function() {
var data = $.data(this, NAMESPACE_KEY) || {};
data[key] = value;
$.data(this, NAMESPACE_KEY, data);
});
};
/** Global focus manager - makes use of "focusin" event supported in jquery 1.4.2 or later.
*/
var lastFocusedElement = null;
$(document).bind("focusin", function(event){
lastFocusedElement = event.target;
});
fluid.getLastFocusedElement = function() {
return lastFocusedElement;
}
var ENABLEMENT_KEY = "enablement";
/** Queries or sets the enabled status of a control. An activatable node
* may be "disabled" in which case its keyboard bindings will be inoperable
* (but still stored) until it is reenabled again.
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.enabled = function(target, state) {
target = $(target);
if (state === undefined) {
return fluid.getScopedData(target, ENABLEMENT_KEY) !== false;
}
else {
$("*", target).add(target).each(function() {
if (fluid.getScopedData(this, ENABLEMENT_KEY) !== undefined) {
fluid.setScopedData(this, ENABLEMENT_KEY, state);
}
else if (/select|textarea|input/i.test(this.nodeName)) {
$(this).prop("disabled", !state);
}
});
fluid.setScopedData(target, ENABLEMENT_KEY, state);
}
};
fluid.initEnablement = function(target) {
fluid.setScopedData(target, ENABLEMENT_KEY, true);
};
// This function is necessary since simulation of focus events by jQuery under IE
// is not sufficiently good to intercept the "focusin" binding. Any code which triggers
// focus or blur synthetically throughout the framework and client code must use this function,
// especially if correct cross-platform interaction is required with the "deadMansBlur" function.
function applyOp(node, func) {
node = $(node);
node.trigger("fluid-"+func);
node[func]();
}
$.each(["focus", "blur"], function(i, name) {
fluid[name] = function(elem) {
applyOp(elem, name);
}
});
})(jQuery, fluid_1_4);
/*
Copyright 2008-2010 University of Cambridge
Copyright 2008-2009 University of Toronto
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_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.dom = fluid.dom || {};
// Node walker function for iterateDom.
var getNextNode = function (iterator) {
if (iterator.node.firstChild) {
iterator.node = iterator.node.firstChild;
iterator.depth += 1;
return iterator;
}
while (iterator.node) {
if (iterator.node.nextSibling) {
iterator.node = iterator.node.nextSibling;
return iterator;
}
iterator.node = iterator.node.parentNode;
iterator.depth -= 1;
}
return iterator;
};
/**
* Walks the DOM, applying the specified acceptor function to each element.
* There is a special case for the acceptor, allowing for quick deletion of elements and their children.
* Return "delete" from your acceptor function if you want to delete the element in question.
* Return "stop" to terminate iteration.
* Implementation note - this utility exists mainly for performance reasons. It was last tested
* carefully some time ago (around jQuery 1.2) but at that time was around 3-4x faster at raw DOM
* filtration tasks than the jQuery equivalents, which was an important source of performance loss in the
* Reorderer component. General clients of the framework should use this method with caution if at all, and
* the performance issues should be reassessed when we have time.
*
* @param {Element} node the node to start walking from
* @param {Function} acceptor the function to invoke with each DOM element
* @param {Boolean} allnodes Use <code>true</code> to call acceptor on all nodes,
* rather than just element nodes (type 1)
*/
fluid.dom.iterateDom = function (node, acceptor, allNodes) {
var currentNode = {node: node, depth: 0};
var prevNode = node;
var condition;
while (currentNode.node !== null && currentNode.depth >= 0 && currentNode.depth < fluid.dom.iterateDom.DOM_BAIL_DEPTH) {
condition = null;
if (currentNode.node.nodeType === 1 || allNodes) {
condition = acceptor(currentNode.node, currentNode.depth);
}
if (condition) {
if (condition === "delete") {
currentNode.node.parentNode.removeChild(currentNode.node);
currentNode.node = prevNode;
}
else if (condition === "stop") {
return currentNode.node;
}
}
prevNode = currentNode.node;
currentNode = getNextNode(currentNode);
}
};
// Work around IE circular DOM issue. This is the default max DOM depth on IE.
// http://msdn2.microsoft.com/en-us/library/ms761392(VS.85).aspx
fluid.dom.iterateDom.DOM_BAIL_DEPTH = 256;
/**
* Checks if the specified container is actually the parent of containee.
*
* @param {Element} container the potential parent
* @param {Element} containee the child in question
*/
fluid.dom.isContainer = function (container, containee) {
for (; containee; containee = containee.parentNode) {
if (container === containee) {
return true;
}
}
return false;
};
/** Return the element text from the supplied DOM node as a single String.
* Implementation note - this is a special-purpose utility used in the framework in just one
* position in the Reorderer. It only performs a "shallow" traversal of the text and was intended
* as a quick and dirty means of extracting element labels where the user had not explicitly provided one.
* It should not be used by general users of the framework and its presence here needs to be
* reassessed.
*/
fluid.dom.getElementText = function (element) {
var nodes = element.childNodes;
var text = "";
for (var i = 0; i < nodes.length; ++i) {
var child = nodes[i];
if (child.nodeType === 3) {
text = text + child.nodeValue;
}
}
return text;
};
})(jQuery, fluid_1_4);
/*
Copyright 2008-2010 University of Cambridge
Copyright 2008-2009 University of Toronto
Copyright 2010 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_4: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 */
fluid_1_4 = fluid_1_4 || {};
(function ($, fluid) {
var unUnicode = /(\\u[\dabcdef]{4}|\\x[\dabcdef]{2})/g;
fluid.unescapeProperties = function (string) {
string = string.replace(unUnicode, function(match) {
var code = match.substring(2);
var parsed = parseInt(code, 16);
return String.fromCharCode(parsed);
}
);
var pos = 0;
while (true) {
var backpos = string.indexOf("\\", pos);
if (backpos === -1) {
break;
}
if (backpos === string.length - 1) {
return [string.substring(0, string.length - 1), true];
}
var replace = string.charAt(backpos + 1);
if (replace === "n") replace = "\n";
if (replace === "r") replace = "\r";
if (replace === "t") replace = "\t";
string = string.substring(0, backpos) + replace + string.substring(backpos + 2);
pos = backpos + 1;
}
return [string, false];
};
var breakPos = /[^\\][\s:=]/;
fluid.parseJavaProperties = function(text) {
// File format described at http://java.sun.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)
var togo = {};
text = text.replace(/\r\n/g, "\n");
text = text.replace(/\r/g, "\n");
lines = text.split("\n");
var contin, key, valueComp, valueRaw, valueEsc;
for (var i = 0; i < lines.length; ++ i) {
var line = $.trim(lines[i]);
if (!line || line.charAt(0) === "#" || line.charAt(0) === '!') {
continue;
}
if (!contin) {
valueComp = "";
var breakpos = line.search(breakPos);
if (breakpos === -1) {
key = line;
valueRaw = "";
}
else {
key = $.trim(line.substring(0, breakpos + 1)); // +1 since first char is escape exclusion
valueRaw = $.trim(line.substring(breakpos + 2));
if (valueRaw.charAt(0) === ":" || valueRaw.charAt(0) === "=") {
valueRaw = $.trim(valueRaw.substring(1));
}
}
key = fluid.unescapeProperties(key)[0];
valueEsc = fluid.unescapeProperties(valueRaw);
}
else {
valueEsc = fluid.unescapeProperties(line);
}
contin = valueEsc[1];
if (!valueEsc[1]) { // this line was not a continuation line - store the value
togo[key] = valueComp + valueEsc[0];
}
else {
valueComp += valueEsc[0];
}
}
return togo;
};
/**
* Expand a message string with respect to a set of arguments, following a basic
* subset of the Java MessageFormat rules.
* http://java.sun.com/j2se/1.4.2/docs/api/java/text/MessageFormat.html
*
* The message string is expected to contain replacement specifications such
* as {0}, {1}, {2}, etc.
* @param messageString {String} The message key to be expanded
* @param args {String/Array of String} An array of arguments to be substituted into the message.
* @return The expanded message string.
*/
fluid.formatMessage = function (messageString, args) {
if (!args) {
return messageString;
}
if (typeof(args) === "string") {
args = [args];
}
for (var i = 0; i < args.length; ++ i) {
messageString = messageString.replace("{" + i + "}", args[i]);
}
return messageString;
};
})(jQuery, fluid_1_4);
/*
Copyright 2007-2010 University of Cambridge
Copyright 2007-2009 University of Toronto
Copyright 2007-2009 University of California, Berkeley
Copyright 2010 OCAD University
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://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global fluid:true, fluid_1_4: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_4 = fluid_1_4 || {};
var fluid = fluid || fluid_1_4;
(function ($, fluid) {
fluid.renderTimestamp = function (date) {
var zeropad = function (num, width) {
if (!width) width = 2;
var numstr = (num == undefined? "" : num.toString());
return "00000".substring(5 - width + numstr.length) + numstr;
}
return zeropad(date.getHours()) + ":" + zeropad(date.getMinutes()) + ":" + zeropad(date.getSeconds()) + "." + zeropad(date.getMilliseconds(), 3);
};
// Exception stripping code taken from https://github.com/emwendelin/javascript-stacktrace/blob/master/stacktrace.js
// BSD licence, see header
fluid.detectStackStyle = function (e) {
var style = "other";
var stackStyle = {
offset: 0
};
if (e["arguments"]) {
style = "chrome";
} else if (typeof window !== "undefined" && window.opera && e.stacktrace) {
style = "opera10";
} else if (e.stack) {
style = "firefox";
// Detect FireFox 4-style stacks which are 1 level less deep
stackStyle.offset = e.stack.indexOf("Trace exception") === -1? 1 : 0;
} else if (typeof window !== 'undefined' && window.opera && !('stacktrace' in e)) { //Opera 9-
style = "opera";
}
stackStyle.style = style;
return stackStyle;
};
fluid.obtainException = function() {
try {
throw new Error("Trace exception");
}
catch (e) {
return e;
}
};
var stackStyle = fluid.detectStackStyle(fluid.obtainException());
fluid.registerNamespace("fluid.exceptionDecoders");
fluid.decodeStack = function() {
if (stackStyle.style !== "firefox") {
return null;
}
var e = fluid.obtainException();
return fluid.exceptionDecoders[stackStyle.style](e);
};
fluid.exceptionDecoders.firefox = function(e) {
var lines = e.stack.replace(/(?:\n@:0)?\s+$/m, '').replace(/^\(/gm, '{anonymous}(').split('\n');
return fluid.transform(lines, function(line) {
var atind = line.indexOf("@");
return atind === -1? [line] : [line.substring(atind + 1), line.substring(0, atind)];
});
};
fluid.getCallerInfo = function(atDepth) {
atDepth = (atDepth || 3) - stackStyle.offset;
var stack = fluid.decodeStack();
return stack? stack[atDepth][0] : null;
};
function generate(c, count) {
var togo = "";
for (var i = 0; i < count; ++ i) {
togo += c;
}
return togo;
}
function printImpl(obj, small, options) {
var big = small + options.indentChars;
if (obj === null) {
return "null";
}
else if (fluid.isPrimitive(obj)) {
return JSON.stringify(obj);
}
else {
var j = [];
if (fluid.isArrayable(obj)) {
if (obj.length === 0) {
return "[]";
}
for (var i = 0; i < obj.length; ++ i) {
j[i] = printImpl(obj[i], big, options);
}
return "[\n" + big + j.join(",\n" + big) + "\n" + small + "]";
}
else {
var i = 0;
fluid.each(obj, function(value, key) {
j[i++] = JSON.stringify(key) + ": " + printImpl(value, big, options);
});
return "{\n" + big + j.join(",\n" + big) + "\n" + small + "}";
}
}
}
fluid.prettyPrintJSON = function(obj, options) {
options = $.extend({indent: 4}, options);
options.indentChars = generate(" ", options.indent);
return printImpl(obj, "", options);
}
/**
* Dumps a DOM element into a readily recognisable form for debugging - produces a
* "semi-selector" summarising its tag name, class and id, whichever are set.
*
* @param {jQueryable} element The element to be dumped
* @return A string representing the element.
*/
fluid.dumpEl = function (element) {
var togo;
if (!element) {
return "null";
}
if (element.nodeType === 3 || element.nodeType === 8) {
return "[data: " + element.data + "]";
}
if (element.nodeType === 9) {
return "[document: location " + element.location + "]";
}
if (!element.nodeType && fluid.isArrayable(element)) {
togo = "[";
for (var i = 0; i < element.length; ++ i) {
togo += fluid.dumpEl(element[i]);
if (i < element.length - 1) {
togo += ", ";
}
}
return togo + "]";
}
element = $(element);
togo = element.get(0).tagName;
if (element.id) {
togo += "#" + element.id;
}
if (element.attr("class")) {
togo += "." + element.attr("class");
}
return togo;
};
})(jQuery, fluid_1_4);
/*
Copyright 2008-2010 University of Cambridge
Copyright 2008-2009 University of Toronto
Copyright 2010-2011 Lucendo Development Ltd.
Copyright 2010 OCAD University
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_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.BINDING_ROOT_KEY = "fluid-binding-root";
/** Recursively find any data stored under a given name from a node upwards
* in its DOM hierarchy **/
fluid.findData = function (elem, name) {
while (elem) {
var data = $.data(elem, name);
if (data) {
return data;
}
elem = elem.parentNode;
}
};
fluid.bindFossils = function (node, data, fossils) {
$.data(node, fluid.BINDING_ROOT_KEY, {data: data, fossils: fossils});
};
fluid.boundPathForNode = function (node, fossils) {
node = fluid.unwrap(node);
var key = node.name || node.id;
var record = fossils[key];
return record ? record.EL: null;
};
fluid.findForm = function (node) {
return fluid.findAncestor(node, function (element) {
return element.nodeName.toLowerCase() === "form";
});
};
/** A generalisation of jQuery.val to correctly handle the case of acquiring and
* setting the value of clustered radio button/checkbox sets, potentially, given
* a node corresponding to just one element.
*/
fluid.value = function (nodeIn, newValue) {
var node = fluid.unwrap(nodeIn);
var multiple = false;
if (node.nodeType === undefined && node.length > 1) {
node = node[0];
multiple = true;
}
if ("input" !== node.nodeName.toLowerCase() || ! /radio|checkbox/.test(node.type)) {
// resist changes to contract of jQuery.val() in jQuery 1.5.1 (see FLUID-4113)
return newValue === undefined? $(node).val() : $(node).val(newValue);
}
var name = node.name;
if (name === undefined) {
fluid.fail("Cannot acquire value from node " + fluid.dumpEl(node) + " which does not have name attribute set");
}
var elements;
if (multiple) {
elements = nodeIn;
}
else {
elements = document.getElementsByName(name);
var scope = fluid.findForm(node);
elements = $.grep(elements,
function (element) {
if (element.name !== name) {
return false;
}
return !scope || fluid.dom.isContainer(scope, element);
});
}
if (newValue !== undefined) {
if (typeof(newValue) === "boolean") {
newValue = (newValue ? "true" : "false");
}
// jQuery gets this partially right, but when dealing with radio button array will
// set all of their values to "newValue" rather than setting the checked property
// of the corresponding control.
$.each(elements, function () {
this.checked = (newValue instanceof Array ?
$.inArray(this.value, newValue) !== -1 : newValue === this.value);
});
}
else { // this part jQuery will not do - extracting value from <input> array
var checked = $.map(elements, function (element) {
return element.checked ? element.value : null;
});
return node.type === "radio" ? checked[0] : checked;
}
};
/** "Automatically" apply to whatever part of the data model is
* relevant, the changed value received at the given DOM node*/
fluid.applyChange = function (node, newValue, applier) {
node = fluid.unwrap(node);
if (newValue === undefined) {
newValue = fluid.value(node);
}
if (node.nodeType === undefined && node.length > 0) {
node = node[0];
} // assume here that they share name and parent
var root = fluid.findData(node, fluid.BINDING_ROOT_KEY);
if (!root) {
fluid.fail("Bound data could not be discovered in any node above " + fluid.dumpEl(node));
}
var name = node.name;
var fossil = root.fossils[name];
if (!fossil) {
fluid.fail("No fossil discovered for name " + name + " in fossil record above " + fluid.dumpEl(node));
}
if (typeof(fossil.oldvalue) === "boolean") { // deal with the case of an "isolated checkbox"
newValue = newValue[0] ? true: false;
}
var EL = root.fossils[name].EL;
if (applier) {
applier.fireChangeRequest({path: EL, value: newValue, source: node.id});
}
else {
fluid.set(root.data, EL, newValue);
}
};
// Implementation notes: The EL path manipulation utilities here are somewhat more thorough
// and expensive versions of those provided in Fluid.js - there is some duplication of
// functionality. This is a tradeoff between stability and performance - the versions in
// Fluid.js are the most frequently used and do not implement escaping of characters .
// as \. and \ as \\ as the versions here. The implementations here are not quite complete
// or very performant and are left here partially as an implementation note. Problems will
// arise if clients manipulate JSON structures containing "." characters in keys as if they
// are models, treating these is best left until the cases where they occur. The now standard
// utilities fluid.path(), fluid.parseEL and fluid.composePath are the ones recommended for
// general users and their implementation can be upgraded if required.
fluid.pathUtil = {};
var getPathSegmentImpl = function (accept, path, i) {
var segment = null; // TODO: rewrite this with regexes and replaces
if (accept) {
segment = "";
}
var escaped = false;
var limit = path.length;
for (; i < limit; ++i) {
var c = path.charAt(i);
if (!escaped) {
if (c === '.') {
break;
}
else if (c === '\\') {
escaped = true;
}
else if (segment !== null) {
segment += c;
}
}
else {
escaped = false;
if (segment !== null) {
accept += c;
}
}
}
if (segment !== null) {
accept[0] = segment;
}
return i;
};
var globalAccept = []; // TODO: serious reentrancy risk here, why is this impl like this?
fluid.pathUtil.getPathSegment = function (path, i) {
getPathSegmentImpl(globalAccept, path, i);
return globalAccept[0];
};
fluid.pathUtil.getHeadPath = function (path) {
return fluid.pathUtil.getPathSegment(path, 0);
};
fluid.pathUtil.getFromHeadPath = function (path) {
var firstdot = getPathSegmentImpl(null, path, 0);
return firstdot === path.length ? null
: path.substring(firstdot + 1);
};
function lastDotIndex(path) {
// TODO: proper escaping rules
return path.lastIndexOf(".");
}
fluid.pathUtil.getToTailPath = function (path) {
var lastdot = lastDotIndex(path);
return lastdot === -1 ? null : path.substring(0, lastdot);
};
/** Returns the very last path component of a bean path */
fluid.pathUtil.getTailPath = function (path) {
var lastdot = lastDotIndex(path);
return fluid.pathUtil.getPathSegment(path, lastdot + 1);
};
var composeSegment = function (prefix, toappend) {
for (var i = 0; i < toappend.length; ++i) {
var c = toappend.charAt(i);
if (c === '.' || c === '\\' || c === '}') {
prefix += '\\';
}
prefix += c;
}
return prefix;
};
/**
* Compose a prefix and suffix EL path, where the prefix is already escaped.
* Prefix may be empty, but not null. The suffix will become escaped.
*/
fluid.pathUtil.composePath = function (prefix, suffix) {
if (prefix.length !== 0) {
prefix += '.';
}
return composeSegment(prefix, suffix);
};
fluid.pathUtil.matchPath = function (spec, path) {
var togo = "";
while (true) {
if (!spec || path === "") {
break;
}
if (!path) {
return null;
}
var spechead = fluid.pathUtil.getHeadPath(spec);
var pathhead = fluid.pathUtil.getHeadPath(path);
// if we fail to match on a specific component, fail.
if (spechead !== "*" && spechead !== pathhead) {
return null;
}
togo = fluid.pathUtil.composePath(togo, pathhead);
spec = fluid.pathUtil.getFromHeadPath(spec);
path = fluid.pathUtil.getFromHeadPath(path);
}
return togo;
};
fluid.model.mergeModel = function (target, source, applier) {
var copySource = fluid.copy(source);
applier = applier || fluid.makeChangeApplier(source);
if (!fluid.isPrimitive(target)) {
applier.fireChangeRequest({type: "ADD", path: "", value: target});
}
applier.fireChangeRequest({type: "MERGE", path: "", value: copySource});
return source;
};
fluid.model.isNullChange = function (model, request, resolverGetConfig) {
if (request.type === "ADD") {
var existing = fluid.get(model, request.path, resolverGetConfig);
if (existing === request.value) {
return true;
}
}
};
/** Applies the supplied ChangeRequest object directly to the supplied model.
*/
fluid.model.applyChangeRequest = function (model, request, resolverSetConfig) {
var pen = fluid.model.getPenultimate(model, request.path, resolverSetConfig || fluid.model.defaultSetConfig);
if (request.type === "ADD" || request.type === "MERGE") {
if (request.path === "" || request.type === "MERGE") {
if (request.type === "ADD") {
fluid.clear(pen.root);
}
$.extend(true, request.path === "" ? pen.root: pen.root[pen.last], request.value);
}
else {
pen.root[pen.last] = request.value;
}
}
else if (request.type === "DELETE") {
if (request.path === "") {
fluid.clear(pen.root);
}
else {
delete pen.root[pen.last];
}
}
};
// Utility shared between changeApplier and superApplier
function bindRequestChange(that) {
that.requestChange = function (path, value, type) {
var changeRequest = {
path: path,
value: value,
type: type
};
that.fireChangeRequest(changeRequest);
};
}
fluid.makeChangeApplier = function (model, options) {
options = options || {};
var baseEvents = {
guards: fluid.event.getEventFirer(false, true),
postGuards: fluid.event.getEventFirer(false, true),
modelChanged: fluid.event.getEventFirer(false, false)
};
var that = {
model: model
};
function makeGuardWrapper(cullUnchanged) {
if (!cullUnchanged) {
return null;
}
var togo = function (guard) {
return function (model, changeRequest, internalApplier) {
var oldRet = guard(model, changeRequest, internalApplier);
if (oldRet === false) {
return false;
}
else {
if (fluid.model.isNullChange(model, changeRequest)) {
togo.culled = true;
return false;
}
}
};
};
return togo;
}
function wrapListener(listener, spec) {
var pathSpec = spec;
var transactional = false;
var priority = Number.MAX_VALUE;
if (typeof (spec) !== "string") {
pathSpec = spec.path;
transactional = spec.transactional;
if (spec.priority !== undefined) {
priority = spec.priority;
}
}
else {
if (pathSpec.charAt(0) === "!") {
transactional = true;
pathSpec = pathSpec.substring(1);
}
}
return function (changePath, fireSpec, accum) {
var guid = fluid.event.identifyListener(listener);
var exist = fireSpec.guids[guid];
if (!exist) {
var match = fluid.pathUtil.matchPath(pathSpec, changePath);
if (match !== null) {
var record = {
changePath: changePath,
pathSpec: pathSpec,
listener: listener,
priority: priority,
transactional: transactional
};
if (accum) {
record.accumulate = [accum];
}
fireSpec.guids[guid] = record;
var collection = transactional ? "transListeners": "listeners";
fireSpec[collection].push(record);
fireSpec.all.push(record);
}
}
else if (accum) {
if (!exist.accumulate) {
exist.accumulate = [];
}
exist.accumulate.push(accum);
}
};
}
function fireFromSpec(name, fireSpec, args, category, wrapper) {
return baseEvents[name].fireToListeners(fireSpec[category], args, wrapper);
}
function fireComparator(recA, recB) {
return recA.priority - recB.priority;
}
function prepareFireEvent(name, changePath, fireSpec, accum) {
baseEvents[name].fire(changePath, fireSpec, accum);
fireSpec.all.sort(fireComparator);
fireSpec.listeners.sort(fireComparator);
fireSpec.transListeners.sort(fireComparator);
}
function makeFireSpec() {
return {guids: {}, all: [], listeners: [], transListeners: []};
}
function getFireSpec(name, changePath) {
var fireSpec = makeFireSpec();
prepareFireEvent(name, changePath, fireSpec);
return fireSpec;
}
function fireEvent(name, changePath, args, wrapper) {
var fireSpec = getFireSpec(name, changePath);
return fireFromSpec(name, fireSpec, args, "all", wrapper);
}
function adaptListener(that, name) {
that[name] = {
addListener: function (spec, listener, namespace) {
baseEvents[name].addListener(wrapListener(listener, spec), namespace);
},
removeListener: function (listener) {
baseEvents[name].removeListener(listener);
}
};
}
adaptListener(that, "guards");
adaptListener(that, "postGuards");
adaptListener(that, "modelChanged");
function preFireChangeRequest(changeRequest) {
if (!changeRequest.type) {
changeRequest.type = "ADD";
}
}
var bareApplier = {
fireChangeRequest: function (changeRequest) {
that.fireChangeRequest(changeRequest, true);
}
};
bindRequestChange(bareApplier);
that.fireChangeRequest = function (changeRequest, defeatGuards) {
preFireChangeRequest(changeRequest);
var guardFireSpec = defeatGuards ? null : getFireSpec("guards", changeRequest.path);
if (guardFireSpec && guardFireSpec.transListeners.length > 0) {
var ation = that.initiate();
ation.fireChangeRequest(changeRequest, guardFireSpec);
ation.commit();
}
else {
if (!defeatGuards) {
// TODO: this use of "listeners" seems pointless since we have just verified that there are no transactional listeners
var prevent = fireFromSpec("guards", guardFireSpec, [model, changeRequest, bareApplier], "listeners");
if (prevent === false) {
return false;
}
}
var oldModel = model;
if (!options.thin) {
oldModel = {};
fluid.model.copyModel(oldModel, model);
}
fluid.model.applyChangeRequest(model, changeRequest, options.resolverSetConfig);
fireEvent("modelChanged", changeRequest.path, [model, oldModel, [changeRequest]]);
}
};
bindRequestChange(that);
function fireAgglomerated(eventName, formName, changes, args, accpos) {
var fireSpec = makeFireSpec();
for (var i = 0; i < changes.length; ++ i) {
prepareFireEvent(eventName, changes[i].path, fireSpec, changes[i]);
}
for (var j = 0; j < fireSpec[formName].length; ++ j) {
var spec = fireSpec[formName][j];
if (accpos) {
args[accpos] = spec.accumulate;
}
var ret = spec.listener.apply(null, args);
if (ret === false) {
return false;
}
}
}
that.initiate = function (newModel) {
var cancelled = false;
var changes = [];
if (options.thin) {
newModel = model;
}
else {
newModel = newModel || {};
fluid.model.copyModel(newModel, model);
}
// the guard in the inner world is given a private applier to "fast track"
// and glob collateral changes it requires
var internalApplier =
{fireChangeRequest: function (changeRequest) {
preFireChangeRequest(changeRequest);
fluid.model.applyChangeRequest(newModel, changeRequest, options.resolverSetConfig);
changes.push(changeRequest);
}};
bindRequestChange(internalApplier);
var ation = {
commit: function () {
var oldModel;
if (cancelled) {
return false;
}
var ret = fireAgglomerated("postGuards", "transListeners", changes, [newModel, null, internalApplier], 1);
if (ret === false) {
return false;
}
if (options.thin) {
oldModel = model;
}
else {
oldModel = {};
fluid.model.copyModel(oldModel, model);
fluid.clear(model);
fluid.model.copyModel(model, newModel);
}
fireAgglomerated("modelChanged", "all", changes, [model, oldModel, null], 2);
},
fireChangeRequest: function (changeRequest) {
preFireChangeRequest(changeRequest);
if (options.cullUnchanged && fluid.model.isNullChange(model, changeRequest, options.resolverGetConfig)) {
return;
}
var wrapper = makeGuardWrapper(options.cullUnchanged);
var prevent = fireEvent("guards", changeRequest.path, [newModel, changeRequest, internalApplier], wrapper);
if (prevent === false && !(wrapper && wrapper.culled)) {
cancelled = true;
}
if (!cancelled) {
if (!(wrapper && wrapper.culled)) {
fluid.model.applyChangeRequest(newModel, changeRequest, options.resolverSetConfig);
changes.push(changeRequest);
}
}
}
};
bindRequestChange(ation);
return ation;
};
return that;
};
fluid.makeSuperApplier = function () {
var subAppliers = [];
var that = {};
that.addSubApplier = function (path, subApplier) {
subAppliers.push({path: path, subApplier: subApplier});
};
that.fireChangeRequest = function (request) {
for (var i = 0; i < subAppliers.length; ++ i) {
var path = subAppliers[i].path;
if (request.path.indexOf(path) === 0) {
var subpath = request.path.substring(path.length + 1);
var subRequest = fluid.copy(request);
subRequest.path = subpath;
// TODO: Deal with the as yet unsupported case of an EL rvalue DAR
subAppliers[i].subApplier.fireChangeRequest(subRequest);
}
}
};
bindRequestChange(that);
return that;
};
fluid.attachModel = function (baseModel, path, model) {
var segs = fluid.model.parseEL(path);
for (var i = 0; i < segs.length - 1; ++ i) {
var seg = segs[i];
var subModel = baseModel[seg];
if (!subModel) {
baseModel[seg] = subModel = {};
}
baseModel = subModel;
}
baseModel[segs[segs.length - 1]] = model;
};
fluid.assembleModel = function (modelSpec) {
var model = {};
var superApplier = fluid.makeSuperApplier();
var togo = {model: model, applier: superApplier};
for (var path in modelSpec) {
var rec = modelSpec[path];
fluid.attachModel(model, path, rec.model);
if (rec.applier) {
superApplier.addSubApplier(path, rec.applier);
}
}
return togo;
};
})(jQuery, fluid_1_4);
/*
Copyright 2008-2010 University of Cambridge
Copyright 2008-2010 University of Toronto
Copyright 2010-2011 Lucendo Development Ltd.
Copyright 2010-2011 OCAD University
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:true, fluid_1_4: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_4 = fluid_1_4 || {};
var fluid = fluid || fluid_1_4;
(function ($, fluid) {
// $().fluid("selectable", args)
// $().fluid("selectable".that()
// $().fluid("pager.pagerBar", args)
// $().fluid("reorderer", options)
/** Create a "bridge" from code written in the Fluid standard "that-ist" style,
* to the standard JQuery UI plugin architecture specified at http://docs.jquery.com/UI/Guidelines .
* Every Fluid component corresponding to the top-level standard signature (JQueryable, options)
* will automatically convert idiomatically to the JQuery UI standard via this adapter.
* Any return value which is a primitive or array type will become the return value
* of the "bridged" function - however, where this function returns a general hash
* (object) this is interpreted as forming part of the Fluid "return that" pattern,
* and the function will instead be bridged to "return this" as per JQuery standard,
* permitting chaining to occur. However, as a courtesy, the particular "this" returned
* will be augmented with a function that() which will allow the original return
* value to be retrieved if desired.
* @param {String} name The name under which the "plugin space" is to be injected into
* JQuery
* @param {Object} peer The root of the namespace corresponding to the peer object.
*/
fluid.thatistBridge = function (name, peer) {
var togo = function(funcname) {
var segs = funcname.split(".");
var move = peer;
for (var i = 0; i < segs.length; ++i) {
move = move[segs[i]];
}
var args = [this];
if (arguments.length === 2) {
args = args.concat($.makeArray(arguments[1]));
}
var ret = move.apply(null, args);
this.that = function() {
return ret;
}
var type = typeof(ret);
return !ret || type === "string" || type === "number" || type === "boolean"
|| ret && ret.length !== undefined? ret: this;
};
$.fn[name] = togo;
return togo;
};
fluid.thatistBridge("fluid", fluid);
fluid.thatistBridge("fluid_1_4", fluid_1_4);
/*************************************************************************
* Tabindex normalization - compensate for browser differences in naming
* and function of "tabindex" attribute and tabbing order.
*/
// -- Private functions --
var normalizeTabindexName = function() {
return $.browser.msie ? "tabIndex" : "tabindex";
};
var canHaveDefaultTabindex = function(elements) {
if (elements.length <= 0) {
return false;
}
return $(elements[0]).is("a, input, button, select, area, textarea, object");
};
var getValue = function(elements) {
if (elements.length <= 0) {
return undefined;
}
if (!fluid.tabindex.hasAttr(elements)) {
return canHaveDefaultTabindex(elements) ? Number(0) : undefined;
}
// Get the attribute and return it as a number value.
var value = elements.attr(normalizeTabindexName());
return Number(value);
};
var setValue = function(elements, toIndex) {
return elements.each(function(i, item) {
$(item).attr(normalizeTabindexName(), toIndex);
});
};
// -- Public API --
/**
* Gets the value of the tabindex attribute for the first item, or sets the tabindex value of all elements
* if toIndex is specified.
*
* @param {String|Number} toIndex
*/
fluid.tabindex = function(target, toIndex) {
target = $(target);
if (toIndex !== null && toIndex !== undefined) {
return setValue(target, toIndex);
} else {
return getValue(target);
}
};
/**
* Removes the tabindex attribute altogether from each element.
*/
fluid.tabindex.remove = function(target) {
target = $(target);
return target.each(function(i, item) {
$(item).removeAttr(normalizeTabindexName());
});
};
/**
* Determines if an element actually has a tabindex attribute present.
*/
fluid.tabindex.hasAttr = function(target) {
target = $(target);
if (target.length <= 0) {
return false;
}
var togo = target.map(
function() {
var attributeNode = this.getAttributeNode(normalizeTabindexName());
return attributeNode ? attributeNode.specified : false;
}
);
return togo.length === 1? togo[0] : togo;
};
/**
* Determines if an element either has a tabindex attribute or is naturally tab-focussable.
*/
fluid.tabindex.has = function(target) {
target = $(target);
return fluid.tabindex.hasAttr(target) || canHaveDefaultTabindex(target);
};
// Keyboard navigation
// Public, static constants needed by the rest of the library.
fluid.a11y = $.a11y || {};
fluid.a11y.orientation = {
HORIZONTAL: 0,
VERTICAL: 1,
BOTH: 2
};
var UP_DOWN_KEYMAP = {
next: $.ui.keyCode.DOWN,
previous: $.ui.keyCode.UP
};
var LEFT_RIGHT_KEYMAP = {
next: $.ui.keyCode.RIGHT,
previous: $.ui.keyCode.LEFT
};
// Private functions.
var unwrap = function(element) {
return element.jquery ? element[0] : element; // Unwrap the element if it's a jQuery.
};
var makeElementsTabFocussable = function(elements) {
// If each element doesn't have a tabindex, or has one set to a negative value, set it to 0.
elements.each(function(idx, item) {
item = $(item);
if (!item.fluid("tabindex.has") || item.fluid("tabindex") < 0) {
item.fluid("tabindex", 0);
}
});
};
// Public API.
/**
* Makes all matched elements available in the tab order by setting their tabindices to "0".
*/
fluid.tabbable = function(target) {
target = $(target);
makeElementsTabFocussable(target);
};
/***********************************************************************
* Selectable functionality - geometrising a set of nodes such that they
* can be navigated (by setting focus) using a set of directional keys
*/
var CONTEXT_KEY = "selectionContext";
var NO_SELECTION = -32768;
var cleanUpWhenLeavingContainer = function(selectionContext) {
if (selectionContext.activeItemIndex !== NO_SELECTION) {
if (selectionContext.options.onLeaveContainer) {
selectionContext.options.onLeaveContainer(
selectionContext.selectables[selectionContext.activeItemIndex]);
} else if (selectionContext.options.onUnselect) {
selectionContext.options.onUnselect(
selectionContext.selectables[selectionContext.activeItemIndex]);
}
}
if (!selectionContext.options.rememberSelectionState) {
selectionContext.activeItemIndex = NO_SELECTION;
}
};
/**
* Does the work of selecting an element and delegating to the client handler.
*/
var drawSelection = function(elementToSelect, handler) {
if (handler) {
handler(elementToSelect);
}
};
/**
* Does does the work of unselecting an element and delegating to the client handler.
*/
var eraseSelection = function(selectedElement, handler) {
if (handler && selectedElement) {
handler(selectedElement);
}
};
var unselectElement = function(selectedElement, selectionContext) {
eraseSelection(selectedElement, selectionContext.options.onUnselect);
};
var selectElement = function(elementToSelect, selectionContext) {
// It's possible that we're being called programmatically, in which case we should clear any previous selection.
unselectElement(selectionContext.selectedElement(), selectionContext);
elementToSelect = unwrap(elementToSelect);
var newIndex = selectionContext.selectables.index(elementToSelect);
// Next check if the element is a known selectable. If not, do nothing.
if (newIndex === -1) {
return;
}
// Select the new element.
selectionContext.activeItemIndex = newIndex;
drawSelection(elementToSelect, selectionContext.options.onSelect);
};
var selectableFocusHandler = function(selectionContext) {
return function(evt) {
// FLUID-3590: newer browsers (FF 3.6, Webkit 4) have a form of "bug" in that they will go bananas
// on attempting to move focus off an element which has tabindex dynamically set to -1.
$(evt.target).fluid("tabindex", 0);
selectElement(evt.target, selectionContext);
// Force focus not to bubble on some browsers.
return evt.stopPropagation();
};
};
var selectableBlurHandler = function(selectionContext) {
return function(evt) {
$(evt.target).fluid("tabindex", selectionContext.options.selectablesTabindex);
unselectElement(evt.target, selectionContext);
// Force blur not to bubble on some browsers.
return evt.stopPropagation();
};
};
var reifyIndex = function(sc_that) {
var elements = sc_that.selectables;
if (sc_that.activeItemIndex >= elements.length) {
sc_that.activeItemIndex = 0;
}
if (sc_that.activeItemIndex < 0 && sc_that.activeItemIndex !== NO_SELECTION) {
sc_that.activeItemIndex = elements.length - 1;
}
if (sc_that.activeItemIndex >= 0) {
fluid.focus(elements[sc_that.activeItemIndex]);
}
};
var prepareShift = function(selectionContext) {
// FLUID-3590: FF 3.6 and Safari 4.x won't fire blur() when programmatically moving focus.
var selElm = selectionContext.selectedElement();
if (selElm) {
fluid.blur(selElm);
}
unselectElement(selectionContext.selectedElement(), selectionContext);
if (selectionContext.activeItemIndex === NO_SELECTION) {
selectionContext.activeItemIndex = -1;
}
};
var focusNextElement = function(selectionContext) {
prepareShift(selectionContext);
++selectionContext.activeItemIndex;
reifyIndex(selectionContext);
};
var focusPreviousElement = function(selectionContext) {
prepareShift(selectionContext);
--selectionContext.activeItemIndex;
reifyIndex(selectionContext);
};
var arrowKeyHandler = function(selectionContext, keyMap, userHandlers) {
return function(evt) {
if (evt.which === keyMap.next) {
focusNextElement(selectionContext);
evt.preventDefault();
} else if (evt.which === keyMap.previous) {
focusPreviousElement(selectionContext);
evt.preventDefault();
}
};
};
var getKeyMapForDirection = function(direction) {
// Determine the appropriate mapping for next and previous based on the specified direction.
var keyMap;
if (direction === fluid.a11y.orientation.HORIZONTAL) {
keyMap = LEFT_RIGHT_KEYMAP;
}
else if (direction === fluid.a11y.orientation.VERTICAL) {
// Assume vertical in any other case.
keyMap = UP_DOWN_KEYMAP;
}
return keyMap;
};
var tabKeyHandler = function(selectionContext) {
return function(evt) {
if (evt.which !== $.ui.keyCode.TAB) {
return;
}
cleanUpWhenLeavingContainer(selectionContext);
// Catch Shift-Tab and note that focus is on its way out of the container.
if (evt.shiftKey) {
selectionContext.focusIsLeavingContainer = true;
}
};
};
var containerFocusHandler = function(selectionContext) {
return function(evt) {
var shouldOrig = selectionContext.options.autoSelectFirstItem;
var shouldSelect = typeof(shouldOrig) === "function" ?
shouldOrig() : shouldOrig;
// Override the autoselection if we're on the way out of the container.
if (selectionContext.focusIsLeavingContainer) {
shouldSelect = false;
}
// This target check works around the fact that sometimes focus bubbles, even though it shouldn't.
if (shouldSelect && evt.target === selectionContext.container.get(0)) {
if (selectionContext.activeItemIndex === NO_SELECTION) {
selectionContext.activeItemIndex = 0;
}
fluid.focus(selectionContext.selectables[selectionContext.activeItemIndex]);
}
// Force focus not to bubble on some browsers.
return evt.stopPropagation();
};
};
var containerBlurHandler = function(selectionContext) {
return function(evt) {
selectionContext.focusIsLeavingContainer = false;
// Force blur not to bubble on some browsers.
return evt.stopPropagation();
};
};
var makeElementsSelectable = function(container, defaults, userOptions) {
var options = $.extend(true, {}, defaults, userOptions);
var keyMap = getKeyMapForDirection(options.direction);
var selectableElements = options.selectableElements? options.selectableElements :
container.find(options.selectableSelector);
// Context stores the currently active item(undefined to start) and list of selectables.
var that = {
container: container,
activeItemIndex: NO_SELECTION,
selectables: selectableElements,
focusIsLeavingContainer: false,
options: options
};
that.selectablesUpdated = function(focusedItem) {
// Remove selectables from the tab order and add focus/blur handlers
if (typeof(that.options.selectablesTabindex) === "number") {
that.selectables.fluid("tabindex", that.options.selectablesTabindex);
}
that.selectables.unbind("focus." + CONTEXT_KEY);
that.selectables.unbind("blur." + CONTEXT_KEY);
that.selectables.bind("focus."+ CONTEXT_KEY, selectableFocusHandler(that));
that.selectables.bind("blur." + CONTEXT_KEY, selectableBlurHandler(that));
if (keyMap && that.options.noBubbleListeners) {
that.selectables.unbind("keydown."+CONTEXT_KEY);
that.selectables.bind("keydown."+CONTEXT_KEY, arrowKeyHandler(that, keyMap));
}
if (focusedItem) {
selectElement(focusedItem, that);
}
else {
reifyIndex(that);
}
};
that.refresh = function() {
if (!that.options.selectableSelector) {
throw("Cannot refresh selectable context which was not initialised by a selector");
}
that.selectables = container.find(options.selectableSelector);
that.selectablesUpdated();
};
that.selectedElement = function() {
return that.activeItemIndex < 0? null : that.selectables[that.activeItemIndex];
};
// Add various handlers to the container.
if (keyMap && !that.options.noBubbleListeners) {
container.keydown(arrowKeyHandler(that, keyMap));
}
container.keydown(tabKeyHandler(that));
container.focus(containerFocusHandler(that));
container.blur(containerBlurHandler(that));
that.selectablesUpdated();
return that;
};
/**
* Makes all matched elements selectable with the arrow keys.
* Supply your own handlers object with onSelect: and onUnselect: properties for custom behaviour.
* Options provide configurability, including direction: and autoSelectFirstItem:
* Currently supported directions are jQuery.a11y.directions.HORIZONTAL and VERTICAL.
*/
fluid.selectable = function(target, options) {
target = $(target);
var that = makeElementsSelectable(target, fluid.selectable.defaults, options);
fluid.setScopedData(target, CONTEXT_KEY, that);
return that;
};
/**
* Selects the specified element.
*/
fluid.selectable.select = function(target, toSelect) {
fluid.focus(toSelect);
};
/**
* Selects the next matched element.
*/
fluid.selectable.selectNext = function(target) {
target = $(target);
focusNextElement(fluid.getScopedData(target, CONTEXT_KEY));
};
/**
* Selects the previous matched element.
*/
fluid.selectable.selectPrevious = function(target) {
target = $(target);
focusPreviousElement(fluid.getScopedData(target, CONTEXT_KEY));
};
/**
* Returns the currently selected item wrapped as a jQuery object.
*/
fluid.selectable.currentSelection = function(target) {
target = $(target);
var that = fluid.getScopedData(target, CONTEXT_KEY);
return $(that.selectedElement());
};
fluid.selectable.defaults = {
direction: fluid.a11y.orientation.VERTICAL,
selectablesTabindex: -1,
autoSelectFirstItem: true,
rememberSelectionState: true,
selectableSelector: ".selectable",
selectableElements: null,
onSelect: null,
onUnselect: null,
onLeaveContainer: null
};
/********************************************************************
* Activation functionality - declaratively associating actions with
* a set of keyboard bindings.
*/
var checkForModifier = function(binding, evt) {
// If no modifier was specified, just return true.
if (!binding.modifier) {
return true;
}
var modifierKey = binding.modifier;
var isCtrlKeyPresent = modifierKey && evt.ctrlKey;
var isAltKeyPresent = modifierKey && evt.altKey;
var isShiftKeyPresent = modifierKey && evt.shiftKey;
return isCtrlKeyPresent || isAltKeyPresent || isShiftKeyPresent;
};
/** Constructs a raw "keydown"-facing handler, given a binding entry. This
* checks whether the key event genuinely triggers the event and forwards it
* to any "activateHandler" registered in the binding.
*/
var makeActivationHandler = function(binding) {
return function(evt) {
var target = evt.target;
if (!fluid.enabled(evt.target)) {
return;
}
// The following 'if' clause works in the real world, but there's a bug in the jQuery simulation
// that causes keyboard simulation to fail in Safari, causing our tests to fail:
// http://ui.jquery.com/bugs/ticket/3229
// The replacement 'if' clause works around this bug.
// When this issue is resolved, we should revert to the original clause.
// if (evt.which === binding.key && binding.activateHandler && checkForModifier(binding, evt)) {
var code = evt.which? evt.which : evt.keyCode;
if (code === binding.key && binding.activateHandler && checkForModifier(binding, evt)) {
var event = $.Event("fluid-activate");
$(evt.target).trigger(event, [binding.activateHandler]);
if (event.isDefaultPrevented()) {
evt.preventDefault();
}
}
};
};
var makeElementsActivatable = function(elements, onActivateHandler, defaultKeys, options) {
// Create bindings for each default key.
var bindings = [];
$(defaultKeys).each(function(index, key) {
bindings.push({
modifier: null,
key: key,
activateHandler: onActivateHandler
});
});
// Merge with any additional key bindings.
if (options && options.additionalBindings) {
bindings = bindings.concat(options.additionalBindings);
}
fluid.initEnablement(elements);
// Add listeners for each key binding.
for (var i = 0; i < bindings.length; ++ i) {
var binding = bindings[i];
elements.keydown(makeActivationHandler(binding));
}
elements.bind("fluid-activate", function(evt, handler) {
handler = handler || onActivateHandler;
return handler? handler(evt): null;
});
};
/**
* Makes all matched elements activatable with the Space and Enter keys.
* Provide your own handler function for custom behaviour.
* Options allow you to provide a list of additionalActivationKeys.
*/
fluid.activatable = function(target, fn, options) {
target = $(target);
makeElementsActivatable(target, fn, fluid.activatable.defaults.keys, options);
};
/**
* Activates the specified element.
*/
fluid.activate = function(target) {
$(target).trigger("fluid-activate");
};
// Public Defaults.
fluid.activatable.defaults = {
keys: [$.ui.keyCode.ENTER, $.ui.keyCode.SPACE]
};
})(jQuery, fluid_1_4);
/*
Copyright 2010-2011 Lucendo Development Ltd.
Copyright 2010-2011 OCAD University
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
*/
/** This file contains functions which depend on the presence of a DOM document
* and which depend on the contents of Fluid.js **/
// Declare dependencies
/*global fluid_1_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.defaults("fluid.ariaLabeller", {
labelAttribute: "aria-label",
liveRegionMarkup: "<div class=\"liveRegion fl-offScreen-hidden\" aria-live=\"polite\"></div>",
liveRegionId: "fluid-ariaLabeller-liveRegion",
invokers: {
generateLiveElement: {funcName: "fluid.ariaLabeller.generateLiveElement", args: ["{ariaLabeller}"]}
}
});
fluid.ariaLabeller = function (element, options) {
var that = fluid.initView("fluid.ariaLabeller", element, options);
fluid.initDependents(that);
that.update = function (newOptions) {
newOptions = newOptions || that.options;
that.container.attr(that.options.labelAttribute, newOptions.text);
if (newOptions.dynamicLabel) {
var live = fluid.jById(that.options.liveRegionId);
if (live.length === 0) {
live = that.generateLiveElement();
}
live.text(newOptions.text);
}
};
that.update();
return that;
};
fluid.ariaLabeller.generateLiveElement = function (that) {
var liveEl = $(that.options.liveRegionMarkup);
liveEl.prop("id", that.options.liveRegionId);
$("body").append(liveEl);
return liveEl;
};
var LABEL_KEY = "aria-labelling";
fluid.getAriaLabeller = function (element) {
element = $(element);
var that = fluid.getScopedData(element, LABEL_KEY);
return that;
};
/** Manages an ARIA-mediated label attached to a given DOM element. An
* aria-labelledby attribute and target node is fabricated in the document
* if they do not exist already, and a "little component" is returned exposing a method
* "update" that allows the text to be updated. */
fluid.updateAriaLabel = function (element, text, options) {
options = $.extend({}, options || {}, {text: text});
var that = fluid.getAriaLabeller(element);
if (!that) {
that = fluid.ariaLabeller(element, options);
fluid.setScopedData(element, LABEL_KEY, that);
} else {
that.update(options);
}
return that;
};
/** Sets an interation on a target control, which morally manages a "blur" for
* a possibly composite region.
* A timed blur listener is set on the control, which waits for a short period of
* time (options.delay, defaults to 150ms) to discover whether the reason for the
* blur interaction is that either a focus or click is being serviced on a nominated
* set of "exclusions" (options.exclusions, a free hash of elements or jQueries).
* If no such event is received within the window, options.handler will be called
* with the argument "control", to service whatever interaction is required of the
* blur.
*/
fluid.deadMansBlur = function (control, options) {
var that = fluid.initLittleComponent("fluid.deadMansBlur", options);
that.blurPending = false;
that.lastCancel = 0;
$(control).bind("focusout", function (event) {
fluid.log("Starting blur timer for element " + fluid.dumpEl(event.target));
var now = new Date().getTime();
fluid.log("back delay: " + (now - that.lastCancel));
if (now - that.lastCancel > that.options.backDelay) {
that.blurPending = true;
}
setTimeout(function () {
if (that.blurPending) {
that.options.handler(control);
}
}, that.options.delay);
});
that.canceller = function (event) {
fluid.log("Cancellation through " + event.type + " on " + fluid.dumpEl(event.target));
that.lastCancel = new Date().getTime();
that.blurPending = false;
};
fluid.each(that.options.exclusions, function (exclusion) {
exclusion = $(exclusion);
fluid.each(exclusion, function (excludeEl) {
$(excludeEl).bind("focusin", that.canceller).
bind("fluid-focus", that.canceller).
click(that.canceller).mousedown(that.canceller);
// Mousedown is added for FLUID-4212, as a result of Chrome bug 6759, 14204
});
});
return that;
};
fluid.defaults("fluid.deadMansBlur", {
delay: 150,
backDelay: 100
});
})(jQuery, fluid_1_4);
/*
Copyright 2011 OCAD University
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://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global fluid_1_4:true, jQuery*/
// JSLint options
/*jslint white: true, funcinvoke: true, continue: true, elsecatch: true, operator: true, jslintok:true, undef: true, newcap: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
var fluid_1_4 = fluid_1_4 || {};
(function ($, fluid) {
/** The Fluid "IoC System proper" - resolution of references and
* completely automated instantiation of declaratively defined
* component trees */
var inCreationMarker = "__CURRENTLY_IN_CREATION__";
// unsupported, non-API function
fluid.isFireBreak = function(component) {
return component.options && component.options["fluid.visitComponents.fireBreak"];
};
// unsupported, non-API function
fluid.visitComponentChildren = function(that, visitor, options, up, down) {
options = options || {};
for (var name in that) {
var component = that[name];
//Every component *should* have an id, but some clients may not yet be compliant
//if (component && component.typeName && !component.id) {
// fluid.fail("No id");
//}
if (!component || !component.typeName || (component.id && options.visited && options.visited[component.id])) {continue; }
if (options.visited) {
options.visited[component.id] = true;
}
if (visitor(component, name, options, up, down)) {
return true;
}
if (!fluid.isFireBreak(component) && !options.flat) {
fluid.visitComponentChildren(component, visitor, options, up, down + 1);
}
}
};
// thatStack contains an increasing list of MORE SPECIFIC thats.
var visitComponents = function(thatStack, visitor, options) {
options = options || {
visited: {},
flat: true
};
var up = 0;
for (var i = thatStack.length - 1; i >= 0; --i) {
var that = thatStack[i];
if (fluid.isFireBreak(that)) {
return;
}
if (that.typeName) {
options.visited[that.id] = true;
if (visitor(that, "", options, 0, 0)) {
return;
}
}
if (fluid.visitComponentChildren(that, visitor, options, up, 1)) {
return;
}
++up;
}
};
// An EL segment resolver strategy that will attempt to trigger creation of
// components that it discovers along the EL path, if they have been defined but not yet
// constructed. Spring, eat your heart out! Wot no SPR-2048?
function makeGingerStrategy(instantiator, that, thatStack) {
return function(component, thisSeg) {
var atval = component[thisSeg];
if (atval === undefined) {
var parentPath = instantiator.idToPath[component.id];
atval = instantiator.pathToComponent[fluid.composePath(parentPath, thisSeg)];
// if it was not attached to the component, but it is in the instantiator, it MUST be in creation - prepare to fail
if (atval) {
atval[inCreationMarker] = true;
}
}
if (atval !== undefined) {
if (atval[inCreationMarker]) {
fluid.fail("Component " + fluid.dumpThat(atval) + " at path \"" + thisSeg
+ "\" of parent " + fluid.dumpThat(component) + " cannot be used for lookup"
+ " since it is still in creation. Please reorganise your dependencies so that they no longer contain circular references");
}
}
else {
if (fluid.get(component, fluid.path("options", "components", thisSeg, "type"))) {
fluid.initDependent(component, thisSeg);
atval = component[thisSeg];
}
}
return atval;
};
}
// unsupported, non-API function
fluid.dumpThat = function(that, instantiator) {
return "{ typeName: \"" + that.typeName + "\" id: " + that.id + "}";
};
// unsupported, non-API function
fluid.dumpThatStack = function(thatStack, instantiator) {
var togo = fluid.transform(thatStack, function(that) {
var path = instantiator.idToPath[that.id];
return fluid.dumpThat(that) + (path? (" - path: " + path) : "");
});
return togo.join("\n");
};
// Return an array of objects describing the current activity
// unsupported, non-API function
fluid.describeActivity = function() {
return fluid.threadLocal().activityStack || [];
};
// Execute the supplied function with the specified activity description pushed onto the stack
// unsupported, non-API function
fluid.pushActivity = function(func, message) {
if (!message) {
return func();
}
var root = fluid.threadLocal();
if (!root.activityStack) {
root.activityStack = [];
}
var frames = fluid.makeArray(message);
frames.push("\n");
frames.unshift("\n");
root.activityStack = frames.concat(root.activityStack);
return fluid.tryCatch(func, null, function() {
root.activityStack = root.activityStack.slice(frames.length);
});
};
// Return a function wrapped by the activity of describing its activity
// unsupported, non-API function
fluid.wrapActivity = function(func, messageSpec) {
return function() {
var args = fluid.makeArray(arguments);
var message = fluid.transform(fluid.makeArray(messageSpec), function(specEl) {
if (specEl.indexOf("arguments.") === 0) {
var el = specEl.substring("arguments.".length);
return fluid.get(args, el);
}
else {
return specEl;
}
});
return fluid.pushActivity(function() {
return func.apply(null, args);
}, message);
};
};
var localRecordExpected = /arguments|options|container/;
function makeStackFetcher(instantiator, parentThat, localRecord, expandOptions) {
expandOptions = expandOptions || {};
var thatStack = instantiator.getFullStack(parentThat);
var fetchStrategies = [fluid.model.funcResolverStrategy, makeGingerStrategy(instantiator, parentThat, thatStack)];
var fetcher = function(parsed) {
var context = parsed.context;
if (localRecord && localRecordExpected.test(context)) {
var fetched = fluid.get(localRecord[context], parsed.path);
return (context === "arguments" || expandOptions.direct)? fetched : {
marker: context === "options"? fluid.EXPAND : fluid.EXPAND_NOW,
value: fetched
};
}
var foundComponent;
visitComponents(thatStack, function(component, name, options, up, down) {
if (context === name || context === component.typeName || context === component.nickName) {
foundComponent = component;
if (down > 1) {
fluid.log("***WARNING: value resolution for context " + context + " found at depth " + down + ": this may not be supported in future");
}
return true; // YOUR VISIT IS AT AN END!!
}
if (fluid.get(component, fluid.path("options", "components", context, "type")) && !component[context]) {
foundComponent = fluid.get(component, context, {strategies: fetchStrategies});
return true;
}
});
if (!foundComponent && parsed.path !== "") {
var ref = fluid.renderContextReference(parsed);
fluid.log("Failed to resolve reference " + ref + ": thatStack contains\n" + fluid.dumpThatStack(thatStack, instantiator));
fluid.fail("Failed to resolve reference " + ref + " - could not match context with name "
+ context + " from component root of type " + thatStack[0].typeName, "\ninstantiator contents: ", instantiator);
}
return fluid.get(foundComponent, parsed.path, fetchStrategies);
};
return fetcher;
}
function makeStackResolverOptions(instantiator, parentThat, localRecord, expandOptions) {
return $.extend({}, fluid.defaults("fluid.resolveEnvironment"), {
fetcher: makeStackFetcher(instantiator, parentThat, localRecord, expandOptions)
});
}
// unsupported, non-API function
fluid.instantiator = function(freeInstantiator) {
// NB: We may not use the options merging framework itself here, since "withInstantiator" below
// will blow up, as it tries to resolve the instantiator which we are instantiating *NOW*
var preThat = {
options: {
"fluid.visitComponents.fireBreak": true
},
idToPath: {},
pathToComponent: {},
stackCount: 0,
nickName: "instantiator"
};
var that = fluid.typeTag("fluid.instantiator");
that = $.extend(that, preThat);
that.stack = function(count) {
return that.stackCount += count;
};
that.getThatStack = function(component) {
var path = that.idToPath[component.id] || "";
var parsed = fluid.model.parseEL(path);
var togo = fluid.transform(parsed, function(value, i) {
var parentPath = fluid.model.composeSegments.apply(null, parsed.slice(0, i + 1));
return that.pathToComponent[parentPath];
});
var root = that.pathToComponent[""];
if (root) {
togo.unshift(root);
}
return togo;
};
that.getEnvironmentalStack = function() {
var togo = [fluid.staticEnvironment];
if (!freeInstantiator) {
togo.push(fluid.threadLocal());
}
return togo;
};
that.getFullStack = function(component) {
var thatStack = component? that.getThatStack(component) : [];
return that.getEnvironmentalStack().concat(thatStack);
};
function recordComponent(component, path) {
that.idToPath[component.id] = path;
if (that.pathToComponent[path]) {
fluid.fail("Error during instantiation - path " + path + " which has just created component " + fluid.dumpThat(component)
+ " has already been used for component " + fluid.dumpThat(that.pathToComponent[path]) + " - this is a circular instantiation or other oversight."
+ " Please clear the component using instantiator.clearComponent() before reusing the path.");
}
that.pathToComponent[path] = component;
}
that.recordRoot = function(component) {
if (component && component.id && !that.pathToComponent[""]) {
recordComponent(component, "");
}
};
that.pushUpcomingInstantiation = function(parent, name) {
that.expectedParent = parent;
that.expectedName = name;
};
that.recordComponent = function(component) {
if (that.expectedName) {
that.recordKnownComponent(that.expectedParent, component, that.expectedName);
delete that.expectedName;
delete that.expectedParent;
}
else {
that.recordRoot(component);
}
};
that.clearComponent = function(component, name, child, options, noModTree) {
options = options || {visited: {}, flat: true};
child = child || component[name];
fluid.visitComponentChildren(child, function(gchild, gchildname) {
that.clearComponent(child, gchildname, null, options, noModTree);
}, options);
var path = that.idToPath[child.id];
delete that.idToPath[child.id];
delete that.pathToComponent[path];
if (!noModTree) {
delete component[name];
}
};
that.recordKnownComponent = function(parent, component, name) {
var parentPath = that.idToPath[parent.id] || "";
var path = fluid.model.composePath(parentPath, name);
recordComponent(component, path);
};
return that;
};
fluid.freeInstantiator = fluid.instantiator(true);
// unsupported, non-API function
fluid.argMapToDemands = function(argMap) {
var togo = [];
fluid.each(argMap, function(value, key) {
togo[value] = "{" + key + "}";
});
return togo;
};
// unsupported, non-API function
fluid.makePassArgsSpec = function(initArgs) {
return fluid.transform(initArgs, function(arg, index) {
return "{arguments}." + index;
});
};
function mergeToMergeAll(options) {
if (options && options.mergeOptions) {
options.mergeAllOptions = ["{options}"].concat(fluid.makeArray(options.mergeOptions));
}
}
function upgradeMergeOptions(demandspec) {
mergeToMergeAll(demandspec);
if (demandspec.mergeAllOptions) {
if (demandspec.options) {
fluid.fail("demandspec ", demandspec,
" is invalid - cannot specify literal options together with mergeOptions or mergeAllOptions");
}
demandspec.options = {
mergeAllOptions: demandspec.mergeAllOptions
};
}
if (demandspec.options) {
delete demandspec.options.mergeOptions;
}
}
/** Given a concrete argument list and/or options, determine the final concrete
* "invocation specification" which is coded by the supplied demandspec in the
* environment "thatStack" - the return is a package of concrete global function name
* and argument list which is suitable to be executed directly by fluid.invokeGlobalFunction.
*/
// unsupported, non-API function
fluid.embodyDemands = function(instantiator, parentThat, demandspec, initArgs, options) {
options = options || {};
upgradeMergeOptions(demandspec);
var oldOptions = fluid.get(options, "componentRecord.options");
options.componentRecord = $.extend(true, {}, options.componentRecord,
fluid.censorKeys(demandspec, ["args", "funcName", "registeredFrom"]));
var mergeAllZero = fluid.get(options, "componentRecord.options.mergeAllOptions.0");
if (mergeAllZero === "{options}") {
fluid.set(options, "componentRecord.options.mergeAllOptions.0", oldOptions);
}
var demands = $.makeArray(demandspec.args);
var upDefaults = fluid.defaults(demandspec.funcName); // I can SEE into TIME!!
var argMap = upDefaults? upDefaults.argumentMap : null;
var inferMap = false;
if (!argMap && (upDefaults || (options && options.componentRecord)) && !options.passArgs) {
inferMap = true;
// infer that it must be a little component if we have any reason to believe it is a component
if (demands.length < 2) {
argMap = fluid.rawDefaults("fluid.littleComponent").argumentMap;
}
else {
argMap = {options: demands.length - 1}; // wild guess in the old style
}
}
options = options || {};
if (demands.length === 0) {
if (options.componentRecord && argMap) {
demands = fluid.argMapToDemands(argMap);
}
else if (options.passArgs) {
demands = fluid.makePassArgsSpec(initArgs);
}
}
var localRecord = $.extend({"arguments": initArgs}, fluid.censorKeys(options.componentRecord, ["type"]));
fluid.each(argMap, function(index, name) {
if (initArgs.length > 0) {
localRecord[name] = localRecord["arguments"][index];
}
if (demandspec[name] !== undefined && localRecord[name] === undefined) {
localRecord[name] = demandspec[name];
}
});
mergeToMergeAll(localRecord.options);
mergeToMergeAll(argMap && demands[argMap.options]);
var upstreamLocalRecord = $.extend({}, localRecord);
if (options.componentRecord.options !== undefined) {
upstreamLocalRecord.options = options.componentRecord.options;
}
var expandOptions = makeStackResolverOptions(instantiator, parentThat, localRecord);
var args = [];
if (demands) {
for (var i = 0; i < demands.length; ++i) {
var arg = demands[i];
// Weak detection since we cannot guarantee this material has not been copied
if (fluid.isMarker(arg) && arg.value === fluid.COMPONENT_OPTIONS.value) {
arg = "{options}";
// Backwards compatibility for non-users of GRADES - last-ditch chance to correct the inference
if (inferMap) {
argMap = {options: i};
}
}
if (typeof(arg) === "string") {
if (arg.charAt(0) === "@") {
var argpos = arg.substring(1);
arg = "{arguments}." + argpos;
}
}
if (!argMap || argMap.options !== i) {
// defer expansion required if it is non-pseudoarguments demands and this argument *is* the options
args[i] = fluid.expander.expandLight(arg, expandOptions);
}
else { // It is the component options
if (arg && typeof(arg) === "object" && !arg.targetTypeName) {
arg.targetTypeName = demandspec.funcName;
}
// ensure to copy the arg since it is an alias of the demand block material (FLUID-4223)
// and will be destructively expanded
args[i] = {marker: fluid.EXPAND, value: fluid.copy(arg), localRecord: upstreamLocalRecord};
}
if (args[i] && fluid.isMarker(args[i].marker, fluid.EXPAND_NOW)) {
args[i] = fluid.expander.expandLight(args[i].value, expandOptions);
}
}
}
else {
args = initArgs? initArgs : [];
}
var togo = {
args: args,
funcName: demandspec.funcName
};
return togo;
};
var aliasTable = {};
fluid.alias = function(demandingName, aliasName) {
if (aliasName) {
aliasTable[demandingName] = aliasName;
}
else {
return aliasTable[demandingName];
}
};
var dependentStore = {};
function searchDemands(demandingName, contextNames) {
var exist = dependentStore[demandingName] || [];
outer: for (var i = 0; i < exist.length; ++i) {
var rec = exist[i];
for (var j = 0; j < contextNames.length; ++j) {
if (rec.contexts[j] !== contextNames[j]) {
continue outer;
}
}
return rec.spec; // jslint:ok
}
}
fluid.demands = function(demandingName, contextName, spec) {
var contextNames = $.makeArray(contextName).sort();
if (!spec) {
return searchDemands(demandingName, contextNames);
}
else if (spec.length) {
spec = {args: spec};
}
if (fluid.getCallerInfo) {
var callerInfo = fluid.getCallerInfo(5);
if (callerInfo) {
spec.registeredFrom = callerInfo;
}
}
var exist = dependentStore[demandingName];
if (!exist) {
exist = [];
dependentStore[demandingName] = exist;
}
exist.push({contexts: contextNames, spec: spec});
};
// unsupported, non-API function
fluid.compareDemands = function(speca, specb) {
var p1 = speca.uncess - specb.uncess;
return p1 === 0? specb.intersect - speca.intersect : p1;
};
// unsupported, non-API function
fluid.isDemandLogging = function(demandingNames) {
return fluid.isLogging() && demandingNames[0] !== "fluid.threadLocal";
};
// unsupported, non-API function
fluid.locateAllDemands = function(instantiator, parentThat, demandingNames) {
var demandLogging = fluid.isDemandLogging(demandingNames);
if (demandLogging) {
fluid.log("Resolving demands for function names ", demandingNames, " in context of " +
(parentThat? "component " + parentThat.typeName : "no component"));
}
var contextNames = {};
var visited = [];
var thatStack = instantiator.getFullStack(parentThat);
visitComponents(thatStack, function(component, xname, options, up, down) {
contextNames[component.typeName] = true;
visited.push(component);
});
if (demandLogging) {
fluid.log("Components in scope for resolution:\n" + fluid.dumpThatStack(visited, instantiator));
}
var matches = [];
for (var i = 0; i < demandingNames.length; ++i) {
var rec = dependentStore[demandingNames[i]] || [];
for (var j = 0; j < rec.length; ++j) {
var spec = rec[j];
var record = {spec: spec, intersect: 0, uncess: 0};
for (var k = 0; k < spec.contexts.length; ++k) {
record[contextNames[spec.contexts[k]]? "intersect" : "uncess"] += 2;
}
if (spec.contexts.length === 0) { // allow weak priority for contextless matches
record.intersect++;
}
// TODO: Potentially more subtle algorithm here - also ambiguity reports
matches.push(record);
}
}
matches.sort(fluid.compareDemands);
return matches;
};
// unsupported, non-API function
fluid.locateDemands = function(instantiator, parentThat, demandingNames) {
var matches = fluid.locateAllDemands(instantiator, parentThat, demandingNames);
var demandspec = matches.length === 0 || matches[0].intersect === 0? null : matches[0].spec.spec;
if (fluid.isDemandLogging(demandingNames)) {
if (demandspec) {
fluid.log("Located " + matches.length + " potential match" + (matches.length === 1? "" : "es") + ", selected best match with " + matches[0].intersect
+ " matched context names: ", demandspec);
}
else {
fluid.log("No matches found for demands, using direct implementation");
}
}
return demandspec;
};
/** Determine the appropriate demand specification held in the fluid.demands environment
* relative to "thatStack" for the function name(s) funcNames.
*/
// unsupported, non-API function
fluid.determineDemands = function (instantiator, parentThat, funcNames) {
funcNames = $.makeArray(funcNames);
var newFuncName = funcNames[0];
var demandspec = fluid.locateDemands(instantiator, parentThat, funcNames) || {};
if (demandspec.funcName) {
newFuncName = demandspec.funcName;
}
var aliasTo = fluid.alias(newFuncName);
if (aliasTo) {
newFuncName = aliasTo;
fluid.log("Following redirect from function name " + newFuncName + " to " + aliasTo);
var demandspec2 = fluid.locateDemands(instantiator, parentThat, [aliasTo]);
if (demandspec2) {
fluid.each(demandspec2, function(value, key) {
if (localRecordExpected.test(key)) {
fluid.fail("Error in demands block ", demandspec2, " - content with key \"" + key
+ "\" is not supported since this demands block was resolved via an alias from \"" + newFuncName + "\"");
}
});
if (demandspec2.funcName) {
newFuncName = demandspec2.funcName;
fluid.log("Followed final inner demands to function name \"" + newFuncName + "\"");
}
}
}
return fluid.merge(null, {funcName: newFuncName, args: fluid.makeArray(demandspec.args)}, fluid.censorKeys(demandspec, ["funcName", "args"]));
};
// unsupported, non-API function
fluid.resolveDemands = function(instantiator, parentThat, funcNames, initArgs, options) {
var demandspec = fluid.determineDemands(instantiator, parentThat, funcNames);
return fluid.embodyDemands(instantiator, parentThat, demandspec, initArgs, options);
};
// TODO: make a *slightly* more performant version of fluid.invoke that perhaps caches the demands
// after the first successful invocation
fluid.invoke = function(functionName, args, that, environment) {
args = fluid.makeArray(args);
return fluid.withInstantiator(that, function(instantiator) {
var invokeSpec = fluid.resolveDemands(instantiator, that, functionName, args, {passArgs: true});
return fluid.invokeGlobalFunction(invokeSpec.funcName, invokeSpec.args, environment);
});
};
fluid.invoke = fluid.wrapActivity(fluid.invoke, [" while invoking function with name \"", "arguments.0", "\" from component", "arguments.2"]);
/** Make a function which performs only "static redispatch" of the supplied function name -
* that is, taking only account of the contents of the "static environment". Since the static
* environment is assumed to be constant, the dispatch of the call will be evaluated at the
* time this call is made, as an optimisation.
*/
fluid.makeFreeInvoker = function(functionName, environment) {
var demandSpec = fluid.determineDemands(fluid.freeInstantiator, null, functionName);
return function() {
var invokeSpec = fluid.embodyDemands(fluid.freeInstantiator, null, demandSpec, arguments, {passArgs: true});
return fluid.invokeGlobalFunction(invokeSpec.funcName, invokeSpec.args, environment);
};
};
fluid.makeInvoker = function(instantiator, that, demandspec, functionName, environment) {
demandspec = demandspec || fluid.determineDemands(instantiator, that, functionName);
return function() {
var args = arguments;
return fluid.pushActivity(function() {
var invokeSpec = fluid.embodyDemands(instantiator, that, demandspec, args, {passArgs: true});
return fluid.invokeGlobalFunction(invokeSpec.funcName, invokeSpec.args, environment);
}, [" while invoking invoker with name " + functionName + " on component", that]);
};
};
// unsupported, non-API function
fluid.event.dispatchListener = function(instantiator, that, listener, eventName, eventSpec) {
return function() {
var demandspec = fluid.determineDemands(instantiator, that, eventName);
if (demandspec.args.length === 0 && eventSpec.args) {
demandspec.args = eventSpec.args;
}
var resolved = fluid.embodyDemands(instantiator, that, demandspec, arguments, {passArgs: true, componentOptions: eventSpec});
listener.apply(null, resolved.args);
};
};
// unsupported, non-API function
fluid.event.resolveEvent = function(that, eventName, eventSpec) {
return fluid.withInstantiator(that, function(instantiator) {
if (typeof(eventSpec) === "string") {
var firer = fluid.expandOptions(eventSpec, that);
if (!firer) {
fluid.fail("Error in fluid.event.resolveEvent - context path " + eventSpec + " could not be looked up to a valid event firer");
}
return firer;
}
else {
var event = eventSpec.event;
var origin;
if (!event) {
fluid.fail("Event specification for event with name " + eventName + " does not include a base event specification");
}
if (event.charAt(0) === "{") {
origin = fluid.expandOptions(event, that);
}
else {
origin = that.events[event];
}
if (!origin) {
fluid.fail("Error in event specification - could not resolve base event reference " + event + " to an event firer");
}
var firer = {}; // jslint:ok - already defined
fluid.each(["fire", "removeListener"], function(method) {
firer[method] = function() {origin[method].apply(null, arguments);};
});
firer.addListener = function(listener, namespace, predicate, priority) {
origin.addListener(fluid.event.dispatchListener(instantiator, that, listener, eventName, eventSpec),
namespace, predicate, priority);
};
return firer;
}
});
};
fluid.registerNamespace("fluid.expander");
/** rescue that part of a component's options which should not be subject to
* options expansion via IoC - this initially consists of "components" and "mergePolicy"
* but will be expanded by the set of paths specified as "noexpand" within "mergePolicy"
*/
// unsupported, non-API function
fluid.expander.preserveFromExpansion = function(options) {
var preserve = {};
var preserveList = fluid.arrayToHash(["mergePolicy", "mergeAllOptions", "components", "invokers", "events", "listeners", "transformOptions"]);
fluid.each(options.mergePolicy, function(value, key) {
if (fluid.mergePolicyIs(value, "noexpand")) {
preserveList[key] = true;
}
});
fluid.each(preserveList, function(xvalue, path) {
var pen = fluid.model.getPenultimate(options, path);
var value = pen.root[pen.last];
delete pen.root[pen.last];
fluid.set(preserve, path, value);
});
return {
restore: function(target) {
fluid.each(preserveList, function(xvalue, path) {
var preserved = fluid.get(preserve, path);
if (preserved !== undefined) {
fluid.set(target, path, preserved);
}
});
}
};
};
/** Expand a set of component options with respect to a set of "expanders" (essentially only
* deferredCall) - This substitution is destructive since it is assumed that the options are already "live" as the
* result of environmental substitutions. Note that options contained inside "components" will not be expanded
* by this call directly to avoid linearly increasing expansion depth if this call is occuring as a result of
* "initDependents" */
// TODO: This needs to be integrated with "embodyDemands" above which makes a call to "resolveEnvironment" directly
// but with very similarly derived options (makeStackResolverOptions)
fluid.expandOptions = function(args, that, localRecord, outerExpandOptions) {
if (!args) {
return args;
}
return fluid.withInstantiator(that, function(instantiator) {
//fluid.log("expandOptions for " + that.typeName + " executing with instantiator " + instantiator.id);
var expandOptions = makeStackResolverOptions(instantiator, that, localRecord, outerExpandOptions);
expandOptions.noCopy = true; // It is still possible a model may be fetched even though it is preserved
var pres;
if (!fluid.isArrayable(args) && !fluid.isPrimitive(args)) {
pres = fluid.expander.preserveFromExpansion(args);
}
var expanded = fluid.expander.expandLight(args, expandOptions);
if (pres) {
pres.restore(expanded);
}
return expanded;
});
};
// unsupported, non-API function
fluid.locateTransformationRecord = function(that) {
return fluid.withInstantiator(that, function(instantiator) {
var matches = fluid.locateAllDemands(instantiator, that, ["fluid.transformOptions"]);
return fluid.find(matches, function(match) {
return match.uncess === 0 && fluid.contains(match.spec.contexts, that.typeName)? match.spec.spec : undefined;
});
});
};
//
fluid.hashToArray = function(hash) {
var togo = [];
fluid.each(hash, function(value, key) {
togo.push(key);
});
return togo;
};
// unsupported, non-API function
fluid.localRecordExpected = ["type", "options", "arguments", "mergeOptions",
"mergeAllOptions", "createOnEvent", "priority"];
// unsupported, non-API function
fluid.checkComponentRecord = function(defaults, localRecord) {
var expected = fluid.arrayToHash(fluid.localRecordExpected);
fluid.each(defaults.argumentMap, function(value, key) {
expected[key] = true;
});
fluid.each(localRecord, function(value, key) {
if (!expected[key]) {
fluid.fail("Probable error in subcomponent record - key \"" + key +
"\" found, where the only legal options are " +
fluid.hashToArray(expected).join(", "));
}
});
};
// unsupported, non-API function
fluid.expandComponentOptions = function(defaults, userOptions, that) {
if (userOptions && userOptions.localRecord) {
fluid.checkComponentRecord(defaults, userOptions.localRecord);
}
defaults = fluid.expandOptions(fluid.copy(defaults), that);
var localRecord = {};
if (userOptions && userOptions.marker === fluid.EXPAND) {
// TODO: Somewhat perplexing... the local record itself, by any route we could get here, consists of unexpanded
// material taken from "componentOptions"
var localOptions = fluid.get(userOptions, "localRecord.options");
if (localOptions) {
if (defaults && defaults.mergePolicy) {
localOptions.mergePolicy = defaults.mergePolicy;
}
localRecord.options = fluid.expandOptions(localOptions, that);
}
localRecord["arguments"] = fluid.get(userOptions, "localRecord.arguments");
var toExpand = userOptions.value;
userOptions = fluid.expandOptions(toExpand, that, localRecord, {direct: true});
}
localRecord.directOptions = userOptions;
if (!localRecord.options) {
// Catch the case where there is no demands block and everything is in the subcomponent record -
// in this case, embodyDemands will not construct a localRecord and what the user refers to by "options"
// is really what we properly call "directOptions".
localRecord.options = userOptions;
}
var mergeOptions = (userOptions && userOptions.mergeAllOptions) || ["{directOptions}"];
var togo = fluid.transform(mergeOptions, function(path) {
// Avoid use of expandOptions in simple case to avoid infinite recursion when constructing instantiator
return path === "{directOptions}"? localRecord.directOptions : fluid.expandOptions(path, that, localRecord, {direct: true});
});
var transRec = fluid.locateTransformationRecord(that);
if (transRec) {
togo[0].transformOptions = transRec.options;
}
return [defaults].concat(togo);
};
fluid.expandComponentOptions = fluid.wrapActivity(fluid.expandComponentOptions,
[" while expanding component options ", "arguments.1.value", " with record ", "arguments.1", " for component ", "arguments.2"]);
// The case without the instantiator is from the ginger strategy - this logic is still a little ragged
fluid.initDependent = function(that, name, userInstantiator, directArgs) {
if (!that || that[name]) { return; }
fluid.log("Beginning instantiation of component with name \"" + name + "\" as child of " + fluid.dumpThat(that));
directArgs = directArgs || [];
var root = fluid.threadLocal();
if (userInstantiator) {
var existing = root["fluid.instantiator"];
if (existing && existing !== userInstantiator) {
fluid.fail("Error in initDependent: user instantiator supplied with id " + userInstantiator.id
+ " which differs from that for currently active instantiation with id " + existing.id);
}
else {
root["fluid.instantiator"] = userInstantiator;
// fluid.log("*** initDependent for " + that.typeName + " member " + name + " was supplied USER instantiator with id " + userInstantiator.id + " - STORED");
}
}
var component = that.options.components[name];
fluid.withInstantiator(that, function(instantiator) {
if (typeof(component) === "string") {
that[name] = fluid.expandOptions([component], that)[0]; // TODO: expose more sensible semantic for expandOptions
}
else if (component.type) {
var invokeSpec = fluid.resolveDemands(instantiator, that, [component.type, name], directArgs, {componentRecord: component});
instantiator.pushUpcomingInstantiation(that, name);
fluid.tryCatch(function() {
that[inCreationMarker] = true;
var instance = fluid.initSubcomponentImpl(that, {type: invokeSpec.funcName}, invokeSpec.args);
// The existing instantiator record will be provisional, adjust it to take account of the true return
// TODO: Instantiator contents are generally extremely incomplete
var path = fluid.composePath(instantiator.idToPath[that.id] || "", name);
var existing = instantiator.pathToComponent[path];
if (existing && existing !== instance) {
instantiator.clearComponent(that, name, existing, null, true);
}
if (instance && instance.typeName && instance.id && instance !== existing) {
instantiator.recordKnownComponent(that, instance, name);
}
that[name] = instance;
}, null, function() {
delete that[inCreationMarker];
instantiator.pushUpcomingInstantiation();
});
}
else {
that[name] = component;
}
}, [" while instantiating dependent component with name \"" + name + "\" with record ", component, " as child of ", that]);
fluid.log("Finished instantiation of component with name \"" + name + "\" as child of " + fluid.dumpThat(that));
};
// NON-API function
// This function is stateful and MUST NOT be called by client code
fluid.withInstantiator = function(that, func, message) {
var root = fluid.threadLocal();
var instantiator = root["fluid.instantiator"];
if (!instantiator) {
instantiator = root["fluid.instantiator"] = fluid.instantiator();
//fluid.log("Created new instantiator with id " + instantiator.id + " in order to operate on component " + typeName);
}
return fluid.pushActivity(function() {
return fluid.tryCatch(function() {
if (that) {
instantiator.recordComponent(that);
}
instantiator.stack(1);
//fluid.log("Instantiator stack +1 to " + instantiator.stackCount + " for " + typeName);
return func(instantiator);
}, null, function() {
var count = instantiator.stack(-1);
//fluid.log("Instantiator stack -1 to " + instantiator.stackCount + " for " + typeName);
if (count === 0) {
//fluid.log("Clearing instantiator with id " + instantiator.id + " from threadLocal for end of " + typeName);
delete root["fluid.instantiator"];
}
});
}, message);
};
// unsupported, non-API function
fluid.bindDeferredComponent = function(that, componentName, component, instantiator) {
var events = fluid.makeArray(component.createOnEvent);
fluid.each(events, function(eventName) {
that.events[eventName].addListener(function() {
if (that[componentName]) {
instantiator.clearComponent(that, componentName);
}
fluid.initDependent(that, componentName, instantiator);
}, null, null, component.priority);
});
};
// unsupported, non-API function
fluid.priorityForComponent = function(component) {
return component.priority? component.priority :
(component.type === "fluid.typeFount" || fluid.hasGrade(fluid.defaults(component.type), "fluid.typeFount"))?
"first" : undefined;
};
fluid.initDependents = function(that) {
var options = that.options;
var components = options.components || {};
var componentSort = {};
fluid.withInstantiator(that, function(instantiator) {
fluid.each(components, function(component, name) {
if (!component.createOnEvent) {
var priority = fluid.priorityForComponent(component);
componentSort[name] = {key: name, priority: fluid.event.mapPriority(priority, 0)};
}
else {
fluid.bindDeferredComponent(that, name, component, instantiator);
}
});
var componentList = fluid.event.sortListeners(componentSort);
fluid.each(componentList, function(entry) {
fluid.initDependent(that, entry.key);
});
var invokers = options.invokers || {};
for (var name in invokers) {
var invokerec = invokers[name];
var funcName = typeof(invokerec) === "string"? invokerec : null;
that[name] = fluid.withInstantiator(that, function(instantiator) {
fluid.log("Beginning instantiation of invoker with name \"" + name + "\" as child of " + fluid.dumpThat(that));
return fluid.makeInvoker(instantiator, that, funcName? null : invokerec, funcName);
}, [" while instantiating invoker with name \"" + name + "\" with record ", invokerec, " as child of ", that]); // jslint:ok
fluid.log("Finished instantiation of invoker with name \"" + name + "\" as child of " + fluid.dumpThat(that));
}
});
};
fluid.staticEnvironment = fluid.typeTag("fluid.staticEnvironment");
fluid.staticEnvironment.environmentClass = fluid.typeTag("fluid.browser");
// fluid.environmentalRoot.environmentClass = fluid.typeTag("fluid.rhino");
fluid.demands("fluid.threadLocal", "fluid.browser", {funcName: "fluid.singleThreadLocal"});
var singleThreadLocal = fluid.typeTag("fluid.dynamicEnvironment");
fluid.singleThreadLocal = function() {
return singleThreadLocal;
};
fluid.threadLocal = function() {
// quick implementation since this is not very dynamic, a hazard to debugging, and used frequently within IoC itself
var demands = fluid.locateDemands(fluid.freeInstantiator, null, ["fluid.threadLocal"]);
return fluid.invokeGlobalFunction(demands.funcName, arguments);
};
function applyLocalChange(applier, type, path, value) {
var change = {
type: type,
path: path,
value: value
};
applier.fireChangeRequest(change);
}
// unsupported, non-API function
fluid.withEnvironment = function(envAdd, func, prefix) {
prefix = prefix || "";
var root = fluid.threadLocal();
var applier = fluid.makeChangeApplier(root, {thin: true});
return fluid.tryCatch(function() {
for (var key in envAdd) {
applyLocalChange(applier, "ADD", fluid.model.composePath(prefix, key), envAdd[key]);
}
$.extend(root, envAdd);
return func();
}, null, function() {
for (var key in envAdd) { // jslint:ok duplicate "value"
// TODO: This could be much better through i) refactoring the ChangeApplier so we could naturally use "rollback" semantics
// and/or implementing this material using some form of "prototype chain"
applyLocalChange(applier, "DELETE", fluid.model.composePath(prefix, key));
}
});
};
// unsupported, non-API function
fluid.makeEnvironmentFetcher = function(prefix, directModel) {
return function(parsed) {
var env = fluid.get(fluid.threadLocal(), prefix);
return fluid.fetchContextReference(parsed, directModel, env);
};
};
// unsupported, non-API function
fluid.extractEL = function(string, options) {
if (options.ELstyle === "ALL") {
return string;
}
else if (options.ELstyle.length === 1) {
if (string.charAt(0) === options.ELstyle) {
return string.substring(1);
}
}
else if (options.ELstyle === "${}") {
var i1 = string.indexOf("${");
var i2 = string.lastIndexOf("}");
if (i1 === 0 && i2 !== -1) {
return string.substring(2, i2);
}
}
};
// unsupported, non-API function
fluid.extractELWithContext = function(string, options) {
var EL = fluid.extractEL(string, options);
if (EL && EL.charAt(0) === "{") {
return fluid.parseContextReference(EL, 0);
}
return EL? {path: EL} : EL;
};
fluid.parseContextReference = function(reference, index, delimiter) {
var endcpos = reference.indexOf("}", index + 1);
if (endcpos === -1) {
fluid.fail("Cannot parse context reference \"" + reference + "\": Malformed context reference without }");
}
var context = reference.substring(index + 1, endcpos);
var endpos = delimiter? reference.indexOf(delimiter, endcpos + 1) : reference.length;
var path = reference.substring(endcpos + 1, endpos);
if (path.charAt(0) === ".") {
path = path.substring(1);
}
return {context: context, path: path, endpos: endpos};
};
fluid.renderContextReference = function(parsed) {
return "{" + parsed.context + "}" + parsed.path;
};
fluid.fetchContextReference = function(parsed, directModel, env) {
var base = parsed.context? env[parsed.context] : directModel;
if (!base) {
return base;
}
return fluid.get(base, parsed.path);
};
// unsupported, non-API function
fluid.resolveContextValue = function(string, options) {
if (options.bareContextRefs && string.charAt(0) === "{") {
var parsed = fluid.parseContextReference(string, 0);
return options.fetcher(parsed);
}
else if (options.ELstyle && options.ELstyle !== "${}") {
var parsed = fluid.extractELWithContext(string, options); // jslint:ok
if (parsed) {
return options.fetcher(parsed);
}
}
while (typeof(string) === "string") {
var i1 = string.indexOf("${");
var i2 = string.indexOf("}", i1 + 2);
if (i1 !== -1 && i2 !== -1) {
var parsed; // jslint:ok
if (string.charAt(i1 + 2) === "{") {
parsed = fluid.parseContextReference(string, i1 + 2, "}");
i2 = parsed.endpos;
}
else {
parsed = {path: string.substring(i1 + 2, i2)};
}
var subs = options.fetcher(parsed);
var all = (i1 === 0 && i2 === string.length - 1);
// TODO: test case for all undefined substitution
if (subs === undefined || subs === null) {
return subs;
}
string = all? subs : string.substring(0, i1) + subs + string.substring(i2 + 1);
}
else {
break;
}
}
return string;
};
fluid.resolveContextValue = fluid.wrapActivity(fluid.resolveContextValue,
[" while resolving context value ", "arguments.0"]);
function resolveEnvironmentImpl(obj, options) {
fluid.guardCircularity(options.seenIds, obj, "expansion",
" - please ensure options are not circularly connected, or protect from expansion using the \"noexpand\" policy or expander");
function recurse(arg) {
return resolveEnvironmentImpl(arg, options);
}
if (typeof(obj) === "string" && !options.noValue) {
return fluid.resolveContextValue(obj, options);
}
else if (fluid.isPrimitive(obj) || obj.nodeType !== undefined || obj.jquery) {
return obj;
}
else if (options.filter) {
return options.filter(obj, recurse, options);
}
else {
return (options.noCopy? fluid.each : fluid.transform)(obj, function(value, key) {
return resolveEnvironmentImpl(value, options);
});
}
}
fluid.defaults("fluid.resolveEnvironment", {
ELstyle: "${}",
seenIds: {},
bareContextRefs: true
});
fluid.resolveEnvironment = function(obj, options) {
// Don't create a component here since this function is itself used in the
// component expansion pathway - avoid all expansion in any case to head off FLUID-4301
options = $.extend(true, {}, fluid.rawDefaults("fluid.resolveEnvironment"), options);
return resolveEnvironmentImpl(obj, options);
};
/** "light" expanders, starting with support functions for the "deferredFetcher" expander **/
fluid.expander.deferredCall = function(target, source, recurse) {
var expander = source.expander;
var args = (!expander.args || fluid.isArrayable(expander.args))? expander.args : $.makeArray(expander.args);
args = recurse(args);
return fluid.invokeGlobalFunction(expander.func, args);
};
fluid.deferredCall = fluid.expander.deferredCall; // put in top namespace for convenience
fluid.deferredInvokeCall = function(target, source, recurse) {
var expander = source.expander;
var args = (!expander.args || fluid.isArrayable(expander.args))? expander.args : $.makeArray(expander.args);
args = recurse(args);
return fluid.invoke(expander.func, args);
};
// The "noexpand" expander which simply unwraps one level of expansion and ceases.
fluid.expander.noexpand = function(target, source) {
return $.extend(target, source.expander.tree);
};
fluid.noexpand = fluid.expander.noexpand; // TODO: check naming and namespacing
// unsupported, non-API function
fluid.expander.lightFilter = function (obj, recurse, options) {
var togo;
if (fluid.isArrayable(obj)) {
togo = options.noCopy? obj : [];
fluid.each(obj, function(value, key) {togo[key] = recurse(value);});
}
else {
togo = options.noCopy? obj : {};
for (var key in obj) {
var value = obj[key];
var expander;
if (key === "expander" && !(options.expandOnly && options.expandOnly[value.type])) {
expander = fluid.getGlobalValue(value.type);
if (expander) {
return expander.call(null, togo, obj, recurse, options);
}
}
if (key !== "expander" || !expander) {
togo[key] = recurse(value);
}
}
}
return options.noCopy? obj : togo;
};
// unsupported, non-API function
fluid.expander.expandLight = function (source, expandOptions) {
var options = $.extend({}, expandOptions);
options.filter = fluid.expander.lightFilter;
return fluid.resolveEnvironment(source, options);
};
})(jQuery, fluid_1_4);
/*
Copyright 2010-2011 OCAD University
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://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global fluid_1_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
/** Framework-global caching state for fluid.fetchResources **/
var resourceCache = {};
var pendingClass = {};
/** Accepts a hash of structures with free keys, where each entry has either
* href/url or nodeId set - on completion, callback will be called with the populated
* structure with fetched resource text in the field "resourceText" for each
* entry. Each structure may contain "options" holding raw options to be forwarded
* to jQuery.ajax().
*/
fluid.fetchResources = function(resourceSpecs, callback, options) {
var that = fluid.initLittleComponent("fluid.fetchResources", options);
that.resourceSpecs = resourceSpecs;
that.callback = callback;
that.operate = function() {
fluid.fetchResources.fetchResourcesImpl(that);
};
fluid.each(resourceSpecs, function(resourceSpec) {
resourceSpec.recurseFirer = fluid.event.getEventFirer();
resourceSpec.recurseFirer.addListener(that.operate);
if (resourceSpec.url && !resourceSpec.href) {
resourceSpec.href = resourceSpec.url;
}
});
if (that.options.amalgamateClasses) {
fluid.fetchResources.amalgamateClasses(resourceSpecs, that.options.amalgamateClasses, that.operate);
}
that.operate();
return that;
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
// Add "synthetic" elements of *this* resourceSpec list corresponding to any
// still pending elements matching the PROLEPTICK CLASS SPECIFICATION supplied
fluid.fetchResources.amalgamateClasses = function(specs, classes, operator) {
fluid.each(classes, function(clazz) {
var pending = pendingClass[clazz];
fluid.each(pending, function(pendingrec, canon) {
specs[clazz+"!"+canon] = pendingrec;
pendingrec.recurseFirer.addListener(operator);
});
});
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.fetchResources.timeSuccessCallback = function(resourceSpec) {
if (resourceSpec.timeSuccess && resourceSpec.options && resourceSpec.options.success) {
var success = resourceSpec.options.success;
resourceSpec.options.success = function() {
var startTime = new Date();
var ret = success.apply(null, arguments);
fluid.log("External callback for URL " + resourceSpec.href + " completed - callback time: " +
(new Date().getTime() - startTime.getTime()) + "ms");
return ret;
};
}
};
// TODO: Integrate punch-through from old Engage implementation
function canonUrl(url) {
return url;
}
fluid.fetchResources.clearResourceCache = function(url) {
if (url) {
delete resourceCache[canonUrl(url)];
}
else {
fluid.clear(resourceCache);
}
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.fetchResources.handleCachedRequest = function(resourceSpec, response) {
var canon = canonUrl(resourceSpec.href);
var cached = resourceCache[canon];
if (cached.$$firer$$) {
fluid.log("Handling request for " + canon + " from cache");
var fetchClass = resourceSpec.fetchClass;
if (fetchClass && pendingClass[fetchClass]) {
fluid.log("Clearing pendingClass entry for class " + fetchClass);
delete pendingClass[fetchClass][canon];
}
resourceCache[canon] = response;
cached.fire(response);
}
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.fetchResources.completeRequest = function(thisSpec, recurseCall) {
thisSpec.queued = false;
thisSpec.completeTime = new Date();
fluid.log("Request to URL " + thisSpec.href + " completed - total elapsed time: " +
(thisSpec.completeTime.getTime() - thisSpec.initTime.getTime()) + "ms");
thisSpec.recurseFirer.fire();
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.fetchResources.makeResourceCallback = function(thisSpec) {
return {
success: function(response) {
thisSpec.resourceText = response;
thisSpec.resourceKey = thisSpec.href;
if (thisSpec.forceCache) {
fluid.fetchResources.handleCachedRequest(thisSpec, response);
}
fluid.fetchResources.completeRequest(thisSpec);
},
error: function(response, textStatus, errorThrown) {
thisSpec.fetchError = {
status: response.status,
textStatus: response.textStatus,
errorThrown: errorThrown
};
fluid.fetchResources.completeRequest(thisSpec);
}
};
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.fetchResources.issueCachedRequest = function(resourceSpec, options) {
var canon = canonUrl(resourceSpec.href);
var cached = resourceCache[canon];
if (!cached) {
fluid.log("First request for cached resource with url " + canon);
cached = fluid.event.getEventFirer();
cached.$$firer$$ = true;
resourceCache[canon] = cached;
var fetchClass = resourceSpec.fetchClass;
if (fetchClass) {
if (!pendingClass[fetchClass]) {
pendingClass[fetchClass] = {};
}
pendingClass[fetchClass][canon] = resourceSpec;
}
options.cache = false; // TODO: Getting weird "not modified" issues on Firefox
$.ajax(options);
}
else {
if (!cached.$$firer$$) {
options.success(cached);
}
else {
fluid.log("Request for cached resource which is in flight: url " + canon);
cached.addListener(function(response) {
options.success(response);
});
}
}
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
// Compose callbacks in such a way that the 2nd, marked "external" will be applied
// first if it exists, but in all cases, the first, marked internal, will be
// CALLED WITHOUT FAIL
fluid.fetchResources.composeCallbacks = function(internal, external) {
return external? function() {
try {
external.apply(null, arguments);
}
catch (e) {
fluid.log("Exception applying external fetchResources callback: " + e);
}
internal.apply(null, arguments); // call the internal callback without fail
} : internal;
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.fetchResources.composePolicy = function(target, source, key) {
return fluid.fetchResources.composeCallbacks(target, source);
};
fluid.defaults("fluid.fetchResources.issueRequest", {
mergePolicy: {
success: fluid.fetchResources.composePolicy,
error: fluid.fetchResources.composePolicy,
url: "reverse"
}
});
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.fetchResources.issueRequest = function(resourceSpec, key) {
var thisCallback = fluid.fetchResources.makeResourceCallback(resourceSpec);
var options = {
url: resourceSpec.href,
success: thisCallback.success,
error: thisCallback.error,
dataType: "text"};
fluid.fetchResources.timeSuccessCallback(resourceSpec);
fluid.merge(fluid.defaults("fluid.fetchResources.issueRequest").mergePolicy,
options, resourceSpec.options);
resourceSpec.queued = true;
resourceSpec.initTime = new Date();
fluid.log("Request with key " + key + " queued for " + resourceSpec.href);
if (resourceSpec.forceCache) {
fluid.fetchResources.issueCachedRequest(resourceSpec, options);
}
else {
$.ajax(options);
}
};
fluid.fetchResources.fetchResourcesImpl = function(that) {
var complete = true;
var allSync = true;
var resourceSpecs = that.resourceSpecs;
for (var key in resourceSpecs) {
var resourceSpec = resourceSpecs[key];
if (!resourceSpec.options || resourceSpec.options.async) {
allSync = false;
}
if (resourceSpec.href && !resourceSpec.completeTime) {
if (!resourceSpec.queued) {
fluid.fetchResources.issueRequest(resourceSpec, key);
}
if (resourceSpec.queued) {
complete = false;
}
}
else if (resourceSpec.nodeId && !resourceSpec.resourceText) {
var node = document.getElementById(resourceSpec.nodeId);
// upgrade this to somehow detect whether node is "armoured" somehow
// with comment or CDATA wrapping
resourceSpec.resourceText = fluid.dom.getElementText(node);
resourceSpec.resourceKey = resourceSpec.nodeId;
}
}
if (complete && that.callback && !that.callbackCalled) {
that.callbackCalled = true;
if ($.browser.mozilla && !allSync) {
// Defer this callback to avoid debugging problems on Firefox
setTimeout(function() {
that.callback(resourceSpecs);
}, 1);
}
else {
that.callback(resourceSpecs);
}
}
};
fluid.fetchResources.primeCacheFromResources = function(componentName) {
var resources = fluid.defaults(componentName).resources;
var that = {typeName: "fluid.fetchResources.primeCacheFromResources"};
var expanded = (fluid.expandOptions ? fluid.expandOptions : fluid.identity)(fluid.copy(resources), that);
fluid.fetchResources(expanded);
};
/** Utilities invoking requests for expansion **/
fluid.registerNamespace("fluid.expander");
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.expander.makeDefaultFetchOptions = function (successdisposer, failid, options) {
return $.extend(true, {dataType: "text"}, options, {
success: function(response, environmentdisposer) {
var json = JSON.parse(response);
environmentdisposer(successdisposer(json));
},
error: function(response, textStatus) {
fluid.log("Error fetching " + failid + ": " + textStatus);
}
});
};
/*
* This function is unsupported: It is not really intended for use by implementors.
*/
fluid.expander.makeFetchExpander = function (options) {
return { expander: {
type: "fluid.expander.deferredFetcher",
href: options.url,
options: fluid.expander.makeDefaultFetchOptions(options.disposer, options.url, options.options),
resourceSpecCollector: "{resourceSpecCollector}",
fetchKey: options.fetchKey
}};
};
fluid.expander.deferredFetcher = function(target, source, recurse, expandOptions) {
var expander = source.expander;
var spec = fluid.copy(expander);
// fetch the "global" collector specified in the external environment to receive
// this resourceSpec
var collector = fluid.resolveEnvironment(expander.resourceSpecCollector, expandOptions);
delete spec.type;
delete spec.resourceSpecCollector;
delete spec.fetchKey;
var environmentdisposer = function(disposed) {
$.extend(target, disposed);
};
// replace the callback which is there (taking 2 arguments) with one which
// directly responds to the request, passing in the result and OUR "disposer" -
// which once the user has processed the response (say, parsing JSON and repackaging)
// finally deposits it in the place of the expander in the tree to which this reference
// has been stored at the point this expander was evaluated.
spec.options.success = function(response) {
expander.options.success(response, environmentdisposer);
};
var key = expander.fetchKey || fluid.allocateGuid();
collector[key] = spec;
return target;
};
})(jQuery, fluid_1_4);
/*
json2.js
2007-11-06
Public Domain
No warranty expressed or implied. Use at your own risk.
See http://www.JSON.org/js.html
This file creates a global JSON object containing two methods:
JSON.stringify(value, whitelist)
value any JavaScript value, usually an object or array.
whitelist an optional that determines how object values are
stringified.
This method produces a JSON text from a JavaScript value.
There are three possible ways to stringify an object, depending
on the optional whitelist parameter.
If an object has a toJSON method, then the toJSON() method will be
called. The value returned from the toJSON method will be
stringified.
Otherwise, if the optional whitelist parameter is an array, then
the elements of the array will be used to select members of the
object for stringification.
Otherwise, if there is no whitelist parameter, then all of the
members of the object will be stringified.
Values that do not have JSON representaions, such as undefined or
functions, will not be serialized. Such values in objects will be
dropped, in arrays will be replaced with null. JSON.stringify()
returns undefined. Dates will be stringified as quoted ISO dates.
Example:
var text = JSON.stringify(['e', {pluribus: 'unum'}]);
// text is '["e",{"pluribus":"unum"}]'
JSON.parse(text, filter)
This method parses a JSON text to produce an object or
array. It can throw a SyntaxError exception.
The optional filter parameter is a function that can filter and
transform the results. It receives each of the keys and values, and
its return value is used instead of the original value. If it
returns what it received, then structure is not modified. If it
returns undefined then the member is deleted.
Example:
// Parse the text. If a key contains the string 'date' then
// convert the value to a date.
myData = JSON.parse(text, function (key, value) {
return key.indexOf('date') >= 0 ? new Date(value) : value;
});
This is a reference implementation. You are free to copy, modify, or
redistribute.
Use your own copy. It is extremely unwise to load third party
code into your pages.
*/
/*jslint evil: true */
/*extern JSON */
if (!this.JSON) {
JSON = function () {
function f(n) { // Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
Date.prototype.toJSON = function () {
// Eventually, this method will be based on the date.toISOString method.
return this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z';
};
var m = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
function stringify(value, whitelist) {
var a, // The array holding the partial texts.
i, // The loop counter.
k, // The member key.
l, // Length.
r = /["\\\x00-\x1f\x7f-\x9f]/g,
v; // The member value.
switch (typeof value) {
case 'string':
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe sequences.
return r.test(value) ?
'"' + value.replace(r, function (a) {
var c = m[a];
if (c) {
return c;
}
c = a.charCodeAt();
return '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"' :
'"' + value + '"';
case 'number':
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value) ? String(value) : 'null';
case 'boolean':
case 'null':
return String(value);
case 'object':
// Due to a specification blunder in ECMAScript,
// typeof null is 'object', so watch out for that case.
if (!value) {
return 'null';
}
// If the object has a toJSON method, call it, and stringify the result.
if (typeof value.toJSON === 'function') {
return stringify(value.toJSON());
}
a = [];
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length'))) {
// The object is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.
l = value.length;
for (i = 0; i < l; i += 1) {
a.push(stringify(value[i], whitelist) || 'null');
}
// Join all of the elements together and wrap them in brackets.
return '[' + a.join(',') + ']';
}
if (whitelist) {
// If a whitelist (array of keys) is provided, use it to select the components
// of the object.
l = whitelist.length;
for (i = 0; i < l; i += 1) {
k = whitelist[i];
if (typeof k === 'string') {
v = stringify(value[k], whitelist);
if (v) {
a.push(stringify(k) + ':' + v);
}
}
}
} else {
// Otherwise, iterate through all of the keys in the object.
for (k in value) {
if (typeof k === 'string') {
v = stringify(value[k], whitelist);
if (v) {
a.push(stringify(k) + ':' + v);
}
}
}
}
// Join all of the member texts together and wrap them in braces.
return '{' + a.join(',') + '}';
}
}
return {
stringify: stringify,
parse: function (text, filter) {
var j;
function walk(k, v) {
var i, n;
if (v && typeof v === 'object') {
for (i in v) {
if (Object.prototype.hasOwnProperty.apply(v, [i])) {
n = walk(i, v[i]);
if (n !== undefined) {
v[i] = n;
}
}
}
}
return filter(k, v);
}
// Parsing happens in three stages. In the first stage, we run the text against
// regular expressions that look for non-JSON patterns. We are especially
// concerned with '()' and 'new' because they can cause invocation, and '='
// because it can cause mutation. But just to be safe, we want to reject all
// unexpected forms.
// We split the first stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace all backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/.test(text.replace(/\\./g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(:?[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
// In the second stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
j = eval('(' + text + ')');
// In the optional third stage, we recursively walk the new structure, passing
// each name/value pair to a filter function for possible transformation.
return typeof filter === 'function' ? walk('', j) : j;
}
// If the text is not JSON parseable, then a SyntaxError is thrown.
throw new SyntaxError('parseJSON');
}
};
}();
}
/*
Copyright 2008-2010 University of Cambridge
Copyright 2008-2010 University of Toronto
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_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
/**
* Returns the absolute position of a supplied DOM node in pixels.
* Implementation taken from quirksmode http://www.quirksmode.org/js/findpos.html
*/
fluid.dom.computeAbsolutePosition = function (element) {
var curleft = 0, curtop = 0;
if (element.offsetParent) {
do {
curleft += element.offsetLeft;
curtop += element.offsetTop;
element = element.offsetParent;
} while (element);
return [curleft, curtop];
}
};
/**
* Cleanse the children of a DOM node by removing all <script> tags.
* This is necessary to prevent the possibility that these blocks are
* reevaluated if the node were reattached to the document.
*/
fluid.dom.cleanseScripts = function (element) {
var cleansed = $.data(element, fluid.dom.cleanseScripts.MARKER);
if (!cleansed) {
fluid.dom.iterateDom(element, function (node) {
return node.tagName.toLowerCase() === "script" ? "delete" : null;
});
$.data(element, fluid.dom.cleanseScripts.MARKER, true);
}
};
fluid.dom.cleanseScripts.MARKER = "fluid-scripts-cleansed";
/**
* Inserts newChild as the next sibling of refChild.
* @param {Object} newChild
* @param {Object} refChild
*/
fluid.dom.insertAfter = function (newChild, refChild) {
var nextSib = refChild.nextSibling;
if (!nextSib) {
refChild.parentNode.appendChild(newChild);
} else {
refChild.parentNode.insertBefore(newChild, nextSib);
}
};
// The following two functions taken from http://developer.mozilla.org/En/Whitespace_in_the_DOM
/**
* Determine whether a node's text content is entirely whitespace.
*
* @param node A node implementing the |CharacterData| interface (i.e.,
* a |Text|, |Comment|, or |CDATASection| node
* @return True if all of the text content of |nod| is whitespace,
* otherwise false.
*/
fluid.dom.isWhitespaceNode = function (node) {
// Use ECMA-262 Edition 3 String and RegExp features
return !(/[^\t\n\r ]/.test(node.data));
};
/**
* Determine if a node should be ignored by the iterator functions.
*
* @param nod An object implementing the DOM1 |Node| interface.
* @return true if the node is:
* 1) A |Text| node that is all whitespace
* 2) A |Comment| node
* and otherwise false.
*/
fluid.dom.isIgnorableNode = function (node) {
return (node.nodeType === 8) || // A comment node
((node.nodeType === 3) && fluid.dom.isWhitespaceNode(node)); // a text node, all ws
};
})(jQuery, fluid_1_4);
/*
Copyright 2008-2010 University of Cambridge
Copyright 2008-2010 University of Toronto
Copyright 2010 OCAD University
Copyright 2010 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 window, fluid_1_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.orientation = {
HORIZONTAL: 4,
VERTICAL: 1
};
fluid.rectSides = {
// agree with fluid.orientation
4: ["left", "right"],
1: ["top", "bottom"],
// agree with fluid.direction
8: "top",
12: "bottom",
2: "left",
3: "right"
};
/**
* This is the position, relative to a given drop target, that a dragged item should be dropped.
*/
fluid.position = {
BEFORE: -1,
AFTER: 1,
INSIDE: 2,
REPLACE: 3
};
/**
* For incrementing/decrementing a count or index, or moving in a rectilinear direction.
*/
fluid.direction = {
NEXT: 1,
PREVIOUS: -1,
UP: 8,
DOWN: 12,
LEFT: 2,
RIGHT: 3
};
fluid.directionSign = function (direction) {
return direction === fluid.direction.UP || direction === fluid.direction.LEFT ?
fluid.direction.PREVIOUS : fluid.direction.NEXT;
};
fluid.directionAxis = function (direction) {
return direction === fluid.direction.LEFT || direction === fluid.direction.RIGHT ?
0 : 1;
};
fluid.directionOrientation = function (direction) {
return fluid.directionAxis(direction) ? fluid.orientation.VERTICAL : fluid.orientation.HORIZONTAL;
};
fluid.keycodeDirection = {
up: fluid.direction.UP,
down: fluid.direction.DOWN,
left: fluid.direction.LEFT,
right: fluid.direction.RIGHT
};
// moves a single node in the DOM to a new position relative to another
fluid.moveDom = function (source, target, position) {
source = fluid.unwrap(source);
target = fluid.unwrap(target);
var scan;
// fluid.log("moveDom source " + fluid.dumpEl(source) + " target " + fluid.dumpEl(target) + " position " + position);
if (position === fluid.position.INSIDE) {
target.appendChild(source);
} else if (position === fluid.position.BEFORE) {
for (scan = target.previousSibling;; scan = scan.previousSibling) {
if (!scan || !fluid.dom.isIgnorableNode(scan)) {
if (scan !== source) {
fluid.dom.cleanseScripts(source);
target.parentNode.insertBefore(source, target);
}
break;
}
}
} else if (position === fluid.position.AFTER) {
for (scan = target.nextSibling;; scan = scan.nextSibling) {
if (!scan || !fluid.dom.isIgnorableNode(scan)) {
if (scan !== source) {
fluid.dom.cleanseScripts(source);
fluid.dom.insertAfter(source, target);
}
break;
}
}
} else {
fluid.fail("Unrecognised position supplied to fluid.moveDom: " + position);
}
};
// unsupported, NON-API function
fluid.normalisePosition = function (position, samespan, targeti, sourcei) {
// convert a REPLACE into a primitive BEFORE/AFTER
if (position === fluid.position.REPLACE) {
position = samespan && targeti >= sourcei ? fluid.position.AFTER : fluid.position.BEFORE;
}
return position;
};
fluid.permuteDom = function (element, target, position, sourceelements, targetelements) {
element = fluid.unwrap(element);
target = fluid.unwrap(target);
var sourcei = $.inArray(element, sourceelements);
if (sourcei === -1) {
fluid.fail("Error in permuteDom: source element " + fluid.dumpEl(element) +
" not found in source list " + fluid.dumpEl(sourceelements));
}
var targeti = $.inArray(target, targetelements);
if (targeti === -1) {
fluid.fail("Error in permuteDom: target element " + fluid.dumpEl(target) +
" not found in source list " + fluid.dumpEl(targetelements));
}
var samespan = sourceelements === targetelements;
position = fluid.normalisePosition(position, samespan, targeti, sourcei);
//fluid.log("permuteDom sourcei " + sourcei + " targeti " + targeti);
// cache the old neighbourhood of the element for the final move
var oldn = {};
oldn[fluid.position.AFTER] = element.nextSibling;
oldn[fluid.position.BEFORE] = element.previousSibling;
fluid.moveDom(sourceelements[sourcei], targetelements[targeti], position);
// perform the leftward-moving, AFTER shift
var frontlimit = samespan ? targeti - 1 : sourceelements.length - 2;
var i;
if (position === fluid.position.BEFORE && samespan) {
// we cannot do skip processing if the element was "fused against the grain"
frontlimit--;
}
if (!samespan || targeti > sourcei) {
for (i = frontlimit; i > sourcei; --i) {
fluid.moveDom(sourceelements[i + 1], sourceelements[i], fluid.position.AFTER);
}
if (sourcei + 1 < sourceelements.length) {
fluid.moveDom(sourceelements[sourcei + 1], oldn[fluid.position.AFTER], fluid.position.BEFORE);
}
}
// perform the rightward-moving, BEFORE shift
var backlimit = samespan ? sourcei - 1 : targetelements.length - 1;
if (position === fluid.position.AFTER) {
// we cannot do skip processing if the element was "fused against the grain"
targeti++;
}
if (!samespan || targeti < sourcei) {
for (i = targeti; i < backlimit; ++i) {
fluid.moveDom(targetelements[i], targetelements[i + 1], fluid.position.BEFORE);
}
if (backlimit >= 0 && backlimit < targetelements.length - 1) {
fluid.moveDom(targetelements[backlimit], oldn[fluid.position.BEFORE], fluid.position.AFTER);
}
}
};
var curCss = function (a, name) {
return window.getComputedStyle ? window.getComputedStyle(a, null).getPropertyValue(name) :
a.currentStyle[name];
};
var isAttached = function (node) {
while (node && node.nodeName) {
if (node.nodeName === "BODY") {
return true;
}
node = node.parentNode;
}
return false;
};
var generalHidden = function (a) {
return "hidden" === a.type || curCss(a, "display") === "none" || curCss(a, "visibility") === "hidden" || !isAttached(a);
};
var computeGeometry = function (element, orientation, disposition) {
var elem = {};
elem.element = element;
elem.orientation = orientation;
if (disposition === fluid.position.INSIDE) {
elem.position = disposition;
}
if (generalHidden(element)) {
elem.clazz = "hidden";
}
var pos = fluid.dom.computeAbsolutePosition(element) || [0, 0];
var width = element.offsetWidth;
var height = element.offsetHeight;
elem.rect = {left: pos[0], top: pos[1]};
elem.rect.right = pos[0] + width;
elem.rect.bottom = pos[1] + height;
return elem;
};
// A "suitable large" value for the sentinel blocks at the ends of spans
var SENTINEL_DIMENSION = 10000;
function dumprect(rect) {
return "Rect top: " + rect.top +
" left: " + rect.left +
" bottom: " + rect.bottom +
" right: " + rect.right;
}
function dumpelem(cacheelem) {
if (!cacheelem || !cacheelem.rect) {
return "null";
} else {
return dumprect(cacheelem.rect) + " position: " +
cacheelem.position +
" for " +
fluid.dumpEl(cacheelem.element);
}
}
// unsupported, NON-API function
fluid.dropManager = function () {
var targets = [];
var cache = {};
var that = {};
var lastClosest;
var lastGeometry;
var displacementX, displacementY;
that.updateGeometry = function (geometricInfo) {
lastGeometry = geometricInfo;
targets = [];
cache = {};
var mapper = geometricInfo.elementMapper;
for (var i = 0; i < geometricInfo.extents.length; ++i) {
var thisInfo = geometricInfo.extents[i];
var orientation = thisInfo.orientation;
var sides = fluid.rectSides[orientation];
var processElement = function (element, sentB, sentF, disposition, j) {
var cacheelem = computeGeometry(element, orientation, disposition);
cacheelem.owner = thisInfo;
if (cacheelem.clazz !== "hidden" && mapper) {
cacheelem.clazz = mapper(element);
}
cache[fluid.dropManager.cacheKey(element)] = cacheelem;
var backClass = fluid.dropManager.getRelativeClass(thisInfo.elements, j, fluid.position.BEFORE, cacheelem.clazz, mapper);
var frontClass = fluid.dropManager.getRelativeClass(thisInfo.elements, j, fluid.position.AFTER, cacheelem.clazz, mapper);
if (disposition === fluid.position.INSIDE) {
targets[targets.length] = cacheelem;
} else {
fluid.dropManager.splitElement(targets, sides, cacheelem, disposition, backClass, frontClass);
}
// deal with sentinel blocks by creating near-copies of the end elements
if (sentB && geometricInfo.sentinelize) {
fluid.dropManager.sentinelizeElement(targets, sides, cacheelem, 1, disposition, backClass);
}
if (sentF && geometricInfo.sentinelize) {
fluid.dropManager.sentinelizeElement(targets, sides, cacheelem, 0, disposition, frontClass);
}
//fluid.log(dumpelem(cacheelem));
return cacheelem;
};
var allHidden = true;
for (var j = 0; j < thisInfo.elements.length; ++j) {
var element = thisInfo.elements[j];
var cacheelem = processElement(element, j === 0, j === thisInfo.elements.length - 1,
fluid.position.INTERLEAVED, j);
if (cacheelem.clazz !== "hidden") {
allHidden = false;
}
}
if (allHidden && thisInfo.parentElement) {
processElement(thisInfo.parentElement, true, true,
fluid.position.INSIDE);
}
}
};
that.startDrag = function (event, handlePos, handleWidth, handleHeight) {
var handleMidX = handlePos[0] + handleWidth / 2;
var handleMidY = handlePos[1] + handleHeight / 2;
var dX = handleMidX - event.pageX;
var dY = handleMidY - event.pageY;
that.updateGeometry(lastGeometry);
lastClosest = null;
displacementX = dX;
displacementY = dY;
$("body").bind("mousemove.fluid-dropManager", that.mouseMove);
};
that.lastPosition = function () {
return lastClosest;
};
that.endDrag = function () {
$("body").unbind("mousemove.fluid-dropManager");
};
that.mouseMove = function (evt) {
var x = evt.pageX + displacementX;
var y = evt.pageY + displacementY;
//fluid.log("Mouse x " + x + " y " + y );
var closestTarget = that.closestTarget(x, y, lastClosest);
if (closestTarget && closestTarget !== fluid.dropManager.NO_CHANGE) {
lastClosest = closestTarget;
that.dropChangeFirer.fire(closestTarget);
}
};
that.dropChangeFirer = fluid.event.getEventFirer();
var blankHolder = {
element: null
};
that.closestTarget = function (x, y, lastClosest) {
var mindistance = Number.MAX_VALUE;
var minelem = blankHolder;
var minlockeddistance = Number.MAX_VALUE;
var minlockedelem = blankHolder;
for (var i = 0; i < targets.length; ++i) {
var cacheelem = targets[i];
if (cacheelem.clazz === "hidden") {
continue;
}
var distance = fluid.geom.minPointRectangle(x, y, cacheelem.rect);
if (cacheelem.clazz === "locked") {
if (distance < minlockeddistance) {
minlockeddistance = distance;
minlockedelem = cacheelem;
}
} else {
if (distance < mindistance) {
mindistance = distance;
minelem = cacheelem;
}
if (distance === 0) {
break;
}
}
}
if (!minelem) {
return minelem;
}
if (minlockeddistance >= mindistance) {
minlockedelem = blankHolder;
}
//fluid.log("PRE: mindistance " + mindistance + " element " +
// fluid.dumpEl(minelem.element) + " minlockeddistance " + minlockeddistance
// + " locked elem " + dumpelem(minlockedelem));
if (lastClosest && lastClosest.position === minelem.position &&
fluid.unwrap(lastClosest.element) === fluid.unwrap(minelem.element) &&
fluid.unwrap(lastClosest.lockedelem) === fluid.unwrap(minlockedelem.element)
) {
return fluid.dropManager.NO_CHANGE;
}
//fluid.log("mindistance " + mindistance + " minlockeddistance " + minlockeddistance);
return {
position: minelem.position,
element: minelem.element,
lockedelem: minlockedelem.element
};
};
that.shuffleProjectFrom = function (element, direction, includeLocked, disableWrap) {
var togo = that.projectFrom(element, direction, includeLocked, disableWrap);
if (togo) {
togo.position = fluid.position.REPLACE;
}
return togo;
};
that.projectFrom = function (element, direction, includeLocked, disableWrap) {
that.updateGeometry(lastGeometry);
var cacheelem = cache[fluid.dropManager.cacheKey(element)];
var projected = fluid.geom.projectFrom(cacheelem.rect, direction, targets, includeLocked, disableWrap);
if (!projected.cacheelem) {
return null;
}
var retpos = projected.cacheelem.position;
return {element: projected.cacheelem.element,
position: retpos ? retpos : fluid.position.BEFORE
};
};
that.logicalFrom = function (element, direction, includeLocked, disableWrap) {
var orderables = that.getOwningSpan(element, fluid.position.INTERLEAVED, includeLocked);
return {element: fluid.dropManager.getRelativeElement(element, direction, orderables, disableWrap),
position: fluid.position.REPLACE};
};
that.lockedWrapFrom = function (element, direction, includeLocked, disableWrap) {
var base = that.logicalFrom(element, direction, includeLocked, disableWrap);
var selectables = that.getOwningSpan(element, fluid.position.INTERLEAVED, includeLocked);
var allElements = cache[fluid.dropManager.cacheKey(element)].owner.elements;
if (includeLocked || selectables[0] === allElements[0]) {
return base;
}
var directElement = fluid.dropManager.getRelativeElement(element, direction, allElements, disableWrap);
if (lastGeometry.elementMapper(directElement) === "locked") {
base.element = null;
base.clazz = "locked";
}
return base;
};
that.getOwningSpan = function (element, position, includeLocked) {
var owner = cache[fluid.dropManager.cacheKey(element)].owner;
var elements = position === fluid.position.INSIDE ? [owner.parentElement] : owner.elements;
if (!includeLocked && lastGeometry.elementMapper) {
elements = $.makeArray(elements);
fluid.remove_if(elements, function (element) {
return lastGeometry.elementMapper(element) === "locked";
});
}
return elements;
};
that.geometricMove = function (element, target, position) {
var sourceElements = that.getOwningSpan(element, null, true);
var targetElements = that.getOwningSpan(target, position, true);
fluid.permuteDom(element, target, position, sourceElements, targetElements);
};
return that;
};
fluid.dropManager.NO_CHANGE = "no change";
fluid.dropManager.cacheKey = function (element) {
return fluid.allocateSimpleId(element);
};
fluid.dropManager.sentinelizeElement = function (targets, sides, cacheelem, fc, disposition, clazz) {
var elemCopy = $.extend(true, {}, cacheelem);
elemCopy.rect[sides[fc]] = elemCopy.rect[sides[1 - fc]] + (fc ? 1 : -1);
elemCopy.rect[sides[1 - fc]] = (fc ? -1 : 1) * SENTINEL_DIMENSION;
elemCopy.position = disposition === fluid.position.INSIDE ?
disposition : (fc ? fluid.position.BEFORE : fluid.position.AFTER);
elemCopy.clazz = clazz;
targets[targets.length] = elemCopy;
};
fluid.dropManager.splitElement = function (targets, sides, cacheelem, disposition, clazz1, clazz2) {
var elem1 = $.extend(true, {}, cacheelem);
var elem2 = $.extend(true, {}, cacheelem);
var midpoint = (elem1.rect[sides[0]] + elem1.rect[sides[1]]) / 2;
elem1.rect[sides[1]] = midpoint;
elem1.position = fluid.position.BEFORE;
elem2.rect[sides[0]] = midpoint;
elem2.position = fluid.position.AFTER;
elem1.clazz = clazz1;
elem2.clazz = clazz2;
targets[targets.length] = elem1;
targets[targets.length] = elem2;
};
// Expand this configuration point if we ever go back to a full "permissions" model
fluid.dropManager.getRelativeClass = function (thisElements, index, relative, thisclazz, mapper) {
index += relative;
if (index < 0 && thisclazz === "locked") {
return "locked";
}
if (index >= thisElements.length || mapper === null) {
return null;
} else {
relative = thisElements[index];
return mapper(relative) === "locked" && thisclazz === "locked" ? "locked" : null;
}
};
fluid.dropManager.getRelativeElement = function (element, direction, elements, disableWrap) {
var folded = fluid.directionSign(direction);
var index = $(elements).index(element) + folded;
if (index < 0) {
index += elements.length;
}
// disable wrap
if (disableWrap) {
if (index === elements.length || index === (elements.length + folded)) {
return element;
}
}
index %= elements.length;
return elements[index];
};
fluid.geom = fluid.geom || {};
// These distance algorithms have been taken from
// http://www.cs.mcgill.ca/~cs644/Godfried/2005/Fall/fzamal/concepts.htm
/** Returns the minimum squared distance between a point and a rectangle **/
fluid.geom.minPointRectangle = function (x, y, rectangle) {
var dx = x < rectangle.left ? (rectangle.left - x) :
(x > rectangle.right ? (x - rectangle.right) : 0);
var dy = y < rectangle.top ? (rectangle.top - y) :
(y > rectangle.bottom ? (y - rectangle.bottom) : 0);
return dx * dx + dy * dy;
};
/** Returns the minimum squared distance between two rectangles **/
fluid.geom.minRectRect = function (rect1, rect2) {
var dx = rect1.right < rect2.left ? rect2.left - rect1.right :
rect2.right < rect1.left ? rect1.left - rect2.right : 0;
var dy = rect1.bottom < rect2.top ? rect2.top - rect1.bottom :
rect2.bottom < rect1.top ? rect1.top - rect2.bottom : 0;
return dx * dx + dy * dy;
};
var makePenCollect = function () {
return {
mindist: Number.MAX_VALUE,
minrdist: Number.MAX_VALUE
};
};
/** Determine the one amongst a set of rectangle targets which is the "best fit"
* for an axial motion from a "base rectangle" (commonly arising from the case
* of cursor key navigation).
* @param {Rectangle} baserect The base rectangl from which the motion is to be referred
* @param {fluid.direction} direction The direction of motion
* @param {Array of Rectangle holders} targets An array of objects "cache elements"
* for which the member <code>rect</code> is the holder of the rectangle to be tested.
* @param disableWrap which is used to enable or disable wrapping of elements
* @return The cache element which is the most appropriate for the requested motion.
*/
fluid.geom.projectFrom = function (baserect, direction, targets, forSelection, disableWrap) {
var axis = fluid.directionAxis(direction);
var frontSide = fluid.rectSides[direction];
var backSide = fluid.rectSides[axis * 15 + 5 - direction];
var dirSign = fluid.directionSign(direction);
var penrect = {left: (7 * baserect.left + 1 * baserect.right) / 8,
right: (5 * baserect.left + 3 * baserect.right) / 8,
top: (7 * baserect.top + 1 * baserect.bottom) / 8,
bottom: (5 * baserect.top + 3 * baserect.bottom) / 8};
penrect[frontSide] = dirSign * SENTINEL_DIMENSION;
penrect[backSide] = -penrect[frontSide];
function accPen(collect, cacheelem, backSign) {
var thisrect = cacheelem.rect;
var pdist = fluid.geom.minRectRect(penrect, thisrect);
var rdist = -dirSign * backSign * (baserect[backSign === 1 ? frontSide : backSide] -
thisrect[backSign === 1 ? backSide : frontSide]);
// fluid.log("pdist: " + pdist + " rdist: " + rdist);
// the oddity in the rdist comparison is intended to express "half-open"-ness of rectangles
// (backSign === 1 ? 0 : 1) - this is now gone - must be possible to move to perpendicularly abutting regions
if (pdist <= collect.mindist && rdist >= 0) {
if (pdist === collect.mindist && rdist * backSign > collect.minrdist) {
return;
}
collect.minrdist = rdist * backSign;
collect.mindist = pdist;
collect.minelem = cacheelem;
}
}
var collect = makePenCollect();
var backcollect = makePenCollect();
var lockedcollect = makePenCollect();
for (var i = 0; i < targets.length; ++i) {
var elem = targets[i];
var isPure = elem.owner && elem.element === elem.owner.parentElement;
if (elem.clazz === "hidden" || (forSelection && isPure)) {
continue;
} else if (!forSelection && elem.clazz === "locked") {
accPen(lockedcollect, elem, 1);
} else {
accPen(collect, elem, 1);
accPen(backcollect, elem, -1);
}
//fluid.log("Element " + i + " " + dumpelem(elem) + " mindist " + collect.mindist);
}
var wrap = !collect.minelem || backcollect.mindist < collect.mindist;
// disable wrap
wrap = wrap && !disableWrap;
var mincollect = wrap ? backcollect : collect;
var togo = {
wrapped: wrap,
cacheelem: mincollect.minelem
};
if (lockedcollect.mindist < mincollect.mindist) {
togo.lockedelem = lockedcollect.minelem;
}
return togo;
};
})(jQuery, fluid_1_4);
/*
Copyright 2007-2009 University of Toronto
Copyright 2007-2010 University of Cambridge
Copyright 2010-2011 OCAD University
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://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global fluid_1_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
var defaultAvatarCreator = function (item, cssClass, dropWarning) {
fluid.dom.cleanseScripts(fluid.unwrap(item));
var avatar = $(item).clone();
fluid.dom.iterateDom(avatar.get(0), function (node) {
node.removeAttribute("id");
if (node.tagName.toLowerCase() === "input") {
node.setAttribute("disabled", "disabled");
}
});
avatar.removeProp("id");
avatar.removeClass("ui-droppable");
avatar.addClass(cssClass);
if (dropWarning) {
// Will a 'div' always be valid in this position?
var avatarContainer = $(document.createElement("div"));
avatarContainer.append(avatar);
avatarContainer.append(dropWarning);
avatar = avatarContainer;
}
$("body").append(avatar);
if (!$.browser.safari) {
// FLUID-1597: Safari appears incapable of correctly determining the dimensions of elements
avatar.css("display", "block").width(item.offsetWidth).height(item.offsetHeight);
}
if ($.browser.opera) { // FLUID-1490. Without this detect, curCSS explodes on the avatar on Firefox.
avatar.hide();
}
return avatar;
};
function bindHandlersToContainer(container, keyDownHandler, keyUpHandler, mouseMoveHandler) {
var actualKeyDown = keyDownHandler;
var advancedPrevention = false;
// FLUID-1598 and others: Opera will refuse to honour a "preventDefault" on a keydown.
// http://forums.devshed.com/javascript-development-115/onkeydown-preventdefault-opera-485371.html
if ($.browser.opera) {
container.keypress(function (evt) {
if (advancedPrevention) {
advancedPrevention = false;
evt.preventDefault();
return false;
}
});
actualKeyDown = function (evt) {
var oldret = keyDownHandler(evt);
if (oldret === false) {
advancedPrevention = true;
}
};
}
container.keydown(actualKeyDown);
container.keyup(keyUpHandler);
}
function addRolesToContainer(that) {
that.container.attr("role", that.options.containerRole.container);
that.container.attr("aria-multiselectable", "false");
that.container.attr("aria-readonly", "false");
that.container.attr("aria-disabled", "false");
// FLUID-3707: We require to have BOTH application role as well as our named role
// This however breaks the component completely under NVDA and causes it to perpetually drop back into "browse mode"
//that.container.wrap("<div role=\"application\"></div>");
}
function createAvatarId(parentId) {
// Generating the avatar's id to be containerId_avatar
// This is safe since there is only a single avatar at a time
return parentId + "_avatar";
}
var adaptKeysets = function (options) {
if (options.keysets && !(options.keysets instanceof Array)) {
options.keysets = [options.keysets];
}
};
/**
* @param container - A jQueryable designator for the root node of the reorderer (a selector, a DOM node, or a jQuery instance)
* @param options - an object containing any of the available options:
* containerRole - indicates the role, or general use, for this instance of the Reorderer
* keysets - an object containing sets of keycodes to use for directional navigation. Must contain:
* modifier - a function that returns a boolean, indicating whether or not the required modifier(s) are activated
* up
* down
* right
* left
* styles - an object containing class names for styling the Reorderer
* defaultStyle
* selected
* dragging
* hover
* dropMarker
* mouseDrag
* avatar
* avatarCreator - a function that returns a valid DOM node to be used as the dragging avatar
*/
fluid.reorderer = function (container, options) {
if (!container) {
fluid.fail("Reorderer initialised with no container");
}
var thatReorderer = fluid.initView("fluid.reorderer", container, options);
options = thatReorderer.options;
var dropManager = fluid.dropManager();
thatReorderer.layoutHandler = fluid.initSubcomponent(thatReorderer,
"layoutHandler", [thatReorderer.container, options, dropManager, thatReorderer.dom]);
thatReorderer.activeItem = undefined;
adaptKeysets(options);
var kbDropWarning = thatReorderer.locate("dropWarning");
var mouseDropWarning;
if (kbDropWarning) {
mouseDropWarning = kbDropWarning.clone();
}
var isMove = function (evt) {
var keysets = options.keysets;
for (var i = 0; i < keysets.length; i++) {
if (keysets[i].modifier(evt)) {
return true;
}
}
return false;
};
var isActiveItemMovable = function () {
return $.inArray(thatReorderer.activeItem, thatReorderer.dom.fastLocate("movables")) >= 0;
};
var setDropEffects = function (value) {
thatReorderer.dom.fastLocate("dropTargets").attr("aria-dropeffect", value);
};
var styles = options.styles;
var noModifier = function (evt) {
return (!evt.ctrlKey && !evt.altKey && !evt.shiftKey && !evt.metaKey);
};
var handleDirectionKeyDown = function (evt) {
var item = thatReorderer.activeItem;
if (!item) {
return true;
}
var keysets = options.keysets;
for (var i = 0; i < keysets.length; i++) {
var keyset = keysets[i];
var keydir = fluid.keyForValue(keyset, evt.keyCode);
if (!keydir) {
continue;
}
var isMovement = keyset.modifier(evt);
var dirnum = fluid.keycodeDirection[keydir];
var relativeItem = thatReorderer.layoutHandler.getRelativePosition(item, dirnum, !isMovement);
if (!relativeItem) {
continue;
}
if (isMovement) {
var prevent = thatReorderer.events.onBeginMove.fire(item);
if (prevent === false) {
return false;
}
if (kbDropWarning.length > 0) {
if (relativeItem.clazz === "locked") {
thatReorderer.events.onShowKeyboardDropWarning.fire(item, kbDropWarning);
kbDropWarning.show();
} else {
kbDropWarning.hide();
}
}
if (relativeItem.element) {
thatReorderer.requestMovement(relativeItem, item);
}
} else if (noModifier(evt)) {
item.blur();
$(relativeItem.element).focus();
}
return false;
}
return true;
};
// unsupported, NON-API function
thatReorderer.handleKeyDown = function (evt) {
if (!thatReorderer.activeItem || thatReorderer.activeItem !== evt.target) {
return true;
}
// If the key pressed is ctrl, and the active item is movable we want to restyle the active item.
var jActiveItem = $(thatReorderer.activeItem);
if (!jActiveItem.hasClass(styles.dragging) && isMove(evt)) {
// Don't treat the active item as dragging unless it is a movable.
if (isActiveItemMovable()) {
jActiveItem.removeClass(styles.selected);
jActiveItem.addClass(styles.dragging);
jActiveItem.attr("aria-grabbed", "true");
setDropEffects("move");
}
return false;
}
// The only other keys we listen for are the arrows.
return handleDirectionKeyDown(evt);
};
// unsupported, NON-API function
thatReorderer.handleKeyUp = function (evt) {
if (!thatReorderer.activeItem || thatReorderer.activeItem !== evt.target) {
return true;
}
var jActiveItem = $(thatReorderer.activeItem);
// Handle a key up event for the modifier
if (jActiveItem.hasClass(styles.dragging) && !isMove(evt)) {
if (kbDropWarning) {
kbDropWarning.hide();
}
jActiveItem.removeClass(styles.dragging);
jActiveItem.addClass(styles.selected);
jActiveItem.attr("aria-grabbed", "false");
setDropEffects("none");
return false;
}
return false;
};
var dropMarker;
var createDropMarker = function (tagName) {
var dropMarker = $(document.createElement(tagName));
dropMarker.addClass(options.styles.dropMarker);
dropMarker.hide();
return dropMarker;
};
// unsupported, NON-API function
thatReorderer.requestMovement = function (requestedPosition, item) {
item = fluid.unwrap(item);
// Temporary censoring to get around ModuleLayout inability to update relative to self.
if (!requestedPosition || fluid.unwrap(requestedPosition.element) === item) {
return;
}
var activeItem = $(thatReorderer.activeItem);
// Fixes FLUID-3288.
// Need to unbind the blur event as safari will call blur on movements.
// This caused the user to have to double tap the arrow keys to move.
activeItem.unbind("blur.fluid.reorderer");
thatReorderer.events.onMove.fire(item, requestedPosition);
dropManager.geometricMove(item, requestedPosition.element, requestedPosition.position);
//$(thatReorderer.activeItem).removeClass(options.styles.selected);
// refocus on the active item because moving places focus on the body
activeItem.focus();
thatReorderer.refresh();
dropManager.updateGeometry(thatReorderer.layoutHandler.getGeometricInfo());
thatReorderer.events.afterMove.fire(item, requestedPosition, thatReorderer.dom.fastLocate("movables"));
};
var hoverStyleHandler = function (item, state) {
thatReorderer.dom.fastLocate("grabHandle", item)[state ? "addClass" : "removeClass"](styles.hover);
};
/**
* Takes a $ object and adds 'movable' functionality to it
*/
function initMovable(item) {
var styles = options.styles;
item.attr("aria-grabbed", "false");
item.mouseover(
function () {
thatReorderer.events.onHover.fire(item, true);
}
);
item.mouseout(
function () {
thatReorderer.events.onHover.fire(item, false);
}
);
var avatar;
thatReorderer.dom.fastLocate("grabHandle", item).draggable({
refreshPositions: false,
scroll: true,
helper: function () {
var dropWarningEl;
if (mouseDropWarning) {
dropWarningEl = mouseDropWarning[0];
}
avatar = $(options.avatarCreator(item[0], styles.avatar, dropWarningEl));
avatar.prop("id", createAvatarId(thatReorderer.container.id));
return avatar;
},
start: function (e, ui) {
var prevent = thatReorderer.events.onBeginMove.fire(item);
if (prevent === false) {
return false;
}
var handle = thatReorderer.dom.fastLocate("grabHandle", item)[0];
var handlePos = fluid.dom.computeAbsolutePosition(handle);
var handleWidth = handle.offsetWidth;
var handleHeight = handle.offsetHeight;
item.focus();
item.removeClass(options.styles.selected);
item.addClass(options.styles.mouseDrag);
item.attr("aria-grabbed", "true");
setDropEffects("move");
dropManager.startDrag(e, handlePos, handleWidth, handleHeight);
avatar.show();
},
stop: function (e, ui) {
item.removeClass(options.styles.mouseDrag);
item.addClass(options.styles.selected);
$(thatReorderer.activeItem).attr("aria-grabbed", "false");
var markerNode = fluid.unwrap(dropMarker);
if (markerNode.parentNode) {
markerNode.parentNode.removeChild(markerNode);
}
avatar.hide();
ui.helper = null;
setDropEffects("none");
dropManager.endDrag();
thatReorderer.requestMovement(dropManager.lastPosition(), item);
// refocus on the active item because moving places focus on the body
thatReorderer.activeItem.focus();
},
handle: thatReorderer.dom.fastLocate("grabHandle", item)
});
}
function changeSelectedToDefault(jItem, styles) {
jItem.removeClass(styles.selected);
jItem.removeClass(styles.dragging);
jItem.addClass(styles.defaultStyle);
jItem.attr("aria-selected", "false");
}
var selectItem = function (anItem) {
thatReorderer.events.onSelect.fire(anItem);
var styles = options.styles;
// Set the previous active item back to its default state.
if (thatReorderer.activeItem && thatReorderer.activeItem !== anItem) {
changeSelectedToDefault($(thatReorderer.activeItem), styles);
}
// Then select the new item.
thatReorderer.activeItem = anItem;
var jItem = $(anItem);
jItem.removeClass(styles.defaultStyle);
jItem.addClass(styles.selected);
jItem.attr("aria-selected", "true");
};
var initSelectables = function () {
var handleBlur = function (evt) {
changeSelectedToDefault($(this), options.styles);
return evt.stopPropagation();
};
var handleFocus = function (evt) {
selectItem(this);
return evt.stopPropagation();
};
var selectables = thatReorderer.dom.fastLocate("selectables");
for (var i = 0; i < selectables.length; ++i) {
var selectable = $(selectables[i]);
if (!$.data(selectable[0], "fluid.reorderer.selectable-initialised")) {
selectable.addClass(styles.defaultStyle);
selectable.bind("blur.fluid.reorderer", handleBlur);
selectable.focus(handleFocus);
selectable.click(function (evt) {
var handle = fluid.unwrap(thatReorderer.dom.fastLocate("grabHandle", this));
if (fluid.dom.isContainer(handle, evt.target)) {
$(this).focus();
}
});
selectable.attr("role", options.containerRole.item);
selectable.attr("aria-selected", "false");
selectable.attr("aria-disabled", "false");
$.data(selectable[0], "fluid.reorderer.selectable-initialised", true);
}
}
if (!thatReorderer.selectableContext) {
thatReorderer.selectableContext = fluid.selectable(thatReorderer.container, {
selectableElements: selectables,
selectablesTabindex: thatReorderer.options.selectablesTabindex,
direction: null
});
}
};
var dropChangeListener = function (dropTarget) {
fluid.moveDom(dropMarker, dropTarget.element, dropTarget.position);
dropMarker.css("display", "");
if (mouseDropWarning) {
if (dropTarget.lockedelem) {
mouseDropWarning.show();
} else {
mouseDropWarning.hide();
}
}
};
var initItems = function () {
var movables = thatReorderer.dom.fastLocate("movables");
var dropTargets = thatReorderer.dom.fastLocate("dropTargets");
initSelectables();
// Setup movables
for (var i = 0; i < movables.length; i++) {
var item = movables[i];
if (!$.data(item, "fluid.reorderer.movable-initialised")) {
initMovable($(item));
$.data(item, "fluid.reorderer.movable-initialised", true);
}
}
// In order to create valid html, the drop marker is the same type as the node being dragged.
// This creates a confusing UI in cases such as an ordered list.
// drop marker functionality should be made pluggable.
if (movables.length > 0 && !dropMarker) {
dropMarker = createDropMarker(movables[0].tagName);
}
dropManager.updateGeometry(thatReorderer.layoutHandler.getGeometricInfo());
dropManager.dropChangeFirer.addListener(dropChangeListener, "fluid.Reorderer");
// Set up dropTargets
dropTargets.attr("aria-dropeffect", "none");
};
// Final initialization of the Reorderer at the end of the construction process
if (thatReorderer.container) {
bindHandlersToContainer(thatReorderer.container,
thatReorderer.handleKeyDown,
thatReorderer.handleKeyUp);
addRolesToContainer(thatReorderer);
fluid.tabbable(thatReorderer.container);
initItems();
}
if (options.afterMoveCallbackUrl) {
thatReorderer.events.afterMove.addListener(function () {
var layoutHandler = thatReorderer.layoutHandler;
var model = layoutHandler.getModel ? layoutHandler.getModel() :
options.acquireModel(thatReorderer);
$.post(options.afterMoveCallbackUrl, JSON.stringify(model));
}, "postModel");
}
thatReorderer.events.onHover.addListener(hoverStyleHandler, "style");
thatReorderer.refresh = function () {
thatReorderer.dom.refresh("movables");
thatReorderer.dom.refresh("selectables");
thatReorderer.dom.refresh("grabHandle", thatReorderer.dom.fastLocate("movables"));
thatReorderer.dom.refresh("stylisticOffset", thatReorderer.dom.fastLocate("movables"));
thatReorderer.dom.refresh("dropTargets");
thatReorderer.events.onRefresh.fire();
initItems();
thatReorderer.selectableContext.selectables = thatReorderer.dom.fastLocate("selectables");
thatReorderer.selectableContext.selectablesUpdated(thatReorderer.activeItem);
};
fluid.initDependents(thatReorderer);
thatReorderer.refresh();
return thatReorderer;
};
/**
* Constants for key codes in events.
*/
fluid.reorderer.keys = {
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
META: 19,
SPACE: 32,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
i: 73,
j: 74,
k: 75,
m: 77
};
/**
* The default key sets for the Reorderer. Should be moved into the proper component defaults.
*/
fluid.reorderer.defaultKeysets = [
{
modifier : function (evt) {
return evt.ctrlKey;
},
up : fluid.reorderer.keys.UP,
down : fluid.reorderer.keys.DOWN,
right : fluid.reorderer.keys.RIGHT,
left : fluid.reorderer.keys.LEFT
},
{
modifier : function (evt) {
return evt.ctrlKey;
},
up : fluid.reorderer.keys.i,
down : fluid.reorderer.keys.m,
right : fluid.reorderer.keys.k,
left : fluid.reorderer.keys.j
}
];
/**
* These roles are used to add ARIA roles to orderable items. This list can be extended as needed,
* but the values of the container and item roles must match ARIA-specified roles.
*/
fluid.reorderer.roles = {
GRID: { container: "grid", item: "gridcell" },
LIST: { container: "list", item: "listitem" },
REGIONS: { container: "main", item: "article" }
};
// Simplified API for reordering lists and grids.
var simpleInit = function (container, layoutHandler, options) {
options = options || {};
options.layoutHandler = layoutHandler;
return fluid.reorderer(container, options);
};
fluid.reorderList = function (container, options) {
return simpleInit(container, "fluid.listLayoutHandler", options);
};
fluid.reorderGrid = function (container, options) {
return simpleInit(container, "fluid.gridLayoutHandler", options);
};
fluid.reorderer.SHUFFLE_GEOMETRIC_STRATEGY = "shuffleProjectFrom";
fluid.reorderer.GEOMETRIC_STRATEGY = "projectFrom";
fluid.reorderer.LOGICAL_STRATEGY = "logicalFrom";
fluid.reorderer.WRAP_LOCKED_STRATEGY = "lockedWrapFrom";
fluid.reorderer.NO_STRATEGY = null;
// unsupported, NON-API function
fluid.reorderer.relativeInfoGetter = function (orientation, coStrategy, contraStrategy, dropManager, dom, disableWrap) {
return function (item, direction, forSelection) {
var dirorient = fluid.directionOrientation(direction);
var strategy = dirorient === orientation ? coStrategy : contraStrategy;
return strategy !== null ? dropManager[strategy](item, direction, forSelection, disableWrap) : null;
};
};
fluid.defaults("fluid.reorderer", {
styles: {
defaultStyle: "fl-reorderer-movable-default",
selected: "fl-reorderer-movable-selected",
dragging: "fl-reorderer-movable-dragging",
mouseDrag: "fl-reorderer-movable-dragging",
hover: "fl-reorderer-movable-hover",
dropMarker: "fl-reorderer-dropMarker",
avatar: "fl-reorderer-avatar"
},
selectors: {
dropWarning: ".flc-reorderer-dropWarning",
movables: ".flc-reorderer-movable",
grabHandle: "",
stylisticOffset: ""
},
avatarCreator: defaultAvatarCreator,
keysets: fluid.reorderer.defaultKeysets,
layoutHandler: {
type: "fluid.listLayoutHandler"
},
events: {
onShowKeyboardDropWarning: null,
onSelect: null,
onBeginMove: "preventable",
onMove: null,
afterMove: null,
onHover: null,
onRefresh: null
},
mergePolicy: {
keysets: "replace",
"selectors.labelSource": "selectors.grabHandle",
"selectors.selectables": "selectors.movables",
"selectors.dropTargets": "selectors.movables"
},
components: {
labeller: {
type: "fluid.reorderer.labeller",
options: {
dom: "{reorderer}.dom",
getGeometricInfo: "{reorderer}.layoutHandler.getGeometricInfo",
orientation: "{reorderer}.options.orientation",
layoutType: "{reorderer}.options.layoutHandler" // TODO, get rid of "global defaults"
}
}
},
// The user option to enable or disable wrapping of elements within the container
disableWrap: false
});
/*******************
* Layout Handlers *
*******************/
// unsupported, NON-API function
fluid.reorderer.makeGeometricInfoGetter = function (orientation, sentinelize, dom) {
return function () {
var that = {
sentinelize: sentinelize,
extents: [{
orientation: orientation,
elements: dom.fastLocate("dropTargets")
}],
elementMapper: function (element) {
return $.inArray(element, dom.fastLocate("movables")) === -1 ? "locked" : null;
},
elementIndexer: function (element) {
var selectables = dom.fastLocate("selectables");
return {
elementClass: that.elementMapper(element),
index: $.inArray(element, selectables),
length: selectables.length
};
}
};
return that;
};
};
fluid.defaults(true, "fluid.listLayoutHandler",
{
orientation: fluid.orientation.VERTICAL,
containerRole: fluid.reorderer.roles.LIST,
selectablesTabindex: -1,
sentinelize: true
});
// Public layout handlers.
fluid.listLayoutHandler = function (container, options, dropManager, dom) {
var that = {};
that.getRelativePosition =
fluid.reorderer.relativeInfoGetter(options.orientation,
fluid.reorderer.LOGICAL_STRATEGY, null, dropManager, dom, options.disableWrap);
that.getGeometricInfo = fluid.reorderer.makeGeometricInfoGetter(options.orientation, options.sentinelize, dom);
return that;
}; // End ListLayoutHandler
fluid.defaults(true, "fluid.gridLayoutHandler",
{
orientation: fluid.orientation.HORIZONTAL,
containerRole: fluid.reorderer.roles.GRID,
selectablesTabindex: -1,
sentinelize: false
});
/*
* Items in the Lightbox are stored in a list, but they are visually presented as a grid that
* changes dimensions when the window changes size. As a result, when the user presses the up or
* down arrow key, what lies above or below depends on the current window size.
*
* The GridLayoutHandler is responsible for handling changes to this virtual 'grid' of items
* in the window, and of informing the Lightbox of which items surround a given item.
*/
fluid.gridLayoutHandler = function (container, options, dropManager, dom) {
var that = {};
that.getRelativePosition =
fluid.reorderer.relativeInfoGetter(options.orientation,
options.disableWrap ? fluid.reorderer.SHUFFLE_GEOMETRIC_STRATEGY : fluid.reorderer.LOGICAL_STRATEGY, fluid.reorderer.SHUFFLE_GEOMETRIC_STRATEGY,
dropManager, dom, options.disableWrap);
that.getGeometricInfo = fluid.reorderer.makeGeometricInfoGetter(options.orientation, options.sentinelize, dom);
return that;
}; // End of GridLayoutHandler
fluid.defaults("fluid.reorderer.labeller", {
strings: {
overallTemplate: "%recentStatus %item %position %movable",
position: "%index of %length",
position_moduleLayoutHandler: "%index of %length in %moduleCell %moduleIndex of %moduleLength",
moduleCell_0: "row", // NB, these keys must agree with fluid.a11y.orientation constants
moduleCell_1: "column",
movable: "movable",
fixed: "fixed",
recentStatus: "moved from position %position"
},
components: {
resolver: {
type: "fluid.messageResolver",
options: {
messageBase: "{labeller}.options.strings"
}
}
},
invokers: {
renderLabel: {
funcName: "fluid.reorderer.labeller.renderLabel",
args: ["{labeller}", "@0", "@1"]
}
}
});
// unsupported, NON-API function
// Convert from 0-based to 1-based indices for announcement
fluid.reorderer.indexRebaser = function (indices) {
indices.index++;
if (indices.moduleIndex !== undefined) {
indices.moduleIndex++;
}
return indices;
};
/*************
* Labelling *
*************/
fluid.reorderer.labeller = function (options) {
var that = fluid.initLittleComponent("fluid.reorderer.labeller", options);
fluid.initDependents(that);
that.dom = that.options.dom;
that.moduleCell = that.resolver.resolve("moduleCell_" + that.options.orientation);
var layoutType = fluid.computeNickName(that.options.layoutType);
that.positionTemplate = that.resolver.lookup(["position_" + layoutType, "position"]);
var movedMap = {};
that.returnedOptions = {
listeners: {
onRefresh: function () {
var selectables = that.dom.locate("selectables");
fluid.each(selectables, function (selectable) {
var labelOptions = {};
var id = fluid.allocateSimpleId(selectable);
var moved = movedMap[id];
var label = that.renderLabel(selectable);
var plainLabel = label;
if (moved) {
moved.newRender = plainLabel;
label = that.renderLabel(selectable, moved.oldRender.position);
$(selectable).one("focusout", function () {
if (movedMap[id]) {
var oldLabel = movedMap[id].newRender.label;
delete movedMap[id];
fluid.updateAriaLabel(selectable, oldLabel);
}
});
labelOptions.dynamicLabel = true;
}
fluid.updateAriaLabel(selectable, label.label, labelOptions);
});
},
onMove: function (item, newPosition) {
fluid.clear(movedMap); // if we somehow were fooled into missing a defocus, at least clear the map on a 2nd move
var movingId = fluid.allocateSimpleId(item);
movedMap[movingId] = {
oldRender: that.renderLabel(item)
};
}
}
};
return that;
};
fluid.reorderer.labeller.renderLabel = function (that, selectable, recentPosition) {
var geom = that.options.getGeometricInfo();
var indices = fluid.reorderer.indexRebaser(geom.elementIndexer(selectable));
indices.moduleCell = that.moduleCell;
var elementClass = geom.elementMapper(selectable);
var labelSource = that.dom.locate("labelSource", selectable);
var recentStatus;
if (recentPosition) {
recentStatus = that.resolver.resolve("recentStatus", {position: recentPosition});
}
var topModel = {
item: typeof (labelSource) === "string" ? labelSource : fluid.dom.getElementText(fluid.unwrap(labelSource)),
position: that.positionTemplate.resolveFunc(that.positionTemplate.template, indices),
movable: that.resolver.resolve(elementClass === "locked" ? "fixed" : "movable"),
recentStatus: recentStatus || ""
};
var template = that.resolver.lookup(["overallTemplate"]);
var label = template.resolveFunc(template.template, topModel);
return {
position: topModel.position,
label: label
};
};
})(jQuery, fluid_1_4);
/*
Copyright 2008-2009 University of Cambridge
Copyright 2008-2009 University of Toronto
Copyright 2010-2011 Lucendo Development Ltd.
Copyright 2011 OCAD University
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_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
var deriveLightboxCellBase = function (namebase, index) {
return namebase + "lightbox-cell:" + index + ":";
};
var addThumbnailActivateHandler = function (container) {
var enterKeyHandler = function (evt) {
if (evt.which === fluid.reorderer.keys.ENTER) {
var thumbnailAnchors = $("a", evt.target);
document.location = thumbnailAnchors.attr("href");
}
};
container.keypress(enterKeyHandler);
};
// Custom query method seeks all tags descended from a given root with a
// particular tag name, whose id matches a regex.
var seekNodesById = function (rootnode, tagname, idmatch) {
var inputs = rootnode.getElementsByTagName(tagname);
var togo = [];
for (var i = 0; i < inputs.length; i += 1) {
var input = inputs[i];
var id = input.id;
if (id && id.match(idmatch)) {
togo.push(input);
}
}
return togo;
};
var createImageCellFinder = function (parentNode, containerId) {
parentNode = fluid.unwrap(parentNode);
var lightboxCellNamePattern = "^" + deriveLightboxCellBase(containerId, "[0-9]+") + "$";
return function () {
// This orderable finder assumes that the lightbox thumbnails are 'div' elements
return seekNodesById(parentNode, "div", lightboxCellNamePattern);
};
};
var seekForm = function (container) {
return fluid.findAncestor(container, function (element) {
return $(element).is("form");
});
};
var seekInputs = function (container, reorderform) {
return seekNodesById(reorderform,
"input",
"^" + deriveLightboxCellBase(container.prop("id"), "[^:]*") + "reorder-index$");
};
var mapIdsToNames = function (container, reorderform) {
var inputs = seekInputs(container, reorderform);
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
var name = input.name;
input.name = name || input.id;
}
};
/**
* Returns a default afterMove listener using the id-based, form-driven scheme for communicating with the server.
* It is implemented by nesting hidden form fields inside each thumbnail container. The value of these form elements
* represent the order for each image. This default listener submits the form's default
* action via AJAX.
*
* @param {jQueryable} container the Image Reorderer's container element
*/
var createIDAfterMoveListener = function (container) {
var reorderform = seekForm(container);
mapIdsToNames(container, reorderform);
return function () {
var inputs, i;
inputs = seekInputs(container, reorderform);
for (i = 0; i < inputs.length; i += 1) {
inputs[i].value = i;
}
if (reorderform && reorderform.action) {
var order = $(reorderform).serialize();
$.post(reorderform.action,
order,
function (type, data, evt) { /* No-op response */ });
}
};
};
var setDefaultValue = function (target, path, value) {
var previousValue = fluid.get(target, path);
var valueToSet = previousValue || value;
fluid.set(target, path, valueToSet);
};
// Public Lightbox API
/**
* Creates a new Lightbox instance from the specified parameters, providing full control over how
* the Lightbox is configured.
*
* @param {Object} container
* @param {Object} options
*/
fluid.reorderImages = function (container, options) {
// TODO: fix up this nonstandard workflow once we IoC-ify reorderer and implement standard
// wrapper facility
var defaults = fluid.defaults("fluid.reorderImages");
var mergedOptions = fluid.merge(defaults.mergePolicy, {}, defaults, options);
container = fluid.container(container);
// If the user didn't specify their own afterMove or movables options,
// set up defaults for them using the old id-based scheme.
// Backwards API compatiblity. Remove references to afterMoveCallback by Infusion 1.5.
setDefaultValue(mergedOptions, "listeners.afterMove",
mergedOptions.afterMoveCallback || createIDAfterMoveListener(container));
setDefaultValue(mergedOptions, "selectors.movables",
createImageCellFinder(container, container.prop("id")));
var reorderer = fluid.reorderer(container, mergedOptions);
fluid.tabindex($("a", container), -1);
addThumbnailActivateHandler(container);
return reorderer;
};
// This function now deprecated. Please use fluid.reorderImages() instead.
fluid.lightbox = fluid.reorderImages;
fluid.defaults("fluid.reorderImages", {
layoutHandler: "fluid.gridLayoutHandler",
selectors: {
labelSource: ".flc-reorderer-imageTitle"
}
});
})(jQuery, fluid_1_4);
/*
Copyright 2008-2009 University of Cambridge
Copyright 2008-2009 University of Toronto
Copyright 2010-2011 OCAD University
Copyright 2010 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_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.registerNamespace("fluid.moduleLayout");
/**
* Calculate the location of the item and the column in which it resides.
* @return An object with column index and item index (within that column) properties.
* These indices are -1 if the item does not exist in the grid.
*/
// unsupported - NON-API function
fluid.moduleLayout.findColumnAndItemIndices = function (item, layout) {
return fluid.find(layout.columns,
function (column, colIndex) {
var index = $.inArray(item, column.elements);
return index === -1 ? undefined : {columnIndex: colIndex, itemIndex: index};
}, {columnIndex: -1, itemIndex: -1});
};
// unsupported - NON-API function
fluid.moduleLayout.findColIndex = function (item, layout) {
return fluid.find(layout.columns,
function (column, colIndex) {
return item === column.container ? colIndex : undefined;
}, -1);
};
/**
* Move an item within the layout object.
*/
// unsupported - NON-API function
fluid.moduleLayout.updateLayout = function (item, target, position, layout) {
item = fluid.unwrap(item);
target = fluid.unwrap(target);
var itemIndices = fluid.moduleLayout.findColumnAndItemIndices(item, layout);
layout.columns[itemIndices.columnIndex].elements.splice(itemIndices.itemIndex, 1);
var targetCol;
if (position === fluid.position.INSIDE) {
targetCol = layout.columns[fluid.moduleLayout.findColIndex(target, layout)].elements;
targetCol.splice(targetCol.length, 0, item);
} else {
var relativeItemIndices = fluid.moduleLayout.findColumnAndItemIndices(target, layout);
targetCol = layout.columns[relativeItemIndices.columnIndex].elements;
position = fluid.normalisePosition(position,
itemIndices.columnIndex === relativeItemIndices.columnIndex,
relativeItemIndices.itemIndex, itemIndices.itemIndex);
var relative = position === fluid.position.BEFORE ? 0 : 1;
targetCol.splice(relativeItemIndices.itemIndex + relative, 0, item);
}
};
/**
* Builds a layout object from a set of columns and modules.
* @param {jQuery} container
* @param {jQuery} columns
* @param {jQuery} portlets
*/
fluid.moduleLayout.layoutFromFlat = function (container, columns, portlets) {
var layout = {};
layout.container = container;
layout.columns = fluid.transform(columns,
function (column) {
return {
container: column,
elements: $.makeArray(portlets.filter(function () {
// is this a bug in filter? would have expected "this" to be 1st arg
return fluid.dom.isContainer(column, this);
}))
};
});
return layout;
};
/**
* Builds a layout object from a serialisable "layout" object consisting of id lists
*/
fluid.moduleLayout.layoutFromIds = function (idLayout) {
return {
container: fluid.byId(idLayout.id),
columns: fluid.transform(idLayout.columns, function (column) {
return {
container: fluid.byId(column.id),
elements: fluid.transform(column.children, fluid.byId)
};
})
};
};
/**
* Serializes the current layout into a structure of ids
*/
fluid.moduleLayout.layoutToIds = function (idLayout) {
return {
id: fluid.getId(idLayout.container),
columns: fluid.transform(idLayout.columns, function (column) {
return {
id: fluid.getId(column.container),
children: fluid.transform(column.elements, fluid.getId)
};
})
};
};
var defaultOnShowKeyboardDropWarning = function (item, dropWarning) {
if (dropWarning) {
var offset = $(item).offset();
dropWarning = $(dropWarning);
dropWarning.css("position", "absolute");
dropWarning.css("top", offset.top);
dropWarning.css("left", offset.left);
}
};
fluid.defaults(true, "fluid.moduleLayoutHandler",
{
orientation: fluid.orientation.VERTICAL,
containerRole: fluid.reorderer.roles.REGIONS,
selectablesTabindex: -1,
sentinelize: true
});
/**
* Module Layout Handler for reordering content modules.
*
* General movement guidelines:
*
* - Arrowing sideways will always go to the top (moveable) module in the column
* - Moving sideways will always move to the top available drop target in the column
* - Wrapping is not necessary at this first pass, but is ok
*/
fluid.moduleLayoutHandler = function (container, options, dropManager, dom) {
var that = {};
function computeLayout() {
var togo;
if (options.selectors.modules) {
togo = fluid.moduleLayout.layoutFromFlat(container, dom.locate("columns"), dom.locate("modules"));
}
if (!togo) {
var idLayout = fluid.get(options, "moduleLayout.layout");
fluid.moduleLayout.layoutFromIds(idLayout);
}
return togo;
}
var layout = computeLayout();
that.layout = layout;
function isLocked(item) {
var lockedModules = options.selectors.lockedModules ? dom.fastLocate("lockedModules") : [];
return $.inArray(item, lockedModules) !== -1;
}
that.getRelativePosition =
fluid.reorderer.relativeInfoGetter(options.orientation,
fluid.reorderer.WRAP_LOCKED_STRATEGY, fluid.reorderer.GEOMETRIC_STRATEGY,
dropManager, dom, options.disableWrap);
that.getGeometricInfo = function () {
var extents = [];
var togo = {extents: extents,
sentinelize: options.sentinelize};
togo.elementMapper = function (element) {
return isLocked(element) ? "locked" : null;
};
togo.elementIndexer = function (element) {
var indices = fluid.moduleLayout.findColumnAndItemIndices(element, that.layout);
return {
index: indices.itemIndex,
length: layout.columns[indices.columnIndex].elements.length,
moduleIndex: indices.columnIndex,
moduleLength: layout.columns.length
};
};
for (var col = 0; col < layout.columns.length; col++) {
var column = layout.columns[col];
var thisEls = {
orientation: options.orientation,
elements: $.makeArray(column.elements),
parentElement: column.container
};
// fluid.log("Geometry col " + col + " elements " + fluid.dumpEl(thisEls.elements) + " isLocked [" +
// fluid.transform(thisEls.elements, togo.elementMapper).join(", ") + "]");
extents.push(thisEls);
}
return togo;
};
function computeModules(all) {
return function () {
var modules = fluid.accumulate(layout.columns, function (column, list) {
return list.concat(column.elements); // note that concat will not work on a jQuery
}, []);
if (!all) {
fluid.remove_if(modules, isLocked);
}
return modules;
};
}
that.returnedOptions = {
selectors: {
movables: computeModules(false),
dropTargets: computeModules(false),
selectables: computeModules(true)
},
listeners: {
onMove: {
priority: "last",
listener: function (item, requestedPosition) {
fluid.moduleLayout.updateLayout(item, requestedPosition.element, requestedPosition.position, layout);
}
},
onRefresh: function () {
layout = computeLayout();
that.layout = layout;
},
"onShowKeyboardDropWarning.setPosition": defaultOnShowKeyboardDropWarning
}
};
that.getModel = function () {
return fluid.moduleLayout.layoutToIds(layout);
};
return that;
};
})(jQuery, fluid_1_4);
/*
Copyright 2008-2009 University of Cambridge
Copyright 2008-2009 University of Toronto
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_4: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_4 = fluid_1_4 || {};
(function ($, fluid) {
/**
* Simple way to create a layout reorderer.
* @param {selector} a jQueryable (selector, element, jQuery) for the layout container
* @param {Object} a map of selectors for columns and modules within the layout
* @param {Function} a function to be called when the order changes
* @param {Object} additional configuration options
*/
fluid.reorderLayout = function (container, userOptions) {
var assembleOptions = {
layoutHandler: "fluid.moduleLayoutHandler",
selectors: {
columns: ".flc-reorderer-column",
modules: ".flc-reorderer-module"
}
};
var options = $.extend(true, assembleOptions, userOptions);
return fluid.reorderer(container, options);
};
})(jQuery, fluid_1_4);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment