From f67fd327f679eebf3ffd9d47984514b06a32d17e Mon Sep 17 00:00:00 2001 From: Antranig Basman Date: Wed, 8 Jul 2015 19:35:25 +0100 Subject: [PATCH] Added (barely) working version of code sample for community meeting --- ppig-2015/example/js/example.js | 65 + ppig-2015/example/lib/binder/binder.js | 69 + ppig-2015/example/lib/infusion/DataBinding.js | 1358 ++ ppig-2015/example/lib/infusion/Fluid.js | 2612 ++++ .../example/lib/infusion/FluidDOMUtilities.js | 116 + .../example/lib/infusion/FluidDebugging.js | 298 + .../example/lib/infusion/FluidDocument.js | 177 + ppig-2015/example/lib/infusion/FluidIoC.js | 2220 ++++ .../example/lib/infusion/FluidPromises.js | 257 + .../example/lib/infusion/FluidRequests.js | 437 + ppig-2015/example/lib/infusion/FluidView.js | 663 + .../lib/infusion/FluidViewDebugging.js | 649 + .../example/lib/infusion/JavaProperties.js | 117 + .../lib/infusion/ModelTransformation.js | 665 + .../infusion/ModelTransformationTransforms.js | 676 + .../lib/infusion/jquery.keyboard-a11y.js | 623 + .../example/lib/infusion/jquery.standalone.js | 173 + ppig-2015/example/lib/jQuery/jquery.js | 10337 ++++++++++++++++ ppig-2015/example/temperature.html | 42 + 19 files changed, 21554 insertions(+) create mode 100644 ppig-2015/example/js/example.js create mode 100644 ppig-2015/example/lib/binder/binder.js create mode 100644 ppig-2015/example/lib/infusion/DataBinding.js create mode 100644 ppig-2015/example/lib/infusion/Fluid.js create mode 100644 ppig-2015/example/lib/infusion/FluidDOMUtilities.js create mode 100644 ppig-2015/example/lib/infusion/FluidDebugging.js create mode 100644 ppig-2015/example/lib/infusion/FluidDocument.js create mode 100644 ppig-2015/example/lib/infusion/FluidIoC.js create mode 100644 ppig-2015/example/lib/infusion/FluidPromises.js create mode 100644 ppig-2015/example/lib/infusion/FluidRequests.js create mode 100644 ppig-2015/example/lib/infusion/FluidView.js create mode 100644 ppig-2015/example/lib/infusion/FluidViewDebugging.js create mode 100644 ppig-2015/example/lib/infusion/JavaProperties.js create mode 100644 ppig-2015/example/lib/infusion/ModelTransformation.js create mode 100644 ppig-2015/example/lib/infusion/ModelTransformationTransforms.js create mode 100644 ppig-2015/example/lib/infusion/jquery.keyboard-a11y.js create mode 100644 ppig-2015/example/lib/infusion/jquery.standalone.js create mode 100644 ppig-2015/example/lib/jQuery/jquery.js create mode 100644 ppig-2015/example/temperature.html diff --git a/ppig-2015/example/js/example.js b/ppig-2015/example/js/example.js new file mode 100644 index 0000000..69296c6 --- /dev/null +++ b/ppig-2015/example/js/example.js @@ -0,0 +1,65 @@ +// User A's original app + +fluid.defaults("examples.simpleRelay", { + gradeNames: "fluid.component", + components: { + celsiusHolder: { + type: "fluid.modelComponent", + options: { + model: { + celsius: 22 + } + } + }, + fahrenheitHolder: { + type: "fluid.modelComponent", + options: { + modelRelay: { + source: "{celsiusHolder}.model.celsius", // IoC reference to celsius model field in the other component + target: "{that}.model.fahrenheit", // this reference could be shortened to just "fahrenheit" + singleTransform: { + type: "fluid.transforms.linearScale", + factor: 9/5, + offset: 32 + } + } + } + } + } +}); + +// User D's view app +fluid.defaults("examples.relayApp", { + gradeNames: ["gpii.templates.binder", "fluid.viewComponent", "examples.simpleRelay"], + model: { + celsius: "{celsiusHolder}.model.celsius", + fahrenheit: "{fahrenheitHolder}.model.fahrenheit" + }, + selectors: { + celsius: "#celsius", + fahrenheit: "#fahrenheit" + }, + bindings: { + celsius: "celsius", + fahrenheit: "fahrenheit" + } +}); + +// User A' designates a "decorated variety" of our simpleRelay type which will log messages on model changes +fluid.defaults("examples.reportingRelay", { + distributeOptions: [{ // options distributions route options to the subcomponents in the tree compactly + record: { + funcName: "fluid.log", + args: ["Celsius value has changed to", "{change}.value"] + }, + target: "{that celsiusHolder}.options.modelListeners.celsius" + }, { + record: { + funcName: "fluid.log", + args: ["Fahrenheit value has changed to", "{change}.value"] + }, + target: "{that fahrenheitHolder}.options.modelListeners.fahrenheit" + }] +}); + +fluid.setLogging(true); \ No newline at end of file diff --git a/ppig-2015/example/lib/binder/binder.js b/ppig-2015/example/lib/binder/binder.js new file mode 100644 index 0000000..b719da1 --- /dev/null +++ b/ppig-2015/example/lib/binder/binder.js @@ -0,0 +1,69 @@ + var gpii = fluid.registerNamespace("gpii"); + + fluid.defaults("gpii.templates.binder", { + events: { + onDomBind: null + }, + listeners: { + "onCreate.bindDom": { + func: "{that}.events.onDomBind.fire" + }, + "onDomBind.applyBinding": { + funcName: "gpii.templates.binder.applyBinding" + } + }, + }); + + gpii.templates.binder.refreshDom = function (that) { + // Adapted from: https://github.com/fluid-project/infusion/blob/master/src/framework/preferences/js/Panels.js#L147 + var userJQuery = that.container.constructor; + that.container = userJQuery(that.container.selector, that.container.context); + fluid.initDomBinder(that, that.options.selectors); + that.events.onDomBind.fire(that); + }; + + // The main function to create bindings between markup and model elements. See above for usage details. + gpii.templates.binder.applyBinding = function (that) { + var bindings = that.options.bindings; + fluid.each(bindings, function (value, key) { + var path = typeof value === "string" ? value : value.path; + var selector = typeof value === "string" ? key : value.selector; + var element = that.locate(selector); + + // Update the model when the form changes + element.change(function () { + var elementValue = Number(fluid.value(element)); // TODO: botched conversion + fluid.log("Changing model at path " + path + " to value " + elementValue + " based on element update."); + that.applier.change(path, elementValue); + }); + + // Update the form elements when the model changes + that.applier.modelChanged.addListener(path, function (change) { + + // This syntax is required until Fluid is updated per the following pull request: + // + // https://github.com/fluid-project/infusion/pull/591 + // + // For a description of a similar problem caused by the same behavior, see: + // + // https://issues.fluidproject.org/browse/FLUID-4739 + // + var value = change[path] ? change[path] : change; + fluid.log("Changing value at path " + path + " to " + value + " based on model update."); + fluid.value(element, value); + }); + + // If we have model data initially, update the form. Model values win out over markup. + var initialModelValue = fluid.get(that.model, path); + if (initialModelValue) { + fluid.value(element, initialModelValue); + } + // If we have no model data, but there are defaults in the markup, using them to update the model. + else { + var initialFormValue = fluid.value(element); + if (initialFormValue) { + that.applier.change(path, initialFormValue); + } + } + }); + }; \ No newline at end of file diff --git a/ppig-2015/example/lib/infusion/DataBinding.js b/ppig-2015/example/lib/infusion/DataBinding.js new file mode 100644 index 0000000..233174e --- /dev/null +++ b/ppig-2015/example/lib/infusion/DataBinding.js @@ -0,0 +1,1358 @@ +/* +Copyright 2008-2010 University of Cambridge +Copyright 2008-2009 University of Toronto +Copyright 2010-2011 Lucendo Development Ltd. +Copyright 2010-2014 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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + /** NOTE: The contents of this file are by default NOT PART OF THE PUBLIC FLUID API unless explicitly annotated before the function **/ + + /** MODEL ACCESSOR ENGINE **/ + + /** Standard strategies for resolving path segments **/ + + fluid.model.makeEnvironmentStrategy = function (environment) { + return function (root, segment, index) { + return index === 0 && environment[segment] ? + environment[segment] : undefined; + }; + }; + + fluid.model.defaultCreatorStrategy = function (root, segment) { + if (root[segment] === undefined) { + root[segment] = {}; + return root[segment]; + } + }; + + fluid.model.defaultFetchStrategy = function (root, segment) { + return root[segment]; + }; + + fluid.model.funcResolverStrategy = function (root, segment) { + if (root.resolvePathSegment) { + return root.resolvePathSegment(segment); + } + }; + + fluid.model.traverseWithStrategy = function (root, segs, initPos, config, uncess) { + var strategies = config.strategies; + var limit = segs.length - uncess; + for (var i = initPos; i < limit; ++i) { + if (!root) { + return root; + } + var accepted; + for (var j = 0; j < strategies.length; ++ j) { + accepted = strategies[j](root, segs[i], i + 1, segs); + if (accepted !== undefined) { + break; // May now short-circuit with stateless strategies + } + } + if (accepted === fluid.NO_VALUE) { + accepted = undefined; + } + root = accepted; + } + return root; + }; + + /** Returns both the value and the path of the value held at the supplied EL path **/ + fluid.model.getValueAndSegments = function (root, EL, config, initSegs) { + return fluid.model.accessWithStrategy(root, EL, fluid.NO_VALUE, config, initSegs, true); + }; + + // Very lightweight remnant of trundler, only used in resolvers + fluid.model.makeTrundler = function (config) { + return function (valueSeg, EL) { + return fluid.model.getValueAndSegments(valueSeg.root, EL, config, valueSeg.segs); + }; + }; + + fluid.model.getWithStrategy = function (root, EL, config, initSegs) { + return fluid.model.accessWithStrategy(root, EL, fluid.NO_VALUE, config, initSegs); + }; + + fluid.model.setWithStrategy = function (root, EL, newValue, config, initSegs) { + fluid.model.accessWithStrategy(root, EL, newValue, config, initSegs); + }; + + fluid.model.accessWithStrategy = function (root, EL, newValue, config, initSegs, returnSegs) { + // This function is written in this unfortunate style largely for efficiency reasons. In many cases + // it should be capable of running with 0 allocations (EL is preparsed, initSegs is empty) + if (!fluid.isPrimitive(EL) && !fluid.isArrayable(EL)) { + var key = EL.type || "default"; + var resolver = config.resolvers[key]; + if (!resolver) { + fluid.fail("Unable to find resolver of type " + key); + } + var trundler = fluid.model.makeTrundler(config); // very lightweight trundler for resolvers + var valueSeg = {root: root, segs: initSegs}; + valueSeg = resolver(valueSeg, EL, trundler); + if (EL.path && valueSeg) { // every resolver supports this piece of output resolution + valueSeg = trundler(valueSeg, EL.path); + } + return returnSegs ? valueSeg : (valueSeg ? valueSeg.root : undefined); + } + else { + return fluid.model.accessImpl(root, EL, newValue, config, initSegs, returnSegs, fluid.model.traverseWithStrategy); + } + }; + + // Implementation notes: The EL path manipulation utilities here are equivalents of the simpler ones + // that are provided in Fluid.js and elsewhere - they apply escaping rules to parse characters . + // as \. and \ as \\ - allowing us to process member names containing periods. These versions are mostly + // in use within model machinery, whereas the cheaper versions based on String.split(".") are mostly used + // within the IoC machinery. + // Performance testing in early 2015 suggests that modern browsers now allow these to execute slightly faster + // than the equivalent machinery written using complex regexps - therefore they will continue to be maintained + // here. However, there is still a significant performance gap with respect to the performance of String.split(".") + // especially on Chrome, so we will continue to insist that component member names do not contain a "." character + // for the time being. + // See http://jsperf.com/parsing-escaped-el for some experiments + + fluid.registerNamespace("fluid.pathUtil"); + + fluid.pathUtil.getPathSegmentImpl = function (accept, path, i) { + var segment = null; + 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) { + segment += c; + } + } + } + if (segment !== null) { + accept[0] = segment; + } + return i; + }; + + var globalAccept = []; // TODO: reentrancy risk here. This holder is here to allow parseEL to make two returns without an allocation. + + /** A version of fluid.model.parseEL that apples escaping rules - this allows path segments + * to contain period characters . - characters "\" and "}" will also be escaped. WARNING - + * this current implementation is EXTREMELY slow compared to fluid.model.parseEL and should + * not be used in performance-sensitive applications */ + // supported, PUBLIC API function + fluid.pathUtil.parseEL = function (path) { + var togo = []; + var index = 0; + var limit = path.length; + while (index < limit) { + var firstdot = fluid.pathUtil.getPathSegmentImpl(globalAccept, path, index); + togo.push(globalAccept[0]); + index = firstdot + 1; + } + return togo; + }; + + // supported, PUBLIC API function + fluid.pathUtil.composeSegment = function (prefix, toappend) { + toappend = toappend.toString(); + for (var i = 0; i < toappend.length; ++i) { + var c = toappend.charAt(i); + if (c === "." || c === "\\" || c === "}") { + prefix += "\\"; + } + prefix += c; + } + return prefix; + }; + + /** Escapes a single path segment by replacing any character ".", "\" or "}" with + * itself prepended by \ + */ + // supported, PUBLIC API function + fluid.pathUtil.escapeSegment = function (segment) { + return fluid.pathUtil.composeSegment("", segment); + }; + + /** + * 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. + */ + // supported, PUBLIC API function + fluid.pathUtil.composePath = function (prefix, suffix) { + if (prefix.length !== 0) { + prefix += "."; + } + return fluid.pathUtil.composeSegment(prefix, suffix); + }; + + /** + * Compose a set of path segments supplied as arguments into an escaped EL expression. Escaped version + * of fluid.model.composeSegments + */ + + // supported, PUBLIC API function + fluid.pathUtil.composeSegments = function () { + var path = ""; + for (var i = 0; i < arguments.length; ++ i) { + path = fluid.pathUtil.composePath(path, arguments[i]); + } + return path; + }; + + /** Helpful utility for use in resolvers - matches a path which has already been + * parsed into segments **/ + + fluid.pathUtil.matchSegments = function (toMatch, segs, start, end) { + if (end - start !== toMatch.length) { + return false; + } + for (var i = start; i < end; ++ i) { + if (segs[i] !== toMatch[i - start]) { + return false; + } + } + return true; + }; + + fluid.model.unescapedParser = { + parse: fluid.model.parseEL, + compose: fluid.model.composeSegments + }; + + // supported, PUBLIC API record + fluid.model.defaultGetConfig = { + parser: fluid.model.unescapedParser, + strategies: [fluid.model.funcResolverStrategy, fluid.model.defaultFetchStrategy] + }; + + // supported, PUBLIC API record + fluid.model.defaultSetConfig = { + parser: fluid.model.unescapedParser, + strategies: [fluid.model.funcResolverStrategy, fluid.model.defaultFetchStrategy, fluid.model.defaultCreatorStrategy] + }; + + fluid.model.escapedParser = { + parse: fluid.pathUtil.parseEL, + compose: fluid.pathUtil.composeSegments + }; + + // supported, PUBLIC API record + fluid.model.escapedGetConfig = { + parser: fluid.model.escapedParser, + strategies: [fluid.model.defaultFetchStrategy] + }; + + // supported, PUBLIC API record + fluid.model.escapedSetConfig = { + parser: fluid.model.escapedParser, + strategies: [fluid.model.defaultFetchStrategy, fluid.model.defaultCreatorStrategy] + }; + + /** MODEL COMPONENT HIERARCHY AND RELAY SYSTEM **/ + + fluid.initRelayModel = function (that) { + fluid.deenlistModelComponent(that); + return that.model; + }; + + // TODO: This utility compensates for our lack of control over "wave of explosions" initialisation - we may + // catch a model when it is apparently "completely initialised" and that's the best we can do, since we have + // missed its own initial transaction + + fluid.isModelComplete = function (that) { + return "model" in that && that.model !== fluid.inEvaluationMarker; + }; + + // Enlist this model component as part of the "initial transaction" wave - note that "special transaction" init + // is indexed by component, not by applier, and has special record type (complete + initModel), not transaction + fluid.enlistModelComponent = function (that) { + var instantiator = fluid.getInstantiator(that); + var enlist = instantiator.modelTransactions.init[that.id]; + if (!enlist) { + enlist = { + that: that, + applier: fluid.getForComponent(that, "applier"), // required for FLUID-5504 even though currently unused + complete: fluid.isModelComplete(that) + }; + instantiator.modelTransactions.init[that.id] = enlist; + } + return enlist; + }; + + fluid.clearTransactions = function () { + var instantiator = fluid.globalInstantiator; + fluid.clear(instantiator.modelTransactions); + instantiator.modelTransactions.init = {}; + }; + + fluid.failureEvent.addListener(fluid.clearTransactions, "clearTransactions", "before:fail"); + + // Utility to coordinate with our crude "oscillation prevention system" which limits each link to 2 updates (presumably + // in opposite directions). In the case of the initial transaction, we need to reset the count given that genuine + // changes are arising in the system with each new enlisted model. TODO: if we ever get users operating their own + // transactions, think of a way to incorporate this into that workflow + fluid.clearLinkCounts = function (transRec, relaysAlso) { + fluid.each(transRec, function (value, key) { + if (typeof(value) === "number") { + transRec[key] = 0; + } else if (relaysAlso && value.options && typeof(value.relayCount) === "number") { + value.relayCount = 0; + } + }); + }; + + fluid.sortCompleteLast = function (reca, recb) { + return (reca.completeOnInit ? 1 : 0) - (recb.completeOnInit ? 1 : 0); + }; + + // Operate all coordinated transactions by bringing models to their respective initial values, and then commit them all + fluid.operateInitialTransaction = function (instantiator, mrec) { + var transId = fluid.allocateGuid(); + var transRec = fluid.getModelTransactionRec(instantiator, transId); + var transac; + var transacs = fluid.transform(mrec, function (recel) { + transac = recel.that.applier.initiate("init", transId); + transRec[recel.that.applier.applierId] = {transaction: transac}; + return transac; + }); + // TODO: This sort has very little effect in any current test (can be replaced by no-op - see FLUID-5339) - but + // at least can't be performed in reverse order ("FLUID-3674 event coordination test" will fail) - need more cases + var recs = fluid.values(mrec).sort(fluid.sortCompleteLast); + fluid.each(recs, function (recel) { + var that = recel.that; + var transac = transacs[that.id]; + if (recel.completeOnInit) { + fluid.initModelEvent(that, that.applier, transac, that.applier.changeListeners.listeners); + } else { + fluid.each(recel.initModels, function (initModel) { + transac.fireChangeRequest({type: "ADD", segs: [], value: initModel}); + fluid.clearLinkCounts(transRec, true); + }); + } + var shadow = fluid.shadowForComponent(that); + shadow.modelComplete = true; // technically this is a little early, but this flag is only read in fluid.connectModelRelay + }); + + transac.commit(); // committing one representative transaction will commit them all + }; + + // This modelComponent has now concluded initialisation - commit its initialisation transaction if it is the last such in the wave + fluid.deenlistModelComponent = function (that) { + var instantiator = fluid.getInstantiator(that); + var mrec = instantiator.modelTransactions.init; + if (!mrec[that.id]) { // avoid double evaluation through currently hacked "members" implementation + return; + } + that.model = undefined; // Abuse of the ginger system - in fact it is "currently in evaluation" - we need to return a proper initial model value even if no init occurred yet + mrec[that.id].complete = true; // flag means - "complete as in ready to participate in this transaction" + var incomplete = fluid.find_if(mrec, function (recel) { + return recel.complete !== true; + }); + if (!incomplete) { + fluid.operateInitialTransaction(instantiator, mrec); + // NB: Don't call fluid.concludeTransaction since "init" is not a standard record - this occurs in commitRelays for the corresponding genuine record as usual + instantiator.modelTransactions.init = {}; + } + }; + + fluid.transformToAdapter = function (transform, targetPath) { + var basedTransform = {}; + basedTransform[targetPath] = transform; + return function (trans, newValue /*, sourceSegs, targetSegs */) { + // TODO: More efficient model that can only run invalidated portion of transform (need to access changeMap of source transaction) + fluid.model.transformWithRules(newValue, basedTransform, {finalApplier: trans}); + }; + }; + + fluid.parseModelReference = function (that, ref) { + var parsed = fluid.parseContextReference(ref); + parsed.segs = that.applier.parseEL(parsed.path); + return parsed; + }; + + fluid.parseValidModelReference = function (that, name, ref) { + var reject = function (message) { + fluid.fail("Error in " + name + ": " + ref + message); + }; + var parsed, target; + if (ref.charAt(0) === "{") { + parsed = fluid.parseModelReference(that, ref); + if (parsed.segs[0] !== "model") { + reject(" must be a reference into a component model beginning with \"model\""); + } else { + parsed.modelSegs = parsed.segs.slice(1); + delete parsed.path; + } + target = fluid.resolveContext(parsed.context, that); + if (!target) { + reject(" must be a reference to an existing component"); + } + } else { + target = that; + parsed = { + path: ref, + modelSegs: that.applier.parseEL(ref) + }; + } + if (!target.applier) { + fluid.getForComponent(target, ["applier"]); + } + if (!target.applier) { + reject(" must be a reference to a component with a ChangeApplier (descended from fluid.modelComponent)"); + } + parsed.that = target; + parsed.applier = target.applier; + if (!parsed.path) { // ChangeToApplicable amongst others rely on this + parsed.path = target.applier.composeSegments.apply(null, parsed.modelSegs); + } + return parsed; + }; + + // Gets global record for a particular transaction id - looks up applier id to transaction, + // as well as looking up source id (linkId in below) to count/true + fluid.getModelTransactionRec = function (instantiator, transId) { + if (!transId) { + fluid.fail("Cannot get transaction record without transaction id"); + } + if (!instantiator) { + return null; + } + var transRec = instantiator.modelTransactions[transId]; + if (!transRec) { + transRec = instantiator.modelTransactions[transId] = { + externalChanges: {} // index by applierId to changePath to listener record + }; + } + return transRec; + }; + + fluid.recordChangeListener = function (component, applier, sourceListener) { + var shadow = fluid.shadowForComponent(component); + fluid.recordListener(applier.modelChanged, sourceListener, shadow); + }; + + // Configure this parameter to tweak the number of relays the model will attempt per transaction before bailing out with an error + fluid.relayRecursionBailout = 100; + + // Used with various arg combinations from different sources. For standard "implicit relay" or fully lensed relay, + // the first 4 args will be set, and "options" will be empty + + // For a model-dependent relay, this will be used in two halves - firstly, all of the model + // sources will bind to the relay transform document itself. In this case the argument "targetApplier" within "options" will be set. + // In this case, the component known as "target" is really the source - it is a component reference discovered by parsing the + // relay document. + + // Secondly, the relay itself will schedule an invalidation (as if receiving change to "*" of its source - which may in most + // cases actually be empty) and play through its transducer. "Source" component itself is never empty, since it is used for listener + // degistration on destruction (check this is correct for external model relay). However, "sourceSegs" may be empty in the case + // there is no "source" component registered for the link. This change is played in a "half-transactional" way - that is, we wait + // for all other changes in the system to settle before playing the relay document, in order to minimise the chances of multiple + // firing and corruption. This is done via the "preCommit" hook registered at top level in establishModelRelay. This listener + // is transactional but it does not require the transaction to conclude in order to fire - it may be reused as many times as + // required within the "overall" transaction whilst genuine (external) changes continue to arrive. + + fluid.registerDirectChangeRelay = function (target, targetSegs, source, sourceSegs, linkId, transducer, options) { + var instantiator = fluid.getInstantiator(target); + var targetApplier = options.targetApplier || target.applier; // implies the target is a relay document + var sourceApplier = options.sourceApplier || source.applier; // implies the source is a relay document - listener will be transactional + var applierId = targetApplier.applierId; + targetSegs = fluid.makeArray(targetSegs); + sourceSegs = sourceSegs ? fluid.makeArray(sourceSegs) : sourceSegs; // take copies since originals will be trashed + var sourceListener = function (newValue, oldValue, path, changeRequest, trans, applier) { + var transId = trans.id; + var transRec = fluid.getModelTransactionRec(instantiator, transId); + if (applier && trans && !transRec[applier.applierId]) { // don't trash existing record which may contain "options" (FLUID-5397) + transRec[applier.applierId] = {transaction: trans}; // enlist the outer user's original transaction + } + var existing = transRec[applierId]; + transRec[linkId] = transRec[linkId] || 0; + // Crude "oscillation prevention" system limits each link to maximum of 2 operations per cycle (presumably in opposite directions) + var relay = true; // TODO: See FLUID-5303 - we currently disable this check entirely to solve FLUID-5293 - perhaps we might remove link counts entirely + if (relay) { + ++transRec[linkId]; + if (transRec[linkId] > fluid.relayRecursionBailout) { + fluid.fail("Error in model relay specification at component ", target, " - operated more than " + fluid.relayRecursionBailout + " relays without model value settling - current model contents are ", trans.newHolder.model); + } + if (!existing) { + var newTrans = targetApplier.initiate("relay", transId); // non-top-level transaction will defeat postCommit + existing = transRec[applierId] = {transaction: newTrans, relayCount: 0, options: options}; + } + if (transducer && !options.targetApplier) { + // TODO: This is just for safety but is still unusual and now abused. The transducer doesn't need the "newValue" since all the transform information + // has been baked into the transform document itself. However, we now rely on this special signalling value to make sure we regenerate transforms in + // the "forwardAdapter" + transducer(existing.transaction, options.sourceApplier ? undefined : newValue, sourceSegs, targetSegs); + } else if (newValue !== undefined) { + existing.transaction.fireChangeRequest({type: "ADD", segs: targetSegs, value: newValue}); + } + } + }; + sourceListener.relayListenerId = fluid.allocateGuid(); + if (sourceSegs) { + fluid.log(fluid.logLevel.TRACE, "Adding relay listener with id " + sourceListener.relayListenerId + " to source applier with id " + + sourceApplier.applierId + " from target applier with id " + applierId + " for target component with id " + target.id); + sourceApplier.modelChanged.addListener({ + isRelay: true, + segs: sourceSegs, + transactional: options.transactional + }, sourceListener); + } + if (source) { // TODO - we actually may require to register on THREE sources in the case modelRelay is attached to a + // component which is neither source nor target. Note there will be problems if source, say, is destroyed and recreated, + // and holder is not - relay will in that case be lost. Need to integrate relay expressions with IoCSS. + fluid.recordChangeListener(source, sourceApplier, sourceListener); + if (target !== source) { + fluid.recordChangeListener(target, sourceApplier, sourceListener); + } + } + }; + + // When called during parsing a contextualised model relay document, these arguments are reversed - "source" refers to the + // current component, and "target" refers successively to the various "source" components. + // "options" will be transformPackage + fluid.connectModelRelay = function (source, sourceSegs, target, targetSegs, options) { + var linkId = fluid.allocateGuid(); + function enlistComponent(component) { + var enlist = fluid.enlistModelComponent(component); + + if (enlist.complete) { + var shadow = fluid.shadowForComponent(component); + if (shadow.modelComplete) { + enlist.completeOnInit = true; + } + } + } + enlistComponent(target); + enlistComponent(source); // role of "source" and "target" may have been swapped in a modelRelay document + + if (options.update) { // it is a call via parseImplicitRelay for a relay document + if (options.targetApplier) { + // register changes from the model onto changes to the model relay document + fluid.registerDirectChangeRelay(source, sourceSegs, target, targetSegs, linkId, null, { + transactional: false, + targetApplier: options.targetApplier, + update: options.update + }); + } else { + // if parsing a contextualised MR, skip the "orthogonal" registration - instead + // register the "half-transactional" listener which binds changes from the relay itself onto the target + fluid.registerDirectChangeRelay(target, targetSegs, source, [], linkId+"-transform", options.forwardAdapter, {transactional: true, sourceApplier: options.forwardApplier}); + } + } else { // more efficient branch where relay is uncontextualised + fluid.registerDirectChangeRelay(target, targetSegs, source, sourceSegs, linkId, options.forwardAdapter, {transactional: false}); + if (sourceSegs) { + fluid.registerDirectChangeRelay(source, sourceSegs, target, targetSegs, linkId, options.backwardAdapter, {transactional: false}); + } + } + }; + + fluid.model.guardedAdapter = function (componentThat, cond, func, args) { + // TODO: We can't use fluid.isModelComplete here because of the broken half-transactional system - it may appear that model has arrived halfway through init transaction + var instantiator = fluid.getInstantiator(componentThat); + var enlist = instantiator.modelTransactions.init[componentThat.id]; + var condValue = cond[enlist ? "init" : "live"]; + if (condValue) { + func.apply(null, args); + } + }; + + fluid.makeTransformPackage = function (componentThat, transform, sourcePath, targetPath, forwardCond, backwardCond) { + var that = { + forwardHolder: {model: transform}, + backwardHolder: {model: null} + }; + that.generateAdapters = function (trans) { + // can't commit "half-transaction" or events will fire - violate encapsulation in this way + that.forwardAdapterImpl = fluid.transformToAdapter(trans ? trans.newHolder.model : that.forwardHolder.model, targetPath); + if (sourcePath !== null) { + that.backwardHolder.model = fluid.model.transform.invertConfiguration(transform); + that.backwardAdapterImpl = fluid.transformToAdapter(that.backwardHolder.model, sourcePath); + } + }; + that.forwardAdapter = function (transaction, newValue) { // create a stable function reference for this possibly changing adapter + if (newValue === undefined) { + that.generateAdapters(); // TODO: Quick fix for incorrect scheduling of invalidation/transducing + // "it so happens" that fluid.registerDirectChangeRelay invokes us with empty newValue in the case of invalidation -> transduction + } + fluid.model.guardedAdapter(componentThat, forwardCond, that.forwardAdapterImpl, arguments); + }; + // fired from fluid.model.updateRelays via invalidator event + that.runTransform = function (trans) { + trans.commit(); // this will reach the special "half-transactional listener" registered in fluid.connectModelRelay, + // branch with options.targetApplier - by committing the transaction, we update the relay document in bulk and then cause + // it to execute (via "transducer") + trans.reset(); + }; + that.forwardApplier = fluid.makeHolderChangeApplier(that.forwardHolder); + that.forwardApplier.isRelayApplier = true; // special annotation so these can be discovered in the transaction record + that.invalidator = fluid.makeEventFirer({name: "Invalidator for model relay with applier " + that.forwardApplier.applierId}); + if (sourcePath !== null) { + that.backwardApplier = fluid.makeHolderChangeApplier(that.backwardHolder); + that.backwardAdapter = function () { + fluid.model.guardedAdapter(componentThat, backwardCond, that.backwardAdapterImpl, arguments); + }; + } + that.update = that.invalidator.fire; // necessary so that both routes to fluid.connectModelRelay from here hit the first branch + var implicitOptions = { + targetApplier: that.forwardApplier, // this special field identifies us to fluid.connectModelRelay + update: that.update, + refCount: 0 + }; + that.forwardHolder.model = fluid.parseImplicitRelay(componentThat, transform, [], implicitOptions); + that.refCount = implicitOptions.refCount; + that.generateAdapters(); + that.invalidator.addListener(that.generateAdapters); + that.invalidator.addListener(that.runTransform); + return that; + }; + + fluid.singleTransformToFull = function (singleTransform) { + var withPath = $.extend(true, {valuePath: ""}, singleTransform); + return { + "": { + transform: withPath + } + }; + }; + + fluid.model.relayConditions = { + initOnly: {init: true, live: false}, + liveOnly: {init: false, live: true}, + never: {init: false, live: false}, + always: {init: true, live: true} + }; + + fluid.model.parseRelayCondition = function (condition) { + return fluid.model.relayConditions[condition || "always"]; + }; + + fluid.parseModelRelay = function (that, mrrec) { + var parsedSource = mrrec.source ? fluid.parseValidModelReference(that, "modelRelay record member \"source\"", mrrec.source) : + {path: null, modelSegs: null}; + var parsedTarget = fluid.parseValidModelReference(that, "modelRelay record member \"target\"", mrrec.target); + + var transform = mrrec.singleTransform ? fluid.singleTransformToFull(mrrec.singleTransform) : mrrec.transform; + if (!transform) { + fluid.fail("Cannot parse modelRelay record without element \"singleTransform\" or \"transform\":", mrrec); + } + var forwardCond = fluid.model.parseRelayCondition(mrrec.forward), backwardCond = fluid.model.parseRelayCondition(mrrec.backward); + var transformPackage = fluid.makeTransformPackage(that, transform, parsedSource.path, parsedTarget.path, forwardCond, backwardCond); + if (transformPackage.refCount === 0) { + // This first call binds changes emitted from the relay ends to each other, synchronously + fluid.connectModelRelay(parsedSource.that || that, parsedSource.modelSegs, parsedTarget.that, parsedTarget.modelSegs, { + forwardAdapter: transformPackage.forwardAdapter, + backwardAdapter: transformPackage.backwardAdapter + }); + } else { + // This second call binds changes emitted from the relay document itself onto the relay ends (using the "half-transactional system") + fluid.connectModelRelay(parsedSource.that || that, parsedSource.modelSegs, parsedTarget.that, parsedTarget.modelSegs, transformPackage); + } + }; + + fluid.parseImplicitRelay = function (that, modelRec, segs, options) { + var value; + if (typeof(modelRec) === "string" && modelRec.charAt(0) === "{") { + var parsed = fluid.parseModelReference(that, modelRec); + var target = fluid.resolveContext(parsed.context, that); + if (parsed.segs[0] === "model") { + var modelSegs = parsed.segs.slice(1); + ++options.refCount; + fluid.connectModelRelay(that, segs, target, modelSegs, options); + } else { + value = fluid.getForComponent(target, parsed.segs); + } + } else if (fluid.isPrimitive(modelRec) || !fluid.isPlainObject(modelRec)) { + value = modelRec; + } else if (modelRec.expander && fluid.isPlainObject(modelRec.expander)) { + value = fluid.expandOptions(modelRec, that); + } else { + value = fluid.freshContainer(modelRec); + fluid.each(modelRec, function (innerValue, key) { + segs.push(key); + var innerTrans = fluid.parseImplicitRelay(that, innerValue, segs, options); + if (innerTrans !== undefined) { + value[key] = innerTrans; + } + segs.pop(); + }); + } + return value; + }; + + + // Conclude the transaction by firing to all external listeners in priority order + fluid.model.notifyExternal = function (transRec) { + var allChanges = transRec ? fluid.values(transRec.externalChanges) : []; + fluid.sortByPriority(allChanges); + for (var i = 0; i < allChanges.length; ++ i) { + var change = allChanges[i]; + var targetApplier = change.args[5]; // NOTE: This argument gets here via fluid.model.storeExternalChange from fluid.notifyModelChanges + if (!targetApplier.destroyed) { // 3rd point of guarding for FLUID-5592 + change.listener.apply(null, change.args); + } + } + fluid.clearLinkCounts(transRec, true); // "options" structures for relayCount are aliased + }; + + fluid.model.commitRelays = function (instantiator, transactionId) { + var transRec = instantiator.modelTransactions[transactionId]; + fluid.each(transRec, function (transEl) { + // EXPLAIN: This must commit ALL current transactions, not just those for relays - why? + if (transEl.transaction) { // some entries are links + transEl.transaction.commit("relay"); + transEl.transaction.reset(); + } + }); + }; + + fluid.model.updateRelays = function (instantiator, transactionId) { + var transRec = instantiator.modelTransactions[transactionId]; + var updates = 0; + fluid.each(transRec, function (transEl) { + // TODO: integrate the "source" if any into this computation, and fire the relay if it has changed - perhaps by adding a listener + // to it that updates changeRecord.changes (assuming we can find it) + if (transEl.options && transEl.transaction && transEl.transaction.changeRecord.changes > 0 && transEl.relayCount < 2 && transEl.options.update) { + transEl.relayCount++; + fluid.clearLinkCounts(transRec); + transEl.options.update(transEl.transaction, transRec); + ++updates; + } + }); + return updates; + }; + + fluid.establishModelRelay = function (that, optionsModel, optionsML, optionsMR, applier) { + fluid.mergeModelListeners(that, optionsML); + + var enlist = fluid.enlistModelComponent(that); + fluid.each(optionsMR, function (mrrec) { + fluid.parseModelRelay(that, mrrec); + }); + + var initModels = fluid.transform(optionsModel, function (modelRec) { + return fluid.parseImplicitRelay(that, modelRec, [], {refCount: 0}); + }); + enlist.initModels = initModels; + + var instantiator = fluid.getInstantiator(that); + + function updateRelays(transaction) { + while (fluid.model.updateRelays(instantiator, transaction.id) > 0){} + } + + function commitRelays(transaction, applier, code) { + if (code !== "relay") { // don't commit relays if this commit is already a relay commit + fluid.model.commitRelays(instantiator, transaction.id); + } + } + + function concludeTransaction(transaction, applier, code) { + if (code !== "relay") { + fluid.model.notifyExternal(instantiator.modelTransactions[transaction.id]); + delete instantiator.modelTransactions[transaction.id]; + } + } + + applier.preCommit.addListener(updateRelays); + applier.preCommit.addListener(commitRelays); + applier.postCommit.addListener(concludeTransaction); + + return null; + }; + + // supported, PUBLIC API grade + fluid.defaults("fluid.modelComponent", { + gradeNames: ["fluid.component"], + changeApplierOptions: { + relayStyle: true, + cullUnchanged: true + }, + members: { + model: "@expand:fluid.initRelayModel({that}, {that}.modelRelay)", + applier: "@expand:fluid.makeHolderChangeApplier({that}, {that}.options.changeApplierOptions)", + modelRelay: "@expand:fluid.establishModelRelay({that}, {that}.options.model, {that}.options.modelListeners, {that}.options.modelRelay, {that}.applier)" + }, + mergePolicy: { + model: { + noexpand: true, + func: fluid.arrayConcatPolicy // TODO: bug here in case a model consists of an array + }, + modelListeners: fluid.makeMergeListenersPolicy(fluid.arrayConcatPolicy), + modelRelay: { + noexpand: true, + func: fluid.arrayConcatPolicy + } + } + }); + + fluid.modelChangedToChange = function (args) { + return { + value: args[0], + oldValue: args[1], + path: args[2] + }; + }; + + fluid.resolveModelListener = function (that, record) { + var togo = function () { + if (fluid.isDestroyed(that)) { // first guarding point to resolve FLUID-5592 + return; + } + var change = fluid.modelChangedToChange(arguments); + var args = [change]; + var localRecord = {change: change, "arguments": args}; + if (record.args) { + args = fluid.expandOptions(record.args, that, {}, localRecord); + } + fluid.event.invokeListener(record.listener, fluid.makeArray(args)); + }; + fluid.event.impersonateListener(record.listener, togo); + return togo; + }; + + fluid.mergeModelListeners = function (that, listeners) { + var listenerCount = 0; + fluid.each(listeners, function (value, path) { + if (typeof(value) === "string") { + value = { + funcName: value + }; + } + var records = fluid.event.resolveListenerRecord(value, that, "modelListeners", null, false); + var parsed = fluid.parseValidModelReference(that, "modelListeners entry", path); + // Bypass fluid.event.dispatchListener by means of "standard = false" and enter our custom workflow including expanding "change": + fluid.each(records.records, function (record) { + var func = fluid.resolveModelListener(that, record); + var spec = { + listener: func, // for initModelEvent + listenerIndex: listenerCount, + segs: parsed.modelSegs, + path: parsed.path, + includeSource: record.includeSource, + excludeSource: record.excludeSource, + priority: record.priority, + transactional: true + }; + ++listenerCount; + // update "spec" so that we parse priority information just once + spec = parsed.applier.modelChanged.addListener(spec, func, record.namespace, record.softNamespace); + + fluid.recordChangeListener(that, parsed.applier, func); + function initModelEvent() { + if (fluid.isModelComplete(parsed.that)) { + var trans = parsed.applier.initiate("init"); + fluid.initModelEvent(that, parsed.applier, trans, [spec]); + trans.commit(); + } + } + if (that !== parsed.that && !fluid.isModelComplete(that)) { // TODO: Use FLUID-4883 "latched events" when available + // Don't confuse the end user by firing their listener before the component is constructed + // TODO: Better detection than this is requred - we assume that the target component will not be discovered as part + // of the initial transaction wave, but if it is, it will get a double notification - we really need "wave of explosions" + // since we are currently too early in initialisation of THIS component in order to tell if other will be found + // independently. + var onCreate = fluid.getForComponent(that, ["events", "onCreate"]); + onCreate.addListener(initModelEvent); + } + }); + }); + }; + + + /** CHANGE APPLIER **/ + + /** Add a listener to a ChangeApplier event that only acts in the case the event + * has not come from the specified source (typically ourself) + * @param modelEvent An model event held by a changeApplier (typically applier.modelChanged) + * @param path The path specification to listen to + * @param source The source value to exclude (direct equality used) + * @param func The listener to be notified of a change + * @param [eventName] - optional - the event name to be listened to - defaults to "modelChanged" + * @param [namespace] - optional - the event namespace + */ + // TODO: Source guarding is not supported by the current ChangeApplier, these methods are no-ops + fluid.addSourceGuardedListener = function(applier, path, source, func, eventName, namespace, softNamespace) { + eventName = eventName || "modelChanged"; + var wrapped = function (newValue, oldValue, path, changes) { // TODO: adapt signature + if (!applier.hasChangeSource(source, changes)) { + return func.apply(null, arguments); + } + }; + fluid.event.impersonateListener(func, wrapped); + return applier[eventName].addListener(path, wrapped, namespace, softNamespace); + }; + + /** Convenience method to fire a change event to a specified applier, including + * a supplied "source" identified (perhaps for use with addSourceGuardedListener) + */ + fluid.fireSourcedChange = function (applier, path, value, source) { + applier.fireChangeRequest({ + path: path, + value: value, + source: source + }); + }; + + /** Dispatches a list of changes to the supplied applier */ + fluid.requestChanges = function (applier, changes) { + for (var i = 0; i < changes.length; ++i) { + applier.fireChangeRequest(changes[i]); + } + }; + + + // Automatically adapts requestChange onto fireChangeRequest + fluid.bindRequestChange = function (that) { + // The name "requestChange" will be deprecated in 1.5, removed in 2.0 + that.requestChange = that.change = function (path, value, type) { + var changeRequest = { + path: path, + value: value, + type: type + }; + that.fireChangeRequest(changeRequest); + }; + }; + + fluid.identifyChangeListener = function (listener) { + return fluid.event.identifyListener(listener) || listener; + }; + + + fluid.model.isChangedPath = function (changeMap, segs) { + for (var i = 0; i <= segs.length; ++ i) { + if (typeof(changeMap) === "string") { + return true; + } + if (i < segs.length && changeMap) { + changeMap = changeMap[segs[i]]; + } + } + return false; + }; + + fluid.model.setChangedPath = function (options, segs, value) { + var notePath = function (record) { + segs.unshift(record); + fluid.model.setSimple(options, segs, value); + segs.shift(); + }; + if (!fluid.model.isChangedPath(options.changeMap, segs)) { + ++options.changes; + notePath("changeMap"); + } + if (!fluid.model.isChangedPath(options.deltaMap, segs)) { + ++options.deltas; + notePath("deltaMap"); + } + }; + + fluid.model.fetchChangeChildren = function (target, i, segs, source, options) { + fluid.each(source, function (value, key) { + segs[i] = key; + fluid.model.applyChangeStrategy(target, key, i, segs, value, options); + segs.length = i; + }); + }; + + // Called with two primitives which are compared for equality. This takes account of "floating point slop" to avoid + // continuing to propagate inverted values as changes + // TODO: replace with a pluggable implementation + fluid.model.isSameValue = function (a, b) { + if (typeof(a) !== "number" || typeof(b) !== "number") { + return a === b; + } else { + if (a === b || a !== a && b !== b) { // Either the same concrete number or both NaN + return true; + } else { + var relError = Math.abs((a - b) / b); + return relError < 1e-12; // 64-bit floats have approx 16 digits accuracy, this should deal with most reasonable transforms + } + } + }; + + fluid.model.applyChangeStrategy = function (target, name, i, segs, source, options) { + var targetSlot = target[name]; + var sourceCode = fluid.typeCode(source); + var targetCode = fluid.typeCode(targetSlot); + var changedValue = fluid.NO_VALUE; + if (sourceCode === "primitive") { + if (!fluid.model.isSameValue(targetSlot, source)) { + changedValue = source; + ++options.unchanged; + } + } else if (targetCode !== sourceCode || sourceCode === "array" && source.length !== targetSlot.length) { + // RH is not primitive - array or object and mismatching or any array rewrite + changedValue = fluid.freshContainer(source); + } + if (changedValue !== fluid.NO_VALUE) { + target[name] = changedValue; + if (options.changeMap) { + fluid.model.setChangedPath(options, segs, options.inverse ? "DELETE" : "ADD"); + } + } + if (sourceCode !== "primitive") { + fluid.model.fetchChangeChildren(target[name], i + 1, segs, source, options); + } + }; + + fluid.model.stepTargetAccess = function (target, type, segs, startpos, endpos, options) { + for (var i = startpos; i < endpos; ++ i) { + if (!target) { + continue; + } + var oldTrunk = target[segs[i]]; + target = fluid.model.traverseWithStrategy(target, segs, i, options[type === "ADD" ? "resolverSetConfig" : "resolverGetConfig"], + segs.length - i - 1); + if (oldTrunk !== target && options.changeMap) { + fluid.model.setChangedPath(options, segs.slice(0, i + 1), "ADD"); + } + } + return {root: target, last: segs[endpos]}; + }; + + fluid.model.defaultAccessorConfig = function (options) { + options = options || {}; + options.resolverSetConfig = options.resolverSetConfig || fluid.model.escapedSetConfig; + options.resolverGetConfig = options.resolverGetConfig || fluid.model.escapedGetConfig; + return options; + }; + + // Changes: "MERGE" action abolished + // ADD/DELETE at root can be destructive + // changes tracked in optional final argument holding "changeMap: {}, changes: 0, unchanged: 0" + fluid.model.applyHolderChangeRequest = function (holder, request, options) { + options = fluid.model.defaultAccessorConfig(options); + options.deltaMap = options.changeMap ? {} : null; + options.deltas = 0; + var length = request.segs.length; + var pen, atRoot = length === 0; + if (atRoot) { + pen = {root: holder, last: "model"}; + } else { + if (!holder.model) { + holder.model = {}; + fluid.model.setChangedPath(options, [], options.inverse ? "DELETE" : "ADD"); + } + pen = fluid.model.stepTargetAccess(holder.model, request.type, request.segs, 0, length - 1, options); + } + if (request.type === "ADD") { + var value = request.value; + var segs = fluid.makeArray(request.segs); + fluid.model.applyChangeStrategy(pen.root, pen.last, length - 1, segs, value, options, atRoot); + } else if (request.type === "DELETE") { + if (pen.root && pen.root[pen.last] !== undefined) { + delete pen.root[pen.last]; + if (options.changeMap) { + fluid.model.setChangedPath(options, request.segs, "DELETE"); + } + } + } else { + fluid.fail("Unrecognised change type of " + request.type); + } + return options.deltas ? options.deltaMap : null; + }; + + /** Compare two models for equality using a deep algorithm. It is assumed that both models are JSON-equivalent and do + * not contain circular links. + * @param modela The first model to be compared + * @param modelb The second model to be compared + * @param options {Object} If supplied, will receive a map and summary of the change content between the objects. Structure is: + * changeMap: {Object/String} An isomorphic map of the object structures to values "ADD" or "DELETE" indicating + * that values have been added/removed at that location. Note that in the case the object structure differs at the root, changeMap will hold + * the plain String value "ADD" or "DELETE" + * changes: {Integer} Counts the number of changes between the objects - The two objects are identical iff changes === 0. + * unchanged: {Integer} Counts the number of leaf (primitive) values at which the two objects are identical. Note that the current implementation will + * double-count, this summary should be considered indicative rather than precise. + * @return true if the models are identical + */ + // TODO: This algorithm is quite inefficient in that both models will be copied once each + // supported, PUBLIC API function + fluid.model.diff = function (modela, modelb, options) { + options = options || {changes: 0, unchanged: 0, changeMap: {}}; // current algorithm can't avoid the expense of changeMap + var typea = fluid.typeCode(modela); + var typeb = fluid.typeCode(modelb); + var togo; + if (typea === "primitive" && typeb === "primitive") { + togo = fluid.model.isSameValue(modela, modelb); + } else if (typea === "primitive" ^ typeb === "primitive") { + togo = false; + } else { + // Apply both forward and reverse changes - if no changes either way, models are identical + // "ADD" reported in the reverse direction must be accounted as a "DELETE" + var holdera = { + model: fluid.copy(modela) + }; + fluid.model.applyHolderChangeRequest(holdera, {value: modelb, segs: [], type: "ADD"}, options); + var holderb = { + model: fluid.copy(modelb) + }; + options.inverse = true; + fluid.model.applyHolderChangeRequest(holderb, {value: modela, segs: [], type: "ADD"}, options); + togo = options.changes === 0; + } + if (togo === false && options.changes === 0) { // catch all primitive cases + options.changes = 1; + options.changeMap = modelb === undefined ? "DELETE" : "ADD"; + } else if (togo === true && options.unchanged === 0) { + options.unchanged = 1; + } + return togo; + }; + + // Here we only support for now very simple expressions which have at most one + // wildcard which must appear in the final segment + fluid.matchChanges = function (changeMap, specSegs, newHolder) { + var root = newHolder.model; + var map = changeMap; + var outSegs = ["model"]; + var wildcard = false; + var togo = []; + for (var i = 0; i < specSegs.length; ++ i) { + var seg = specSegs[i]; + if (seg === "*") { + if (i === specSegs.length - 1) { + wildcard = true; + } else { + fluid.fail("Wildcard specification in modelChanged listener is only supported for the final path segment: " + specSegs.join(".")); + } + } else { + outSegs.push(seg); + map = fluid.isPrimitive(map) ? map : map[seg]; + root = root ? root[seg] : undefined; + } + } + if (map) { + if (wildcard) { + fluid.each(root, function (value, key) { + togo.push(outSegs.concat(key)); + }); + } else { + togo.push(outSegs); + } + } + return togo; + }; + + fluid.storeExternalChange = function (transRec, applier, invalidPath, spec, args) { + var pathString = applier.composeSegments.apply(null, invalidPath); + var keySegs = [applier.applierId, fluid.event.identifyListener(spec.listener), spec.listenerIndex, pathString]; + var keyString = keySegs.join("|"); + // These are unbottled in fluid.concludeTransaction + transRec.externalChanges[keyString] = {listener: spec.listener, priority: spec.priority, args: args}; + }; + + fluid.isExcludedChangeSource = function (transaction, spec) { + if (!spec.excludeSource) { // mergeModelListeners initModelEvent fabricates a fake spec that bypasses processing + return false; + } + var excluded = spec.excludeSource["*"]; + for (var source in transaction.sources) { + if (spec.excludeSource[source]) { + excluded = true; + } + if (spec.includeSource[source]) { + excluded = false; + } + } + return excluded; + }; + + fluid.notifyModelChanges = function (listeners, changeMap, newHolder, oldHolder, changeRequest, transaction, applier, that) { + var instantiator = fluid.getInstantiator && fluid.getInstantiator(that); // may return nothing for non-component holder + var transRec = transaction && fluid.getModelTransactionRec(instantiator, transaction.id); + for (var i = 0; i < listeners.length; ++ i) { + var spec = listeners[i]; + var invalidPaths = fluid.matchChanges(changeMap, spec.segs, newHolder); + for (var j = 0; j < invalidPaths.length; ++ j) { + if (applier.destroyed) { // 2nd guarding point for FLUID-5592 + return; + } + var invalidPath = invalidPaths[j]; + spec.listener = fluid.event.resolveListener(spec.listener); + // TODO: process namespace and softNamespace rules, and propagate "sources" in 4th argument + var args = [fluid.model.getSimple(newHolder, invalidPath), fluid.model.getSimple(oldHolder, invalidPath), invalidPath.slice(1), changeRequest, transaction, applier]; + // FLUID-5489: Do not notify of null changes which were reported as a result of invalidating a higher path + // TODO: We can improve greatly on efficiency by i) reporting a special code from fluid.matchChanges which signals the difference between invalidating a higher and lower path, + // ii) improving fluid.model.diff to create fewer intermediate structures and no copies + // TODO: The relay invalidation system is broken and must always be notified (branch 1) - since our old/new value detection is based on the wrong (global) timepoints in the transaction here, + // rather than the "last received model" by the holder of the transform document + if (!spec.isRelay) { + var isNull = fluid.model.diff(args[0], args[1]); + if (isNull) { + continue; + } + var sourceExcluded = fluid.isExcludedChangeSource(transaction, spec); + if (sourceExcluded) { + continue; + } + } + if (transRec && !spec.isRelay && spec.transactional) { // bottle up genuine external changes so we can sort and dedupe them later + fluid.storeExternalChange(transRec, applier, invalidPath, spec, args); + } else { + spec.listener.apply(null, args); + } + } + } + }; + + fluid.bindELMethods = function (applier) { + applier.parseEL = function (EL) { + return fluid.model.pathToSegments(EL, applier.options.resolverSetConfig); + }; + applier.composeSegments = function () { + return applier.options.resolverSetConfig.parser.compose.apply(null, arguments); + }; + }; + + fluid.initModelEvent = function (that, applier, trans, listeners) { + fluid.notifyModelChanges(listeners, "ADD", trans.oldHolder, fluid.emptyHolder, null, trans, applier, that); + }; + + fluid.emptyHolder = { model: undefined }; + + fluid.preFireChangeRequest = function (applier, changeRequest) { + if (!changeRequest.type) { + changeRequest.type = "ADD"; + } + changeRequest.segs = changeRequest.segs || applier.parseEL(changeRequest.path); + }; + + fluid.ChangeApplier = function () {}; + + fluid.makeHolderChangeApplier = function (holder, options) { + options = fluid.model.defaultAccessorConfig(options); + var applierId = fluid.allocateGuid(); + var that = new fluid.ChangeApplier(); + $.extend(that, { + applierId: applierId, + holder: holder, + changeListeners: { + listeners: [], + transListeners: [] + }, + options: options, + modelChanged: {}, + preCommit: fluid.makeEventFirer({name: "preCommit event for ChangeApplier " }), + postCommit: fluid.makeEventFirer({name: "postCommit event for ChangeApplier "}) + }); + that.destroy = function () { + that.preCommit.destroy(); + that.postCommit.destroy(); + that.destroyed = true; + }; + that.modelChanged.addListener = function (spec, listener, namespace, softNamespace) { + if (typeof(spec) === "string") { + spec = {path: spec}; + } else { + spec = fluid.copy(spec); + } + spec.id = fluid.event.identifyListener(listener); + spec.namespace = namespace; + spec.softNamespace = softNamespace; + if (typeof(listener) === "string") { // TODO: replicate this nonsense from Fluid.js until we remember its purpose + listener = {globalName: listener}; + } + spec.listener = listener; + if (spec.transactional !== false) { + spec.transactional = true; + } + spec.segs = spec.segs || that.parseEL(spec.path); + var collection = that.changeListeners[spec.transactional ? "transListeners" : "listeners"]; + spec.excludeSource = fluid.arrayToHash(fluid.makeArray(spec.excludeSource || (spec.includeSource ? "*" : undefined))); + spec.includeSource = fluid.arrayToHash(fluid.makeArray(spec.includeSource)); + spec.priority = fluid.parsePriority(spec.priority, collection.length, true, "model listener"); + collection.push(spec); + return spec; + }; + that.modelChanged.removeListener = function (listener) { + var id = fluid.event.identifyListener(listener); + var namespace = typeof(listener) === "string" ? listener: null; + var removePred = function (record) { + return record.id === id || record.namespace === namespace; + }; + fluid.remove_if(that.changeListeners.listeners, removePred); + fluid.remove_if(that.changeListeners.transListeners, removePred); + }; + that.fireChangeRequest = function (changeRequest) { + var ation = that.initiate(); + ation.fireChangeRequest(changeRequest); + ation.commit(); + }; + + that.initiate = function (source, transactionId) { + source = source || "local"; + var defeatPost = source === "relay"; // defeatPost is supplied for all non-top-level transactions + var trans = { + instanceId: fluid.allocateGuid(), // for debugging only + id: transactionId || fluid.allocateGuid(), + sources: {}, + changeRecord: { + resolverSetConfig: options.resolverSetConfig, // here to act as "options" in applyHolderChangeRequest + resolverGetConfig: options.resolverGetConfig + }, + reset: function () { + trans.oldHolder = holder; + trans.newHolder = { model: fluid.copy(holder.model) }; + trans.changeRecord.changes = 0; + trans.changeRecord.unchanged = 0; // just for type consistency - we don't use these values in the ChangeApplier + trans.changeRecord.changeMap = {}; + }, + commit: function (code) { + that.preCommit.fire(trans, that, code); + if (trans.changeRecord.changes > 0) { + var oldHolder = {model: holder.model}; + holder.model = trans.newHolder.model; + fluid.notifyModelChanges(that.changeListeners.transListeners, trans.changeRecord.changeMap, holder, oldHolder, null, trans, that, holder); + } + if (!defeatPost) { + that.postCommit.fire(trans, that, code); + } + }, + fireChangeRequest: function (changeRequest) { + fluid.preFireChangeRequest(that, changeRequest); + changeRequest.transactionId = trans.id; + var deltaMap = fluid.model.applyHolderChangeRequest(trans.newHolder, changeRequest, trans.changeRecord); + fluid.notifyModelChanges(that.changeListeners.listeners, deltaMap, trans.newHolder, holder, changeRequest, trans, that, holder); + } + }; + trans.sources[source] = true; + trans.reset(); + fluid.bindRequestChange(trans); + return trans; + }; + that.hasChangeSource = function (source, changes) { // compatibility for old API + return changes ? changes[source] : false; + }; + + fluid.bindRequestChange(that); + fluid.bindELMethods(that); + return that; + }; + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/Fluid.js b/ppig-2015/example/lib/infusion/Fluid.js new file mode 100644 index 0000000..e53ac08 --- /dev/null +++ b/ppig-2015/example/lib/infusion/Fluid.js @@ -0,0 +1,2612 @@ +/*! + * Fluid Infusion v2.0 + * + * Infusion is distributed under the Educational Community License 2.0 and new BSD licenses: + * http://wiki.fluidproject.org/display/fluid/Fluid+Licensing + * + * For information on copyright, see the individual Infusion source code files: + * https://github.com/fluid-project/infusion/ + */ +/* +Copyright 2007-2010 University of Cambridge +Copyright 2007-2009 University of Toronto +Copyright 2007-2009 University of California, Berkeley +Copyright 2010-2015 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 +*/ + +/* global console, opera, YAHOO*/ + +var fluid_2_0 = fluid_2_0 || {}; +var fluid = fluid || fluid_2_0; + +(function ($, fluid) { + "use strict"; + + fluid.version = "Infusion 2.0-SNAPSHOT"; + + // Export this for use in environments like node.js, where it is useful for + // configuring stack trace behaviour + fluid.Error = Error; + + fluid.environment = { + fluid: fluid + }; + + fluid.global = fluid.global || window || {}; + + // A standard utility to schedule the invocation of a function after the current + // stack returns. On browsers this defaults to setTimeout(func, 1) but in + // other environments can be customised - e.g. to process.nextTick in node.js + // In future, this could be optimised in the browser to not dispatch into the event queue + fluid.invokeLater = function (func) { + return setTimeout(func, 1); + }; + + // The following flag defeats all logging/tracing activities in the most performance-critical parts of the framework. + // This should really be performed by a build-time step which eliminates calls to pushActivity/popActivity and fluid.log. + fluid.defeatLogging = true; + + // This flag enables the accumulating of all "activity" records generated by pushActivity into a running trace, rather + // than removing them from the stack record permanently when receiving popActivity. This trace will be consumed by + // visual debugging tools. + fluid.activityTracing = false; + fluid.activityTrace = []; + + var activityParser = /(%\w+)/g; + + // Renders a single activity element in a form suitable to be sent to a modern browser's console + // unsupported, non-API function + fluid.renderOneActivity = function (activity, nowhile) { + var togo = nowhile === true ? [] : [" while "]; + var message = activity.message; + var index = activityParser.lastIndex = 0; + while (true) { + var match = activityParser.exec(message); + if (match) { + var key = match[1].substring(1); + togo.push(message.substring(index, match.index)); + togo.push(activity.args[key]); + index = activityParser.lastIndex; + } + else { + break; + } + } + if (index < message.length) { + togo.push(message.substring(index)); + } + return togo; + }; + + // Renders an activity stack in a form suitable to be sent to a modern browser's console + // unsupported, non-API function + fluid.renderActivity = function (activityStack, renderer) { + renderer = renderer || fluid.renderOneActivity; + return fluid.transform(activityStack, renderer); + }; + + // Definitions for ThreadLocals, the static and dynamic environment - lifted here from + // FluidIoC.js so that we can issue calls to fluid.describeActivity for debugging purposes + // in the core framework + + // unsupported, non-API function + fluid.singleThreadLocal = function (initFunc) { + var value = initFunc(); + return function (newValue) { + return newValue === undefined ? value : value = newValue; + }; + }; + + // Currently we only support single-threaded environments - ensure that this function + // is not used on startup so it can be successfully monkey-patched + // only remaining uses of threadLocals are for activity reporting and in the renderer utilities + // unsupported, non-API function + fluid.threadLocal = fluid.singleThreadLocal; + + // unsupported, non-API function + fluid.globalThreadLocal = fluid.threadLocal(function () { + return {}; + }); + + // Return an array of objects describing the current activity + // unsupported, non-API function + fluid.getActivityStack = function () { + var root = fluid.globalThreadLocal(); + if (!root.activityStack) { + root.activityStack = []; + } + return root.activityStack; + }; + + // Return an array of objects describing the current activity + // unsupported, non-API function + fluid.describeActivity = fluid.getActivityStack; + + // Renders either the current activity or the supplied activity to the console + fluid.logActivity = function (activity) { + activity = activity || fluid.describeActivity(); + var rendered = fluid.renderActivity(activity).reverse(); + fluid.log("Current activity: "); + fluid.each(rendered, function (args) { + fluid.doLog(args); + }); + }; + + // Execute the supplied function with the specified activity description pushed onto the stack + // unsupported, non-API function + fluid.pushActivity = function (type, message, args) { + var record = {type: type, message: message, args: args, time: new Date().getTime()}; + if (fluid.activityTracing) { + fluid.activityTrace.push(record); + } + if (fluid.passLogLevel(fluid.logLevel.TRACE)) { + fluid.doLog(fluid.renderOneActivity(record, true)); + } + var activityStack = fluid.getActivityStack(); + activityStack.push(record); + }; + + // Undo the effect of the most recent pushActivity, or multiple frames if an argument is supplied + fluid.popActivity = function (popframes) { + popframes = popframes || 1; + if (fluid.activityTracing) { + fluid.activityTrace.push({pop: popframes}); + } + var activityStack = fluid.getActivityStack(); + var popped = activityStack.length - popframes; + activityStack.length = popped < 0 ? 0 : popped; + }; + // "this-ist" style Error so that we can distinguish framework errors whilst still retaining access to platform Error features + // unsupported, non-API function + fluid.FluidError = function (message) { + this.message = message; + this.stack = new Error().stack; + }; + fluid.FluidError.prototype = new Error(); + + // The framework's built-in "log" failure handler - this logs the supplied message as well as any framework activity in progress via fluid.log + fluid.logFailure = function (args, activity) { + fluid.log.apply(null, [fluid.logLevel.FAIL, "ASSERTION FAILED: "].concat(args)); + fluid.logActivity(activity); + }; + + // The framework's built-in "fail" failure handler - this throws an exception of type fluid.FluidError + fluid.builtinFail = function (args /*, activity*/) { + var message = args.join(""); + throw new fluid.FluidError("Assertion failure - check console for more details: " + message); + }; + + /** + * Signals an error to the framework. The default behaviour is to log a structured error message and throw an exception. This strategy may be configured using the legacy + * API fluid.pushSoftFailure or else by adding and removing suitably namespaced listeners to the special event fluid.failureEvent + * + * @param {String} message the error message to log + * @param ... Additional arguments, suitable for being sent to the native console.log function + */ + fluid.fail = function (/* message, ... */) { + var args = fluid.makeArray(arguments); + var activity = fluid.makeArray(fluid.describeActivity()); // Take copy since we will destructively modify + fluid.popActivity(activity.length); // clear any current activity - TODO: the framework currently has no exception handlers, although it will in time + if (fluid.failureEvent) { // notify any framework failure prior to successfully setting up the failure event below + fluid.failureEvent.fire(args, activity); + } else { + fluid.logFailure(args, activity); + fluid.builtinFail(args, activity); + } + }; + + // TODO: rescued from kettleCouchDB.js - clean up in time + fluid.expect = function (name, target, members) { + fluid.transform(fluid.makeArray(members), function (key) { + if (typeof target[key] === "undefined") { + fluid.fail(name + " missing required parameter " + key); + } + }); + }; + + // Logging + + /** Returns whether logging is enabled **/ + fluid.isLogging = function () { + return logLevelStack[0].priority > fluid.logLevel.IMPORTANT.priority; + }; + + /** Determines whether the supplied argument is a valid logLevel marker **/ + fluid.isLogLevel = function (arg) { + return fluid.isMarker(arg) && arg.priority !== undefined; + }; + + /** Accepts one of the members of the fluid.logLevel structure. Returns true if + * a message supplied at that log priority would be accepted at the current logging level. Clients who + * issue particularly expensive log payload arguments are recommended to guard their logging statements with this + * function */ + + fluid.passLogLevel = function (testLogLevel) { + return testLogLevel.priority <= logLevelStack[0].priority; + }; + + /** Method to allow user to control the logging level. Accepts either a boolean, for which true + * represents fluid.logLevel.INFO and false represents fluid.logLevel.IMPORTANT (the default), + * or else any other member of the structure fluid.logLevel + * Messages whose priority is strictly less than the current logging level will not be shown*/ + fluid.setLogging = function (enabled) { + var logLevel; + if (typeof enabled === "boolean") { + logLevel = fluid.logLevel[enabled? "INFO" : "IMPORTANT"]; + } else if (fluid.isLogLevel(enabled)) { + logLevel = enabled; + } else { + fluid.fail("Unrecognised fluid logging level ", enabled); + } + logLevelStack.unshift(logLevel); + fluid.defeatLogging = !fluid.isLogging(); + }; + + fluid.setLogLevel = fluid.setLogging; + + /** Undo the effect of the most recent "setLogging", returning the logging system to its previous state **/ + fluid.popLogging = function () { + var togo = logLevelStack.length === 1? logLevelStack[0] : logLevelStack.shift(); + fluid.defeatLogging = !fluid.isLogging(); + return togo; + }; + + /** Actually do the work of logging args to the environment's 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. On capable environments (those other than + * IE8 or IE9) the entire argument set will be dispatched to the logger - otherwise they will be flattened into + * a string first, destroying any information held in non-primitive values. + */ + fluid.doLog = function (args) { + var str = args.join(""); + if (typeof (console) !== "undefined") { + if (console.debug) { + console.debug.apply(console, args); + } else if (typeof (console.log) === "function") { + console.log.apply(console, 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); + } + }; + + /** Log a message to a suitable environmental console. If the first argument to fluid.log is + * one of the members of the fluid.logLevel structure, this will be taken as the priority + * of the logged message - else if will default to fluid.logLevel.INFO. If the logged message + * priority does not exceed that set by the most recent call to the fluid.setLogging function, + * the message will not appear. + */ + fluid.log = function (/* message /*, ... */) { + var directArgs = fluid.makeArray(arguments); + var userLogLevel = fluid.logLevel.INFO; + if (fluid.isLogLevel(directArgs[0])) { + userLogLevel = directArgs.shift(); + } + if (fluid.passLogLevel(userLogLevel)) { + var arg0 = fluid.renderTimestamp(new Date()) + ": "; + var args = [arg0].concat(directArgs); + fluid.doLog(args); + } + }; + + // 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"; + }; + + /** Determines whether the supplied object is an array. The strategy used is an optimised + * approach taken from an earlier version of jQuery - detecting whether the toString() version + * of the object agrees with the textual form [object Array], or else whether the object is a + * jQuery object (the most common source of "fake arrays"). + */ + fluid.isArrayable = function (totest) { + return totest && (totest.jquery || Object.prototype.toString.call(totest) === "[object Array]"); + }; + + /** Determines whether the supplied object is a plain JSON-forming container - that is, it is either a plain Object + * or a plain Array */ + fluid.isPlainObject = function (totest) { + if (!totest) { + return false; // FLUID-5172 - on IE8 the line below produces [object Object] rather than [object Null] or [object Undefined] + } + var string = Object.prototype.toString.call(totest); + if (string === "[object Array]") { + return true; + } else if (string !== "[object Object]") { + return false; + } // FLUID-5226: This inventive strategy taken from jQuery detects whether the object's prototype is directly Object.prototype by virtue of having an "isPrototypeOf" direct member + return Object.prototype.hasOwnProperty.call(totest.constructor.prototype, "isPrototypeOf"); + }; + + /** Returns primitive, array or object depending on whether the supplied object has + * one of those types, by use of the fluid.isPrimitive, fluid.isPlainObject and fluid.isArrayable utilities + */ + fluid.typeCode = function (totest) { + return fluid.isPrimitive(totest) || !fluid.isPlainObject(totest) ? "primitive" : + fluid.isArrayable(totest) ? "array" : "object"; + }; + + 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 + // The real problem is browsers like IE6, 7 and 8 which still do not feature a "constructor" property on DOM nodes + return obj && typeof (obj.nodeType) === "number"; + }; + + fluid.isDOMish = function (obj) { + return fluid.isDOMNode(obj) || obj.jquery; + }; + + fluid.isComponent = function (obj) { + // TODO: improve this strategy in time - we may want to actually use a constructor-based test when we can drop IE8 + return obj && obj.typeName && obj.id; + }; + + /** Return an empty container as the same type as the argument (either an + * array or hash */ + fluid.freshContainer = function (tocopy) { + return fluid.isArrayable(tocopy) ? [] : {}; + }; + + fluid.isUncopyable = function (totest) { + return fluid.isPrimitive(totest) || fluid.isDOMish(totest) || !fluid.isPlainObject(totest); + }; + + fluid.copyRecurse = function (tocopy, segs) { + if (segs.length > fluid.strategyRecursionBailout) { + fluid.fail("Runaway recursion encountered in fluid.copy - reached path depth of " + fluid.strategyRecursionBailout + " via path of " + segs.join(".") + + "this object is probably circularly connected. Either adjust your object structure to remove the circularity or increase fluid.strategyRecursionBailout"); + } + if (fluid.isUncopyable(tocopy)) { + return tocopy; + } else { + return fluid.transform(tocopy, function (value, key) { + segs.push(key); + var togo = fluid.copyRecurse(value, segs); + segs.pop(); + return togo; + }); + } + }; + + /** Performs a deep copy (clone) of its argument. This will guard against cloning a circular object by terminating if it reaches a path depth + * greater than fluid.strategyRecursionBailout + **/ + + fluid.copy = function (tocopy) { + return fluid.copyRecurse(tocopy, []); + }; + + /** Corrected version of jQuery makeArray that returns an empty array on undefined rather than crashing. + * We don't deal with as many pathological cases as jQuery **/ + fluid.makeArray = function (arg) { + var togo = []; + if (arg !== null && arg !== undefined) { + if (fluid.isPrimitive(arg) || typeof(arg.length) !== "number") { + togo.push(arg); + } + else { + for (var i = 0; i < arg.length; ++ i) { + togo[i] = arg[i]; + } + } + } + return togo; + }; + + 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. + */ + 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); + } + } + }; + + fluid.make_find = function (find_if) { + var target = find_if ? false : undefined; + return function (source, func, deffolt) { + var disp; + if (fluid.isArrayable(source)) { + for (var i = 0; i < source.length; ++i) { + disp = func(source[i], i); + if (disp !== target) { + return find_if ? source[i] : disp; + } + } + } else { + for (var key in source) { + disp = func(source[key], key); + if (disp !== target) { + return find_if ? source[key] : disp; + } + } + } + return deffolt; + }; + }; + + /** 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 undefined 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 undefined + * @return The first return value from the predicate function which is not undefined + */ + fluid.find = fluid.make_find(false); + /** The same signature as fluid.find, only the return value is the actual element for which the + * predicate returns a value different from false + */ + fluid.find_if = fluid.make_find(true); + + /** Scan through a list of objects, "accumulating" a value over them + * (may be a straightforward "sum" or some other chained computation). "accumulate" is the name derived + * from the C++ STL, other names for this algorithm are "reduce" or "fold". + * @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; + }; + + /** Scan through a list or hash 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 or hash 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. + * @param target {Array|Object} (optional) A target object of the same type as source, which will + * receive any objects removed from it. + * @return target, containing the removed elements, if it was supplied, or else source + * modified by the operation of removing the matched elements. + */ + fluid.remove_if = function (source, fn, target) { + if (fluid.isArrayable(source)) { + for (var i = source.length - 1; i >= 0; --i) { + if (fn(source[i], i)) { + if (target) { + target.unshift(source[i]); + } + source.splice(i, 1); + } + } + } else { + for (var key in source) { + if (fn(source[key], key)) { + if (target) { + target[key] = source[key]; + } + delete source[key]; + } + } + } + return target || source; + }; + + /** Fills an array of given size with copies of a value or result of a function invocation + * @param n {Number} The size of the array to be filled + * @param generator {Object|Function} Either a value to be replicated or function to be called + * @param applyFunc {Boolean} If true, treat the generator value as a function to be invoked with + * argument equal to the index position + */ + + fluid.generate = function (n, generator, applyFunc) { + var togo = []; + for (var i = 0; i < n; ++ i) { + togo[i] = applyFunc? generator(i) : generator; + } + return togo; + }; + + /** Returns an array of size count, filled with increasing integers, starting at 0 or at the index specified by first. + * @param count {Number} Size of the filled array to be returned + * @param first {Number} (optional, defaults to 0) First element to appear in the array + */ + + fluid.iota = function (count, first) { + first = first || 0; + var togo = []; + for (var i = 0; i < count; ++i) { + togo[togo.length] = first++; + } + return togo; + }; + + /** Extracts a particular member from each top-level member of a container, returning a new container of the same type + * @param holder {Array|Object} The container to be filtered + * @param name {String|Array of String} An EL path to be fetched from each top-level member + */ + + fluid.getMembers = function (holder, name) { + return fluid.transform(holder, function(member) { + return fluid.get(member, name); + }); + }; + + /** 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 NOT modified by the operation (current implementation + * passes through $.extend shallow algorithm) + * @param keys {Array of String} The list of keys to operate with + * @param exclude {boolean} If true, the keys listed are removed rather than included + * @return the filtered object (the same object that was supplied as toFilter + */ + + fluid.filterKeys = function (toFilter, keys, exclude) { + return fluid.remove_if($.extend({}, toFilter), function (value, key) { + return exclude ^ (keys.indexOf(key) === -1); + }); + }; + + /** A convenience wrapper for fluid.filterKeys with the parameter exclude set to true + * Returns the supplied object with listed keys removed */ + + fluid.censorKeys = function (toCensor, keys) { + return fluid.filterKeys(toCensor, keys, true); + }; + + // TODO: This is not as clever an idea as we think it is - this typically inner-loop function will optimise badly due to closure + fluid.makeFlatten = function (index) { + return function (obj) { + var togo = []; + fluid.each(obj, function (/* value, key */) { + togo.push(arguments[index]); + }); + return togo; + }; + }; + + /** Return the keys in the supplied object as an array. Note that this will return keys found in the prototype chain as well as "own properties", unlike Object.keys() **/ + fluid.keys = fluid.makeFlatten(1); + + /** Return the values in the supplied object as an array **/ + fluid.values = fluid.makeFlatten(0); + + /** + * Searches through the supplied object, and returns true if the supplied value + * can be found + */ + fluid.contains = function (obj, value) { + return obj ? (fluid.isArrayable(obj) ? obj.indexOf(value) !== -1 : fluid.find(obj, function (thisValue) { + 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 null if no + * such key is found. + */ + fluid.keyForValue = function (obj, value) { + return fluid.find(obj, function (thisValue, key) { + if (value === thisValue) { + return key; + } + }); + }; + + /** Converts an array into an object whose keys are the elements of the array, each with the value "true" + * @param array {Array of String} The array to be converted to a hash + * @return hash {Object} An object with value true for each key taken from a member of array + */ + + fluid.arrayToHash = function (array) { + var togo = {}; + fluid.each(array, function (el) { + togo[el] = true; + }); + return togo; + }; + + /** Applies a stable sorting algorithm to the supplied array and comparator (note that Array.sort in JavaScript is not specified + * to be stable). The algorithm used will be an insertion sort, which whilst quadratic in time, will perform well + * on small array sizes. + * @param array {Array} The array to be sorted. This input array will be modified in place. + * @param func {Function} A comparator returning >0, 0, or <0 on pairs of elements representing their sort order (same contract as Array.sort comparator) + */ + + fluid.stableSort = function (array, func) { + for (var i = 0; i < array.length; i++) { + var k = array[i]; + for (var j = i; j > 0 && func(k, array[j - 1]) < 0; j--) { + array[j] = array[j - 1]; + } + array[j] = k; + } + }; + + /** Converts a hash into an object by hoisting out the object's keys into an array element via the supplied String "key", and then transforming via an optional further function, which receives the signature + * (newElement, oldElement, key) where newElement is the freshly cloned element, oldElement is the original hash's element, and key is the key of the element. + * If the function is not supplied, the old element is simply deep-cloned onto the new element (same effect + * as transform fluid.transforms.objectToArray) + */ + fluid.hashToArray = function (hash, keyName, func) { + var togo = []; + fluid.each(hash, function (el, key) { + var newEl = {}; + newEl[keyName] = key; + if (func) { + newEl = func(newEl, el, key) || newEl; + } else { + $.extend(true, newEl, el); + } + togo.push(newEl); + }); + return togo; + }; + + /** Converts an array consisting of a mixture of arrays and non-arrays into the concatenation of any inner arrays + * with the non-array elements + */ + fluid.flatten = function (array) { + var togo = []; + fluid.each(array, function (element) { + if (fluid.isArrayable(element)) { + togo = togo.concat(element); + } else { + togo.push(element); + } + }); + 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]; + } + } + }; + + /** + * @param boolean ascending true if a comparator is to be returned which + * sorts strings in descending order of length + */ + fluid.compareStringLength = function (ascending) { + return ascending ? function (a, b) { + return a.length - b.length; + } : function (a, b) { + return b.length - a.length; + }; + }; + + /** + * Returns the converted integer if the input string can be converted to an integer. Otherwise, return NaN. + * @param {String} a string to be returned in integer + */ + fluid.parseInteger = function (string) { + return isFinite(string) && ((string % 1) === 0) ? Number(string) : NaN; + }; + + fluid.logLevelsSpec = { + "FATAL": 0, + "FAIL": 5, + "WARN": 10, + "IMPORTANT": 12, // The default logging "off" level - corresponds to the old "false" + "INFO": 15, // The default logging "on" level - corresponds to the old "true" + "TRACE": 20 + }; + + /** A structure holding all supported log levels as supplied as a possible first argument to fluid.log + * Members with a higher value of the "priority" field represent lower priority logging levels */ + // Moved down here since it uses fluid.transform on startup + fluid.logLevel = fluid.transform(fluid.logLevelsSpec, function (value, key) { + return {type: "fluid.marker", value: key, priority: value}; + }); + var logLevelStack = [fluid.logLevel.IMPORTANT]; // The stack of active logging levels, with the current level at index 0 + + /** A set of special "marker values" used in signalling in function arguments and return values, + * to partially compensate for JavaScript's lack of distinguished types. These should never appear + * in JSON structures or other kinds of static configuration. An API specifically documents if it + * accepts or returns any of these values, and if so, what its semantic is - most are of private + * use internal to the framework **/ + + /** A special "marker object" representing that a distinguished + * (probably context-dependent) value should be substituted. + */ + fluid.VALUE = {type: "fluid.marker", value: "VALUE"}; + + /** A special "marker object" representing that no value is present (where + * signalling using the value "undefined" is not possible - e.g. the return value from a "strategy") */ + 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"}; + + /** Determine whether an object is any marker, or a particular marker - omit the + * 2nd argument to detect any marker + */ + fluid.isMarker = function (totest, type) { + if (!totest || typeof (totest) !== "object" || totest.type !== "fluid.marker") { + return false; + } + if (!type) { + return true; + } + return totest.value === type.value; + }; + + // Model functions + fluid.model = {}; // cannot call registerNamespace yet since it depends on fluid.model + + /** 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 fluid.makeArray(arguments).join("."); + }; + + /** Returns the index of the last occurrence of the period character . in the supplied string */ + fluid.lastDotIndex = function (path) { + return path.lastIndexOf("."); + }; + + /** Returns all of an EL path minus its final segment - if the path consists of just one segment, returns "" - + * WARNING - this method does not follow escaping rules */ + fluid.model.getToTailPath = function (path) { + var lastdot = fluid.lastDotIndex(path); + return lastdot === -1 ? "" : path.substring(0, lastdot); + }; + + /** Returns the very last path component of an EL path + * WARNING - this method does not follow escaping rules */ + fluid.model.getTailPath = function (path) { + var lastdot = fluid.lastDotIndex(path); + return path.substring(lastdot + 1); + }; + + /** Helpful alias for old-style API **/ + fluid.path = fluid.model.composeSegments; + fluid.composePath = fluid.model.composePath; + + + // unsupported, NON-API function + fluid.requireDataBinding = function () { + fluid.fail("Please include DataBinding.js in order to operate complex model accessor configuration"); + }; + + fluid.model.setWithStrategy = fluid.model.getWithStrategy = fluid.requireDataBinding; + + // unsupported, NON-API function + fluid.model.resolvePathSegment = function (root, segment, create, origEnv) { + if (!origEnv && root.resolvePathSegment) { + return root.resolvePathSegment(segment); + } + if (create && root[segment] === undefined) { + // This optimisation in this heavily used function has a fair effect + return root[segment] = {}; // jshint ignore:line + } + return root[segment]; + }; + + // unsupported, NON-API function + fluid.model.parseToSegments = function (EL, parseEL, copy) { + return typeof(EL) === "number" || typeof(EL) === "string" ? parseEL(EL) : (copy ? fluid.makeArray(EL) : EL); + }; + + // unsupported, NON-API function + fluid.model.pathToSegments = function (EL, config) { + var parser = config && config.parser ? config.parser.parse : fluid.model.parseEL; + return fluid.model.parseToSegments(EL, parser); + }; + + // Overall strategy skeleton for all implementations of fluid.get/set + fluid.model.accessImpl = function (root, EL, newValue, config, initSegs, returnSegs, traverser) { + var segs = fluid.model.pathToSegments(EL, config); + var initPos = 0; + if (initSegs) { + initPos = initSegs.length; + segs = initSegs.concat(segs); + } + var uncess = newValue === fluid.NO_VALUE ? 0 : 1; + root = traverser(root, segs, initPos, config, uncess); + if (newValue === fluid.NO_VALUE || newValue === fluid.VALUE) { // get or custom + return returnSegs ? {root: root, segs: segs} : root; + } + else { // set + root[segs[segs.length - 1]] = newValue; + } + }; + + // unsupported, NON-API function + fluid.model.accessSimple = function (root, EL, newValue, environment, initSegs, returnSegs) { + return fluid.model.accessImpl(root, EL, newValue, environment, initSegs, returnSegs, fluid.model.traverseSimple); + }; + + // unsupported, NON-API function + fluid.model.traverseSimple = function (root, segs, initPos, environment, uncess) { + var origEnv = environment; + var limit = segs.length - uncess; + for (var i = 0; i < limit; ++i) { + if (!root) { + return root; + } + var segment = segs[i]; + if (environment && environment[segment]) { + root = environment[segment]; + } else { + root = fluid.model.resolvePathSegment(root, segment, uncess === 1, origEnv); + } + environment = null; + } + return root; + }; + + fluid.model.setSimple = function (root, EL, newValue, environment, initSegs) { + fluid.model.accessSimple(root, EL, newValue, environment, initSegs, false); + }; + + /** Optimised version of fluid.get for uncustomised configurations **/ + + fluid.model.getSimple = function (root, EL, environment, initSegs) { + if (EL === null || EL === undefined || EL.length === 0) { + return root; + } + return fluid.model.accessSimple(root, EL, fluid.NO_VALUE, environment, initSegs, false); + }; + + /** Even more optimised version which assumes segs are parsed and no configuration **/ + fluid.getImmediate = function (root, segs, i) { + var limit = (i === undefined ? segs.length: i + 1); + for (var j = 0; j < limit; ++ j) { + root = root ? root[segs[j]] : undefined; + } + return root; + }; + + // unsupported, NON-API function + // Returns undefined to signal complex configuration which needs to be farmed out to DataBinding.js + // any other return represents an environment value AND a simple configuration we can handle here + fluid.decodeAccessorArg = function (arg3) { + return (!arg3 || arg3 === fluid.model.defaultGetConfig || arg3 === fluid.model.defaultSetConfig) ? + null : (arg3.type === "environment" ? arg3.value : undefined); + }; + + fluid.set = function (root, EL, newValue, config, initSegs) { + var env = fluid.decodeAccessorArg(config); + if (env === undefined) { + fluid.model.setWithStrategy(root, EL, newValue, config, initSegs); + } else { + fluid.model.setSimple(root, EL, newValue, env, initSegs); + } + }; + + /** 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/array} EL The EL expression to be evaluated, or an array of path segments + * @param config An optional configuration or environment structure which can customise the fetch operation + * @return The fetched data value. + */ + + fluid.get = function (root, EL, config, initSegs) { + var env = fluid.decodeAccessorArg(config); + return env === undefined ? + fluid.model.getWithStrategy(root, EL, config, initSegs) + : fluid.model.accessImpl(root, EL, fluid.NO_VALUE, env, null, false, fluid.model.traverseSimple); + }; + + fluid.getGlobalValue = function (path, env) { + if (path) { + env = env || fluid.environment; + return fluid.get(fluid.global, path, {type: "environment", value: env}); + } + }; + + /** + * Allows for the binding to a "this-ist" function + * @param {Object} obj, "this-ist" object to bind to + * @param {Object} fnName, the name of the function to call + * @param {Object} args, arguments to call the function with + */ + fluid.bind = function (obj, fnName, args) { + return obj[fnName].apply(obj, fluid.makeArray(args)); + }; + + /** + * 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, fluid.isArrayable(args) ? args : fluid.makeArray(args)); + } + }; + + /** Registers a new global function at a given path + */ + + fluid.registerGlobalFunction = function (functionPath, func, env) { + env = env || fluid.environment; + fluid.set(fluid.global, functionPath, func, {type: "environment", value: env}); + }; + + fluid.setGlobalValue = fluid.registerGlobalFunction; + + /** Ensures that an entry in the global namespace exists. If it does not, a new entry is created as {} and returned. If an existing + * value is found, it is returned instead **/ + 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 Fluid Event system. ***/ + + fluid.registerNamespace("fluid.event"); + + // unsupported, NON-API function + fluid.generateUniquePrefix = function () { + return (Math.floor(Math.random() * 1e12)).toString(36) + "-"; + }; + + var fluid_prefix = fluid.generateUniquePrefix(); + + fluid.fluidInstance = fluid_prefix; + + var fluid_guid = 1; + + /** Allocate a string value that will be unique within this Infusion instance (frame or process), and + * globally unique with high probability (50% chance of collision after a million trials) **/ + + fluid.allocateGuid = function () { + return fluid_prefix + (fluid_guid++); + }; + + // Fluid priority system for encoding relative positions of, e.g. listeners, transforms, options, in lists + + fluid.extremePriority = 4e9; // around 2^32 - allows headroom of 21 fractional bits for sub-priorities + fluid.priorityTypes = { + first: -1, + last: 1, + before: 0, + after: 0 + }; + // TODO: This should be properly done with defaults blocks and a much more performant fluid.indexDefaults + fluid.extremalPriorities = { + // a built-in definition to allow test infrastructure "last" listeners to sort after all impl listeners, and authoring/debugging listeners to sort after those + // these are "priority intensities", and will be flipped for "first" listeners + none: 0, + testing: 10, + authoring: 20 + }; + + // unsupported, NON-API function + fluid.parsePriorityConstraint = function (constraint, fixedOnly, site) { + var segs = constraint.split(":"); + var type = segs[0]; + var lookup = fluid.priorityTypes[type]; + if (lookup === undefined) { + fluid.fail("Invalid priority constraint type in constraint " + constraint + ": the only supported values are " + fluid.keys(fluid.priorityType).join(", ")); + } + if (fixedOnly && lookup === 0) { + fluid.fail("Constraint-based priority in constraint " + constraint + " is not supported in a " + site + " record - you must use either a numeric value or first, last"); + } + return { + type: segs[0], + target: segs[1] + }; + }; + + // unsupported, NON-API function + fluid.parsePriority = function (priority, count, fixedOnly, site) { + priority = priority || 0; + var togo = { + count: count || 0, + fixed: null, + constraint: null, + site: site + }; + if (typeof(priority) === "number") { + togo.fixed = -priority; + } else { + togo.constraint = fluid.parsePriorityConstraint(priority, fixedOnly, site); + } + var multiplier = togo.constraint ? fluid.priorityTypes[togo.constraint.type] : 0; + if (multiplier !== 0) { + var target = togo.constraint.target || "none"; + var extremal = fluid.extremalPriorities[target]; + if (extremal === undefined) { + fluid.fail("Unrecognised extremal priority target " + target + ": the currently supported values are " + fluid.keys(fluid.extremalPriorities).join(", ") + ": register your value in fluid.extremalPriorities"); + } + togo.fixed = multiplier * (fluid.extremePriority + extremal); + } + if (togo.fixed !== null) { + togo.fixed += togo.count / 1024; // use some fractional bits to encode count bias + } + + return togo; + }; + + fluid.renderPriority = function (parsed) { + return parsed.constraint ? (parsed.constraint.target ? parsed.constraint.type + ":" + parsed.constraint.target : parsed.constraint.type ) : Math.floor(parsed.fixed); + }; + + // unsupported, NON-API function + fluid.compareByPriority = function (recA, recB) { + if (recA.priority.fixed !== null && recB.priority.fixed !== null) { + return recA.priority.fixed - recB.priority.fixed; + } else { // sort constraint records to the end + // relies on JavaScript boolean coercion rules (ECMA 9.3 toNumber) + return (recA.priority.fixed === null) - (recB.priority.fixed === null); + } + }; + + fluid.honourConstraint = function (array, firstConstraint, c) { + var constraint = array[c].priority.constraint; + var matchIndex = fluid.find(array, function (element, index) { + return element.namespace === constraint.target ? index : undefined; + }, -1); + if (matchIndex === -1) { // TODO: We should report an error during firing if this condition persists until then + return true; + } else if (matchIndex >= firstConstraint) { + return false; + } else { + var offset = constraint.type === "after" ? 1 : 0; + var target = matchIndex + offset; + var temp = array[c]; + for (var shift = c; shift >= target; -- shift) { + array[shift] = array[shift - 1]; + } + array[target] = temp; + return true; + } + }; + + // unsupported, NON-API function + // Priorities accepted from users have higher numbers representing high priority (sort first) - + fluid.sortByPriority = function (array) { + fluid.stableSort(array, fluid.compareByPriority); + + var firstConstraint = fluid.find(array, function (element, index) { + return element.priority.constraint && fluid.priorityTypes[element.priority.constraint.type] === 0 ? index : undefined; + }, array.length); + + while (true) { + if (firstConstraint === array.length) { + return array; + } + var oldFirstConstraint = firstConstraint; + for (var c = firstConstraint; c < array.length; ++ c) { + var applied = fluid.honourConstraint(array, firstConstraint, c); + if (applied) { + ++firstConstraint; + } + } + if (firstConstraint === oldFirstConstraint) { + var holders = array.slice(firstConstraint); + fluid.fail("Could not find targets for any constraints in " + holders[0].priority.site + " ", holders, ": none of the targets (" + fluid.getMembers(holders, "priority.constraint.target").join(", ") + + ") matched any namespaces of the elements in (", array.slice(0, firstConstraint) + ") - this is caused by either an invalid or circular reference"); + } + } + }; + + fluid.event.identifyListener = function (listener, soft) { + if (typeof(listener) !== "string" && !listener.$$fluid_guid && !soft) { + listener.$$fluid_guid = fluid.allocateGuid(); + } + return listener.$$fluid_guid; + }; + + // unsupported, NON-API function + fluid.event.impersonateListener = function (origListener, newListener) { + fluid.event.identifyListener(origListener); + newListener.$$fluid_guid = origListener.$$fluid_guid; + }; + + + // unsupported, NON-API function + fluid.event.sortListeners = function (listeners) { + var togo = []; + fluid.each(listeners, function (oneNamespace) { + var headHard; // notify only the first listener with hard namespace - or else all if all are soft + for (var i = 0; i < oneNamespace.length; ++ i) { + var thisListener = oneNamespace[i]; + if (!thisListener.softNamespace && !headHard) { + headHard = thisListener; + } + } + if (headHard) { + togo.push(headHard); + } else { + togo = togo.concat(oneNamespace); + } + }); + return fluid.sortByPriority(togo); + }; + + // unsupported, non-API function + fluid.event.invokeListener = function (listener, args) { + if (typeof(listener) === "string") { + listener = fluid.event.resolveListener({globalName: listener}); // just resolves globals + } + return listener.apply(null, args); + }; + + // unsupported, NON-API function + fluid.event.resolveListener = function (listener) { + if (listener.globalName) { + var listenerFunc = fluid.getGlobalValue(listener.globalName); + if (!listenerFunc) { + fluid.fail("Unable to look up name " + listener.globalName + " as a global function"); + } else { + listener = listenerFunc; + } + } + return listener; + }; + + /** Generate a name for a component for debugging purposes */ + fluid.nameComponent = function (that) { + return that ? "component with typename " + that.typeName + " and id " + that.id : "[unknown component]"; + }; + + fluid.event.nameEvent = function (that, eventName) { + return eventName + " of " + fluid.nameComponent(that); + }; + + /** 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 {Object} options - A structure to configure this event firer. Supported fields: + * {String} name - a name for this firer + * {Boolean} preventable - If true the return value of each handler will + * be checked for false in which case further listeners will be shortcircuited, and this + * will be the return value of fire() + */ + fluid.makeEventFirer = function (options) { + options = options || {}; + var name = options.name || ""; + var that; + function fireToListeners(listeners, args, wrapper) { + if (!listeners || that.destroyed) { return; } + fluid.log(fluid.logLevel.TRACE, "Firing event " + name + " to list of " + listeners.length + " listeners"); + for (var i = 0; i < listeners.length; ++i) { + var lisrec = listeners[i]; + lisrec.listener = fluid.event.resolveListener(lisrec.listener); + var listener = lisrec.listener; + + if (lisrec.predicate && !lisrec.predicate(listener, args)) { + continue; + } + var value; + var ret = (wrapper ? wrapper(listener) : listener).apply(null, args); + if (options.preventable && ret === false || that.destroyed) { + value = false; + } + if (value !== undefined) { + return value; + } + } + } + var identify = fluid.event.identifyListener; + + var lazyInit = function () { // Lazy init function to economise on object references for events which are never listened to + that.listeners = {}; + that.byId = {}; + that.sortedListeners = []; + that.addListener = function (listener, namespace, priority, predicate, softNamespace) { + if (that.destroyed) { + fluid.fail("Cannot add listener to destroyed event firer " + that.name); + } + if (!listener) { + return; + } + if (typeof(listener) === "string") { + listener = {globalName: listener}; + } + var id = identify(listener); + namespace = namespace || id; + var record = {listener: listener, predicate: predicate, + namespace: namespace, + softNamespace: softNamespace, + priority: fluid.parsePriority(priority, that.sortedListeners.length, false, "listeners")}; + that.byId[id] = record; + + var thisListeners = (that.listeners[namespace] = fluid.makeArray(that.listeners[namespace])); + thisListeners[softNamespace ? "push" : "unshift"] (record); + + that.sortedListeners = fluid.event.sortListeners(that.listeners); + }; + that.addListener.apply(null, arguments); + }; + that = { + eventId: fluid.allocateGuid(), + name: name, + ownerId: options.ownerId, + typeName: "fluid.event.firer", + destroy: function () { + that.destroyed = true; + }, + addListener: function () { + lazyInit.apply(null, arguments); + }, + + removeListener: function (listener) { + if (!that.listeners) { return; } + var namespace, id, record; + if (typeof (listener) === "string") { + namespace = listener; + record = that.listeners[namespace]; + if (!record) { + return; + } + } + else if (typeof(listener) === "function") { + id = identify(listener, true); + if (!id) { + fluid.fail("Cannot remove unregistered listener function ", listener, " from event " + that.name); + } + } + var rec = that.byId[id]; + var softNamespace = rec && rec.softNamespace; + namespace = namespace || (rec && rec.namespace) || id; + delete that.byId[id]; + record = that.listeners[namespace]; + if (!record) { + return; + } + if (softNamespace) { + fluid.remove_if(record, function (thisLis) { + return thisLis.listener.$$fluid_guid === id; + }); + } else { + record.shift(); + } + if (record.length === 0) { + delete that.listeners[namespace]; + } + that.sortedListeners = fluid.event.sortListeners(that.listeners); + }, + fire: function () { + return fireToListeners(that.sortedListeners, arguments); + } + }; + return that; + }; + + /** Fire the specified event with supplied arguments. This call is an optimisation utility + * which handles the case where the firer has not been instantiated (presumably as a result + * of having no listeners registered) + */ + + fluid.fireEvent = function (component, path, args) { + var firer = fluid.get(component, path); + if (firer) { + firer.fire.apply(null, fluid.makeArray(args)); + } + }; + + // unsupported, NON-API function + fluid.event.addListenerToFirer = function (firer, value, namespace, wrapper) { + wrapper = wrapper || fluid.identity; + if (fluid.isArrayable(value)) { + for (var i = 0; i < value.length; ++i) { + fluid.event.addListenerToFirer(firer, value[i], namespace, wrapper); + } + } else if (typeof (value) === "function" || typeof (value) === "string") { + wrapper(firer).addListener(value, namespace); + } else if (value && typeof (value) === "object") { + wrapper(firer).addListener(value.listener, namespace || value.namespace, value.priority, value.predicate, value.softNamespace); + } + }; + + // unsupported, NON-API function - non-IOC passthrough + fluid.event.resolveListenerRecord = function (records) { + return { records: records }; + }; + + fluid.expandImmediate = function (material) { + fluid.fail("fluid.expandImmediate could not be loaded - please include FluidIoC.js in order to operate IoC-driven event with descriptor " + material); + }; + + // unsupported, NON-API function + fluid.mergeListeners = function (that, events, listeners) { + fluid.each(listeners, function (value, key) { + var firer, namespace; + if (key.charAt(0) === "{") { + firer = fluid.expandImmediate(key, that); + if (!firer) { + fluid.fail("Error in listener record: key " + key + " could not be looked up to an event firer - did you miss out \"events.\" when referring to an event firer?"); + } + } 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"); + } + firer = events[key]; + } + var record = fluid.event.resolveListenerRecord(value, that, key, namespace, true); + fluid.event.addListenerToFirer(firer, record.records, namespace, record.adderWrapper); + }); + }; + + // unsupported, NON-API function + fluid.eventFromRecord = function (eventSpec, eventKey, that) { + var isIoCEvent = eventSpec && (typeof (eventSpec) !== "string" || eventSpec.charAt(0) === "{"); + var event; + if (isIoCEvent) { + 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 { + event = fluid.makeEventFirer({ + name: fluid.event.nameEvent(that, eventKey), + preventable: eventSpec === "preventable", + ownerId: that.id + }); + } + return event; + }; + + // unsupported, NON-API function - this is patched from FluidIoC.js + fluid.instantiateFirers = function (that, options) { + fluid.each(options.events, function (eventSpec, eventKey) { + that.events[eventKey] = fluid.eventFromRecord(eventSpec, eventKey, that); + }); + }; + + // unsupported, NON-API function + fluid.mergeListenerPolicy = function (target, source, key) { + if (typeof (key) !== "string") { + fluid.fail("Error in listeners declaration - the keys in this structure must resolve to event names - got " + key + " from ", source); + } + // cf. triage in mergeListeners + var hasNamespace = key.charAt(0) !== "{" && key.indexOf(".") !== -1; + return hasNamespace ? (source || target) : fluid.arrayConcatPolicy(target, source); + }; + + // unsupported, NON-API function + fluid.makeMergeListenersPolicy = function (merger) { + return function (target, source) { + target = target || {}; + fluid.each(source, function (listeners, key) { + target[key] = merger(target[key], listeners, key); + }); + return target; + }; + }; + + /** Removes duplicated and empty elements from an already sorted array **/ + fluid.unique = function (array) { + return fluid.remove_if(array, function (element, i) { + return !element || i > 0 && element === array[i - 1]; + }); + }; + + fluid.arrayConcatPolicy = function (target, source) { + return fluid.makeArray(target).concat(fluid.makeArray(source)); + }; + + /*** FLUID ERROR SYSTEM ***/ + + fluid.failureEvent = fluid.makeEventFirer({name: "failure event"}); + + fluid.failureEvent.addListener(fluid.builtinFail, "fail"); + fluid.failureEvent.addListener(fluid.logFailure, "log", "before:fail"); + + /** + * Configure the behaviour of fluid.fail by pushing or popping a disposition record onto a stack. + * @param {Number|Function} condition + & Supply either a function, which will be called with two arguments, args (the complete arguments to + * fluid.fail) and activity, an array of strings describing the current framework invocation state. + * Or, the argument may be the number -1 indicating that the previously supplied disposition should + * be popped off the stack + */ + fluid.pushSoftFailure = function (condition) { + if (typeof (condition) === "function") { + fluid.failureEvent.addListener(condition, "fail"); + } else if (condition === -1) { + fluid.failureEvent.removeListener("fail"); + } else if (typeof(condition) === "boolean") { + fluid.fail("pushSoftFailure with boolean value is no longer supported"); + } + }; + + /*** DEFAULTS AND OPTIONS MERGING SYSTEM ***/ + + + /** Create a "type tag" component with no state but simply a type name and id. The most + * minimal form of Fluid component */ + // No longer a publically supported function - we don't abolish this because it is too annoying to prevent + // circularity during the bootup of the IoC system if we try to construct full components before it is complete + // unsupported, non-API function + fluid.typeTag = function (name) { + return name ? { + typeName: name, + id: fluid.allocateGuid() + } : null; + }; + + + + var gradeTick = 1; // tick counter for managing grade cache invalidation + var gradeTickStore = {}; + + fluid.defaultsStore = {}; + + var resolveGradesImpl = function (gs, gradeNames, base) { + var raw = true; + if (base) { + raw = gradeNames.length === 1; // We are just resolving a single grade and populating the cache + } + else { + gradeNames = fluid.makeArray(gradeNames); + } + fluid.each(gradeNames, function (gradeName) { + if (gradeName && !gs.gradeHash[gradeName]) { + var isDynamic = gradeName.charAt(0) === "{"; + var options = (isDynamic ? null : (raw ? fluid.rawDefaults(gradeName) : fluid.getGradedDefaults(gradeName))) || {}; + var thisTick = gradeTickStore[gradeName] || (gradeTick - 1); // a nonexistent grade is recorded as previous to current + gs.lastTick = Math.max(gs.lastTick, thisTick); + gs.gradeHash[gradeName] = true; + gs.gradeChain.push(gradeName); + gs.optionsChain.push(options); + var oGradeNames = fluid.makeArray(options.gradeNames); + for (var i = 0; i < oGradeNames.length; ++ i) { + var oGradeName = oGradeNames[i]; + if (raw) { + resolveGradesImpl(gs, oGradeName); + } else { + if (!gs.gradeHash[oGradeName]) { + gs.gradeHash[oGradeName] = true; // these have already been resolved + gs.gradeChain.push(oGradeName); + } + } + } + } + }); + return gs; + }; + + // unsupported, NON-API function + fluid.resolveGradeStructure = function (defaultName, gradeNames) { + var gradeStruct = { + lastTick: 0, + gradeChain: [], + gradeHash: {}, + optionsChain: [] + }; + // stronger grades appear to the left in defaults - dynamic grades are stronger still - FLUID-5085 + return resolveGradesImpl(gradeStruct, (fluid.makeArray(gradeNames).reverse() || []).concat([defaultName]), true); + }; + + var mergedDefaultsCache = {}; + + // unsupported, NON-API function + fluid.gradeNamesToKey = function (defaultName, gradeNames) { + return defaultName + "|" + gradeNames.join("|"); + }; + + fluid.hasGrade = function (options, gradeName) { + return !options || !options.gradeNames ? false : fluid.contains(options.gradeNames, gradeName); + }; + + // unsupported, NON-API function + fluid.resolveGrade = function (defaults, defaultName, gradeNames) { + var gradeStruct = fluid.resolveGradeStructure(defaultName, gradeNames); + var mergeArgs = gradeStruct.optionsChain.reverse(); + var mergePolicy = {}; + for (var i = 0; i < mergeArgs.length; ++ i) { + if (mergeArgs[i] && mergeArgs[i].mergePolicy) { + mergePolicy = $.extend(true, mergePolicy, mergeArgs[i].mergePolicy); + } + } + mergeArgs = [mergePolicy, {}].concat(mergeArgs); + var mergedDefaults = fluid.merge.apply(null, mergeArgs); + mergedDefaults.gradeNames = gradeStruct.gradeChain; + return {defaults: mergedDefaults, lastTick: gradeStruct && gradeStruct.lastTick}; + }; + + // unsupported, NON-API function + fluid.getGradedDefaults = function (defaultName, gradeNames) { + gradeNames = fluid.makeArray(gradeNames); + var key = fluid.gradeNamesToKey(defaultName, gradeNames); + var mergedDefaults = mergedDefaultsCache[key]; + if (mergedDefaults) { + var lastTick = 0; // check if cache should be invalidated through real latest tick being later than the one stored + var searchGrades = mergedDefaults.defaults.gradeNames || []; + for (var i = 0; i < searchGrades.length; ++ i) { + lastTick = Math.max(lastTick, gradeTickStore[searchGrades[i]] || 0); + } + if (lastTick > mergedDefaults.lastTick) { + fluid.log("Clearing cache for component " + defaultName + " with gradeNames ", searchGrades); + mergedDefaults = null; + } + } + if (!mergedDefaults) { + var defaults = fluid.rawDefaults(defaultName); + if (!defaults) { + return defaults; + } + mergedDefaults = mergedDefaultsCache[key] = fluid.resolveGrade(defaults, defaultName, gradeNames); + } + return mergedDefaults.defaults; + }; + + // unsupported, NON-API function + // Modify supplied options record to include "componentSource" annotation required by FLUID-5082 + // TODO: This function really needs to act recursively in order to catch listeners registered for subcomponents + fluid.annotateListeners = function (componentName, options) { + if (options.listeners) { + options.listeners = fluid.transform(options.listeners, function (record) { + var togo = fluid.makeArray(record); + return fluid.transform(togo, function (onerec) { + if (!fluid.isPrimitive(onerec)) { + onerec.componentSource = componentName; + } + return onerec; + }); + }); + } + }; + + // unsupported, NON-API function + fluid.rawDefaults = function (componentName, options) { + if (options === undefined) { + var entry = fluid.defaultsStore[componentName]; + return entry && entry.options; + } else { + fluid.pushActivity("registerDefaults", "registering defaults for grade %componentName with options %options", + {componentName: componentName, options: options}); + var optionsCopy = fluid.expandCompact ? fluid.expandCompact(options) : fluid.copy(options); + fluid.annotateListeners(componentName, optionsCopy); + var callerInfo = fluid.getCallerInfo && fluid.getCallerInfo(6); + fluid.defaultsStore[componentName] = { + options: optionsCopy, + callerInfo: callerInfo + }; + gradeTickStore[componentName] = gradeTick++; + fluid.popActivity(); + } + }; + + // unsupported, NON-API function + fluid.doIndexDefaults = function (defaultName, defaults, index, indexSpec) { + var requiredGrades = fluid.makeArray(indexSpec.gradeNames); + for (var i = 0; i < requiredGrades.length; ++ i) { + if (!fluid.hasGrade(defaults, requiredGrades[i])) { return; } + } + var indexFunc = typeof(indexSpec.indexFunc) === "function" ? indexSpec.indexFunc : fluid.getGlobalValue(indexSpec.indexFunc); + var keys = indexFunc(defaults) || []; + for (var j = 0; j < keys.length; ++ j) { + (index[keys[j]] = index[keys[j]] || []).push(defaultName); + } + }; + + /** Evaluates an index specification over all the defaults records registered into the system. + * @param indexName {String} The name of this index record (currently ignored) + * @param indexSpec {Object} Specification of the index to be performed - fields: + * gradeNames: {String/Array of String} List of grades that must be matched by this indexer + * indexFunc: {String/Function} An index function which accepts a defaults record and returns a list of keys + * @return A structure indexing keys to lists of matched gradenames + */ + // The expectation is that this function is extremely rarely used with respect to registration of defaults + // in the system, so currently we do not make any attempts to cache the results. The field "indexName" is + // supplied in case a future implementation chooses to implement caching + fluid.indexDefaults = function (indexName, indexSpec) { + var index = {}; + for (var defaultName in fluid.defaultsStore) { + var defaults = fluid.getGradedDefaults(defaultName); + fluid.doIndexDefaults(defaultName, defaults, index, indexSpec); + } + return index; + }; + + /** + * Retrieves and stores a component's default settings centrally. + * @param {String} componentName the name of the component + * @param {Object} (optional) an container of key/value pairs to set + */ + + fluid.defaults = function (componentName, options) { + if (options === undefined) { + return fluid.getGradedDefaults(componentName); + } + else { + if (options && options.options) { + fluid.fail("Probable error in options structure for " + componentName + + " with option named \"options\" - perhaps you meant to write these options at top level in fluid.defaults? - ", options); + } + fluid.rawDefaults(componentName, options); + var gradedDefaults = fluid.getGradedDefaults(componentName); + if (!fluid.hasGrade(gradedDefaults, "fluid.function")) { + fluid.makeComponentCreator(componentName); + } + } + }; + + fluid.makeComponentCreator = function (componentName) { + var creator = function () { + var defaults = fluid.getGradedDefaults(componentName); + if (!defaults.gradeNames || defaults.gradeNames.length === 0) { + fluid.fail("Cannot make component creator for type " + componentName + " which does not have any gradeNames defined"); + } else if (!defaults.initFunction) { + var blankGrades = []; + for (var i = 0; i < defaults.gradeNames.length; ++ i) { + var gradeName = defaults.gradeNames[i]; + var rawDefaults = fluid.rawDefaults(gradeName); + if (!rawDefaults) { + blankGrades.push(gradeName); + } + } + if (blankGrades.length === 0) { + fluid.fail("Cannot make component creator for type " + componentName + " which does not have an initFunction defined"); + } else { + fluid.fail("The grade hierarchy of component with type " + componentName + " is incomplete - it inherits from the following grade(s): " + + blankGrades.join(", ") + " for which the grade definitions are corrupt or missing. Please check the files which might include these " + + "grades and ensure they are readable and have been loaded by this instance of Infusion"); + } + } else { + return fluid.initComponent(componentName, arguments); + } + }; + var existing = fluid.getGlobalValue(componentName); + if (existing) { + $.extend(creator, existing); + } + fluid.setGlobalValue(componentName, creator); + }; + + // Cheapskate implementation which avoids dependency on DataBinding.js + fluid.model.mergeModel = function (target, source) { + if (!fluid.isPrimitive(target)) { + var copySource = fluid.copy(source); + $.extend(true, source, target); + $.extend(true, source, copySource); + } + return source; + }; + + var emptyPolicy = {}; + // unsupported, NON-API function + fluid.derefMergePolicy = function (policy) { + return (policy? policy["*"]: emptyPolicy) || emptyPolicy; + }; + + // unsupported, NON-API function + fluid.compileMergePolicy = function (mergePolicy) { + var builtins = {}, defaultValues = {}; + var togo = {builtins: builtins, defaultValues: defaultValues}; + + if (!mergePolicy) { + return togo; + } + fluid.each(mergePolicy, function (value, key) { + var parsed = {}, builtin = true; + if (typeof(value) === "function") { + parsed.func = value; + } + else if (typeof(value) === "object") { + parsed = value; + } + else if (!fluid.isDefaultValueMergePolicy(value)) { + var split = value.split(/\s*,\s*/); + for (var i = 0; i < split.length; ++ i) { + parsed[split[i]] = true; + } + } + else { + // Convert to ginger self-reference - NB, this can only be parsed by IoC + fluid.set(defaultValues, key, "{that}.options." + value); + togo.hasDefaults = true; + builtin = false; + } + if (builtin) { + fluid.set(builtins, fluid.composePath(key, "*"), parsed); + } + }); + return togo; + }; + + // TODO: deprecate this method of detecting default value merge policies before 1.6 in favour of + // explicit typed records a la ModelTransformations + // unsupported, NON-API function + fluid.isDefaultValueMergePolicy = function (policy) { + return typeof(policy) === "string" && + (policy.indexOf(",") === -1 && !/replace|nomerge|noexpand/.test(policy)); + }; + + // unsupported, NON-API function + fluid.mergeOneImpl = function (thisTarget, thisSource, j, sources, newPolicy, i, segs) { + var togo = thisTarget; + + var primitiveTarget = fluid.isPrimitive(thisTarget); + + if (thisSource !== undefined) { + if (!newPolicy.func && thisSource !== null && fluid.isPlainObject(thisSource) && + !fluid.isDOMish(thisSource) && !fluid.isComponent(thisSource) && thisSource !== fluid.VALUE && !newPolicy.nomerge) { + if (primitiveTarget) { + togo = thisTarget = fluid.freshContainer(thisSource); + } + // recursion is now external? We can't do it from here since sources are not all known + // options.recurse(thisTarget, i + 1, segs, sources, newPolicyHolder, options); + } else { + sources[j] = undefined; + if (newPolicy.func) { + togo = newPolicy.func.call(null, thisTarget, thisSource, segs[i - 1], segs, i); // NB - change in this mostly unused argument + } else { + togo = fluid.isValue(thisTarget) ? fluid.model.mergeModel(thisTarget, thisSource) : thisSource; + } + } + } + return togo; + }; + // NB - same quadratic worry about these as in FluidIoC in the case the RHS trundler is live - + // since at each regeneration step driving the RHS we are discarding the "cursor arguments" these + // would have to be regenerated at each step - although in practice this can only happen once for + // each object for all time, since after first resolution it will be concrete. + function regenerateCursor (source, segs, limit, sourceStrategy) { + for (var i = 0; i < limit; ++ i) { + source = sourceStrategy(source, segs[i], i, fluid.makeArray(segs)); // copy for FLUID-5243 + } + return source; + } + + function regenerateSources (sources, segs, limit, sourceStrategies) { + var togo = []; + for (var i = 0; i < sources.length; ++ i) { + var thisSource = regenerateCursor(sources[i], segs, limit, sourceStrategies[i]); + if (thisSource !== undefined) { + togo.push(thisSource); + } + } + return togo; + } + + // unsupported, NON-API function + fluid.fetchMergeChildren = function (target, i, segs, sources, mergePolicy, options) { /* unused parameter left for documentation purposes */ // jshint ignore:line + var thisPolicy = fluid.derefMergePolicy(mergePolicy); + for (var j = sources.length - 1; j >= 0; -- j) { // this direction now irrelevant - control is in the strategy + var source = sources[j]; + // NB - this detection relies on strategy return being complete objects - which they are + // although we need to set up the roots separately. We need to START the process of evaluating each + // object root (sources) COMPLETELY, before we even begin! Even if the effect of this is to cause a + // dispatch into ourselves almost immediately. We can do this because we can take control over our + // TARGET objects and construct them early. Even if there is a self-dispatch, it will be fine since it is + // DIRECTED and so will not trouble our "slow" detection of properties. After all self-dispatches end, control + // will THEN return to "evaluation of arguments" (expander blocks) and only then FINALLY to this "slow" + // traversal of concrete properties to do the final merge. + if (source !== undefined) { + fluid.each(source, function (newSource, name) { + if (!(name in target)) { // only request each new target key once -- all sources will be queried per strategy + segs[i] = name; + if (!fluid.getImmediate(options.exceptions, segs, i)) { + options.strategy(target, name, i + 1, segs, sources, mergePolicy); + } + } + }); /* function in loop */ // jshint ignore:line + if (thisPolicy.replace) { // this branch primarily deals with a policy of replace at the root + break; + } + } + } + return target; + }; + + // A special marker object which will be placed at a current evaluation point in the tree in order + // to protect against circular evaluation + fluid.inEvaluationMarker = Object.freeze({"__CURRENTLY_IN_EVALUATION__": true}); + + // A path depth above which the core "process strategies" will bail out, assuming that the + // structure has become circularly linked. Helpful in environments such as Firebug which will + // kill the browser process if they happen to be open when a stack overflow occurs. Also provides + // a more helpful diagnostic. + fluid.strategyRecursionBailout = 50; + + // unsupported, NON-API function + fluid.makeMergeStrategy = function (options) { + var strategy = function (target, name, i, segs, sources, policy) { + if (i > fluid.strategyRecursionBailout) { + fluid.fail("Overflow/circularity in options merging, current path is ", segs, " at depth " , i, " - please protect components from merging using the \"nomerge\" merge policy"); + } + if (fluid.isPrimitive(target)) { // For "use strict" + return undefined; // Review this after FLUID-4925 since the only trigger is in slow component lookahead + } + if (fluid.isTracing) { + fluid.tracing.pathCount.push(fluid.path(segs.slice(0, i))); + } + + var oldTarget; + if (name in target) { // bail out if our work has already been done + oldTarget = target[name]; + if (!options.evaluateFully) { // see notes on this hack in "initter" - early attempt to deal with FLUID-4930 + return oldTarget; + } + } + else { + if (target !== fluid.inEvaluationMarker) { // TODO: blatant "coding to the test" - this enables the simplest "re-trunking" in + // FluidIoCTests to function. In practice, we need to throw away this implementation entirely in favour of the + // "iterative deepening" model coming with FLUID-4925 + target[name] = fluid.inEvaluationMarker; + } + } + if (sources === undefined) { // recover our state in case this is an external entry point + segs = fluid.makeArray(segs); // avoid trashing caller's segs + sources = regenerateSources(options.sources, segs, i - 1, options.sourceStrategies); + policy = regenerateCursor(options.mergePolicy, segs, i - 1, fluid.concreteTrundler); + } + // var thisPolicy = fluid.derefMergePolicy(policy); + var newPolicyHolder = fluid.concreteTrundler(policy, name); + var newPolicy = fluid.derefMergePolicy(newPolicyHolder); + + var start, limit, mul; + if (newPolicy.replace) { + start = 1 - sources.length; limit = 0; mul = -1; /* on one line for easier visual comparison of the two algorithms */ // jshint ignore:line + } + else { + start = 0; limit = sources.length - 1; mul = +1; /* on one line for easier visual comparison of the two algorithms */ // jshint ignore:line + } + var newSources = []; + var thisTarget; + + for (var j = start; j <= limit; ++j) { // TODO: try to economise on this array and on gaps + var k = mul * j; + var thisSource = options.sourceStrategies[k](sources[k], name, i, segs); // Run the RH algorithm in "driving" mode + if (thisSource !== undefined) { + newSources[k] = thisSource; + if (oldTarget === undefined) { + if (mul === -1) { // if we are going backwards, it is "replace" + thisTarget = target[name] = thisSource; + break; + } + else { + // write this in early, since early expansions may generate a trunk object which is written in to by later ones + thisTarget = fluid.mergeOneImpl(thisTarget, thisSource, j, newSources, newPolicy, i, segs, options); + if (target !== fluid.inEvaluationMarker) { + target[name] = thisTarget; + } + } + } + } + } + if (oldTarget !== undefined) { + thisTarget = oldTarget; + } + if (newSources.length > 0) { + if (fluid.isPlainObject(thisTarget)) { + fluid.fetchMergeChildren(thisTarget, i, segs, newSources, newPolicyHolder, options); + } + } + if (oldTarget === undefined && newSources.length === 0) { + delete target[name]; // remove the evaluation marker - nothing to evaluate + } + return thisTarget; + }; + options.strategy = strategy; + return strategy; + }; + + // A simple stand-in for "fluid.get" where the material is covered by a single strategy + fluid.driveStrategy = function (root, pathSegs, strategy) { + pathSegs = fluid.makeArray(pathSegs); + for (var i = 0; i < pathSegs.length; ++ i) { + if (!root) { + return undefined; + } + root = strategy(root, pathSegs[i], i + 1, pathSegs); + } + return root; + }; + + // A very simple "new inner trundler" that just performs concrete property access + // Note that every "strategy" is also a "trundler" of this type, considering just the first two arguments + fluid.concreteTrundler = function (source, seg) { + return !source? undefined : source[seg]; + }; + + /** Merge a collection of options structures onto a target, following an optional policy. + * This method is now used only for the purpose of merging "dead" option documents in order to + * cache graded component defaults. Component option merging is now performed by the + * fluid.makeMergeOptions pathway which sets up a deferred merging process. This function + * will not be removed in the Fluid 2.0 release but it is recommended that users not call it + * directly. + * 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 "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 options1, options2, .... {Object} an arbitrary list of options structure which are to + * be merged together. These will not be modified. + */ + + fluid.merge = function (policy /*, ... sources */) { + var sources = Array.prototype.slice.call(arguments, 1); + var compiled = fluid.compileMergePolicy(policy).builtins; + var options = fluid.makeMergeOptions(compiled, sources, {}); + options.initter(); + return options.target; + }; + + // unsupported, NON-API function + fluid.simpleGingerBlock = function (source, recordType) { + var block = { + target: source, + simple: true, + strategy: fluid.concreteTrundler, + initter: fluid.identity, + recordType: recordType, + priority: fluid.mergeRecordTypes[recordType] + }; + return block; + }; + + // unsupported, NON-API function + fluid.makeMergeOptions = function (policy, sources, userOptions) { + var options = { + mergePolicy: policy, + sources: sources + }; + options = $.extend(options, userOptions); + options.target = options.target || fluid.freshContainer(options.sources[0]); + options.sourceStrategies = options.sourceStrategies || fluid.generate(options.sources.length, fluid.concreteTrundler); + options.initter = function () { + // This hack is necessary to ensure that the FINAL evaluation doesn't balk when discovering a trunk path which was already + // visited during self-driving via the expander. This bi-modality is sort of rubbish, but we currently don't have "room" + // in the strategy API to express when full evaluation is required - and the "flooding API" is not standardised. See FLUID-4930 + options.evaluateFully = true; + fluid.fetchMergeChildren(options.target, 0, [], options.sources, options.mergePolicy, options); + }; + fluid.makeMergeStrategy(options); + return options; + }; + + // unsupported, NON-API function + fluid.transformOptions = function (options, transRec) { + fluid.expect("Options transformation record", transRec, ["transformer", "config"]); + var transFunc = fluid.getGlobalValue(transRec.transformer); + return transFunc.call(null, options, transRec.config); + }; + + // unsupported, NON-API function + fluid.findMergeBlocks = function (mergeBlocks, recordType) { + return fluid.remove_if(fluid.makeArray(mergeBlocks), function (block) { return block.recordType !== recordType; }); + }; + + // unsupported, NON-API function + fluid.transformOptionsBlocks = function (mergeBlocks, transformOptions, recordTypes) { + fluid.each(recordTypes, function (recordType) { + var blocks = fluid.findMergeBlocks(mergeBlocks, recordType); + fluid.each(blocks, function (block) { + var source = block.source ? "source" : "target"; // TODO: Problem here with irregular presentation of options which consist of a reference in their entirety + block[block.simple || source === "target" ? "target": "source"] = fluid.transformOptions(block[source], transformOptions); + }); + }); + }; + + // unsupported, NON-API function + fluid.deliverOptionsStrategy = fluid.identity; + fluid.computeComponentAccessor = fluid.identity; + fluid.computeDynamicComponents = fluid.identity; + + // The types of merge record the system supports, with the weakest records first + fluid.mergeRecordTypes = { + defaults: 1000, + defaultValueMerge: 900, + subcomponentRecord: 800, + user: 700, + distribution: 100 // and above + }; + + // Utility used in the framework (primarily with distribution assembly), unconnected with new ChangeApplier + // unsupported, NON-API function + fluid.model.applyChangeRequest = function (model, request) { + var segs = request.segs; + if (segs.length === 0) { + if (request.type === "ADD") { + $.extend(true, model, request.value); + } else { + fluid.clear(model); + } + } else if (request.type === "ADD") { + fluid.model.setSimple(model, request.segs, request.value); + } else { + for (var i = 0; i < segs.length - 1; ++ i) { + model = model[segs[i]]; + if (!model) { + return; + } + } + var last = segs[segs.length - 1]; + delete model[last]; + } + }; + + /** Delete the value in the supplied object held at the specified path + * @param target {Object} The object holding the value to be deleted (possibly empty) + * @param segs {Array of String} the path of the value to be deleted + */ + // unsupported, NON-API function + fluid.destroyValue = function (target, segs) { + if (target) { + fluid.model.applyChangeRequest(target, {type: "DELETE", segs: segs}); + } + }; + + /** + * 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 rawDefaults = fluid.rawDefaults(componentName); + var defaults = fluid.getGradedDefaults(componentName, rawDefaults && rawDefaults.gradeNames ? null : localOptions.gradeNames); + var sharedMergePolicy = {}; + + var mergeBlocks = []; + + if (fluid.expandComponentOptions) { + mergeBlocks = mergeBlocks.concat(fluid.expandComponentOptions(sharedMergePolicy, defaults, userOptions, that)); + } + else { + mergeBlocks = mergeBlocks.concat([fluid.simpleGingerBlock(defaults, "defaults"), + fluid.simpleGingerBlock(userOptions, "user")]); + } + var options = {}; // ultimate target + var sourceStrategies = [], sources = []; + var baseMergeOptions = { + target: options, + sourceStrategies: sourceStrategies + }; + // Called both from here and from IoC whenever there is a change of block content or arguments which + // requires them to be resorted and rebound + var updateBlocks = function () { + fluid.each(mergeBlocks, function (block) { + if (fluid.isPrimitive(block.priority)) { + block.priority = fluid.parsePriority(block.priority, 0, false, "options distribution"); + } + }); + fluid.sortByPriority(mergeBlocks); + sourceStrategies.length = 0; + sources.length = 0; + fluid.each(mergeBlocks, function (block) { + sourceStrategies.push(block.strategy); + sources.push(block.target); + }); + }; + updateBlocks(); + var mergeOptions = fluid.makeMergeOptions(sharedMergePolicy, sources, baseMergeOptions); + mergeOptions.mergeBlocks = mergeBlocks; + mergeOptions.updateBlocks = updateBlocks; + mergeOptions.destroyValue = function (segs) { // This method is a temporary hack to assist FLUID-5091 + for (var i = 0; i < mergeBlocks.length; ++ i) { + fluid.destroyValue(mergeBlocks[i].target, segs); + } + fluid.destroyValue(baseMergeOptions.target, segs); + }; + + var compiledPolicy; + var mergePolicy; + function computeMergePolicy() { + // Decode the now available mergePolicy + mergePolicy = fluid.driveStrategy(options, "mergePolicy", mergeOptions.strategy); + mergePolicy = $.extend({}, fluid.rootMergePolicy, mergePolicy); + compiledPolicy = fluid.compileMergePolicy(mergePolicy); + // TODO: expandComponentOptions has already put some builtins here - performance implications of the now huge + // default mergePolicy material need to be investigated as well as this deep merge + $.extend(true, sharedMergePolicy, compiledPolicy.builtins); // ensure it gets broadcast to all sharers + } + computeMergePolicy(); + + if (compiledPolicy.hasDefaults) { + if (fluid.generateExpandBlock) { + mergeBlocks.push(fluid.generateExpandBlock({ + options: compiledPolicy.defaultValues, + recordType: "defaultValueMerge", + priority: fluid.mergeRecordTypes.defaultValueMerge + }, that, {})); + updateBlocks(); + } + else { + fluid.fail("Cannot operate mergePolicy ", mergePolicy, " for component ", that, " without including FluidIoC.js"); + } + } + that.options = options; + fluid.driveStrategy(options, "gradeNames", mergeOptions.strategy); + + fluid.deliverOptionsStrategy(that, options, mergeOptions); // do this early to broadcast and receive "distributeOptions" + + fluid.computeComponentAccessor(that); + + var transformOptions = fluid.driveStrategy(options, "transformOptions", mergeOptions.strategy); + if (transformOptions) { + fluid.transformOptionsBlocks(mergeBlocks, transformOptions, ["user", "subcomponentRecord"]); + updateBlocks(); // because the possibly simple blocks may have changed target + } + + if (!baseMergeOptions.target.mergePolicy) { + computeMergePolicy(); + } + + return mergeOptions; + }; + + // The Fluid Component System proper + + // The base system grade definitions + + fluid.defaults("fluid.function", {}); + + /** Invoke a global function by name and named arguments. A courtesy to allow declaratively encoded function calls + * to use named arguments rather than bare arrays. + * @param name {String} A global name which can be resolved to a Function. The defaults for this name must + * resolve onto a grade including "fluid.function". The defaults record should also contain an entry + * argumentMap, a hash of argument names onto indexes. + * @param spec {Object} A named hash holding the argument values to be sent to the function. These will be looked + * up in the argumentMap and resolved into a flat list of arguments. + * @return {Any} The return value from the function + */ + + fluid.invokeGradedFunction = function (name, spec) { + var defaults = fluid.defaults(name); + if (!defaults || !defaults.argumentMap || !fluid.hasGrade(defaults, "fluid.function")) { + fluid.fail("Cannot look up name " + name + + " to a function with registered argumentMap - got defaults ", defaults); + } + var args = []; + fluid.each(defaults.argumentMap, function (value, key) { + args[value] = spec[key]; + }); + return fluid.invokeGlobalFunction(name, args); + }; + + fluid.noNamespaceDistributionPrefix = "no-namespace-distribution-"; + + fluid.mergeOneDistribution = function (target, source, key) { + var namespace = source.namespace || key || fluid.noNamespaceDistributionPrefix + fluid.allocateGuid(); + source.namespace = namespace; + target[namespace] = source; + }; + + fluid.distributeOptionsPolicy = function (target, source) { + target = target || {}; + if (fluid.isArrayable(source)) { + for (var i = 0; i < source.length; ++ i) { + fluid.mergeOneDistribution(target, source[i]); + } + } else if (typeof(source.target) === "string") { + fluid.mergeOneDistribution(target, source); + } else { + fluid.each(source, function (oneSource, key) { + fluid.mergeOneDistribution(target, oneSource, key); + }); + } + return target; + }; + + fluid.mergingArray = function () {}; + fluid.mergingArray.prototype = []; + + // Defer all evaluation of all nested members to resolve FLUID-5668 + fluid.membersOptionsPolicy = function (target, source) { + target = target || {}; + fluid.each(source, function (oneSource, key) { + if (!target[key]) { + target[key] = new fluid.mergingArray(); + } + if (oneSource instanceof fluid.mergingArray) { + target[key].push.apply(target[key], oneSource); + } else if (oneSource !== undefined) { + target[key].push(oneSource); + } + }); + return target; + }; + + fluid.rootMergePolicy = { + gradeNames: fluid.arrayConcatPolicy, + distributeOptions: fluid.distributeOptionsPolicy, + members: { + noexpand: true, + func: fluid.membersOptionsPolicy + }, + transformOptions: "replace", + listeners: fluid.makeMergeListenersPolicy(fluid.mergeListenerPolicy) + }; + + fluid.defaults("fluid.component", { + initFunction: "fluid.initLittleComponent", + mergePolicy: fluid.rootMergePolicy, + argumentMap: { + options: 0 + }, + events: { // Three standard lifecycle points common to all components + onCreate: null, + onDestroy: null, + afterDestroy: null + } + }); + + fluid.defaults("fluid.emptySubcomponent", { + gradeNames: ["fluid.component"] + }); + + /** 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]; + }; + + /** A specially recognised grade tag which directs the IoC framework to instantiate this component first amongst + * its set of siblings, since it is likely to bear a context-forming type name. This will be removed from the framework + * once we have implemented FLUID-4925 "wave of explosions" */ + + fluid.defaults("fluid.typeFount", { + gradeNames: ["fluid.component"] + }); + + /** + * 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 - similarly for 4th arg "receiver" + // NOTE historical name to avoid confusion with fluid.initComponent below - this will all be refactored with FLUID-4925 + fluid.initLittleComponent = function (name, userOptions, localOptions, receiver) { + var that = fluid.typeTag(name); + that.lifecycleStatus = "constructing"; + localOptions = localOptions || {gradeNames: "fluid.component"}; + + that.destroy = fluid.makeRootDestroy(that); // overwritten by FluidIoC for constructed subcomponents + var mergeOptions = fluid.mergeComponentOptions(that, name, userOptions, localOptions); + mergeOptions.exceptions = {members: {model: true, modelRelay: true}}; // don't evaluate these in "early flooding" - they must be fetched explicitly + var options = that.options; + that.events = {}; + // deliver to a non-IoC side early receiver of the component (currently only initView) + (receiver || fluid.identity)(that, options, mergeOptions.strategy); + fluid.computeDynamicComponents(that, mergeOptions); + + // TODO: ****THIS**** is the point we must deliver and suspend!! Construct the "component skeleton" first, and then continue + // for as long as we can continue to find components. + for (var i = 0; i < mergeOptions.mergeBlocks.length; ++ i) { + mergeOptions.mergeBlocks[i].initter(); + } + mergeOptions.initter(); + delete options.mergePolicy; + + fluid.instantiateFirers(that, options); + fluid.mergeListeners(that, that.events, options.listeners); + + return that; + }; + + fluid.diagnoseFailedView = fluid.identity; + + // unsupported, NON-API function + fluid.makeRootDestroy = function (that) { + return function () { + fluid.doDestroy(that); + fluid.fireEvent(that, "events.afterDestroy", [that, "", null]); + }; + }; + + /** Returns true if the supplied reference holds a component which has been destroyed **/ + + fluid.isDestroyed = function (that) { + return that.lifecycleStatus === "destroyed"; + }; + + // unsupported, NON-API function + fluid.doDestroy = function (that, name, parent) { + fluid.fireEvent(that, "events.onDestroy", [that, name || "", parent]); + that.lifecycleStatus = "destroyed"; + for (var key in that.events) { + if (key !== "afterDestroy" && typeof(that.events[key].destroy) === "function") { + that.events[key].destroy(); + } + } + if (that.applier) { // TODO: Break this out into the grade's destroyer + that.applier.destroy(); + } + }; + + // unsupported, NON-API function + 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)); + var that; + fluid.pushActivity("initComponent", "constructing component of type %componentName with arguments %initArgs", + {componentName: componentName, initArgs: initArgs}); + that = fluid.invokeGlobalFunction(options.initFunction, args); + fluid.diagnoseFailedView(componentName, that, options, args); + if (fluid.initDependents) { + fluid.initDependents(that); + } + that.lifecycleStatus = "constructed"; + fluid.fireEvent(that, "events.onCreate", that); + fluid.popActivity(); + return 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; + togo = entryType === "fluid.emptySubcomponent" ? + null : fluid.invokeGlobalFunction(entryType, args); + } else { + togo = entry.apply(null, args); + } + return togo; + }; + + // ******* SELECTOR ENGINE ********* + + // selector regexps copied from jQuery - recent versions correct the range to start C0 + // The initial portion of the main character selector: "just add water" to add on extra + // accepted characters, as well as the "\\\\." -> "\." portion necessary for matching + // period characters escaped in selectors + var charStart = "(?:[\\w\\u00c0-\\uFFFF*_-"; + + fluid.simpleCSSMatcher = { + regexp: new RegExp("([#.]?)(" + charStart + "]|\\\\.)+)", "g"), + charToTag: { + "": "tag", + "#": "id", + ".": "clazz" + } + }; + + fluid.IoCSSMatcher = { + regexp: new RegExp("([&#]?)(" + charStart + "]|\\.|\\/)+)", "g"), + charToTag: { + "": "context", + "&": "context", + "#": "id" + } + }; + + var childSeg = new RegExp("\\s*(>)?\\s*", "g"); +// var whiteSpace = new RegExp("^\\w*$"); + + // Parses a selector expression into a data structure holding a list of predicates + // 2nd argument is a "strategy" structure, e.g. fluid.simpleCSSMatcher or fluid.IoCSSMatcher + // unsupported, non-API function + fluid.parseSelector = function (selstring, strategy) { + var togo = []; + selstring = $.trim(selstring); + //ws-(ss*)[ws/>] + var regexp = strategy.regexp; + regexp.lastIndex = 0; + var lastIndex = 0; + while (true) { + var atNode = []; // a list of predicates at a particular node + var first = true; + while (true) { + var segMatch = regexp.exec(selstring); + if (!segMatch) { + break; + } + if (segMatch.index !== lastIndex) { + if (first) { + fluid.fail("Error in selector string - cannot match child selector expression starting at " + selstring.substring(lastIndex)); + } + else { + break; + } + } + var thisNode = {}; + var text = segMatch[2]; + var targetTag = strategy.charToTag[segMatch[1]]; + if (targetTag) { + thisNode[targetTag] = text; + } + atNode[atNode.length] = thisNode; + lastIndex = regexp.lastIndex; + first = false; + } + childSeg.lastIndex = lastIndex; + var fullAtNode = {predList: atNode}; + var childMatch = childSeg.exec(selstring); + if (!childMatch || childMatch.index !== lastIndex) { + fluid.fail("Error in selector string - can not match child selector expression at " + selstring.substring(lastIndex)); + } + if (childMatch[1] === ">") { + fullAtNode.child = true; + } + togo[togo.length] = fullAtNode; + // >= test here to compensate for IE bug http://blog.stevenlevithan.com/archives/exec-bugs + if (childSeg.lastIndex >= selstring.length) { + break; + } + lastIndex = childSeg.lastIndex; + regexp.lastIndex = childSeg.lastIndex; + } + return togo; + }; + + // 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 + */ + // TODO: this is an abominably inefficient technique for something that could simply be done by means of indexOf and slice + 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 keys = fluid.keys(values); + keys = keys.sort(fluid.compareStringLength()); + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + var re = fluid.stringToRegExp("%" + key, "g"); + template = template.replace(re, values[key]); + } + return template; + }; + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/FluidDOMUtilities.js b/ppig-2015/example/lib/infusion/FluidDOMUtilities.js new file mode 100644 index 0000000..7247654 --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidDOMUtilities.js @@ -0,0 +1,116 @@ +/* +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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + 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 true 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_2_0); diff --git a/ppig-2015/example/lib/infusion/FluidDebugging.js b/ppig-2015/example/lib/infusion/FluidDebugging.js new file mode 100644 index 0000000..d1e1e8f --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidDebugging.js @@ -0,0 +1,298 @@ +/* +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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; +var fluid = fluid || fluid_2_0; + +(function ($, fluid) { + "use strict"; + + /** Render a timestamp from a Date object into a helpful fixed format for debug logs to millisecond accuracy + * @param date {Date} The date to be rendered + * @return {String} A string format consisting of hours:minutes:seconds.millis for the datestamp padded to fixed with + */ + + 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); + }; + + fluid.isTracing = false; + + fluid.registerNamespace("fluid.tracing"); + + fluid.tracing.pathCount = []; + + fluid.tracing.summarisePathCount = function (pathCount) { + pathCount = pathCount || fluid.tracing.pathCount; + var togo = {}; + for (var i = 0; i < pathCount.length; ++ i) { + var path = pathCount[i]; + if (!togo[path]) { + togo[path] = 1; + } + else { + ++togo[path]; + } + } + var toReallyGo = []; + fluid.each(togo, function (el, path) { + toReallyGo.push({path: path, count: el}); + }); + toReallyGo.sort(function (a, b) {return b.count - a.count;}); + return toReallyGo; + }; + + fluid.tracing.condensePathCount = function (prefixes, pathCount) { + prefixes = fluid.makeArray(prefixes); + var prefixCount = {}; + fluid.each(prefixes, function(prefix) { + prefixCount[prefix] = 0; + }); + var togo = []; + fluid.each(pathCount, function (el) { + var path = el.path; + if (!fluid.find(prefixes, function(prefix) { + if (path.indexOf(prefix) === 0) { + prefixCount[prefix] += el.count; + return true; + } + })) { + togo.push(el); + } + }); + fluid.each(prefixCount, function(count, path) { + togo.unshift({path: path, count: count}); + }); + return togo; + }; + + // 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 delimiter = "at "; + var lines = e.stack.replace(/(?:\n@:0)?\s+$/m, "").replace(/^\(/gm, "{anonymous}(").split("\n"); + return fluid.transform(lines, function (line) { + line = line.replace(/\)/g, ""); + var atind = line.indexOf(delimiter); + return atind === -1? [line] : [line.substring(atind + delimiter.length), line.substring(0, atind)]; + }); + }; + + // Main entry point for callers. + fluid.getCallerInfo = function (atDepth) { + atDepth = (atDepth || 3) - stackStyle.offset; + var stack = fluid.decodeStack(); + var element = stack && stack[atDepth][0]; + if (element) { + var lastslash = element.lastIndexOf("/"); + if (lastslash === -1) { + lastslash = 0; + } + var nextColon = element.indexOf(":", lastslash); + return { + path: element.substring(0, lastslash), + filename: element.substring(lastslash + 1, nextColon), + index: element.substring(nextColon + 1) + }; + } else { + return null; + } + }; + + /** Generates a string for padding purposes by replicating a character a given number of times + * @param c {Character} A character to be used for padding + * @param count {Integer} The number of times to repeat the character + * @return A string of length count consisting of repetitions of the supplied character + */ + // UNOPTIMISED + fluid.generatePadding = function (c, count) { + var togo = ""; + for (var i = 0; i < count; ++ i) { + togo += c; + } + return togo; + }; + + // Marker so that we can render a custom string for properties which are not direct and concrete + fluid.SYNTHETIC_PROPERTY = {}; + + // utility to avoid triggering custom getter code which could throw an exception - e.g. express 3.x's request object + fluid.getSafeProperty = function (obj, key) { + var desc = Object.getOwnPropertyDescriptor(obj, key); // supported on all of our environments - is broken on IE8 + return desc && !desc.get ? obj[key] : fluid.SYNTHETIC_PROPERTY; + }; + + function printImpl (obj, small, options) { + function out(str) { + options.output += str; + } + var big = small + options.indentChars, isFunction = typeof(obj) === "function"; + if (options.maxRenderChars !== undefined && options.output.length > options.maxRenderChars) { + return true; + } + if (obj === null) { + out("null"); + } else if (obj === undefined) { + out("undefined"); // NB - object invalid for JSON interchange + } else if (obj === fluid.SYNTHETIC_PROPERTY) { + out("[Synthetic property]"); + } else if (fluid.isPrimitive(obj) && !isFunction) { + out(JSON.stringify(obj)); + } + else { + if (options.stack.indexOf(obj) !== -1) { + out("(CIRCULAR)"); // NB - object invalid for JSON interchange + return; + } + options.stack.push(obj); + var i; + if (fluid.isArrayable(obj)) { + if (obj.length === 0) { + out("[]"); + } else { + out("[\n" + big); + for (i = 0; i < obj.length; ++ i) { + if (printImpl(obj[i], big, options)) { + return true; + } + if (i !== obj.length - 1) { + out(",\n" + big); + } + } + out("\n" + small + "]"); + } + } + else { + out("{" + (isFunction ? " Function" : "") + "\n" + big); // NB - Function object invalid for JSON interchange + var keys = fluid.keys(obj); + for (i = 0; i < keys.length; ++ i) { + var key = keys[i]; + var value = fluid.getSafeProperty(obj, key); + out(JSON.stringify(key) + ": "); + if (printImpl(value, big, options)) { + return true; + } + if (i !== keys.length - 1) { + out(",\n" + big); + } + } + out("\n" + small + "}"); + } + options.stack.pop(); + } + return; + } + + /** Render a complex JSON object into a nicely indented format suitable for human readability. + * @param obj {Object} The object to be rendered + * @param options {Object} An options structure governing the rendering process. This supports the following options: + * indent {Integer} the number of space characters to be used to indent each level of containment (default value: 4) + * maxRenderChars {Integer} rendering the object will cease once this number of characters has been generated + */ + fluid.prettyPrintJSON = function (obj, options) { + options = $.extend({indent: 4, stack: [], output: ""}, options); + options.indentChars = fluid.generatePadding(" ", options.indent); + printImpl(obj, "", options); + return options.output; + }; + + /** + * 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_2_0); diff --git a/ppig-2015/example/lib/infusion/FluidDocument.js b/ppig-2015/example/lib/infusion/FluidDocument.js new file mode 100644 index 0000000..59bbf0b --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidDocument.js @@ -0,0 +1,177 @@ +/* +Copyright 2007-2010 University of Cambridge +Copyright 2007-2009 University of Toronto +Copyright 2010-2011 Lucendo Development Ltd. +Copyright 2010 OCAD University +Copyright 2005-2013 jQuery Foundation, Inc. and other contributors + +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 **/ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + // polyfill for $.browser which was removed in jQuery 1.9 and later + // Taken from jquery-migrate-1.2.1.js, + // jQuery Migrate - v1.2.1 - 2013-05-08 + // https://github.com/jquery/jquery-migrate + // Copyright 2005, 2013 jQuery Foundation, Inc. and other contributors; Licensed MIT + + fluid.uaMatch = function (ua) { + ua = ua.toLowerCase(); + + var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) || + /(webkit)[ \/]([\w.]+)/.exec( ua ) || + /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) || + /(msie) ([\w.]+)/.exec( ua ) || + ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) || []; + + return { + browser: match[ 1 ] || "", + version: match[ 2 ] || "0" + }; + }; + + var matched, browser; + + // Don't clobber any existing jQuery.browser in case it's different + if (!$.browser) { + if (!!navigator.userAgent.match(/Trident\/7\./)) { + browser = { // From http://stackoverflow.com/questions/18684099/jquery-fail-to-detect-ie-11 + msie: true, + version: 11 + }; + } else { + matched = fluid.uaMatch(navigator.userAgent); + browser = {}; + + if (matched.browser) { + browser[matched.browser] = true; + browser.version = matched.version; + } + // Chrome is Webkit, but Webkit is also Safari. + if (browser.chrome) { + browser.webkit = true; + } else if (browser.webkit) { + browser.safari = true; + } + } + $.browser = browser; + } + + // 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 utility is required through the use of newer versions of jQuery which will obscure the original + // event responsible for interaction with a target. This is currently use in Tooltip.js and FluidView.js + // "dead man's blur" but would be of general utility + + fluid.resolveEventTarget = function (event) { + while (event.originalEvent && event.originalEvent.target) { + event = event.originalEvent; + } + return event.target; + }; + + // These function (fluid.focus() and fluid.blur()) serve several functions. They should be used by + // all implementation both in test cases and component implementation which require to trigger a focus + // event. Firstly, they restore the old behaviour in jQuery versions prior to 1.10 in which a focus + // trigger synchronously relays to a focus handler. In newer jQueries this defers to the real browser + // relay with numerous platform and timing-dependent effects. + // Secondly, they are 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.triggerHandler(func); + node[func](); + return node; + } + + $.each(["focus", "blur"], function(i, name) { + fluid[name] = function(elem) { + return applyOp(elem, name); + }; + }); + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/FluidIoC.js b/ppig-2015/example/lib/infusion/FluidIoC.js new file mode 100644 index 0000000..70fb7f7 --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidIoC.js @@ -0,0 +1,2220 @@ +/* +Copyright 2011-2013 OCAD University +Copyright 2010-2015 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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + /** NOTE: The contents of this file are by default NOT PART OF THE PUBLIC FLUID API unless explicitly annotated before the function **/ + + /** The Fluid "IoC System proper" - resolution of references and + * completely automated instantiation of declaratively defined + * component trees */ + + // Currently still uses manual traversal - once we ban manually instantiated components, + // it will use the instantiator's records instead. + fluid.visitComponentChildren = function (that, visitor, options, segs) { + segs = segs || []; + for (var name in that) { + var component = that[name]; + // This entire algorithm is primitive and expensive and will be removed once we can abolish manual init components + if (!fluid.isComponent(component) || (options.visited && options.visited[component.id])) { + continue; + } + segs.push(name); + if (options.visited) { // recall that this is here because we may run into a component that has been cross-injected which might otherwise cause cyclicity + options.visited[component.id] = true; + } + if (visitor(component, name, segs, segs.length - 1)) { + return true; + } + if (!options.flat) { + fluid.visitComponentChildren(component, visitor, options, segs); + } + segs.pop(); + } + }; + + fluid.getContextHash = function (instantiator, that) { + var shadow = instantiator.idToShadow[that.id]; + return shadow && shadow.contextHash; + }; + + fluid.componentHasGrade = function (that, gradeName) { + var contextHash = fluid.getContextHash(fluid.globalInstantiator, that); + return !!(contextHash && contextHash[gradeName]); + }; + + // A variant of fluid.visitComponentChildren that supplies the signature expected for fluid.matchIoCSelector + // this is: thatStack, contextHashes, memberNames, i - note, the supplied arrays are NOT writeable and shared through the iteration + fluid.visitComponentsForMatching = function (that, options, visitor) { + var instantiator = fluid.getInstantiator(that); + options = $.extend({ + visited: {}, + instantiator: instantiator + }, options); + var thatStack = [that]; + var contextHashes = [fluid.getContextHash(instantiator, that)]; + var visitorWrapper = function (component, name, segs) { + thatStack.length = 1; + contextHashes.length = 1; + for (var i = 0; i < segs.length; ++ i) { + var child = thatStack[i][segs[i]]; + thatStack[i + 1] = child; + contextHashes[i + 1] = fluid.getContextHash(instantiator, child) || {}; + } + return visitor(component, thatStack, contextHashes, segs, segs.length); + }; + fluid.visitComponentChildren(that, visitorWrapper, options, []); + }; + + fluid.getMemberNames = function (instantiator, thatStack) { + var path = instantiator.idToPath(thatStack[thatStack.length - 1].id); + var segs = instantiator.parseEL(path); + // TODO: we should now have no longer shortness in the stack + segs.unshift.apply(segs, fluid.generate(thatStack.length - segs.length, "")); + + return segs; + }; + + // thatStack contains an increasing list of MORE SPECIFIC thats. + // this visits all components starting from the current location (end of stack) + // in visibility order UP the tree. + fluid.visitComponentsForVisibility = function (instantiator, thatStack, visitor, options) { + options = options || { + visited: {}, + flat: true, + instantiator: instantiator + }; + var memberNames = fluid.getMemberNames(instantiator, thatStack); + for (var i = thatStack.length - 1; i >= 0; --i) { + var that = thatStack[i]; + + // explicitly visit the direct parent first + options.visited[that.id] = true; + if (visitor(that, memberNames[i], memberNames, i)) { + return; + } + + if (fluid.visitComponentChildren(that, visitor, options, memberNames)) { + return; + } + memberNames.pop(); + } + }; + + fluid.mountStrategy = function (prefix, root, toMount) { + var offset = prefix.length; + return function (target, name, i, segs) { + if (i <= prefix.length) { // Avoid OOB to not trigger deoptimisation! + return; + } + for (var j = 0; j < prefix.length; ++ j) { + if (segs[j] !== prefix[j]) { + return; + } + } + return toMount(target, name, i - prefix.length, segs.slice(offset)); + }; + }; + + fluid.invokerFromRecord = function (invokerec, name, that) { + fluid.pushActivity("makeInvoker", "beginning instantiation of invoker with name %name and record %record as child of %that", + {name: name, record: invokerec, that: that}); + var invoker = invokerec ? fluid.makeInvoker(that, invokerec, name) : undefined; + fluid.popActivity(); + return invoker; + }; + + fluid.memberFromRecord = function (memberrecs, name, that) { + var togo; + for (var i = 0; i < memberrecs.length; ++ i) { // memberrecs is the special "fluid.mergingArray" type which is not Arrayable + var expanded = fluid.expandImmediate(memberrecs[i], that); + if (!fluid.isPlainObject(togo)) { // poor man's "merge" algorithm to hack FLUID-5668 for now + togo = expanded; + } else { + togo = $.extend(true, togo, expanded); + } + } + return togo; + }; + + fluid.recordStrategy = function (that, options, optionsStrategy, recordPath, recordMaker, prefix, exceptions) { + prefix = prefix || []; + return { + strategy: function (target, name, i) { + if (i !== 1) { + return; + } + var record = fluid.driveStrategy(options, [recordPath, name], optionsStrategy); + if (record === undefined) { + return; + } + fluid.set(target, [name], fluid.inEvaluationMarker); + var member = recordMaker(record, name, that); + fluid.set(target, [name], member); + return member; + }, + initter: function () { + var records = fluid.driveStrategy(options, recordPath, optionsStrategy) || {}; + for (var name in records) { + if (!exceptions || !exceptions[name]) { + fluid.getForComponent(that, prefix.concat([name])); + } + } + } + }; + }; + + // patch Fluid.js version for timing + fluid.instantiateFirers = function (that) { + var shadow = fluid.shadowForComponent(that); + var initter = fluid.get(shadow, ["eventStrategyBlock", "initter"]) || fluid.identity; + initter(); + }; + + fluid.makeDistributionRecord = function (contextThat, sourceRecord, sourcePath, targetSegs, exclusions, sourceType) { + sourceType = sourceType || "distribution"; + + var source = fluid.copy(fluid.get(sourceRecord, sourcePath)); + fluid.each(exclusions, function (exclusion) { + fluid.model.applyChangeRequest(source, {segs: exclusion, type: "DELETE"}); + }); + + var record = {options: {}}; + fluid.model.applyChangeRequest(record, {segs: targetSegs, type: "ADD", value: source}); + return $.extend(record, {contextThat: contextThat, recordType: sourceType}); + }; + + // Part of the early "distributeOptions" workflow. Given the description of the blocks to be distributed, assembles "canned" records + // suitable to be either registered into the shadow record for later or directly pushed to an existing component, as well as honouring + // any "removeSource" annotations by removing these options from the source block. + fluid.filterBlocks = function (contextThat, sourceBlocks, sourceSegs, targetSegs, exclusions, removeSource) { + var togo = []; + fluid.each(sourceBlocks, function (block) { + var source = fluid.get(block.source, sourceSegs); + if (source) { + togo.push(fluid.makeDistributionRecord(contextThat, block.source, sourceSegs, targetSegs, exclusions, block.recordType)); + var rescued = $.extend({}, source); + if (removeSource) { + fluid.model.applyChangeRequest(block.source, {segs: sourceSegs, type: "DELETE"}); + } + fluid.each(exclusions, function (exclusion) { + var orig = fluid.get(rescued, exclusion); + fluid.set(block.source, sourceSegs.concat(exclusion), orig); + }); + } + }); + return togo; + }; + + // Use this peculiar signature since the actual component and shadow itself may not exist yet. Perhaps clean up with FLUID-4925 + fluid.noteCollectedDistribution = function (parentShadow, memberName, distribution) { + fluid.model.setSimple(parentShadow, ["collectedDistributions", memberName, distribution.id], true); + }; + + fluid.isCollectedDistribution = function (parentShadow, memberName, distribution) { + return fluid.model.getSimple(parentShadow, ["collectedDistributions", memberName, distribution.id]); + }; + + fluid.clearCollectedDistributions = function (parentShadow, memberName) { + fluid.model.applyChangeRequest(parentShadow, {segs: ["collectedDistributions", memberName], type: "DELETE"}); + }; + + fluid.collectDistributions = function (distributedBlocks, parentShadow, distribution, thatStack, contextHashes, memberNames, i) { + var lastMember = memberNames[memberNames.length - 1]; + if (!fluid.isCollectedDistribution(parentShadow, lastMember, distribution) && + fluid.matchIoCSelector(distribution.selector, thatStack, contextHashes, memberNames, i)) { + distributedBlocks.push.apply(distributedBlocks, distribution.blocks); + fluid.noteCollectedDistribution(parentShadow, lastMember, distribution); + } + }; + + // Slightly silly function to clean up the "appliedDistributions" records. In general we need to be much more aggressive both + // about clearing instantiation garbage (e.g. onCreate and most of the shadow) + // as well as caching frequently-used records such as the "thatStack" which + // would mean this function could be written in a sensible way + fluid.registerCollectedClearer = function (shadow, parentShadow, memberName) { + if (!shadow.collectedClearer && parentShadow) { + shadow.collectedClearer = function () { + fluid.clearCollectedDistributions(parentShadow, memberName); + }; + } + }; + + fluid.receiveDistributions = function (parentThat, gradeNames, memberName, that) { + var instantiator = fluid.getInstantiator(parentThat || that); + var thatStack = instantiator.getThatStack(parentThat || that); // most specific is at end + thatStack.unshift(fluid.rootComponent); + var memberNames = fluid.getMemberNames(instantiator, thatStack); + var shadows = fluid.transform(thatStack, function (thisThat) { + return instantiator.idToShadow[thisThat.id]; + }); + var parentShadow = shadows[shadows.length - (parentThat ? 1 : 2)]; + var contextHashes = fluid.getMembers(shadows, "contextHash"); + if (parentThat) { // if called before construction of component from assembleCreatorArguments - NB this path will be abolished/amalgamated + memberNames.push(memberName); + contextHashes.push(fluid.gradeNamesToHash(gradeNames)); + thatStack.push(that); + } else { + fluid.registerCollectedClearer(shadows[shadows.length - 1], parentShadow, memberNames[memberNames.length - 1]); + } + var distributedBlocks = []; + for (var i = 0; i < thatStack.length - 1; ++ i) { + fluid.each(shadows[i].distributions, function (distribution) { + fluid.collectDistributions(distributedBlocks, parentShadow, distribution, thatStack, contextHashes, memberNames, i); + }); /* function in loop */ /* jshint ignore:line */ + } + return distributedBlocks; + }; + + fluid.computeTreeDistance = function (path1, path2) { + var i = 0; + while (i < path1.length && i < path2.length && path1[i] === path2[i]) { + ++i; + } + return path1.length + path2.length - 2*i; + }; + + // Called from applyDistributions (immediate application route) as well as mergeRecordsToList (pre-instantiation route) + fluid.computeDistributionPriority = function (targetThat, distributedBlock) { + if (!distributedBlock.priority) { + var instantiator = fluid.getInstantiator(targetThat); + var targetStack = instantiator.getThatStack(targetThat); + var targetPath = fluid.getMemberNames(instantiator, targetStack); + var sourceStack = instantiator.getThatStack(distributedBlock.contextThat); + var sourcePath = fluid.getMemberNames(instantiator, sourceStack); + var distance = fluid.computeTreeDistance(targetPath, sourcePath); + distributedBlock.priority = fluid.mergeRecordTypes.distribution + distance; + } + return distributedBlock; + }; + + // convert "preBlocks" as produced from fluid.filterBlocks into "real blocks" suitable to be used by the expansion machinery. + fluid.applyDistributions = function (that, preBlocks, targetShadow) { + var distributedBlocks = fluid.transform(preBlocks, function (preBlock) { + return fluid.generateExpandBlock(preBlock, that, targetShadow.mergePolicy); + }, function (distributedBlock) { + return fluid.computeDistributionPriority(that, distributedBlock); + }); + var mergeOptions = targetShadow.mergeOptions; + mergeOptions.mergeBlocks.push.apply(mergeOptions.mergeBlocks, distributedBlocks); + mergeOptions.updateBlocks(); + return distributedBlocks; + }; + + // TODO: This implementation is obviously poor and has numerous flaws - in particular it does no backtracking as well as matching backwards through the selector + fluid.matchIoCSelector = function (selector, thatStack, contextHashes, memberNames, i) { + var thatpos = thatStack.length - 1; + var selpos = selector.length - 1; + while (true) { + var mustMatchHere = thatpos === thatStack.length - 1 || selector[selpos].child; + + var that = thatStack[thatpos]; + var selel = selector[selpos]; + var match = true; + for (var j = 0; j < selel.predList.length; ++j) { + var pred = selel.predList[j]; + if (pred.context && !(contextHashes[thatpos][pred.context] || memberNames[thatpos] === pred.context)) { + match = false; + break; + } + if (pred.id && that.id !== pred.id) { + match = false; + break; + } + } + if (selpos === 0 && thatpos > i && mustMatchHere) { + match = false; // child selector must exhaust stack completely - FLUID-5029 + } + if (match) { + if (selpos === 0) { + return true; + } + --thatpos; + --selpos; + } + else { + if (mustMatchHere) { + return false; + } + else { + --thatpos; + } + } + if (thatpos < i) { + return false; + } + } + }; + + /** Query for all components matching a selector in a particular tree + * @param root {Component} The root component at which to start the search + * @param selector {String} An IoCSS selector, in form of a string. Note that since selectors supplied to this function implicitly + * match downwards, they need not contain the "head context" followed by whitespace required in the distributeOptions form. E.g. + * simply "fluid.viewComponent" will match all viewComponents below the root. + * @param flat {Boolean} [Optional] true if the search should just be performed at top level of the component tree + * Note that with flat=true this search will scan every component in the tree and may well be very slow. + */ + // supported, PUBLIC API function + fluid.queryIoCSelector = function (root, selector, flat) { + var parsed = fluid.parseSelector(selector, fluid.IoCSSMatcher); + var togo = []; + + fluid.visitComponentsForMatching(root, {flat: flat}, function (that, thatStack, contextHashes, memberNames, i) { + if (fluid.matchIoCSelector(parsed, thatStack, contextHashes, memberNames, i)) { + togo.push(that); + } + }); + return togo; + }; + + fluid.isIoCSSSelector = function (context) { + return context.indexOf(" ") !== -1; // simple-minded check for an IoCSS reference + }; + + fluid.pushDistributions = function (targetHead, selector, blocks) { + var targetShadow = fluid.shadowForComponent(targetHead); + var id = fluid.allocateGuid(); + var distributions = (targetShadow.distributions = targetShadow.distributions || []); + distributions.push({ + id: id, // This id is used in clearDistributions + selector: selector, + blocks: blocks + }); + return id; + }; + + fluid.clearDistribution = function (targetHead, id) { + var targetShadow = fluid.shadowForComponent(targetHead); + fluid.remove_if(targetShadow.distributions, function (distribution) { + return distribution.id === id; + }); + }; + + fluid.clearDistributions = function (shadow) { + fluid.each(shadow.outDistributions, function (outDist) { + fluid.clearDistribution(outDist.targetComponent, outDist.distributionId); + }); + }; + + // Modifies a parsed selector to extract and remove its head context which will be matched upwards + fluid.extractSelectorHead = function (parsedSelector) { + var predList = parsedSelector[0].predList; + var context = predList[0].context; + predList.length = 0; + return context; + }; + + fluid.parseExpectedOptionsPath = function (path, role) { + var segs = fluid.model.parseEL(path); + if (segs.length > 1 && segs[0] !== "options") { + fluid.fail("Error in options distribution path ", path, " - only " + role + " paths beginning with \"options\" are supported"); + } + return segs.slice(1); + }; + + fluid.replicateProperty = function (source, property, targets) { + if (source[property] !== undefined) { + fluid.each(targets, function (target) { + target[property] = source[property]; + }); + } + }; + + fluid.undistributableOptions = ["gradeNames", "distributeOptions", "argumentMap", "initFunction", "mergePolicy", "progressiveCheckerOptions"]; // automatically added to "exclusions" of every distribution + + fluid.distributeOptions = function (that, optionsStrategy) { + var thatShadow = fluid.shadowForComponent(that); + var records = fluid.driveStrategy(that.options, "distributeOptions", optionsStrategy); + fluid.each(records, function (record) { + var targetRef = fluid.parseContextReference(record.target); + var targetComp, selector, context; + if (fluid.isIoCSSSelector(targetRef.context)) { + selector = fluid.parseSelector(targetRef.context, fluid.IoCSSMatcher); + var headContext = fluid.extractSelectorHead(selector); + if (headContext === "/") { + targetComp = fluid.rootComponent; + } else { + context = headContext; + } + } + else { + context = targetRef.context; + } + targetComp = targetComp || fluid.resolveContext(context, that); + if (!targetComp) { + fluid.fail("Error in options distribution record ", record, " - could not resolve context {"+context+"} to a root component"); + } + var targetSegs = fluid.model.parseEL(targetRef.path); + var preBlocks; + if (record.record !== undefined) { + preBlocks = [(fluid.makeDistributionRecord(that, record.record, [], targetSegs, []))]; + } + else { + var source = fluid.parseContextReference(record.source || "{that}.options"); // TODO: This is probably not a sensible default + if (source.context !== "that") { + fluid.fail("Error in options distribution record ", record, " only a context of {that} is supported"); + } + var sourceSegs = fluid.parseExpectedOptionsPath(source.path, "source"); + var fullExclusions = fluid.makeArray(record.exclusions).concat(sourceSegs.length === 0 ? fluid.undistributableOptions : []); + + var exclusions = fluid.transform(fullExclusions, function (exclusion) { + return fluid.model.parseEL(exclusion); + }); + + preBlocks = fluid.filterBlocks(that, thatShadow.mergeOptions.mergeBlocks, sourceSegs, targetSegs, exclusions, record.removeSource); + thatShadow.mergeOptions.updateBlocks(); // perhaps unnecessary + } + fluid.replicateProperty(record, "priority", preBlocks); + fluid.replicateProperty(record, "namespace", preBlocks); + // TODO: inline material has to be expanded in its original context! + + if (selector) { + var distributionId = fluid.pushDistributions(targetComp, selector, preBlocks); + thatShadow.outDistributions = thatShadow.outDistributions || []; + thatShadow.outDistributions.push({ + targetComponent: targetComp, + distributionId: distributionId + }); + } + else { // The component exists now, we must rebalance it + var targetShadow = fluid.shadowForComponent(targetComp); + fluid.applyDistributions(that, preBlocks, targetShadow); + } + }); + }; + + fluid.gradeNamesToHash = function (gradeNames) { + var contextHash = {}; + fluid.each(gradeNames, function (gradeName) { + contextHash[gradeName] = true; + contextHash[fluid.computeNickName(gradeName)] = true; + }); + return contextHash; + }; + + fluid.cacheShadowGrades = function (that, shadow) { + var contextHash = fluid.gradeNamesToHash(that.options.gradeNames); + if (!contextHash[shadow.memberName]) { + contextHash[shadow.memberName] = "memberName"; // This is filtered out again in recordComponent - TODO: Ensure that ALL resolution uses the scope chain eventually + } + shadow.contextHash = contextHash; + fluid.each(contextHash, function (troo, context) { + shadow.ownScope[context] = that; + if (shadow.parentShadow && shadow.parentShadow.that.type !== "fluid.rootComponent") { + shadow.parentShadow.childrenScope[context] = that; + } + }); + }; + + // First sequence point where the mergeOptions strategy is delivered from Fluid.js - here we take care + // of both receiving and transmitting options distributions + fluid.deliverOptionsStrategy = function (that, target, mergeOptions) { + var shadow = fluid.shadowForComponent(that, shadow); + fluid.cacheShadowGrades(that, shadow); + shadow.mergeOptions = mergeOptions; + }; + + fluid.expandDynamicGrades = function (that, shadow, gradeNames, dynamicGrades) { + var resolved = []; + // Receive distributions first since these may cause arrival of more contextAwareness blocks. + // TODO: this closure algorithm is not reliable since we only get one shot at a "function" grade source when + // really we should perform complete closure over all other sources of options before we try it at the very end - particularly important for contextAwareness + var distributedBlocks = fluid.receiveDistributions(null, null, null, that); + if (distributedBlocks.length > 0) { + var readyBlocks = fluid.applyDistributions(that, distributedBlocks, shadow); + // rely on the fact that "dirty tricks are not permitted" wrt. resolving gradeNames - each element must be a literal entry or array + // holding primitive or EL values - otherwise we would have to go all round the houses and reenter the top of fluid.computeDynamicGrades + var gradeNamesList = fluid.transform(fluid.getMembers(readyBlocks, ["source", "gradeNames"]), fluid.makeArray); + resolved = resolved.concat.apply(resolved, gradeNamesList); + } + fluid.each(dynamicGrades, function (dynamicGrade) { + var expanded = fluid.expandOptions(dynamicGrade, that); + if (typeof(expanded) === "function") { + expanded = expanded(); + } + if (expanded) { + resolved = resolved.concat(expanded); + } + }); + return resolved; + }; + + // Discover further grades that are entailed by the given base typeName and the current total "dynamic grades list" held in the argument "resolved". + // These are looked up conjointly in the grade registry, and then any further dynamic grades references + // are expanded and added into the list and concatenated into "resolved". Additional grades discovered during this function are returned as + // "furtherResolved". + fluid.collectDynamicGrades = function (that, shadow, defaultsBlock, gradeNames, dynamicGrades, resolved) { + var newDefaults = fluid.copy(fluid.getGradedDefaults(that.typeName, resolved)); + gradeNames.length = 0; // acquire derivatives of dynamic grades (FLUID-5054) + gradeNames.push.apply(gradeNames, newDefaults.gradeNames); + + fluid.cacheShadowGrades(that, shadow); + // This cheap strategy patches FLUID-5091 for now - some more sophisticated activity will take place + // at this site when we have a full fix for FLUID-5028 + shadow.mergeOptions.destroyValue(["mergePolicy"]); + shadow.mergeOptions.destroyValue(["components"]); + shadow.mergeOptions.destroyValue(["invokers"]); + + defaultsBlock.source = newDefaults; + shadow.mergeOptions.updateBlocks(); + + var furtherResolved = fluid.remove_if(gradeNames, function (gradeName) { + return gradeName.charAt(0) === "{" && !fluid.contains(dynamicGrades, gradeName); + }, []); + dynamicGrades.push.apply(dynamicGrades, furtherResolved); + furtherResolved = fluid.expandDynamicGrades(that, shadow, gradeNames, furtherResolved); + + resolved.push.apply(resolved, furtherResolved); + + return furtherResolved; + }; + + fluid.computeDynamicGrades = function (that, shadow, strategy) { + delete that.options.gradeNames; // Recompute gradeNames for FLUID-5012 and others + + var gradeNames = fluid.driveStrategy(that.options, "gradeNames", strategy); + // TODO: In complex distribution cases, a component might end up with multiple default blocks + var defaultsBlock = fluid.findMergeBlocks(shadow.mergeOptions.mergeBlocks, "defaults")[0]; + var dynamicGrades = fluid.remove_if(gradeNames, function (gradeName) { + return gradeName.charAt(0) === "{" || !fluid.hasGrade(defaultsBlock.target, gradeName); + }, []); + var resolved = fluid.expandDynamicGrades(that, shadow, gradeNames, dynamicGrades); + if (resolved.length !== 0) { + var furtherResolved; + do { // repeatedly collect dynamic grades whilst they arrive (FLUID-5155) + furtherResolved = fluid.collectDynamicGrades(that, shadow, defaultsBlock, gradeNames, dynamicGrades, resolved); + } + while (furtherResolved.length !== 0); + } + if (shadow.collectedClearer) { + shadow.collectedClearer(); + delete shadow.collectedClearer; + } + }; + + fluid.computeDynamicComponentKey = function (recordKey, sourceKey) { + return recordKey + (sourceKey === 0 ? "" : "-" + sourceKey); // TODO: configurable name strategies + }; + + fluid.registerDynamicRecord = function (that, recordKey, sourceKey, record, toCensor) { + var key = fluid.computeDynamicComponentKey(recordKey, sourceKey); + var cRecord = fluid.copy(record); + delete cRecord[toCensor]; + fluid.set(that.options, ["components", key], cRecord); + return key; + }; + + fluid.computeDynamicComponents = function (that, mergeOptions) { + var shadow = fluid.shadowForComponent(that); + var localSub = shadow.subcomponentLocal = {}; + var records = fluid.driveStrategy(that.options, "dynamicComponents", mergeOptions.strategy); + fluid.each(records, function (record, recordKey) { + if (!record.sources && !record.createOnEvent) { + fluid.fail("Cannot process dynamicComponents record ", record, " without a \"sources\" or \"createOnEvent\" entry"); + } + if (record.sources) { + var sources = fluid.expandOptions(record.sources, that); + fluid.each(sources, function (source, sourceKey) { + var key = fluid.registerDynamicRecord(that, recordKey, sourceKey, record, "sources"); + localSub[key] = {"source": source, "sourcePath": sourceKey}; + }); + } + else if (record.createOnEvent) { + var event = fluid.event.expandOneEvent(that, record.createOnEvent); + fluid.set(shadow, ["dynamicComponentCount", recordKey], 0); + var listener = function () { + var key = fluid.registerDynamicRecord(that, recordKey, shadow.dynamicComponentCount[recordKey]++, record, "createOnEvent"); + localSub[key] = {"arguments": fluid.makeArray(arguments)}; + fluid.initDependent(that, key); + }; + event.addListener(listener); + fluid.recordListener(event, listener, shadow); + } + }); + }; + + // Second sequence point for mergeOptions from Fluid.js - here we construct all further + // strategies required on the IoC side and mount them into the shadow's getConfig for universal use + fluid.computeComponentAccessor = function (that) { + var instantiator = fluid.globalInstantiator; + var shadow = fluid.shadowForComponent(that); + var options = that.options; + var strategy = shadow.mergeOptions.strategy; + var optionsStrategy = fluid.mountStrategy(["options"], options, strategy); + shadow.invokerStrategy = fluid.recordStrategy(that, options, strategy, "invokers", fluid.invokerFromRecord); + shadow.eventStrategyBlock = fluid.recordStrategy(that, options, strategy, "events", fluid.eventFromRecord, ["events"]); + var eventStrategy = fluid.mountStrategy(["events"], that, shadow.eventStrategyBlock.strategy, ["events"]); + shadow.memberStrategy = fluid.recordStrategy(that, options, strategy, "members", fluid.memberFromRecord, null, {model: true, modelRelay: true}); + // NB - ginger strategy handles concrete, rationalise + shadow.getConfig = {strategies: [fluid.model.funcResolverStrategy, fluid.makeGingerStrategy(that), + optionsStrategy, shadow.invokerStrategy.strategy, shadow.memberStrategy.strategy, eventStrategy]}; + + fluid.computeDynamicGrades(that, shadow, strategy, shadow.mergeOptions.mergeBlocks); + fluid.distributeOptions(that, strategy); + if (shadow.contextHash["fluid.resolveRoot"]) { + var memberName; + if (shadow.contextHash["fluid.resolveRootSingle"]) { + var singleRootType = fluid.getForComponent(that, ["options", "singleRootType"]); + if (!singleRootType) { + fluid.fail("Cannot register object with grades " + Object.keys(shadow.contextHash).join(", ") + " as fluid.resolveRootSingle since it has not defined option singleRootType"); + } + memberName = fluid.typeNameToMemberName(singleRootType); + } else { + memberName = fluid.computeGlobalMemberName(that); + } + var parent = fluid.resolveRootComponent; + if (parent[memberName]) { + instantiator.clearComponent(parent, memberName); + } + instantiator.recordKnownComponent(parent, that, memberName, false); + } + + return shadow.getConfig; + }; + + // About the SHADOW: + // Allocated at: instantiator's "recordComponent" + // Contents: + // path {String} Principal allocated path (point of construction) in tree + // that {Component} The component itself + // contextHash {String to Boolean} Map of context names which this component matches + // mergePolicy, mergeOptions: Machinery for last phase of options merging + // invokerStrategy, eventStrategyBlock, memberStrategy, getConfig: Junk required to operate the accessor + // listeners: Listeners registered during this component's construction, to be cleared during clearListeners + // distributions, collectedClearer: Managing options distributions + // subcomponentLocal: Signalling local record from computeDynamicComponents to assembleCreatorArguments + + fluid.shadowForComponent = function (component) { + var instantiator = fluid.getInstantiator(component); + return instantiator && component ? instantiator.idToShadow[component.id] : null; + }; + + // Access the member at a particular path in a component, forcing it to be constructed gingerly if necessary + // supported, PUBLIC API function + fluid.getForComponent = function (component, path) { + var shadow = fluid.shadowForComponent(component); + var getConfig = shadow ? shadow.getConfig : undefined; + return fluid.get(component, path, getConfig); + }; + + // 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. + fluid.makeGingerStrategy = function (that) { + var instantiator = fluid.getInstantiator(that); + return function (component, thisSeg, index, segs) { + var atval = component[thisSeg]; + if (atval === fluid.inEvaluationMarker && index === segs.length) { + fluid.fail("Error in component configuration - a circular reference was found during evaluation of path segment \"" + thisSeg + + "\": for more details, see the activity records following this message in the console, or issue fluid.setLogging(fluid.logLevel.TRACE) when running your application"); + } + if (index > 1) { + return atval; + } + if (atval === undefined && component.hasOwnProperty(thisSeg)) { // avoid recomputing properties that have been explicitly evaluated to undefined + return fluid.NO_VALUE; + } + if (atval === undefined) { // pick up components in instantiation here - we can cut this branch by attaching early + var parentPath = instantiator.idToShadow[component.id].path; + var childPath = instantiator.composePath(parentPath, thisSeg); + atval = instantiator.pathToComponent[childPath]; + } + if (atval === undefined) { + // TODO: This check is very expensive - once gingerness is stable, we ought to be able to + // eagerly compute and cache the value of options.components - check is also incorrect and will miss injections + var subRecord = fluid.getForComponent(component, ["options", "components", thisSeg]); + if (subRecord) { + if (subRecord.createOnEvent) { + fluid.fail("Error resolving path segment \"" + thisSeg + "\" of path " + segs.join(".") + " since component with record ", subRecord, + " has annotation \"createOnEvent\" - this very likely represents an implementation error. Either alter the reference so it does not " + + " match this component, or alter your workflow to ensure that the component is instantiated by the time this reference resolves"); + } + fluid.initDependent(component, thisSeg); + atval = component[thisSeg]; + } + } + return atval; + }; + }; + + // Listed in dependence order + fluid.frameworkGrades = ["fluid.component", "fluid.modelComponent", "fluid.viewComponent", "fluid.rendererComponent"]; + + fluid.filterBuiltinGrades = function (gradeNames) { + return fluid.remove_if(fluid.makeArray(gradeNames), function (gradeName) { + return fluid.frameworkGrades.indexOf(gradeName) !== -1; + }); + }; + + fluid.dumpGradeNames = function (that) { + return that.options && that.options.gradeNames ? + " gradeNames: " + JSON.stringify(fluid.filterBuiltinGrades(that.options.gradeNames)) : ""; + }; + + fluid.dumpThat = function (that) { + return "{ typeName: \"" + that.typeName + "\"" + fluid.dumpGradeNames(that) + " id: " + that.id + "}"; + }; + + 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"); + }; + + fluid.resolveContext = function (context, that, fast) { + var instantiator = fluid.getInstantiator(that); + if (context === "that") { + return that; + } + var foundComponent; + if (fast) { + var shadow = instantiator.idToShadow[that.id]; + return shadow.ownScope[context]; + } else { + var thatStack = instantiator.getFullStack(that); + fluid.visitComponentsForVisibility(instantiator, thatStack, function (component, name) { + var shadow = fluid.shadowForComponent(component); + // TODO: Some components, e.g. the static environment and typeTags do not have a shadow, which slows us down here + if (context === name || shadow && shadow.contextHash && shadow.contextHash[context] || context === component.typeName) { + foundComponent = component; + return true; // YOUR VISIT IS AT AN END!! + } + if (fluid.getForComponent(component, ["options", "components", context]) && !component[context]) { + // This is an expensive guess since we make it for every component up the stack - must apply the WAVE OF EXPLOSIONS (FLUID-4925) to discover all components first + // This line attempts a hopeful construction of components that could be guessed by nickname through finding them unconstructed + // in options. In the near future we should eagerly BEGIN the process of constructing components, discovering their + // types and then attaching them to the tree VERY EARLY so that we get consistent results from different strategies. + foundComponent = fluid.getForComponent(component, context); + return true; + } + }); + return foundComponent; + } + }; + + fluid.makeStackFetcher = function (parentThat, localRecord, fast) { + var fetcher = function (parsed) { + if (parentThat && parentThat.lifecycleStatus === "destroyed") { + fluid.fail("Cannot resolve reference " + fluid.renderContextReference(parsed) + " from component " + fluid.dumpThat(parentThat) + " which has been destroyed"); + } + var context = parsed.context; + if (localRecord && context in localRecord) { + return fluid.get(localRecord[context], parsed.path); + } + var foundComponent = fluid.resolveContext(context, parentThat, fast); + if (!foundComponent && parsed.path !== "") { + var ref = fluid.renderContextReference(parsed); + fluid.fail("Failed to resolve reference " + ref + " - could not match context with name " + + context + " from component " + fluid.dumpThat(parentThat), parentThat); + } + return fluid.getForComponent(foundComponent, parsed.path); + }; + return fetcher; + }; + + fluid.makeStackResolverOptions = function (parentThat, localRecord, fast) { + return $.extend(fluid.copy(fluid.rawDefaults("fluid.makeExpandOptions")), { + localRecord: localRecord || {}, + fetcher: fluid.makeStackFetcher(parentThat, localRecord, fast), + contextThat: parentThat, + exceptions: {members: {model: true, modelRelay: true}} + }); + }; + + fluid.clearListeners = function (shadow) { + // TODO: bug here - "afterDestroy" listeners will be unregistered already unless they come from this component + fluid.each(shadow.listeners, function (rec) { + rec.event.removeListener(rec.listener); + }); + delete shadow.listeners; + }; + + fluid.recordListener = function (event, listener, shadow) { + if (event.ownerId !== shadow.that.id) { // don't bother recording listeners registered from this component itself + var listeners = shadow.listeners; + if (!listeners) { + listeners = shadow.listeners = []; + } + listeners.push({event: event, listener: listener}); + } + }; + + fluid.constructScopeObjects = function (instantiator, parent, child, childShadow) { + var parentShadow = parent ? instantiator.idToShadow[parent.id] : null; + childShadow.childrenScope = parentShadow ? Object.create(parentShadow.ownScope) : {}; + childShadow.ownScope = Object.create(childShadow.childrenScope); + childShadow.parentShadow = parentShadow; + }; + + fluid.clearChildrenScope = function (instantiator, parentShadow, child, childShadow) { + fluid.each(childShadow.contextHash, function (troo, context) { + if (parentShadow.childrenScope[context] === child) { + delete parentShadow.childrenScope[context]; // TODO: ambiguous resolution + } + }); + }; + + // unsupported, non-API function - however, this structure is of considerable interest to those debugging + // into IoC issues. The structures idToShadow and pathToComponent contain a complete map of the component tree + // forming the surrounding scope + fluid.instantiator = function () { + var that = { + id: fluid.allocateGuid(), + typeName: "instantiator", + lifecycleStatus: "constructed", + pathToComponent: {}, + idToShadow: {}, + modelTransactions: {init: {}}, // a map of transaction id to map of component id to records of components enlisted in a current model initialisation transaction + composePath: fluid.model.composePath, // For speed, we declare that no component's name may contain a period + composeSegments: fluid.model.composeSegments, + parseEL: fluid.model.parseEL, + events: { + onComponentAttach: fluid.makeEventFirer({name: "instantiator's onComponentAttach event"}), + onComponentClear: fluid.makeEventFirer({name: "instantiator's onComponentClear event"}) + } + }; + // TODO: this API can shortly be removed + that.idToPath = function (id) { + var shadow = that.idToShadow[id]; + return shadow ? shadow.path : ""; + }; + // Note - the returned stack is assumed writeable and does not include the root + that.getThatStack = function (component) { + var shadow = that.idToShadow[component.id]; + if (shadow) { + var path = shadow.path; + var parsed = fluid.model.parseEL(path); + var root = that.pathToComponent[""], togo = []; + for (var i = 0; i < parsed.length; ++ i) { + root = root[parsed[i]]; + togo.push(root); + } + return togo; + } + else { return [];} + }; + that.getFullStack = function (component) { + var thatStack = component? that.getThatStack(component) : []; + thatStack.unshift(fluid.resolveRootComponent); + return thatStack; + }; + function recordComponent(parent, component, path, name, created) { + var shadow; + if (created) { + shadow = that.idToShadow[component.id] = {}; + shadow.that = component; + shadow.path = path; + shadow.memberName = name; + fluid.constructScopeObjects(that, parent, component, shadow); + } else { + shadow = that.idToShadow[component.id]; + shadow.injectedPaths = shadow.injectedPaths || []; + shadow.injectedPaths.push(path); + var parentShadow = that.idToShadow[parent.id]; // structural parent shadow - e.g. resolveRootComponent + var keys = fluid.keys(shadow.contextHash); + keys.push(name); // add local name - FLUID-5696 + fluid.remove_if(keys, function (key) { + return shadow.contextHash && shadow.contextHash[key] === "memberName"; + }); + fluid.each(keys, function (context) { + if (!parentShadow.childrenScope[context]) { + parentShadow.childrenScope[context] = component; + } + }); + } + 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) { + recordComponent(null, component, "", "", true); + }; + that.recordKnownComponent = function (parent, component, name, created) { + parent[name] = component; + if (fluid.isComponent(component) || component.type === "instantiator") { + var parentPath = that.idToShadow[parent.id].path; + var path = that.composePath(parentPath, name); + recordComponent(parent, component, path, name, created); + that.events.onComponentAttach.fire(component, path, that, created); + } else { + fluid.fail("Cannot record non-component"); + } + }; + that.clearComponent = function (component, name, child, options, noModTree, path) { + // options are visitor options for recursive driving + var shadow = that.idToShadow[component.id]; + // use flat recursion since we want to use our own recursion rather than rely on "visited" records + options = options || {flat: true, instantiator: that}; + child = child || component[name]; + path = path || shadow.path; + if (path === undefined) { + fluid.fail("Cannot clear component " + name + " from component ", component, + " which was not created by this instantiator"); + } + + var childPath = that.composePath(path, name); + var childShadow = that.idToShadow[child.id]; + var created = childShadow.path === childPath; + that.events.onComponentClear.fire(child, childPath, component, created); + + // only recurse on components which were created in place - if the id record disagrees with the + // recurse path, it must have been injected + if (created) { + // Clear injected instance of this component from all other paths - historically we didn't bother + // to do this since injecting into a shorter scope is an error - but now we have resolveRoot area + fluid.each(childShadow.injectedPaths, function (injectedPath) { + var parentPath = fluid.model.getToTailPath(injectedPath); + var otherParent = that.pathToComponent[parentPath]; + that.clearComponent(otherParent, fluid.model.getTailPath(injectedPath), child); + }); + fluid.visitComponentChildren(child, function(gchild, gchildname, segs, i) { + var parentPath = that.composeSegments.apply(null, segs.slice(0, i)); + that.clearComponent(child, gchildname, null, options, true, parentPath); + }, options, that.parseEL(childPath)); + fluid.doDestroy(child, name, component); + fluid.clearDistributions(childShadow); + fluid.clearListeners(childShadow); + fluid.fireEvent(child, "events.afterDestroy", [child, name, component]); + delete that.idToShadow[child.id]; + } + fluid.clearChildrenScope(that, shadow, child, childShadow); + delete that.pathToComponent[childPath]; + if (!noModTree) { + delete component[name]; // there may be no entry - if creation is not concluded + } + }; + return that; + }; + + // The global instantiator, holding all components instantiated in this context (instance of Infusion) + fluid.globalInstantiator = fluid.instantiator(); + + // Look up the globally registered instantiator for a particular component - we now only really support a + // single, global instantiator, but this method is left as a notation point in case this ever reverts + fluid.getInstantiator = function (component) { + var instantiator = fluid.globalInstantiator; + return component && instantiator.idToShadow[component.id] ? instantiator : null; + }; + + // The grade supplied to components which will be resolvable from all parts of the component tree + fluid.defaults("fluid.resolveRoot"); + // In addition to being resolvable at the root, "resolveRootSingle" component will have just a single instance available. Fresh + // instances will displace older ones. + fluid.defaults("fluid.resolveRootSingle", { + gradeNames: "fluid.resolveRoot" + }); + + fluid.constructRootComponents = function (instantiator) { + // Instantiate the primordial components at the root of each context tree + fluid.rootComponent = instantiator.rootComponent = fluid.typeTag("fluid.rootComponent"); + instantiator.recordRoot(fluid.rootComponent); + + // The component which for convenience holds injected instances of all components with fluid.resolveRoot grade + fluid.resolveRootComponent = instantiator.resolveRootComponent = fluid.typeTag("fluid.resolveRootComponent"); + instantiator.recordKnownComponent(fluid.rootComponent, fluid.resolveRootComponent, "resolveRootComponent", true); + + // obliterate resolveRoot's scope objects and replace by the real root scope - which is unused by its own children + var rootShadow = instantiator.idToShadow[fluid.rootComponent.id]; + var resolveRootShadow = instantiator.idToShadow[fluid.resolveRootComponent.id]; + resolveRootShadow.ownScope = rootShadow.ownScope; + resolveRootShadow.childrenScope = rootShadow.childrenScope; + + instantiator.recordKnownComponent(fluid.resolveRootComponent, instantiator, "instantiator", true); // needs to have a shadow so it can be injected + resolveRootShadow.childrenScope.instantiator = instantiator; // needs to be mounted since it never passes through cacheShadowGrades + }; + + fluid.constructRootComponents(fluid.globalInstantiator); // currently a singleton - in future, alternative instantiators might come back + + /** Expand a set of component options either immediately, or with deferred effect. + * The current policy is to expand immediately function arguments within fluid.assembleCreatorArguments which are not the main options of a + * component. The component's own options take {defer: true} as part of + * outerExpandOptions which produces an "expandOptions" structure holding the "strategy" and "initter" pattern + * common to ginger participants. + * Probably not to be advertised as part of a public API, but is considerably more stable than most of the rest + * of the IoC API structure especially with respect to the first arguments. + */ + +// TODO: Can we move outerExpandOptions to 2nd place? only user of 3 and 4 is fluid.makeExpandBlock + fluid.expandOptions = function (args, that, mergePolicy, localRecord, outerExpandOptions) { + if (!args) { + return args; + } + fluid.pushActivity("expandOptions", "expanding options %args for component %that ", {that: that, args: args}); + var expandOptions = fluid.makeStackResolverOptions(that, localRecord); + expandOptions.mergePolicy = mergePolicy; + var expanded = outerExpandOptions && outerExpandOptions.defer ? + fluid.makeExpandOptions(args, expandOptions) : fluid.expand(args, expandOptions); + fluid.popActivity(); + return expanded; + }; + + fluid.localRecordExpected = ["type", "options", "args", "createOnEvent", "priority", "recordType"]; // last element unavoidably polluting + + fluid.checkComponentRecord = function (defaults, localRecord) { + var expected = fluid.arrayToHash(fluid.localRecordExpected); + fluid.each(defaults && defaults.argumentMap, function(value, key) { + expected[key] = true; + }); + fluid.each(localRecord, function (value, key) { + if (!expected[key]) { + fluid.fail("Probable error in subcomponent record ", localRecord, " - key \"" + key + + "\" found, where the only legal options are " + + fluid.keys(expected).join(", ")); + } + }); + }; + + fluid.mergeRecordsToList = function (that, mergeRecords) { + var list = []; + fluid.each(mergeRecords, function (value, key) { + value.recordType = key; + if (key === "distributions") { + list.push.apply(list, fluid.transform(value, function (distributedBlock) { + return fluid.computeDistributionPriority(that, distributedBlock); + })); + } + else { + if (!value.options) { return; } + value.priority = fluid.mergeRecordTypes[key]; + if (value.priority === undefined) { + fluid.fail("Merge record with unrecognised type " + key + ": ", value); + } + list.push(value); + } + }); + return list; + }; + + // TODO: overall efficiency could huge be improved by resorting to the hated PROTOTYPALISM as an optimisation + // for this mergePolicy which occurs in every component. Although it is a deep structure, the root keys are all we need + var addPolicyBuiltins = function (policy) { + fluid.each(["gradeNames", "mergePolicy", "argumentMap", "components", "dynamicComponents", "invokers", "events", "listeners", "modelListeners", "distributeOptions", "transformOptions"], function (key) { + fluid.set(policy, [key, "*", "noexpand"], true); + }); + return policy; + }; + + // used from Fluid.js + fluid.generateExpandBlock = function (record, that, mergePolicy, localRecord) { + var expanded = fluid.expandOptions(record.options, record.contextThat || that, mergePolicy, localRecord, {defer: true}); + expanded.priority = record.priority; + expanded.namespace = record.namespace; + expanded.recordType = record.recordType; + return expanded; + }; + + var expandComponentOptionsImpl = function (mergePolicy, defaults, initRecord, that) { + var defaultCopy = fluid.copy(defaults); + addPolicyBuiltins(mergePolicy); + var shadow = fluid.shadowForComponent(that); + shadow.mergePolicy = mergePolicy; + var mergeRecords = { + defaults: {options: defaultCopy} + }; + + $.extend(mergeRecords, initRecord.mergeRecords); + // Do this here for gradeless components that were corrected by "localOptions" + if (mergeRecords.subcomponentRecord) { + fluid.checkComponentRecord(defaults, mergeRecords.subcomponentRecord); + } + + var expandList = fluid.mergeRecordsToList(that, mergeRecords); + + var togo = fluid.transform(expandList, function (value) { + return fluid.generateExpandBlock(value, that, mergePolicy, initRecord.localRecord); + }); + return togo; + }; + + fluid.fabricateDestroyMethod = function (that, name, instantiator, child) { + return function () { + instantiator.clearComponent(that, name, child); + }; + }; + + // Computes a name for a component appearing at the global root which is globally unique, from its nickName and id + fluid.computeGlobalMemberName = function (that) { + var nickName = fluid.computeNickName(that.typeName); + return nickName + "-" + that.id; + }; + + // Maps a type name to the member name to be used for it at a particular path level where it is intended to be unique + // Note that "." is still not supported within a member name + // unsupported, NON-API function + fluid.typeNameToMemberName = function (typeName) { + return typeName.replace(/\./g, "_"); + }; + + // This is the initial entry point from the non-IoC side reporting the first presence of a new component - called from fluid.mergeComponentOptions + fluid.expandComponentOptions = function (mergePolicy, defaults, userOptions, that) { + var initRecord = userOptions; // might have been tunnelled through "userOptions" from "assembleCreatorArguments" + var instantiator = userOptions && userOptions.marker === fluid.EXPAND ? userOptions.instantiator : null; + if (!instantiator) { // it is a top-level component which needs to be attached to the global root + instantiator = fluid.globalInstantiator; + initRecord = { // upgrade "userOptions" to the same format produced by fluid.assembleCreatorArguments via the subcomponent route + mergeRecords: {user: {options: fluid.expandCompact(userOptions, true)}}, + memberName: fluid.computeGlobalMemberName(that), + instantiator: instantiator, + parentThat: fluid.rootComponent + }; + } + that.destroy = fluid.fabricateDestroyMethod(initRecord.parentThat, initRecord.memberName, instantiator, that); + fluid.pushActivity("expandComponentOptions", "expanding component options %options with record %record for component %that", + {options: fluid.get(initRecord.mergeRecords, "user.options"), record: initRecord, that: that}); + + instantiator.recordKnownComponent(initRecord.parentThat, that, initRecord.memberName, true); + var togo = expandComponentOptionsImpl(mergePolicy, defaults, initRecord, that); + + fluid.popActivity(); + return togo; + }; + + /** Given a typeName, determine the final concrete + * "invocation specification" consisting of a concrete global function name + * and argument list which is suitable to be executed directly by fluid.invokeGlobalFunction. + */ + // options is just a disposition record containing memberName, componentRecord + fluid.assembleCreatorArguments = function (parentThat, typeName, options) { + var upDefaults = fluid.defaults(typeName); // we're not responsive to dynamic changes in argMap, but we don't believe in these anyway + if (!upDefaults || !upDefaults.argumentMap) { + fluid.fail("Error in assembleCreatorArguments: cannot look up component type name " + typeName + " to a component creator grade with an argumentMap"); + } + + var fakeThat = {}; // fake "that" for receiveDistributions since we try to match selectors before creation for FLUID-5013 + var distributions = parentThat ? fluid.receiveDistributions(parentThat, upDefaults.gradeNames, options.memberName, fakeThat) : []; + + var shadow = fluid.shadowForComponent(parentThat); + var localDynamic = shadow && shadow.subcomponentLocal && options.memberName ? shadow.subcomponentLocal[options.memberName] : null; + + var localRecord = $.extend({}, fluid.censorKeys(options.componentRecord, ["type"]), localDynamic); + + var argMap = upDefaults.argumentMap; + var findKeys = Object.keys(argMap).concat(["type"]); + + fluid.each(findKeys, function (name) { + for (var i = 0; i < distributions.length; ++ i) { // Apply non-options material from distributions (FLUID-5013) + if (distributions[i][name] !== undefined) { + localRecord[name] = distributions[i][name]; + } + } + }); + typeName = localRecord.type || typeName; + + delete localRecord.type; + delete localRecord.options; + + var mergeRecords = {distributions: distributions}; + + if (options.componentRecord !== undefined) { + // Deliberately put too many things here so they can be checked in expandComponentOptions (FLUID-4285) + mergeRecords.subcomponentRecord = $.extend({}, options.componentRecord); + } + var args = []; + fluid.each(argMap, function (index, name) { + var arg; + if (name === "options") { + arg = {marker: fluid.EXPAND, + localRecord: localDynamic, + mergeRecords: mergeRecords, + instantiator: fluid.getInstantiator(parentThat), + parentThat: parentThat, + memberName: options.memberName}; + } else { + var value = localRecord[name]; + arg = fluid.expandImmediate(value, parentThat, localRecord); + } + args[index] = arg; + }); + + var togo = { + args: args, + funcName: typeName + }; + return togo; + }; + + /** Instantiate the subcomponent with the supplied name of the supplied top-level component. Although this method + * is published as part of the Fluid API, it should not be called by general users and may not remain stable. It is + * currently the only mechanism provided for instantiating components whose definitions are dynamic, and will be + * replaced in time by dedicated declarative framework described by FLUID-5022. + * @param that {Component} the parent component for which the subcomponent is to be instantiated + * @param name {String} the name of the component - the index of the options block which configures it as part of the + * components section of its parent's options + */ + fluid.initDependent = function (that, name) { + if (that[name]) { return; } // TODO: move this into strategy + var component = that.options.components[name]; + fluid.pushActivity("initDependent", "instantiating dependent component with name \"%name\" with record %record as child of %parent", + {name: name, record: component, parent: that}); + var instance; + var instantiator = fluid.globalInstantiator; + + if (typeof(component) === "string") { + that[name] = fluid.inEvaluationMarker; + instance = fluid.expandImmediate(component, that); + if (instance) { + instantiator.recordKnownComponent(that, instance, name, false); + } else { + delete that[name]; + } + } + else if (component.type) { + var type = fluid.expandImmediate(component.type, that); + if (!type) { + fluid.fail("Error in subcomponent record: ", component.type, " could not be resolved to a type for component ", name, + " of parent ", that); + } + var invokeSpec = fluid.assembleCreatorArguments(that, type, {componentRecord: component, memberName: name}); + instance = fluid.initSubcomponentImpl(that, {type: invokeSpec.funcName}, invokeSpec.args); + } + else { + fluid.fail("Unrecognised material in place of subcomponent " + name + " - no \"type\" field found"); + } + fluid.popActivity(); + return instance; + }; + + fluid.bindDeferredComponent = function (that, componentName, component) { + var events = fluid.makeArray(component.createOnEvent); + fluid.each(events, function(eventName) { + var event = eventName.charAt(0) === "{" ? fluid.expandOptions(eventName, that) : that.events[eventName]; + if (!event || !event.addListener) { + fluid.fail("Error instantiating createOnEvent component with name " + componentName + " of parent ", that, " since event specification " + + eventName + " could not be expanded to an event - got ", event); + } + event.addListener(function () { + fluid.pushActivity("initDeferred", "instantiating deferred component %componentName of parent %that due to event %eventName", + {componentName: componentName, that: that, eventName: eventName}); + if (that[componentName]) { + fluid.globalInstantiator.clearComponent(that, componentName); + } + fluid.initDependent(that, componentName); + fluid.popActivity(); + }, null, component.priority); + }); + }; + + 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) { + fluid.pushActivity("initDependents", "instantiating dependent components for component %that", {that: that}); + var shadow = fluid.shadowForComponent(that); + shadow.memberStrategy.initter(); + shadow.invokerStrategy.initter(); + + fluid.getForComponent(that, "modelRelay"); + fluid.getForComponent(that, "model"); // trigger this as late as possible - but must be before components so that child component has model on its onCreate + + var options = that.options; + var components = options.components || {}; + var componentSort = []; + + fluid.each(components, function (component, name) { + if (!component.createOnEvent) { + var priority = fluid.priorityForComponent(component); + componentSort.push({namespace: name, priority: fluid.parsePriority(priority)}); + } + else { + fluid.bindDeferredComponent(that, name, component); + } + }); + fluid.sortByPriority(componentSort); + fluid.each(componentSort, function (entry) { + fluid.initDependent(that, entry.namespace); + }); + + fluid.popActivity(); + }; + + + /** BEGIN NEXUS METHODS **/ + + /** Construct a component with the supplied options at the specified path in the component tree. The parent path of the location must already be a component. + * @param path {String|Array of String} Path where the new component is to be constructed, represented as a string or array of segments + * @param typeName {String} The principal type of the component (name of its creator function) + * @param options {Object} [optional] Options supplied to the component + * @param instantiator {Instantiator} [optional] The instantiator holding the component to be created - if blank, the global instantiator will be used + */ + fluid.construct = function (path, options, instantiator) { + var record = fluid.destroy(path, instantiator); + // TODO: We must construct a more principled scheme for designating child components than this - especially once options become immutable + fluid.set(record.parent, ["options", "components", record.memberName], { + type: options.type, + options: options + }); + return fluid.initDependent(record.parent, record.memberName); + }; + + /** Destroys a component held at the specified path. The parent path must represent a component, although the component itself may be nonexistent + * @param path {String|Array of String} Path where the new component is to be destroyed, represented as a string or array of segments + * @param instantiator {Instantiator} [optional] The instantiator holding the component to be destroyed - if blank, the global instantiator will be used + */ + fluid.destroy = function (path, instantiator) { + instantiator = instantiator || fluid.globalInstantiator; + var segs = fluid.model.parseToSegments(path, instantiator.parseEL, true); + if (segs.length === 0) { + fluid.fail("Cannot destroy the root component"); + } + var memberName = segs.pop(), parentPath = instantiator.composeSegments.apply(null, segs); + var parent = instantiator.pathToComponent[parentPath]; + if (!parent) { + fluid.fail("Cannot modify component with nonexistent parent at path ", path); + } + if (parent[memberName]) { + parent[memberName].destroy(); + } + return { + parent: parent, + memberName: memberName + }; + }; + + /** END NEXUS METHODS **/ + + /** BEGIN IOC DEBUGGING METHODS **/ + fluid["debugger"] = function () { + /* jshint ignore:start */ + debugger; + /* jshint ignore:end */ + }; + + fluid.defaults("fluid.debuggingProbe", { + gradeNames: ["fluid.component"] + }); + + // probe looks like: + // target: {preview other}.listeners.eventName + // priority: first/last + // func: console.log/fluid.log/fluid.debugger + fluid.probeToDistribution = function (probe) { + var instantiator = fluid.globalInstantiator; + var parsed = fluid.parseContextReference(probe.target); + var segs = fluid.model.parseToSegments(parsed.path, instantiator.parseEL, true); + if (segs[0] !== "options") { + segs.unshift("options"); // compensate for this insanity until we have the great options flattening + } + var parsedPriority = fluid.parsePriority(probe.priority); + if (parsedPriority.constraint && !parsedPriority.constraint.target) { + parsedPriority.constraint.target = "authoring"; + } + return { + target: "{/ " + parsed.context + "}." + instantiator.composeSegments.apply(null, segs), + record: { + func: probe.func, + funcName: probe.funcName, + args: probe.args, + priority: fluid.renderPriority(parsedPriority) + } + }; + }; + + fluid.registerProbes = function (probes) { + var probeDistribution = fluid.transform(probes, fluid.probeToDistribution); + var memberName = "fluid_debuggingProbe_" + fluid.allocateGuid(); + fluid.construct([memberName], { + type: "fluid.debuggingProbe", + distributeOptions: probeDistribution + }); + return memberName; + }; + + fluid.deregisterProbes = function (probeName) { + fluid.destroy([probeName]); + }; + + /** END IOC DEBUGGING METHODS **/ + + fluid.thisistToApplicable = function (record, recthis, that) { + return { + apply: function (noThis, args) { + // Resolve this material late, to deal with cases where the target has only just been brought into existence + // (e.g. a jQuery target for rendered material) - TODO: Possibly implement cached versions of these as we might do for invokers + var resolvedThis = fluid.expandOptions(recthis, that); + if (typeof(resolvedThis) === "string") { + resolvedThis = fluid.getGlobalValue(resolvedThis); + } + if (!resolvedThis) { + fluid.fail("Could not resolve reference " + recthis + " to a value"); + } + var resolvedFunc = resolvedThis[record.method]; + if (typeof(resolvedFunc) !== "function") { + fluid.fail("Object ", resolvedThis, " at reference " + recthis + " has no member named " + record.method + " which is a function "); + } + fluid.log("Applying arguments ", args, " to method " + record.method + " of instance ", resolvedThis); + return resolvedFunc.apply(resolvedThis, args); + } + }; + }; + + fluid.changeToApplicable = function (record, that) { + return { + apply: function (noThis, args) { + var parsed = fluid.parseValidModelReference(that, "changePath listener record", record.changePath); + var value = fluid.expandOptions(record.value, that, {}, {"arguments": args}); + fluid.fireSourcedChange(parsed.applier, parsed.path, value, record.source); + } + }; + }; + + // Convert "exotic records" into an applicable form ("this/method" for FLUID-4878 or "changePath" for FLUID-3674) + fluid.recordToApplicable = function (record, that) { + if (record.changePath) { + return fluid.changeToApplicable(record, that); + } + var recthis = record["this"]; + if (record.method ^ recthis) { + fluid.fail("Record ", that, " must contain both entries \"method\" and \"this\" if it contains either"); + } + return record.method ? fluid.thisistToApplicable(record, recthis, that) : null; + }; + + fluid.getGlobalValueNonComponent = function (funcName, context) { // TODO: Guard this in listeners as well + var defaults = fluid.defaults(funcName); + if (defaults && fluid.hasGrade(defaults, "fluid.component")) { + fluid.fail("Error in function specification - cannot invoke function " + funcName + " in the context of " + context + ": component creator functions can only be used as subcomponents"); + } + return fluid.getGlobalValue(funcName); + }; + + fluid.makeInvoker = function (that, invokerec, name) { + if (typeof(invokerec) === "string") { + if (fluid.isIoCReference(invokerec)) { // shorthand case for direct function invokers (FLUID-4926) + invokerec = {func: invokerec}; + } else { + invokerec = {funcName: invokerec}; + } + } + if (invokerec.args !== undefined && !fluid.isArrayable(invokerec.args)) { + invokerec.args = fluid.makeArray(invokerec.args); + } + var func = fluid.recordToApplicable(invokerec, that); + var invokePre = fluid.preExpand(invokerec.args); + var localRecord = {}; + var expandOptions = fluid.makeStackResolverOptions(that, localRecord, true); + func = func || (invokerec.funcName? fluid.getGlobalValueNonComponent(invokerec.funcName, "an invoker") : fluid.expandImmediate(invokerec.func, that)); + if (!func || !func.apply) { + fluid.fail("Error in invoker record: could not resolve members func, funcName or method to a function implementation - got " + func + " from ", invokerec); + } + return function invokeInvoker () { + if (fluid.defeatLogging === false) { + fluid.pushActivity("invokeInvoker", "invoking invoker with name %name and record %record from component %that", {name: name, record: invokerec, that: that}); + } + var togo, finalArgs; + localRecord["arguments"] = arguments; + if (invokerec.args === undefined) { + finalArgs = arguments; + } else { + fluid.expandImmediateImpl(invokePre, expandOptions); + finalArgs = invokePre.source; + } + togo = func.apply(null, finalArgs); + if (fluid.defeatLogging === false) { + fluid.popActivity(); + } + return togo; + }; + }; + + // weird higher-order function so that we can staightforwardly dispatch original args back onto listener + fluid.event.makeTrackedListenerAdder = function (source) { + var shadow = fluid.shadowForComponent(source); + return function (event) { + return {addListener: function (listener) { + fluid.recordListener(event, listener, shadow); + event.addListener.apply(null, arguments); + } + }; + }; + }; + + fluid.event.listenerEngine = function (eventSpec, callback, adder) { + var argstruc = {}; + function checkFire() { + var notall = fluid.find(eventSpec, function(value, key) { + if (argstruc[key] === undefined) { + return true; + } + }); + if (!notall) { + var oldstruc = argstruc; + argstruc = {}; // guard against the case the callback perversely fires one of its prerequisites (FLUID-5112) + callback(oldstruc); + } + } + fluid.each(eventSpec, function (event, eventName) { + adder(event).addListener(function () { + argstruc[eventName] = fluid.makeArray(arguments); + checkFire(); + }); + }); + }; + + fluid.event.dispatchListener = function (that, listener, eventName, eventSpec, indirectArgs) { + var togo = function () { + fluid.pushActivity("dispatchListener", "firing to listener to event named %eventName of component %that", + {eventName: eventName, that: that}); + + var args = indirectArgs ? arguments[0] : fluid.makeArray(arguments); + if (eventSpec.args !== undefined) { + if (!fluid.isArrayable(eventSpec.args)) { + eventSpec.args = fluid.makeArray(eventSpec.args); + } + args = fluid.expandImmediate(eventSpec.args, that, {"arguments": args}); + } + var togo = fluid.event.invokeListener(listener, args); + + fluid.popActivity(); + return togo; + }; + fluid.event.impersonateListener(listener, togo); + return togo; + }; + + fluid.event.resolveSoftNamespace = function (key) { + if (typeof(key) !== "string") { + return null; + } else { + var lastpos = Math.max(key.lastIndexOf("."), key.lastIndexOf("}")); + return key.substring(lastpos + 1); + } + }; + + fluid.event.resolveListenerRecord = function (lisrec, that, eventName, namespace, standard) { + var badRec = function (record, extra) { + fluid.fail("Error in listener record - could not resolve reference ", record, " to a listener or firer. " + + "Did you miss out \"events.\" when referring to an event firer?" + extra); + }; + fluid.pushActivity("resolveListenerRecord", "resolving listener record for event named %eventName for component %that", + {eventName: eventName, that: that}); + var records = fluid.makeArray(lisrec); + var transRecs = fluid.transform(records, function (record) { + // TODO: FLUID-5242 fix - we copy here since distributeOptions does not copy options blocks that it distributes and we can hence corrupt them. + // need to clarify policy on options sharing - for slightly better efficiency, copy should happen during distribution and not here + var expanded = fluid.isPrimitive(record) || record.expander ? {listener: record} : fluid.copy(record); + var methodist = fluid.recordToApplicable(record, that); + if (methodist) { + expanded.listener = methodist; + } + else { + expanded.listener = expanded.listener || expanded.func || expanded.funcName; + } + if (!expanded.listener) { + badRec(record, " Listener record must contain a member named \"listener\", \"func\", \"funcName\" or \"method\""); + } + var softNamespace = record.method ? + fluid.event.resolveSoftNamespace(record["this"]) + "." + record.method : + fluid.event.resolveSoftNamespace(expanded.listener); + if (!expanded.namespace && !namespace && softNamespace) { + expanded.softNamespace = true; + expanded.namespace = (record.componentSource ? record.componentSource : that.typeName) + "." + softNamespace; + } + var listener = expanded.listener = fluid.expandOptions(expanded.listener, that); + if (!listener) { + badRec(record, ""); + } + var firer = false; + if (listener.typeName === "fluid.event.firer") { + listener = listener.fire; + firer = true; + } + expanded.listener = (standard && (expanded.args || firer)) ? fluid.event.dispatchListener(that, listener, eventName, expanded) : listener; + return expanded; + }); + var togo = { + records: transRecs, + adderWrapper: standard ? fluid.event.makeTrackedListenerAdder(that) : null + }; + fluid.popActivity(); + return togo; + }; + + fluid.event.expandOneEvent = function (that, event) { + var origin; + if (typeof(event) === "string" && event.charAt(0) !== "{") { + // Shorthand for resolving onto our own events, but with GINGER WORLD! + origin = fluid.getForComponent(that, ["events", event]); + } + else { + origin = fluid.expandOptions(event, that); + } + if (!origin || origin.typeName !== "fluid.event.firer") { + fluid.fail("Error in event specification - could not resolve base event reference ", event, " to an event firer: got ", origin); + } + return origin; + }; + + fluid.event.expandEvents = function (that, event) { + return typeof(event) === "string" ? + fluid.event.expandOneEvent(that, event) : + fluid.transform(event, function (oneEvent) { + return fluid.event.expandOneEvent(that, oneEvent); + }); + }; + + fluid.event.resolveEvent = function (that, eventName, eventSpec) { + fluid.pushActivity("resolveEvent", "resolving event with name %eventName attached to component %that", + {eventName: eventName, that: that}); + var adder = fluid.event.makeTrackedListenerAdder(that); + if (typeof(eventSpec) === "string") { + eventSpec = {event: eventSpec}; + } + var event = eventSpec.typeName === "fluid.event.firer" ? eventSpec : eventSpec.event || eventSpec.events; + if (!event) { + fluid.fail("Event specification for event with name " + eventName + " does not include a base event specification: ", eventSpec); + } + + var origin = event.typeName === "fluid.event.firer" ? event : fluid.event.expandEvents(that, event); + + var isMultiple = origin.typeName !== "fluid.event.firer"; + var isComposite = eventSpec.args || isMultiple; + // If "event" is not composite, we want to share the listener list and FIRE method with the original + // If "event" is composite, we need to create a new firer. "composite" includes case where any boiling + // occurred - this was implemented wrongly in 1.4. + var firer; + if (isComposite) { + firer = fluid.makeEventFirer({name: " [composite] " + fluid.event.nameEvent(that, eventName)}); + var dispatcher = fluid.event.dispatchListener(that, firer.fire, eventName, eventSpec, isMultiple); + if (isMultiple) { + fluid.event.listenerEngine(origin, dispatcher, adder); + } + else { + adder(origin).addListener(dispatcher); + } + } + else { + firer = {typeName: "fluid.event.firer"}; // jslint:ok - already defined + firer.fire = function () { + var outerArgs = fluid.makeArray(arguments); + fluid.pushActivity("fireSynthetic", "firing synthetic event %eventName ", {eventName: eventName}); + var togo = origin.fire.apply(null, outerArgs); + fluid.popActivity(); + return togo; + }; + firer.addListener = function (listener, namespace, priority, predicate, softNamespace) { + var dispatcher = fluid.event.dispatchListener(that, listener, eventName, eventSpec); + adder(origin).addListener(dispatcher, namespace, priority, predicate, softNamespace); + }; + firer.removeListener = function (listener) { + origin.removeListener(listener); + }; + } + fluid.popActivity(); + return firer; + }; + + /** BEGIN unofficial IoC material **/ + // The following three functions are unsupported ane only used in the renderer expander. + // The material they produce is no longer recognised for component resolution. + + fluid.withEnvironment = function (envAdd, func, root) { + root = root || fluid.globalThreadLocal(); + try { + for (var key in envAdd) { + root[key] = envAdd[key]; + } + $.extend(root, envAdd); + return func(); + } finally { + for (var key in envAdd) { /* jshint ignore:line */ /* duplicate "key" */ + delete root[key]; // TODO: users may want a recursive "scoping" model + } + } + }; + + fluid.fetchContextReference = function (parsed, directModel, env, elResolver, externalFetcher) { + // The "elResolver" is a hack to make certain common idioms in protoTrees work correctly, where a contextualised EL + // path actually resolves onto a further EL reference rather than directly onto a value target + if (elResolver) { + parsed = elResolver(parsed, env); + } + var base = parsed.context? env[parsed.context] : directModel; + if (!base) { + var resolveExternal = externalFetcher && externalFetcher(parsed); + return resolveExternal || base; + } + return parsed.noDereference? parsed.path : fluid.get(base, parsed.path); + }; + + fluid.makeEnvironmentFetcher = function (directModel, elResolver, envGetter, externalFetcher) { + envGetter = envGetter || fluid.globalThreadLocal; + return function(parsed) { + var env = envGetter(); + return fluid.fetchContextReference(parsed, directModel, env, elResolver, externalFetcher); + }; + }; + + /** END of unofficial IoC material **/ + + /* Compact expansion machinery - for short form invoker and expander references such as @expand:func(arg) and func(arg) */ + + fluid.coerceToPrimitive = function (string) { + return string === "false" ? false : (string === "true" ? true : + (isFinite(string) ? Number(string) : string)); + }; + + fluid.compactStringToRec = function (string, type) { + var openPos = string.indexOf("("); + var closePos = string.indexOf(")"); + if (openPos === -1 ^ closePos === -1 || openPos > closePos) { + fluid.fail("Badly-formed compact " + type + " record without matching parentheses: ", string); + } + if (openPos !== -1 && closePos !== -1) { + var prefix = string.substring(0, openPos); + var body = string.substring(openPos + 1, closePos); + var args = fluid.transform(body.split(","), $.trim, fluid.coerceToPrimitive); + var togo = { + args: args + }; + togo[prefix.charAt(0) === "{" ? "func" : "funcName"] = prefix; + return togo; + } + else if (type === "expander") { + fluid.fail("Badly-formed compact expander record without parentheses: ", string); + } + return string; + }; + + fluid.expandPrefix = "@expand:"; + + fluid.expandCompactString = function (string, active) { + var rec = string; + if (string.indexOf(fluid.expandPrefix) === 0) { + var rem = string.substring(fluid.expandPrefix.length); + rec = { + expander: fluid.compactStringToRec(rem, "expander") + }; + } + else if (active) { + rec = fluid.compactStringToRec(string, active); + } + return rec; + }; + + var singularPenRecord = { + listeners: "listener", + modelListeners: "modelListener" + }; + + var singularRecord = $.extend({ + invokers: "invoker" + }, singularPenRecord); + + fluid.expandCompactRec = function (segs, target, source, userOptions) { + fluid.guardCircularExpansion(segs, segs.length); + var pen = segs.length > 0 ? segs[segs.length - 1] : ""; + var active = singularRecord[pen]; + if (!active && segs.length > 1) { + active = singularPenRecord[segs[segs.length - 2]]; // support array of listeners and modelListeners + } + fluid.each(source, function (value, key) { + // TODO: hack here to avoid corrupting old-style model references which were listed with "preserve" - eliminate this along with that mergePolicy + if (fluid.isPlainObject(value) && !fluid.isDOMish(value) && !(userOptions && key === "model" && segs.length === 0)) { + target[key] = fluid.freshContainer(value); + segs.push(key); + fluid.expandCompactRec(segs, target[key], value); + segs.pop(); + return; + } + else if (typeof(value) === "string") { + value = fluid.expandCompactString(value, active); + } + target[key] = value; + }); + }; + + fluid.expandCompact = function (options, userOptions) { + var togo = {}; + fluid.expandCompactRec([], togo, options, userOptions); + return togo; + }; + + /** End compact record expansion machinery **/ + + fluid.isIoCReference = function (ref) { + return typeof(ref) === "string" && ref.charAt(0) === "{" && ref.indexOf("}") > 0; + }; + + 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); + } + } + }; + + fluid.extractELWithContext = function (string, options) { + var EL = fluid.extractEL(string, options); + if (fluid.isIoCReference(EL)) { + return fluid.parseContextReference(EL); + } + return EL? {path: EL} : EL; + }; + + fluid.parseContextReference = function (reference, index, delimiter) { + index = index || 0; + 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 ? "." + parsed.path : ""); + }; + + // TODO: Once we eliminate expandSource, all of this tree of functions can be hived off to RendererUtilities + fluid.resolveContextValue = function (string, options) { + function fetch(parsed) { + fluid.pushActivity("resolveContextValue", "resolving context value %string", {string: string}); + var togo = options.fetcher(parsed); + fluid.pushActivity("resolvedContextValue", "resolved value %string to value %value", {string: string, value: togo}); + fluid.popActivity(2); + return togo; + } + var parsed; + if (options.bareContextRefs && fluid.isIoCReference(string)) { + parsed = fluid.parseContextReference(string); + return fetch(parsed); + } + else if (options.ELstyle && options.ELstyle !== "${}") { + parsed = fluid.extractELWithContext(string, options); + if (parsed) { + return fetch(parsed); + } + } + while (typeof(string) === "string") { + var i1 = string.indexOf("${"); + var i2 = string.indexOf("}", i1 + 2); + if (i1 !== -1 && i2 !== -1) { + if (string.charAt(i1 + 2) === "{") { + parsed = fluid.parseContextReference(string, i1 + 2, "}"); + i2 = parsed.endpos; + } + else { + parsed = {path: string.substring(i1 + 2, i2)}; + } + var subs = fetch(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; + }; + + // This function appears somewhat reusable, but not entirely - it probably needs to be packaged + // along with the particular "strategy". Very similar to the old "filter"... the "outer driver" needs + // to execute it to get the first recursion going at top level. This was one of the most odd results + // of the reorganisation, since the "old work" seemed much more naturally expressed in terms of values + // and what happened to them. The "new work" is expressed in terms of paths and how to move amongst them. + fluid.fetchExpandChildren = function (target, i, segs, source, mergePolicy, options) { + if (source.expander) { // possible expander at top level + var expanded = fluid.expandExpander(target, source, options); + if (fluid.isPrimitive(expanded) || fluid.isDOMish(expanded) || !fluid.isPlainObject(expanded) || (fluid.isArrayable(expanded) ^ fluid.isArrayable(target))) { + return expanded; + } + else { // make an attempt to preserve the root reference if possible + $.extend(true, target, expanded); + } + } + // NOTE! This expects that RHS is concrete! For material input to "expansion" this happens to be the case, but is not + // true for other algorithms. Inconsistently, this algorithm uses "sourceStrategy" below. In fact, this "fetchChildren" + // operation looks like it is a fundamental primitive of the system. We do call "deliverer" early which enables correct + // reference to parent nodes up the tree - however, anyone processing a tree IN THE CHAIN requires that it is produced + // concretely at the point STRATEGY returns. Which in fact it is............... + fluid.each(source, function (newSource, key) { + if (newSource === undefined) { + target[key] = undefined; // avoid ever dispatching to ourselves with undefined source + } + else if (key !== "expander") { + segs[i] = key; + if (fluid.getImmediate(options.exceptions, segs, i) !== true) { + options.strategy(target, key, i + 1, segs, source, mergePolicy); + } + } + }); + return target; + }; + + // TODO: This method is unnecessary and will quadratic inefficiency if RHS block is not concrete. + // The driver should detect "homogeneous uni-strategy trundling" and agree to preserve the extra + // "cursor arguments" which should be advertised somehow (at least their number) + function regenerateCursor (source, segs, limit, sourceStrategy) { + for (var i = 0; i < limit; ++ i) { + // copy segs to avoid aliasing with FLUID-5243 + source = sourceStrategy(source, segs[i], i, fluid.makeArray(segs)); + } + return source; + } + + fluid.isUnexpandable = function (source) { // slightly more efficient compound of fluid.isCopyable and fluid.isComponent - review performance + return fluid.isPrimitive(source) || fluid.isComponent(source) || source.nodeType !== undefined || source.jquery || !fluid.isPlainObject(source); + }; + + fluid.expandSource = function (options, target, i, segs, deliverer, source, policy, recurse) { + var expanded, isTrunk; + var thisPolicy = fluid.derefMergePolicy(policy); + if (typeof (source) === "string" && !thisPolicy.noexpand) { + if (!options.defaultEL || source.charAt(0) === "{") { // hard-code this for performance + fluid.pushActivity("expandContextValue", "expanding context value %source held at path %path", {source: source, path: fluid.path.apply(null, segs.slice(0, i))}); + expanded = fluid.resolveContextValue(source, options); + fluid.popActivity(1); + } else { + expanded = source; + } + } + else if (thisPolicy.noexpand || fluid.isUnexpandable(source)) { + expanded = source; + } + else if (source.expander) { + expanded = fluid.expandExpander(deliverer, source, options); + } + else { + expanded = fluid.freshContainer(source); + isTrunk = true; + } + if (expanded !== fluid.NO_VALUE) { + deliverer(expanded); + } + if (isTrunk) { + recurse(expanded, source, i, segs, policy); + } + return expanded; + }; + + fluid.guardCircularExpansion = function (segs, i) { + if (i > fluid.strategyRecursionBailout) { + fluid.fail("Overflow/circularity in options expansion, current path is ", segs, " at depth " , i, " - please ensure options are not circularly connected, or protect from expansion using the \"noexpand\" policy or expander"); + } + }; + + fluid.makeExpandStrategy = function (options) { + var recurse = function (target, source, i, segs, policy) { + return fluid.fetchExpandChildren(target, i || 0, segs || [], source, policy, options); + }; + var strategy = function (target, name, i, segs, source, policy) { + fluid.guardCircularExpansion(segs, i); + if (!target) { + return; + } + if (target.hasOwnProperty(name)) { // bail out if our work has already been done + return target[name]; + } + if (source === undefined) { // recover our state in case this is an external entry point + source = regenerateCursor(options.source, segs, i - 1, options.sourceStrategy); + policy = regenerateCursor(options.mergePolicy, segs, i - 1, fluid.concreteTrundler); + } + var thisSource = options.sourceStrategy(source, name, i, segs); + var thisPolicy = fluid.concreteTrundler(policy, name); + function deliverer(value) { + target[name] = value; + } + return fluid.expandSource(options, target, i, segs, deliverer, thisSource, thisPolicy, recurse); + }; + options.recurse = recurse; + options.strategy = strategy; + return strategy; + }; + + fluid.defaults("fluid.makeExpandOptions", { + ELstyle: "${}", + bareContextRefs: true, + target: fluid.inCreationMarker + }); + + fluid.makeExpandOptions = function (source, options) { + options = $.extend({}, fluid.rawDefaults("fluid.makeExpandOptions"), options); + options.defaultEL = options.ELStyle === "${}" && options.bareContextRefs; // optimisation to help expander + options.expandSource = function (source) { + return fluid.expandSource(options, null, 0, [], fluid.identity, source, options.mergePolicy, false); + }; + if (!fluid.isUnexpandable(source)) { + options.source = source; + options.target = fluid.freshContainer(source); + options.sourceStrategy = options.sourceStrategy || fluid.concreteTrundler; + fluid.makeExpandStrategy(options); + options.initter = function () { + options.target = fluid.fetchExpandChildren(options.target, 0, [], options.source, options.mergePolicy, options); + }; + } + else { // these init immediately since we must deliver a valid root target + options.strategy = fluid.concreteTrundler; + options.initter = fluid.identity; + if (typeof(source) === "string") { + options.target = options.expandSource(source); + } + else { + options.target = source; + } + } + return options; + }; + + // supported, PUBLIC API function + fluid.expand = function (source, options) { + var expandOptions = fluid.makeExpandOptions(source, options); + expandOptions.initter(); + return expandOptions.target; + }; + + fluid.preExpandRecurse = function (root, source, holder, member, rootSegs) { // on entry, holder[member] = source + fluid.guardCircularExpansion(rootSegs, rootSegs.length); + function pushExpander(expander) { + root.expanders.push({expander: expander, holder: holder, member: member}); + delete holder[member]; + } + if (fluid.isIoCReference(source)) { + var parsed = fluid.parseContextReference(source); + var segs = fluid.model.parseEL(parsed.path); + pushExpander({ + typeFunc: fluid.expander.fetch, + context: parsed.context, + segs: segs + }); + } else if (fluid.isPlainObject(source)) { + if (source.expander) { + source.expander.typeFunc = fluid.getGlobalValue(source.expander.type || "fluid.invokeFunc"); + pushExpander(source.expander); + } else { + fluid.each(source, function (value, key) { + rootSegs.push(key); + fluid.preExpandRecurse(root, value, source, key, rootSegs); + rootSegs.pop(); + }); + } + } + }; + + fluid.preExpand = function (source) { + var root = { + expanders: [], + source: fluid.isUnexpandable(source) ? source : fluid.copy(source) + }; + fluid.preExpandRecurse(root, root.source, root, "source", []); + return root; + }; + + // Main pathway for freestanding material that is not part of a component's options + fluid.expandImmediate = function (source, that, localRecord) { + var options = fluid.makeStackResolverOptions(that, localRecord, true); // TODO: ELstyle and target are now ignored + var root = fluid.preExpand(source); + fluid.expandImmediateImpl(root, options); + return root.source; + }; + + // High performance expander for situations such as invokers, listeners, where raw materials can be cached - consumes "root" structure produced by preExpand + fluid.expandImmediateImpl = function (root, options) { + var expanders = root.expanders; + for (var i = 0; i < expanders.length; ++ i) { + var expander = expanders[i]; + expander.holder[expander.member] = expander.expander.typeFunc(null, expander, options); + } + }; + + fluid.expandExpander = function (deliverer, source, options) { + var expander = fluid.getGlobalValue(source.expander.type || "fluid.invokeFunc"); + if (!expander) { + fluid.fail("Unknown expander with type " + source.expander.type); + } + return expander(deliverer, source, options); + }; + + fluid.registerNamespace("fluid.expander"); + + fluid.expander.fetch = function (deliverer, source, options) { + var localRecord = options.localRecord, context = source.expander.context, segs = source.expander.segs; + var inLocal = localRecord[context] !== undefined; + // somewhat hack to anticipate "fits" for FLUID-4925 - we assume that if THIS component is in construction, its reference target might be too + var component = inLocal ? localRecord[context] : fluid.resolveContext(context, options.contextThat, options.contextThat.lifecycleStatus === "constructed"); + if (component) { + var root = component; + if (inLocal || component.lifecycleStatus === "constructed") { + for (var i = 0; i < segs.length; ++ i) { + root = root ? root[segs[i]] : undefined; + } + } else if (component.lifecycleStatus !== "destroyed") { + root = fluid.getForComponent(component, segs); + } else { + fluid.fail("Cannot resolve path " + segs.join(".") + " into component ", component, " which has been destroyed"); + } + if (root === undefined && !inLocal) { // last-ditch attempt to get exotic EL value from component + root = fluid.getForComponent(component, segs); + } + return root; + } + }; + + /** "light" expanders, starting with the default expander invokeFunc, + which makes an arbitrary function call (after expanding arguments) and are then replaced in + the configuration with the call results. These will probably be abolished and replaced with + equivalent model transformation machinery **/ + + // This one is now positioned as the "universal expander" - default if no type supplied + fluid.invokeFunc = function (deliverer, source, options) { + var expander = source.expander; + var args = fluid.makeArray(expander.args); + expander.args = args; // head off case where args is an EL reference which resolves to an array + if (options.recurse) { // only available in the path from fluid.expandOptions - this will be abolished in the end + args = options.recurse([], args); + } else { + expander = fluid.expandImmediate(expander, options.contextThat); + args = expander.args; + } + var funcEntry = expander.func || expander.funcName; + var func = (options.expandSource ? options.expandSource(funcEntry) : funcEntry) || fluid.recordToApplicable(expander, options.contextThat); + if (!func) { + fluid.fail("Error in expander record - " + funcEntry + " could not be resolved to a function for component ", options.contextThat); + } + return func.apply ? func.apply(null, args) : fluid.invokeGlobalFunction(func, args); + }; + + // The "noexpand" expander which simply unwraps one level of expansion and ceases. + fluid.noexpand = function (deliverer, source) { + return source.expander.value ? source.expander.value : source.expander.tree; + }; + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/FluidPromises.js b/ppig-2015/example/lib/infusion/FluidPromises.js new file mode 100644 index 0000000..91856f4 --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidPromises.js @@ -0,0 +1,257 @@ +/*! + Copyright unscriptable.com / John Hann 2011 + Copyright Lucendo Development Ltd. 2014 + + License MIT +*/ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + +// Light fluidification of minimal promises library. See original gist at +// https://gist.github.com/unscriptable/814052 for limitations and commentary + +// This implementation provides what could be described as "flat promises" with +// no support for structure programming idioms involving promise chaining or composition. +// It provides what a proponent of mainstream promises would describe as +// a "glorified callback aggregator" + + fluid.promise = function () { + var that = { + onResolve: [], + onReject: [] + // disposition + // value + }; + that.then = function (onResolve, onReject) { + if (onResolve) { + if (that.disposition === "resolve") { + onResolve(that.value); + } else { + that.onResolve.push(onResolve); + } + } + if (onReject) { + if (that.disposition === "reject") { + onReject(that.value); + } else { + that.onReject.push(onReject); + } + } + }; + that.resolve = function (value) { + if (that.disposition) { + fluid.fail("Error: resolving promise ", that, + " which has already received \"" + that.disposition + "\""); + } else { + that.complete("resolve", that.onResolve, value); + } + }; + that.reject = function (reason) { + if (that.disposition) { + fluid.fail("Error: rejecting promise ", that, + "which has already received \"" + that.disposition + "\""); + } else { + that.complete("reject", that.onReject, reason); + } + }; + // PRIVATE, NON-API METHOD + that.complete = function (which, queue, arg) { + that.disposition = which; + that.value = arg; + for (var i = 0; i < queue.length; ++ i) { + queue[i](arg); + } + }; + return that; + }; + + /** Any object with a member then of type function passes this test. + * This includes essentially every known variety, including jQuery promises. + */ + fluid.isPromise = function (totest) { + return totest && typeof(totest.then) === "function"; + }; + + /** Coerces any value to a promise + * @param promiseOrValue The value to be coerced + * @return If the supplied value is already a promise, it is returned unchanged. Otherwise a fresh promise is created with the value as resolution and returned + */ + fluid.toPromise = function (promiseOrValue) { + if (fluid.isPromise(promiseOrValue)) { + return promiseOrValue; + } else { + var togo = fluid.promise(); + togo.resolve(promiseOrValue); + return togo; + } + }; + + /** Chains the resolution methods of one promise (target) so that they follow those of another (source). + * That is, whenever source resolves, target will resolve, or when source rejects, target will reject, with the + * same payloads in each case. + */ + fluid.promise.follow = function (source, target) { + source.then(target.resolve, target.reject); + }; + + /** Returns a promise whose resolved value is mapped from the source promise or value by the supplied function. + * @param source {Object|Promise} An object or promise whose value is to be mapped + * @param func {Function} A function which will map the resolved promise value + * @return {Promise} A promise for the resolved mapped value. + */ + fluid.promise.map = function (source, func) { + var promise = fluid.toPromise(source); + var togo = fluid.promise(); + promise.then(function (value) { + var mapped = func(value); + togo.resolve(mapped); + }, function (error) { + togo.reject(error); + }); + return togo; + }; + + /* General skeleton for all sequential promise algorithms, e.g. transform, reduce, sequence, etc. + * These accept a variable "strategy" pair to customise the interchange of values and final return + */ + + fluid.promise.makeSequencer = function (sources, options, strategy) { + if (!fluid.isArrayable(sources)) { + fluid.fail("fluid.promise sequence algorithms must be supplied an array as source"); + } + return { + sources: sources, + resolvedSources: [], // the values of "sources" only with functions invoked (an array of promises or values) + index: 0, + strategy: strategy, + options: options, // available to be supplied to each listener + returns: [], + promise: fluid.promise() // the final return value + }; + }; + + fluid.promise.progressSequence = function (that, retValue) { + that.returns.push(retValue); + that.index++; + // No we dun't have no tail recursion elimination + fluid.promise.resumeSequence(that); + }; + + fluid.promise.processSequenceReject = function (that, error) { // Allow earlier promises in the sequence to wrap the rejection supplied by later ones (FLUID-5584) + for (var i = that.index - 1; i >= 0; -- i) { + var resolved = that.resolvedSources[i]; + var accumulator = fluid.isPromise(resolved) && typeof(resolved.accumulateRejectionReason) === "function" ? resolved.accumulateRejectionReason : fluid.identity; + error = accumulator(error); + } + that.promise.reject(error); + }; + + fluid.promise.resumeSequence = function (that) { + if (that.index === that.sources.length) { + that.promise.resolve(that.strategy.resolveResult(that)); + } else { + var value = that.strategy.invokeNext(that); + that.resolvedSources[that.index] = value; + if (fluid.isPromise(value)) { + value.then(function (retValue) { + fluid.promise.progressSequence(that, retValue); + }, function (error) { + fluid.promise.processSequenceReject(that, error); + }); + } else { + fluid.promise.progressSequence(that, value); + } + } + }; + + // SEQUENCE ALGORITHM APPLYING PROMISES + + fluid.promise.makeSequenceStrategy = function () { + return { + invokeNext: function (that) { + var source = that.sources[that.index]; + return typeof(source) === "function" ? source(that.options) : source; + }, + resolveResult: function (that) { + return that.returns; + } + }; + }; + + // accepts an array of values, promises or functions returning promises - in the case of functions returning promises, + // will assure that at most one of these is "in flight" at a time - that is, the succeeding function will not be invoked + // until the promise at the preceding position has resolved + fluid.promise.sequence = function (sources, options) { + var sequencer = fluid.promise.makeSequencer(sources, options, fluid.promise.makeSequenceStrategy()); + fluid.promise.resumeSequence(sequencer); + return sequencer.promise; + }; + + // TRANSFORM ALGORITHM APPLYING PROMISES + + fluid.promise.makeTransformerStrategy = function () { + return { + invokeNext: function (that) { + var lisrec = that.sources[that.index]; + lisrec.listener = fluid.event.resolveListener(lisrec.listener); + var value = lisrec.listener(that.returns[that.index], that.options); + return value; + }, + resolveResult: function (that) { + return that.returns[that.index]; + } + }; + }; + + // Construct a "mini-object" managing the process of a sequence of transforms, + // each of which may be synchronous or return a promise + fluid.promise.makeTransformer = function (listeners, payload, options) { + listeners.unshift({listener: + function () { + return payload; + } + }); + var sequencer = fluid.promise.makeSequencer(listeners, options, fluid.promise.makeTransformerStrategy()); + sequencer.returns.push(null); // first dummy return from initial entry + fluid.promise.resumeSequence(sequencer); + return sequencer; + }; + + fluid.promise.filterNamespaces = function (listeners, namespaces) { + if (!namespaces) { + return listeners; + } + return fluid.remove_if(fluid.makeArray(listeners), function (element) { + return element.namespace && !element.softNamespace && !fluid.contains(namespaces, element.namespace); + }); + }; + + /** Top-level API to operate a Fluid event which manages a sequence of + * chained transforms. Rather than being a standard listener accepting the + * same payload, each listener to the event accepts the payload returned by the + * previous listener, and returns either a transformed payload or else a promise + * yielding such a payload. + * @param event {fluid.eventFirer} A Fluid event to which the listeners are to be interpreted as + * elements cooperating in a chained transform. Each listener will receive arguments (payload, options) where payload + * is the (successful, resolved) return value of the previous listener, and options is the final argument to this function + * @param payload {Object|Promise} The initial payload input to the transform chain + * @param options {Object} A free object containing options governing the transform. Fields interpreted at this top level are: + * reverse {Boolean}: true if the listeners are to be called in reverse order of priority (typically the case for an inverse transform) + * filterTransforms {Array}: An array of listener namespaces. If this field is set, only the transform elements whose listener namespaces listed in this array will be applied. + * @return {fluid.promise} A promise which will yield either the final transformed value, or the response of the first transform which fails. + */ + + fluid.promise.fireTransformEvent = function (event, payload, options) { + options = options || {}; + var listeners = options.reverse ? fluid.makeArray(event.sortedListeners).reverse() : + fluid.makeArray(event.sortedListeners); + listeners = fluid.promise.filterNamespaces(listeners, options.filterNamespaces); + var transformer = fluid.promise.makeTransformer(listeners, payload, options); + return transformer.promise; + }; + + +})(jQuery, fluid_2_0); \ No newline at end of file diff --git a/ppig-2015/example/lib/infusion/FluidRequests.js b/ppig-2015/example/lib/infusion/FluidRequests.js new file mode 100644 index 0000000..9606095 --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidRequests.js @@ -0,0 +1,437 @@ +/* +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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + /** NOTE: All contents of this file are DEPRECATED and no entry point should be considered a supported API **/ + + fluid.explodeLocalisedName = function (fileName, locale, defaultLocale) { + var lastDot = fileName.lastIndexOf("."); + if (lastDot === -1 || lastDot === 0) { + lastDot = fileName.length; + } + var baseName = fileName.substring(0, lastDot); + var extension = fileName.substring(lastDot); + + var segs = locale.split("_"); + + var exploded = fluid.transform(segs, function (seg, index) { + var shortSegs = segs.slice(0, index + 1); + return baseName + "_" + shortSegs.join("_") + extension; + }); + if (defaultLocale) { + exploded.unshift(baseName + "_" + defaultLocale + extension); + } + return exploded; + }; + + /** 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 = { + options: fluid.copy(options || {}) + }; + that.resourceSpecs = resourceSpecs; + that.callback = callback; + that.operate = function() { + fluid.fetchResources.fetchResourcesImpl(that); + }; + fluid.each(resourceSpecs, function(resourceSpec, key) { + resourceSpec.recurseFirer = fluid.makeEventFirer({name: "I/O completion for resource \"" + key + "\""}); + resourceSpec.recurseFirer.addListener(that.operate); + if (resourceSpec.url && !resourceSpec.href) { + resourceSpec.href = resourceSpec.url; + } + if (that.options.defaultLocale) { + resourceSpec.defaultLocale = that.options.defaultLocale; + if (resourceSpec.locale === undefined) { + resourceSpec.locale = that.options.defaultLocale; + } + } + }); + if (that.options.amalgamateClasses) { + fluid.fetchResources.amalgamateClasses(resourceSpecs, that.options.amalgamateClasses, that.operate); + } + fluid.fetchResources.explodeForLocales(resourceSpecs); + that.operate(); + return that; + }; + + fluid.fetchResources.explodeForLocales = function (resourceSpecs) { + fluid.each(resourceSpecs, function (resourceSpec, key) { + if (resourceSpec.locale) { + var exploded = fluid.explodeLocalisedName(resourceSpec.href, resourceSpec.locale, resourceSpec.defaultLocale); + for (var i = 0; i < exploded.length; ++ i) { + var newKey = key + "$localised-" + i; + var newRecord = $.extend(true, {}, resourceSpec, { + href: exploded[i], + localeExploded: true + }); + resourceSpecs[newKey] = newRecord; + } + resourceSpec.localeExploded = exploded.length; + } + }); + return resourceSpecs; + }; + + fluid.fetchResources.condenseOneResource = function (resourceSpecs, resourceSpec, key, localeCount) { + var localeSpecs = [resourceSpec]; + for (var i = 0; i < localeCount; ++ i) { + var localKey = key + "$localised-" + i; + localeSpecs.unshift(resourceSpecs[localKey]); + delete resourceSpecs[localKey]; + } + var lastNonError = fluid.find_if(localeSpecs, function (spec) { + return !spec.fetchError; + }); + if (lastNonError) { + resourceSpecs[key] = lastNonError; + } + }; + + fluid.fetchResources.condenseForLocales = function (resourceSpecs) { + fluid.each(resourceSpecs, function (resourceSpec, key) { + if (typeof(resourceSpec.localeExploded) === "number") { + fluid.fetchResources.condenseOneResource(resourceSpecs, resourceSpec, key, resourceSpec.localeExploded); + } + }); + }; + + fluid.fetchResources.notifyResources = function (that, resourceSpecs, callback) { + fluid.fetchResources.condenseForLocales(resourceSpecs); + callback(resourceSpecs); + }; + + /* + * 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, fetchError) { + 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]; + } + var result = {response: response, fetchError: fetchError}; + resourceCache[canon] = result; + cached.fire(response, fetchError); + } + }; + + /* + * This function is unsupported: It is not really intended for use by implementors. + */ + fluid.fetchResources.completeRequest = function(thisSpec) { + 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 + }; + if (thisSpec.forceCache) { + fluid.fetchResources.handleCachedRequest(thisSpec, null, thisSpec.fetchError); + } + 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.makeEventFirer({name: "cache notifier for resource URL " + canon}); + 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$$) { + if (cached.response) { + options.success(cached.response); + } else { + options.error(cached.fetchError); + } + } + else { + fluid.log("Request for cached resource which is in flight: url " + canon); + cached.addListener(function(response, fetchError) { + if (response) { + options.success(response); + } else { + options.error(fetchError); + } + }); + } + } + }; + + /* + * 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 ? (internal ? + 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 + } : external ) : internal; + }; + + // unsupported, NON-API function + fluid.fetchResources.composePolicy = function(target, source) { + return fluid.fetchResources.composeCallbacks(target, source); + }; + + fluid.defaults("fluid.fetchResources.issueRequest", { + mergePolicy: { + success: fluid.fetchResources.composePolicy, + error: fluid.fetchResources.composePolicy, + url: "reverse" + } + }); + + // unsupported, NON-API function + fluid.fetchResources.issueRequest = function(resourceSpec, key) { + var thisCallback = fluid.fetchResources.makeResourceCallback(resourceSpec); + var options = { + url: resourceSpec.href, + success: thisCallback.success, + error: thisCallback.error, + dataType: resourceSpec.dataType || "text" + }; + fluid.fetchResources.timeSuccessCallback(resourceSpec); + options = 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() { + fluid.fetchResources.notifyResources(that, resourceSpecs, that.callback); + }, 1); + } + else { + fluid.fetchResources.notifyResources(that, resourceSpecs, that.callback); + } + } + }; + + // TODO: This framework function is a stop-gap before the "ginger world" is capable of + // asynchronous instantiation. It currently performs very poor fidelity expansion of a + // component's options to discover "resources" only held in the static environment + fluid.fetchResources.primeCacheFromResources = function(componentName) { + var resources = fluid.defaults(componentName).resources; + var expanded = (fluid.expandOptions ? fluid.expandOptions : fluid.identity)(fluid.copy(resources)); + 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(deliverer, source, 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.expand(expander.resourceSpecCollector, expandOptions); + delete spec.type; + delete spec.resourceSpecCollector; + delete spec.fetchKey; + var environmentdisposer = function(disposed) { + deliverer(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 fluid.NO_VALUE; + }; + + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/FluidView.js b/ppig-2015/example/lib/infusion/FluidView.js new file mode 100644 index 0000000..9dbf959 --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidView.js @@ -0,0 +1,663 @@ +/* +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 **/ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + fluid.defaults("fluid.viewComponent", { + gradeNames: ["fluid.modelComponent"], + initFunction: "fluid.initView", + argumentMap: { + container: 0, + options: 1 + }, + members: { // Used to allow early access to DOM binder via IoC, but to also avoid triggering evaluation of selectors + dom: "@expand:fluid.initDomBinder({that}, {that}.options.selectors)" + } + }); + + // unsupported, NON-API function + fluid.dumpSelector = function (selectable) { + return typeof (selectable) === "string" ? selectable : + selectable.selector ? selectable.selector : ""; + }; + + // unsupported, NON-API function + // NOTE: this function represents a temporary strategy until we have more integrated IoC debugging. + // It preserves the 1.3 and previous framework behaviour for the 1.x releases, 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.initLittleComponent. Revisit this issue for 2.0 + fluid.diagnoseFailedView = function (componentName, that, options, args) { + if (!that && (fluid.hasGrade(options, "fluid.viewComponent") || fluid.hasGrade(options, "fluid.viewComponent"))) { + var container = fluid.wrap(args[1]); + var message1 = "Instantiation of autoInit component with type " + componentName + " failed, since "; + if (!container) { + fluid.fail(message1 + " container argument is empty"); + } + else if (container.length === 0) { + fluid.fail(message1 + "selector \"", fluid.dumpSelector(args[1]), "\" did not match any markup in the document"); + } else { + fluid.fail(message1 + " component creator function did not return a value"); + } + } + }; + + fluid.checkTryCatchParameter = function () { + var location = window.location || { search: "", protocol: "file:" }; + var GETparams = location.search.slice(1).split("&"); + return fluid.find(GETparams, function (param) { + if (param.indexOf("notrycatch") === 0) { + return true; + } + }) === true; + }; + + fluid.notrycatch = fluid.checkTryCatchParameter(); + + + /** + * 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 + * @param {jQuery} userJQuery the jQuery object to use for the wrapping, optional - use the current jQuery if absent + */ + fluid.wrap = function (obj, userJQuery) { + userJQuery = userJQuery || $; + return ((!obj || obj.jquery) ? obj : userJQuery(obj)); + }; + + /** + * If obj is a jQuery, this function will return the first DOM element within it. Otherwise, the object will be returned unchanged. + * + * @param {jQuery} obj the jQuery instance to unwrap into a pure DOM element + */ + fluid.unwrap = function (obj) { + return obj && obj.jquery ? obj[0] : obj; + }; + + /** + * 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 true if an empty container is to be reported as a valid condition + * @return a single-element jQuery of container + */ + fluid.container = function (containerSpec, fallible, userJQuery) { + if (userJQuery) { + containerSpec = fluid.unwrap(containerSpec); + } + var container = fluid.wrap(containerSpec, userJQuery); + if (fallible && (!container || container.length === 0)) { + return null; + } + + 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) { + // don't put on a typename to avoid confusing primitive visitComponentChildren + var that = { + id: fluid.allocateGuid(), + cache: {} + }; + var userJQuery = container.constructor; + + function cacheKey(name, thisContainer) { + return fluid.allocateSimpleId(thisContainer) + "-" + name; + } + + function record(name, thisContainer, result) { + that.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 = userJQuery(selector.call(null, fluid.unwrap(thisContainer))); + } else { + togo = userJQuery(selector, thisContainer); + } + if (togo.get(0) === document) { + togo = []; + } + 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 = that.cache[key]; + return togo ? togo : that.locate(name, localContainer); + }; + that.clear = function () { + that.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, containerSpec, userOptions, localOptions) { + var container = fluid.container(containerSpec, true); + fluid.expectFilledSelector(container, "Error instantiating component with name \"" + componentName); + if (!container) { + return null; + } + // Need to ensure container is set early, without relying on an IoC mechanism - rethink this with asynchrony + var receiver = function (that) { + that.container = container; + }; + var that = fluid.initLittleComponent(componentName, userOptions, localOptions || {gradeNames: ["fluid.viewComponent"]}, receiver); + + if (!that.dom) { + fluid.initDomBinder(that); + } + // TODO: cannot afford a mutable container - put this into proper workflow + var userJQuery = that.options.jQuery; // Do it a second time to correct for jQuery injection + // if (userJQuery) { + // container = fluid.container(containerSpec, true, userJQuery); + // } + fluid.log("Constructing view component " + componentName + " with container " + container.constructor.expando + + (userJQuery ? " user jQuery " + userJQuery.expando : "") + " env: " + $.expando); + + 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, selectors) { + that.dom = fluid.createDomBinder(that.container, selectors || that.options.selectors || {}); + that.locate = that.dom.locate; + return that.dom; + }; + + // DOM Utilities. + + /** + * Finds the nearest ancestor of the element that matches a predicate + * @param {Element} element DOM element + * @param {Function} test A function (predicate) accepting a DOM element, returning a truthy value representing a match + * @return The first element parent for which the predicate returns truthy - or undefined if no parent matches + */ + fluid.findAncestor = function (element, test) { + element = fluid.unwrap(element); + while (element) { + if (test(element)) { + return element; + } + element = element.parentNode; + } + }; + + fluid.findForm = function (node) { + return fluid.findAncestor(node, function (element) { + return element.nodeName.toLowerCase() === "form"; + }); + }; + + /** A utility with the same signature as jQuery.text and jQuery.html, but without the API irregularity + * that treats a single argument of undefined as different to no arguments */ + // in jQuery 1.7.1, jQuery pulled the same dumb trick with $.text() that they did with $.val() previously, + // see comment in fluid.value below + fluid.each(["text", "html"], function (method) { + fluid[method] = function (node, newValue) { + node = $(node); + return newValue === undefined ? node[method]() : node[method](newValue); + }; + }); + + /** 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 = node.ownerDocument.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 ? + newValue.indexOf(this.value) !== -1 : newValue === this.value); + }); + } else { // this part jQuery will not do - extracting value from array + var checked = $.map(elements, function (element) { + return element.checked ? element.value : null; + }); + return node.type === "radio" ? checked[0] : checked; + } + }; + + + 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; + }; + + /** "Automatically" apply to whatever part of the data model is + * relevant, the changed value received at the given DOM node*/ + fluid.applyBoundChange = 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: "DOM:" + node.id}); + } else { + fluid.set(root.data, EL, newValue); + } + }; + + + /** + * 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) { + element = fluid.unwrap(element); + if (!element || fluid.isPrimitive(element)) { + return null; + } + + if (!element.id) { + var simpleId = "fluid-id-" + fluid.allocateGuid(); + element.id = simpleId; + } + return element.id; + }; + + fluid.defaults("fluid.ariaLabeller", { + gradeNames: ["fluid.viewComponent"], + labelAttribute: "aria-label", + liveRegionMarkup: "
", + liveRegionId: "fluid-ariaLabeller-liveRegion", + invokers: { + generateLiveElement: { + funcName: "fluid.ariaLabeller.generateLiveElement", + args: "{that}" + }, + update: { + funcName: "fluid.ariaLabeller.update", + args: ["{that}", "{arguments}.0"] + } + }, + listeners: { + onCreate: { + func: "{that}.update", + args: [null] + } + } + }); + + fluid.ariaLabeller.update = function (that, 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); + } + }; + + 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; + }; + + /** "Global Dismissal Handler" for the entire page. Attaches a click handler to the + * document root that will cause dismissal of any elements (typically dialogs) which + * have registered themselves. Dismissal through this route will automatically clean up + * the record - however, the dismisser themselves must take care to deregister in the case + * dismissal is triggered through the dialog interface itself. This component can also be + * automatically configured by fluid.deadMansBlur by means of the "cancelByDefault" option */ + + var dismissList = {}; + + $(document).click(function (event) { + var target = fluid.resolveEventTarget(event); + while (target) { + if (dismissList[target.id]) { + return; + } + target = target.parentNode; + } + fluid.each(dismissList, function (dismissFunc, key) { + dismissFunc(event); + delete dismissList[key]; + }); + }); + // TODO: extend a configurable equivalent of the above dealing with "focusin" events + + /** Accepts a free hash of nodes and an optional "dismissal function". + * If dismissFunc is set, this "arms" the dismissal system, such that when a click + * is received OUTSIDE any of the hierarchy covered by "nodes", the dismissal function + * will be executed. + */ + fluid.globalDismissal = function (nodes, dismissFunc) { + fluid.each(nodes, function (node) { + // Don't bother to use the real id if it is from a foreign document - we will never receive events + // from it directly in any case - and foreign documents may be under the control of malign fiends + // such as tinyMCE who allocate the same id to everything + var id = fluid.unwrap(node).ownerDocument === document? fluid.allocateSimpleId(node) : fluid.allocateGuid(); + if (dismissFunc) { + dismissList[id] = dismissFunc; + } + else { + delete dismissList[id]; + } + }); + }; + + /** Provides an abstraction for determing the current time. + * This is to provide a fix for FLUID-4762, where IE6 - IE8 + * do not support Date.now(). + */ + fluid.now = function () { + return Date.now ? Date.now() : (new Date()).getTime(); + }; + + + /** 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) { + // TODO: This should be rewritten as a proper component + var that = {options: $.extend(true, {}, fluid.defaults("fluid.deadMansBlur"), options)}; + that.blurPending = false; + that.lastCancel = 0; + that.canceller = function (event) { + fluid.log("Cancellation through " + event.type + " on " + fluid.dumpEl(event.target)); + that.lastCancel = fluid.now(); + that.blurPending = false; + }; + that.noteProceeded = function () { + fluid.globalDismissal(that.options.exclusions); + }; + that.reArm = function () { + fluid.globalDismissal(that.options.exclusions, that.proceed); + }; + that.addExclusion = function (exclusions) { + fluid.globalDismissal(exclusions, that.proceed); + }; + that.proceed = function (event) { + fluid.log("Direct proceed through " + event.type + " on " + fluid.dumpEl(event.target)); + that.blurPending = false; + that.options.handler(control); + }; + 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 + }); + }); + if (!that.options.cancelByDefault) { + $(control).bind("focusout", function (event) { + fluid.log("Starting blur timer for element " + fluid.dumpEl(event.target)); + var now = fluid.now(); + 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); + }); + } + else { + that.reArm(); + } + return that; + }; + + fluid.defaults("fluid.deadMansBlur", { + gradeNames: "fluid.function", + delay: 150, + backDelay: 100 + }); + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/FluidViewDebugging.js b/ppig-2015/example/lib/infusion/FluidViewDebugging.js new file mode 100644 index 0000000..a13c72a --- /dev/null +++ b/ppig-2015/example/lib/infusion/FluidViewDebugging.js @@ -0,0 +1,649 @@ +/* +Copyright 2015 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 +*/ + +/* global console */ + +var fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + fluid.registerNamespace("fluid.debug"); + + fluid.debug.toggleClass = function (styles, element, openStyle, closedStyle, state) { + if (openStyle) { + element.toggleClass(styles[openStyle], state); + } + if (closedStyle) { + element.toggleClass(styles[closedStyle], !state); + } + }; + + fluid.debug.bindToggleClick = function (element, applier, path) { + element.click(function () { + var state = fluid.get(applier.holder.model, path); + applier.change(path, !state); + }); + }; + + + fluid.defaults("fluid.debug.highlighter", { + gradeNames: ["fluid.viewComponent"], + selectors: { + highlightRoot: "#fluid-debug-highlightRoot" + }, + markup: { + highlightRoot: "
", + highlightElement: "
" + }, + events: { + highlightClick: null + }, + listeners: { + onCreate: "fluid.debug.highlighter.renderRoot" + }, + invokers: { + clear: "fluid.debug.highlighter.clear({that}.dom.highlightRoot)", + highlight: "fluid.debug.highlighter.highlight({that}, {that}.dom.highlightRoot, {arguments}.0)" // dispositions + } + }); + + fluid.debug.highlighter.renderRoot = function (that) { + var highlightRoot = $(that.options.markup.highlightRoot); + that.container.append(highlightRoot); + highlightRoot.click(that.events.highlightClick.fire); + }; + + fluid.debug.highlighter.clear = function (highlightRoot) { + highlightRoot.empty(); + }; + + fluid.debug.highlighter.positionProps = ["width","height","marginLeft","marginTop","paddingLeft","paddingTop"]; + + fluid.debug.highlighter.colours = { + components: [ + [0, 0, 0], // black + [255, 0, 0], // red + [255, 255, 0] // yellow + ], + domBinder: [0, 255, 0], // green + renderer: [0, 255, 255] // cyan + }; + + fluid.debug.arrayToRGBA = function (array) { + return "rgba(" + array.join(", ") + ")"; + }; + + fluid.debug.assignColour = function (colour, alpha) { + return [colour[0], colour[1], colour[2], alpha]; + }; + + fluid.debug.highlighter.indexToColour = function (i, isDomBind, isRenderer) { + var a = fluid.debug.assignColour, c = fluid.debug.highlighter.colours.components; + var base; + if (isRenderer) { + base = a(fluid.debug.highlighter.colours.renderer, 0.5); + } else if (isDomBind) { + base = a(fluid.debug.highlighter.colours.domBinder, 0.5); + } else { + base = a(c[i % c.length], i > c.length ? 0.2 : 0.5); + } + return base; + }; + + fluid.debug.isRendererSelector = function (component, selectorName) { + var isRendererComponent = fluid.componentHasGrade(component, "fluid.rendererComponent"); + var ignoreContains = fluid.contains(component.options.selectorsToIgnore, selectorName); + + return isRendererComponent ? (!selectorName || ignoreContains ? false : true) : false; + }; + + fluid.debug.highlighter.disposeEntries = function (entries, domIds) { + return fluid.transform(entries, function (entry, i) { + var component = entry.component; + var container = component.container; + var element = fluid.jById(domIds[i], container[0].ownerDocument); + var selectorName = entry.selectorName; + var isRendererSelector = fluid.debug.isRendererSelector(component, selectorName); + var noHighlight = container.is("body"); + return { + component: component, + container: element, + noHighlight: noHighlight, + selectorName: selectorName, + colour: fluid.debug.highlighter.indexToColour(i, selectorName, isRendererSelector) + }; + }); + }; + + fluid.debug.domIdtoHighlightId = function (domId) { + return "highlight-for:" + domId; + }; + + fluid.debug.highlighter.highlight = function (that, highlightRoot, dispositions) { + var p = fluid.debug.highlighter.positionProps; + for (var i = 0; i < dispositions.length; ++ i) { + var disp = dispositions[i]; + var container = disp.container; + if (disp.noHighlight) { + continue; + } + + var highlight = $(that.options.markup.highlightElement); + highlight.prop("id", fluid.debug.domIdtoHighlightId(container.prop("id"))); + highlightRoot.append(highlight); + + highlight.css("background-color", fluid.debug.arrayToRGBA(disp.colour)); + for (var j = 0; j < p.length; ++ j) { + highlight.css(p[j], container.css(p[j] || "")); + } + var offset = container.offset(); + var containerBody = container[0].ownerDocument.body; + if (containerBody !== document.body) { // TODO: This primitive algorithm will not account for nested iframes + offset.left -= $(containerBody).scrollLeft(); + offset.top -= $(containerBody).scrollTop(); + } + highlight.offset(offset); + + } + }; + + fluid.debug.ignorableGrades = ["fluid.debug.listeningView", "fluid.debug.listeningPanel", "fluid.debug.listeningRenderer"]; + + fluid.debug.frameworkGrades = fluid.frameworkGrades; + + fluid.debug.filterGrades = function (gradeNames) { + var highestFrameworkIndex = -1; + var output = []; + fluid.each(gradeNames, function (gradeName) { // TODO: remove fluid.indexOf + var findex = fluid.debug.frameworkGrades.indexOf(gradeName); + if (findex > highestFrameworkIndex) { + highestFrameworkIndex = findex; + } else if (findex === -1 && fluid.debug.ignorableGrades.indexOf(gradeName) === -1 && gradeName.indexOf("{") === -1) { + output.push(gradeName); + } + }); + output.push(fluid.debug.frameworkGrades[highestFrameworkIndex]); + return output; + }; + + fluid.debug.renderDefaults = function (typeName, options) { + var string = "fluid.defaults(\"" + typeName + "\", " + JSON.stringify(options, null, 4) + ");\n"; + var markup = "
" + string + "
"; + + return markup; + }; + + fluid.debug.renderSelectorUsageRecurse = function (source, segs, options) { + if (fluid.isPrimitive(source)) { + if (typeof(source) === "string" && source.indexOf(options.findString) !== -1) { + var path = segs.slice(0, 2); + fluid.set(options.target, path, fluid.copy(fluid.get(options.fullSource, path))); + } + } else { + fluid.each(source, function (value, key) { + segs.push(key); + fluid.debug.renderSelectorUsageRecurse(source[key], segs, options); + segs.pop(key); + }); + } + }; + + fluid.debug.renderSelectorUsage = function (selectorName, options) { + var target = {}, segs = [], findString = "}.dom." + selectorName; + fluid.debug.renderSelectorUsageRecurse(options, segs, { + findString: findString, + target: target, + fullSource: options + }); + var markup = "
" + JSON.stringify(target, null, 4) + "
"; + return markup; + }; + + fluid.debug.renderIndexElement = function (indexElTemplate, colour) { + return fluid.stringTemplate(indexElTemplate, {colour: fluid.debug.arrayToRGBA(colour)}); + }; + + fluid.debug.domIdtoRowId = function (domId) { + return "row-for:" + domId; + }; + + fluid.debug.rowForDomId = function (row, indexElTemplate, disp, rowIdToDomId) { + row.indexEl = fluid.debug.renderIndexElement(indexElTemplate, disp.colour); + row.domId = disp.container.prop("id"); + row.rowId = fluid.debug.domIdtoRowId(row.domId); + rowIdToDomId[row.rowId] = row.domId; + }; + + fluid.debug.renderOneDisposition = function (disp, indexElTemplate, defaultsIdToContent, rowIdToDomId) { + var applyTooltipClass = "flc-debug-tooltip-trigger"; + var rows; + if (disp.selectorName) { + var tooltipTriggerId = fluid.allocateGuid(); + var options = disp.component.options; + defaultsIdToContent[tooltipTriggerId] = fluid.debug.renderSelectorUsage(disp.selectorName, options); + rows = [{ + componentId: "", + extraTooltipClass: applyTooltipClass, + extraGradesClass: "fl-debug-selector-cell", + grade: options.selectors[disp.selectorName], + line: disp.selectorName, + tooltipTriggerId: tooltipTriggerId + }]; + } else { + var filtered = fluid.debug.filterGrades(disp.component.options.gradeNames); + rows = fluid.transform(filtered, function (oneGrade) { + var defaults = fluid.defaultsStore[oneGrade]; + var line = defaults && defaults.callerInfo ? defaults.callerInfo.filename + ":" + defaults.callerInfo.index : ""; + // horrible mixture of semantic levels in this rendering function - don't we need a new renderer! + var extraTooltipClass = ""; + var tooltipTriggerId = fluid.allocateGuid(); + if (line) { + extraTooltipClass = applyTooltipClass; + defaultsIdToContent[tooltipTriggerId] = fluid.debug.renderDefaults(oneGrade, defaults.options); + } + return { + rowId: fluid.allocateGuid(), + indexEl: "", + domId: "", + componentId: "", + grade: oneGrade, + line: line, + extraGradesClass: "", + extraTooltipClass: extraTooltipClass, + tooltipTriggerId: tooltipTriggerId + }; + }); + rows[0].componentId = disp.component.id; + } + fluid.debug.rowForDomId(rows[0], indexElTemplate, disp, rowIdToDomId); + return rows; + }; + + fluid.debug.renderInspecting = function (that, paneBody, indexElTemplate, paneRowTemplate, inspecting) { + if (!paneBody || !that.highlighter) { // stupid ginger world failure + return; + } + var defaultsIdToContent = {}; // driver for tooltips showing defaults source + paneBody.empty(); + that.highlighter.clear(); + + var ids = fluid.keys(inspecting).reverse(); // TODO: more principled ordering + var entries = fluid.transform(ids, function (inspectingId) { + return that.viewMapper.domIdToEntry[inspectingId]; + }); + var dispositions = fluid.debug.highlighter.disposeEntries(entries, ids); + var allRows = [], rowIdToDomId = {}; + fluid.each(dispositions, function (disp) { + var rows = fluid.debug.renderOneDisposition(disp, indexElTemplate, defaultsIdToContent, rowIdToDomId); + allRows = allRows.concat(rows); // yeah yeah we dun't have no mapcat + }); + + var contents = fluid.transform(allRows, function (row) { + return fluid.stringTemplate(paneRowTemplate, row); + }); + paneBody.html(contents.join("")); + that.highlighter.highlight(dispositions); + that.tooltips.applier.change("idToContent", defaultsIdToContent); + that.rowIdToDomId = rowIdToDomId; + that.dispositions = dispositions; // currently for looking up colour + var initSelection = fluid.arrayToHash(fluid.values(rowIdToDomId)); + that.applier.change("highlightSelected", initSelection); + }; + + + fluid.defaults("fluid.debug.browser", { + gradeNames: ["fluid.viewComponent"], + model: { + isOpen: false, + isInspecting: false, + isFrozen: false, + inspecting: {}, + highlightSelected: {} + }, + members: { + rowIdToDomId: {} + }, + modelListeners: { + isOpen: { + funcName: "fluid.debug.toggleClass", + args: ["{that}.options.styles", "{that}.dom.holder", "holderOpen", "holderClosed", "{change}.value"] + }, + isInspecting: [{ + funcName: "fluid.debug.toggleClass", + args: ["{that}.options.styles", "{that}.dom.inspectTrigger", "inspecting", null, "{change}.value"] + }, { + funcName: "fluid.debug.browser.finishInspecting", + args: ["{that}", "{change}.value"] + }], + inspecting: { + funcName: "fluid.debug.renderInspecting", + args: ["{that}", "{that}.dom.paneBody", "{that}.options.markup.indexElement", "{that}.options.markup.paneRow", "{change}.value"] + }, + "highlightSelected.*": { + funcName: "fluid.debug.renderHighlightSelection", + args: ["{that}", "{change}.value", "{change}.path"] + } + }, + styles: { + holderOpen: "fl-debug-holder-open", + holderClosed: "fl-debug-holder-closed", + inspecting: "fl-debug-inspect-active" + }, + markup: { + holder: "
", + pane: "
DOM IDComponent IDGrades / SelectorLine / Selector name
", + paneRow: "%indexEl%domId%componentId%grade%line", + indexElement: "
" + }, + selectors: { + openPaneTrigger: ".flc-debug-open-pane-trigger", + inspectTrigger: ".flc-debug-inspect-trigger", + holder: ".fl-debug-holder", + pane: ".fl-debug-pane", + paneBody: ".flc-debug-pane-body", + indexEl: ".flc-debug-pane-indexel", + row: ".flc-debug-pane-row" + }, + events: { + onNewDocument: null, + onMarkupReady: null, + highlightClick: null + }, + listeners: { + "onCreate.render": { + priority: "first", + funcName: "fluid.debug.browser.renderMarkup", + args: ["{that}", "{that}.options.markup.holder", "{that}.options.markup.pane"] + }, + "onCreate.toggleTabClick": { + funcName: "fluid.debug.bindToggleClick", + args: ["{that}.dom.openPaneTrigger", "{that}.applier", "isOpen"] + }, + "onCreate.toggleInspectClick": { + funcName: "fluid.debug.bindToggleClick", + args: ["{that}.dom.inspectTrigger", "{that}.applier", "isInspecting"] + }, + "onCreate.bindHighlightSelection": { + funcName: "fluid.debug.browser.bindHighlightSelection", + args: ["{that}", "{that}.dom.pane"] + }, + "onNewDocument.bindHover": { + funcName: "fluid.debug.browser.bindHover", + args: ["{that}", "{arguments}.0"] + }, + "onNewDocument.bindHighlightClick": { + funcName: "fluid.debug.browser.bindHighlightClick", + args: ["{that}", "{arguments}.0"] + }, + highlightClick: { + funcName: "fluid.debug.browser.highlightClick", + args: "{that}" + } + }, + components: { + tooltips: { + createOnEvent: "onMarkupReady", + type: "fluid.tooltip", + container: "{browser}.dom.pane", + options: { + items: ".flc-debug-tooltip-trigger", + styles: { + tooltip: "fl-debug-tooltip" + }, + position: { + my: "right centre", + at: "left centre" + }, + duration: 0, + delay: 0 + } + }, + viewMapper: { + type: "fluid.debug.viewMapper", + options: { + events: { + onNewDocument: "{fluid.debug.browser}.events.onNewDocument" + } + } + }, + highlighter: { + type: "fluid.debug.highlighter", + container: "{fluid.debug.browser}.container", + options: { + events: { + highlightClick: "{browser}.events.highlightClick" + } + } + } + } + }); + + fluid.debug.browser.finishInspecting = function (that, isInspecting) { + if (!isInspecting) { + var ation = that.applier.initiate(); + ation.change("inspecting", null, "DELETE"); // TODO - reform this terrible API through FLUID-5373 + ation.change("", { + "inspecting": {} + }); + ation.change("isFrozen", false); + ation.commit(); + } + }; + + // go into frozen state if we are not in it and are inspecting. + // if we are already frozen, finish inspecting (which will also finish frozen) + fluid.debug.browser.highlightClick = function (that) { + if (that.model.isFrozen) { + that.applier.change("isInspecting", false); + } else if (that.model.isInspecting) { + that.applier.change("isFrozen", true); + } + }; + + fluid.debug.browser.renderMarkup = function (that, holderMarkup, paneMarkup) { + that.container.append(holderMarkup); + var debugPane = that.locate("pane"); + debugPane.append(paneMarkup); + that.events.onMarkupReady.fire(); + }; + + fluid.debug.browser.domIdForElement = function (rowIdToDomId, rowSelector, element) { + var row = $(element).closest(rowSelector); + if (row.length > 0) { + var rowId = row[0].id; + return rowIdToDomId[rowId]; + } + }; + + fluid.debug.browser.bindHighlightSelection = function (that, pane) { + pane.on("click", that.options.selectors.indexEl, function (evt) { + var domId = fluid.debug.browser.domIdForElement(that.rowIdToDomId, that.options.selectors.row, evt.target); + var path = ["highlightSelected", domId]; + that.applier.change(path, !fluid.get(that.model, path)); + }); + }; + + fluid.debug.renderHighlightSelection = function (that, newState, path) { + var domId = path[1]; + var disposition = fluid.find_if(that.dispositions, function (disp) { + return disp.container.prop("id") === domId; + }); + if (disposition.noHighlight) { + return; + } + var outColour = fluid.copy(disposition.colour); + outColour[3] = outColour[3] * (newState ? 1.0 : 0.1); + var colourString = fluid.debug.arrayToRGBA(outColour); + var row = fluid.jById(fluid.debug.domIdtoRowId(domId)); + $(that.options.selectors.indexEl, row).css("background-color", colourString); + fluid.jById(fluid.debug.domIdtoHighlightId(domId)).css("background-color", colourString); + }; + + fluid.debug.browser.bindHighlightClick = function (that, dokkument) { + // We have a global problem in that we can't accept pointer events on the highlight elements + // themselves since this will cause their own mouseenter/mouseleave events to self-block. + dokkument.on("mousedown", "*", function (evt) { + var target = $(evt.target); + var holderParents = target.parents(that.options.selectors.holder); + if (holderParents.length > 0) { + return; + } + if (that.model.isInspecting) { + that.events.highlightClick.fire(); + return false; + } + }); + }; + + fluid.debug.browser.bindHover = function (that, dokkument) { + console.log("Binding to document " + dokkument.prop("id")); + var listener = function (event) { + if (!that.model.isInspecting || that.model.isFrozen) { + return; + } + var allParents = $(event.target).parents().addBack().get(); + for (var i = 0; i < allParents.length; ++ i) { + var id = allParents[i].id; + var entry = that.viewMapper.domIdToEntry[id]; + if (entry) { + if (event.type === "mouseleave") { + that.applier.change(["inspecting", id], null, "DELETE"); + } else if (event.type === "mouseenter") { + that.applier.change(["inspecting", id], true); + } + } + } + }; + dokkument.on("mouseenter mouseleave", "*", listener); + }; + + fluid.defaults("fluid.debug.listeningView", { + listeners: { + onCreate: { + funcName: "fluid.debug.viewMapper.registerView", + args: ["{fluid.debug.viewMapper}", "{that}", "add"] + }, + onDestroy: { + funcName: "fluid.debug.viewMapper.registerView", + args: ["{fluid.debug.viewMapper}", "{that}", "remove"] + } + } + }); + + fluid.defaults("fluid.debug.listeningPanel", { + listeners: { + onDomBind: { + funcName: "fluid.debug.viewMapper.registerView", + args: ["{fluid.debug.viewMapper}", "{that}", "rebind"] + } + } + }); + + fluid.defaults("fluid.debug.listeningRenderer", { + listeners: { + afterRender: { + funcName: "fluid.debug.viewMapper.registerView", + args: ["{fluid.debug.viewMapper}", "{that}", "rebind"] + } + } + }); + + fluid.defaults("fluid.debug.viewMapper", { + gradeNames: ["fluid.component", "fluid.resolveRoot"], + members: { + seenDocuments: {}, + idToEntry: {}, + domIdToEntry: {} + }, + distributeOptions: [{ + record: "fluid.debug.listeningView", + target: "{/ fluid.viewComponent}.options.gradeNames" + }, { + record: "fluid.debug.listeningPanel", + target: "{/ fluid.prefs.panel}.options.gradeNames" + }, { + record: "fluid.debug.listeningRenderer", + target: "{/ fluid.rendererComponent}.options.gradeNames" + }], + events: { + onNewDocument: null + }, + listeners: { + onCreate: { + funcName: "fluid.debug.viewMapper.scanInit" + } + } + }); + + fluid.debug.viewMapper.registerComponent = function (that, component, containerId) { + var domBound = fluid.transform(component.options.selectors, function (selector, selectorName) { + return fluid.allocateSimpleId(component.locate(selectorName)); + }); + var entry = { + component: component, + containerId: containerId, + domBound: domBound + }; + that.idToEntry[component.id] = entry; + if (containerId) { + that.domIdToEntry[containerId] = entry; + + fluid.each(domBound, function (subId, selectorName) { + var subEntry = $.extend({}, entry); + subEntry.selectorName = selectorName; + that.domIdToEntry[subId] = subEntry; + }); + } + }; + + fluid.debug.viewMapper.deregisterComponent = function (that, id) { + var entry = that.idToEntry[id]; + delete that.idToEntry[id]; + delete that.domIdToEntry[entry.containerId]; + fluid.each(entry.domBound, function (subId) { + delete that.domIdToEntry[subId]; + }); + }; + + fluid.debug.viewMapper.registerView = function (that, component, action) { + var id = component.id; + var containerId = fluid.allocateSimpleId(component.container); + if (containerId) { + var dokkument = $(component.container[0].ownerDocument); + var dokkumentId = fluid.allocateSimpleId(dokkument); + if (!that.seenDocuments[dokkumentId]) { + that.seenDocuments[dokkumentId] = true; + that.events.onNewDocument.fire(dokkument); + } + } + if (action === "add") { + fluid.debug.viewMapper.registerComponent(that, component, containerId); + } else if (action === "remove") { + fluid.debug.viewMapper.deregisterComponent(that, id); + } else if (action === "rebind") { + fluid.debug.viewMapper.deregisterComponent(that, id); + fluid.debug.viewMapper.registerComponent(that, component, containerId); + } + }; + + fluid.debug.viewMapper.scanInit = function (that) { + var views = fluid.queryIoCSelector(fluid.rootComponent, "fluid.viewComponent"); + for (var i = 0; i < views.length; ++ i) { + fluid.debug.viewMapper.registerView(that, views[i], true); + } + }; + + $(document).ready(function () { + fluid.debug.browser("body"); + }); + +})(jQuery, fluid_2_0); \ No newline at end of file diff --git a/ppig-2015/example/lib/infusion/JavaProperties.js b/ppig-2015/example/lib/infusion/JavaProperties.js new file mode 100644 index 0000000..067497f --- /dev/null +++ b/ppig-2015/example/lib/infusion/JavaProperties.js @@ -0,0 +1,117 @@ +/* +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 +*/ + +fluid_2_0 = fluid_2_0 || {}; + +(function ($, fluid) { + "use strict"; + + 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"); + var 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_2_0); diff --git a/ppig-2015/example/lib/infusion/ModelTransformation.js b/ppig-2015/example/lib/infusion/ModelTransformation.js new file mode 100644 index 0000000..a5a8045 --- /dev/null +++ b/ppig-2015/example/lib/infusion/ModelTransformation.js @@ -0,0 +1,665 @@ +/* +Copyright 2010 University of Toronto +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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; +var fluid = fluid || fluid_2_0; + +(function ($, fluid) { + "use strict"; + + fluid.registerNamespace("fluid.model.transform"); + + /** Grade definitions for standard transformation function hierarchy **/ + + fluid.defaults("fluid.transformFunction", { + gradeNames: "fluid.function" + }); + + // uses standard layout and workflow involving inputPath + fluid.defaults("fluid.standardInputTransformFunction", { + gradeNames: "fluid.transformFunction" + }); + + fluid.defaults("fluid.standardOutputTransformFunction", { + gradeNames: "fluid.transformFunction" + }); + + fluid.defaults("fluid.multiInputTransformFunction", { + gradeNames: "fluid.transformFunction" + }); + + // uses the standard layout and workflow involving inputPath and outputPath + fluid.defaults("fluid.standardTransformFunction", { + gradeNames: ["fluid.standardInputTransformFunction", "fluid.standardOutputTransformFunction"] + }); + + fluid.defaults("fluid.lens", { + gradeNames: "fluid.transformFunction", + invertConfiguration: null + // this function method returns "inverted configuration" rather than actually performing inversion + // TODO: harmonise with strategy used in VideoPlayer_framework.js + }); + + /*********************************** + * Base utilities for transformers * + ***********************************/ + + // unsupported, NON-API function + fluid.model.transform.pathToRule = function (inputPath) { + return { + transform: { + type: "fluid.transforms.value", + inputPath: inputPath + } + }; + }; + + // unsupported, NON-API function + fluid.model.transform.literalValueToRule = function (value) { + return { + transform: { + type: "fluid.transforms.literalValue", + value: value + } + }; + }; + + /** Accepts two fully escaped paths, either of which may be empty or null **/ + fluid.model.composePaths = function (prefix, suffix) { + prefix = prefix === 0 ? "0" : prefix || ""; + suffix = suffix === 0 ? "0" : suffix || ""; + return !prefix ? suffix : (!suffix ? prefix : prefix + "." + suffix); + }; + + fluid.model.transform.accumulateInputPath = function (inputPath, transform, paths) { + if (inputPath !== undefined) { + paths.push(fluid.model.composePaths(transform.inputPrefix, inputPath)); + } + }; + + fluid.model.transform.accumulateStandardInputPath = function (input, transformSpec, transform, paths) { + fluid.model.transform.getValue(undefined, transformSpec[input], transform); + fluid.model.transform.accumulateInputPath(transformSpec[input + "Path"], transform, paths); + }; + + fluid.model.transform.accumulateMultiInputPaths = function (inputVariables, transformSpec, transform, paths) { + fluid.each(inputVariables, function (v, k) { + fluid.model.transform.accumulateStandardInputPath(k, transformSpec, transform, paths); + }); + }; + + fluid.model.transform.getValue = function (inputPath, value, transform) { + var togo; + if (inputPath !== undefined) { // NB: We may one day want to reverse the crazy jQuery-like convention that "no path means root path" + togo = fluid.get(transform.source, fluid.model.composePaths(transform.inputPrefix, inputPath), transform.resolverGetConfig); + } + if (togo === undefined) { + togo = fluid.isPrimitive(value) ? value : transform.expand(value); + } + return togo; + }; + + // distinguished value which indicates that a transformation rule supplied a + // non-default output path, and so the user should be prevented from making use of it + // in a compound transform definition + fluid.model.transform.NONDEFAULT_OUTPUT_PATH_RETURN = {}; + + fluid.model.transform.setValue = function (userOutputPath, value, transform) { + // avoid crosslinking to input object - this might be controlled by a "nocopy" option in future + var toset = fluid.copy(value); + var outputPath = fluid.model.composePaths(transform.outputPrefix, userOutputPath); + // TODO: custom resolver config here to create non-hash output model structure + if (toset !== undefined) { + transform.applier.requestChange(outputPath, toset); + } + return userOutputPath ? fluid.model.transform.NONDEFAULT_OUTPUT_PATH_RETURN : toset; + }; + + /* Resolves the given as parameter by looking up the path Path in the object + * to be transformed. If not present, it resolves the by using the literal value if primitive, + * or expanding otherwise. defines the default value if unableto resolve the key. If no + * default value is given undefined is returned + */ + fluid.model.transform.resolveParam = function (transformSpec, transform, key, def) { + var val = fluid.model.transform.getValue(transformSpec[key + "Path"], transformSpec[key], transform); + return (val !== undefined) ? val : def; + }; + + // Compute a "match score" between two pieces of model material, with 0 indicating a complete mismatch, and + // higher values indicating increasingly good matches + fluid.model.transform.matchValue = function (expected, actual, partialMatches) { + var stats = {changes: 0, unchanged: 0, changeMap: {}}; + fluid.model.diff(expected, actual, stats); + // i) a pair with 0 matches counts for 0 in all cases + // ii) without "partial match mode" (the default), we simply count matches, with any mismatch giving 0 + // iii) with "partial match mode", a "perfect score" in the top 24 bits is + // penalised for each mismatch, with a positive score of matches store in the bottom 24 bits + return stats.unchanged === 0 ? 0 + : (partialMatches ? 0xffffff000000 - 0x1000000 * stats.changes + stats.unchanged : + (stats.changes ? 0 : 0xffffff000000 + stats.unchanged)); + }; + + fluid.firstDefined = function (a, b) { + return a === undefined ? b : a; + }; + + + // TODO: prefixApplier is a transform which is currently unused and untested + fluid.model.transform.prefixApplier = function (transformSpec, transform) { + if (transformSpec.inputPrefix) { + transform.inputPrefixOp.push(transformSpec.inputPrefix); + } + if (transformSpec.outputPrefix) { + transform.outputPrefixOp.push(transformSpec.outputPrefix); + } + transform.expand(transformSpec.value); + if (transformSpec.inputPrefix) { + transform.inputPrefixOp.pop(); + } + if (transformSpec.outputPrefix) { + transform.outputPrefixOp.pop(); + } + }; + + fluid.defaults("fluid.model.transform.prefixApplier", { + gradeNames: ["fluid.transformFunction"] + }); + + // unsupported, NON-API function + fluid.model.makePathStack = function (transform, prefixName) { + var stack = transform[prefixName + "Stack"] = []; + transform[prefixName] = ""; + return { + push: function (prefix) { + var newPath = fluid.model.composePaths(transform[prefixName], prefix); + stack.push(transform[prefixName]); + transform[prefixName] = newPath; + }, + pop: function () { + transform[prefixName] = stack.pop(); + } + }; + }; + + fluid.model.transform.aliasStandardInput = function (transformSpec) { + return { // alias input and value, and their paths + value: transformSpec.value === undefined ? transformSpec.input : transformSpec.value, + valuePath: transformSpec.valuePath === undefined ? transformSpec.inputPath : transformSpec.valuePath + }; + }; + + // unsupported, NON-API function + fluid.model.transform.doTransform = function (transformSpec, transform, transformOpts) { + var expdef = transformOpts.defaults; + var transformFn = fluid.getGlobalValue(transformOpts.typeName); + if (typeof(transformFn) !== "function") { + fluid.fail("Transformation record specifies transformation function with name " + + transformSpec.type + " which is not a function - ", transformFn); + } + if (!fluid.hasGrade(expdef, "fluid.transformFunction")) { + // If no suitable grade is set up, assume that it is intended to be used as a standardTransformFunction + expdef = fluid.defaults("fluid.standardTransformFunction"); + } + var transformArgs = [transformSpec, transform]; + if (fluid.hasGrade(expdef, "fluid.standardInputTransformFunction")) { + var valueHolder = fluid.model.transform.aliasStandardInput(transformSpec); + var expanded = fluid.model.transform.getValue(valueHolder.valuePath, valueHolder.value, transform); + + transformArgs.unshift(expanded); + // if the function has no input, the result is considered undefined, and this is returned + if (expanded === undefined) { + return undefined; + } + } else if (fluid.hasGrade(expdef, "fluid.multiInputTransformFunction")) { + var inputs = {}; + fluid.each(expdef.inputVariables, function (v, k) { + inputs[k] = function () { + var input = fluid.model.transform.getValue(transformSpec[k + "Path"], transformSpec[k], transform); + // if no match, assign default if one exists (v != null) + input = (input === undefined && v !== null) ? v : input; + return input; + }; + }); + transformArgs.unshift(inputs); + } + var transformed = transformFn.apply(null, transformArgs); + if (fluid.hasGrade(expdef, "fluid.standardOutputTransformFunction")) { + // "doOutput" flag is currently set nowhere, but could be used in future + var outputPath = transformSpec.outputPath !== undefined ? transformSpec.outputPath : (transformOpts.doOutput ? "" : undefined); + if (outputPath !== undefined && transformed !== undefined) { + //If outputPath is given in the expander we want to: + // (1) output to the document + // (2) return undefined, to ensure that expanders higher up in the hierarchy doesn't attempt to output it again + fluid.model.transform.setValue(transformSpec.outputPath, transformed, transform); + transformed = undefined; + } + } + return transformed; + }; + + // OLD PATHUTIL utilities: Rescued from old DataBinding implementation to support obsolete "schema" scheme for transforms - all of this needs to be rethought + var globalAccept = []; + + fluid.registerNamespace("fluid.pathUtil"); + + /** Parses a path segment, following escaping rules, starting from character index i in the supplied path */ + fluid.pathUtil.getPathSegment = function (path, i) { + fluid.pathUtil.getPathSegmentImpl(globalAccept, path, i); + return globalAccept[0]; + }; + /** Returns just the head segment of an EL path */ + fluid.pathUtil.getHeadPath = function (path) { + return fluid.pathUtil.getPathSegment(path, 0); + }; + + /** Returns all of an EL path minus its first segment - if the path consists of just one segment, returns "" */ + fluid.pathUtil.getFromHeadPath = function (path) { + var firstdot = fluid.pathUtil.getPathSegmentImpl(null, path, 0); + return firstdot === path.length ? "" : path.substring(firstdot + 1); + }; + /** Determines whether a particular EL path matches a given path specification. + * The specification consists of a path with optional wildcard segments represented by "*". + * @param spec (string) The specification to be matched + * @param path (string) The path to be tested + * @param exact (boolean) Whether the path must exactly match the length of the specification in + * terms of path segments in order to count as match. If exact is falsy, short specifications will + * match all longer paths as if they were padded out with "*" segments + * @return (array of string) The path segments which matched the specification, or null if there was no match + */ + + fluid.pathUtil.matchPath = function (spec, path, exact) { + var togo = []; + while (true) { + if (((path === "") ^ (spec === "")) && exact) { + return null; + } + // FLUID-4625 - symmetry on spec and path is actually undesirable, but this + // quickly avoids at least missed notifications - improved (but slower) + // implementation should explode composite changes + if (!spec || !path) { + break; + } + 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.push(pathhead); + spec = fluid.pathUtil.getFromHeadPath(spec); + path = fluid.pathUtil.getFromHeadPath(path); + } + return togo; + }; + + // unsupported, NON-API function + fluid.model.transform.expandWildcards = function (transform, source) { + fluid.each(source, function (value, key) { + var q = transform.queuedTransforms; + transform.pathOp.push(fluid.pathUtil.escapeSegment(key.toString())); + for (var i = 0; i < q.length; ++i) { + if (fluid.pathUtil.matchPath(q[i].matchPath, transform.path, true)) { + var esCopy = fluid.copy(q[i].transformSpec); + if (esCopy.inputPath === undefined || fluid.model.transform.hasWildcard(esCopy.inputPath)) { + esCopy.inputPath = ""; + } + // TODO: allow some kind of interpolation for output path + // TODO: Also, we now require outputPath to be specified in these cases for output to be produced as well.. Is that something we want to continue with? + transform.inputPrefixOp.push(transform.path); + transform.outputPrefixOp.push(transform.path); + var transformOpts = fluid.model.transform.lookupType(esCopy.type); + var result = fluid.model.transform.doTransform(esCopy, transform, transformOpts); + if (result !== undefined) { + fluid.model.transform.setValue(null, result, transform); + } + transform.outputPrefixOp.pop(); + transform.inputPrefixOp.pop(); + } + } + if (!fluid.isPrimitive(value)) { + fluid.model.transform.expandWildcards(transform, value); + } + transform.pathOp.pop(); + }); + }; + + // unsupported, NON-API function + fluid.model.transform.hasWildcard = function (path) { + return typeof(path) === "string" && path.indexOf("*") !== -1; + }; + + // unsupported, NON-API function + fluid.model.transform.maybePushWildcard = function (transformSpec, transform) { + var hw = fluid.model.transform.hasWildcard; + var matchPath; + if (hw(transformSpec.inputPath)) { + matchPath = fluid.model.composePaths(transform.inputPrefix, transformSpec.inputPath); + } + else if (hw(transform.outputPrefix) || hw(transformSpec.outputPath)) { + matchPath = fluid.model.composePaths(transform.outputPrefix, transformSpec.outputPath); + } + + if (matchPath) { + transform.queuedTransforms.push({transformSpec: transformSpec, outputPrefix: transform.outputPrefix, inputPrefix: transform.inputPrefix, matchPath: matchPath}); + return true; + } + return false; + }; + + fluid.model.sortByKeyLength = function (inObject) { + var keys = fluid.keys(inObject); + return keys.sort(fluid.compareStringLength(true)); + }; + + // Three handler functions operating the (currently) three different processing modes + // unsupported, NON-API function + fluid.model.transform.handleTransformStrategy = function (transformSpec, transform, transformOpts) { + if (fluid.model.transform.maybePushWildcard(transformSpec, transform)) { + return; + } + else { + return fluid.model.transform.doTransform(transformSpec, transform, transformOpts); + } + }; + // unsupported, NON-API function + fluid.model.transform.handleInvertStrategy = function (transformSpec, transform, transformOpts) { + var invertor = transformOpts.defaults && transformOpts.defaults.invertConfiguration; + if (invertor) { + var inverted = fluid.invokeGlobalFunction(invertor, [transformSpec, transform]); + transform.inverted.push(inverted); + } + }; + + // unsupported, NON-API function + fluid.model.transform.handleCollectStrategy = function (transformSpec, transform, transformOpts) { + var defaults = transformOpts.defaults; + var standardInput = fluid.hasGrade(defaults, "fluid.standardInputTransformFunction"); + var multiInput = fluid.hasGrade(defaults, "fluid.multiInputTransformFunction"); + + if (standardInput) { + fluid.model.transform.accumulateStandardInputPath("input", transformSpec, transform, transform.inputPaths); + } else if (multiInput) { + fluid.model.transform.accumulateMultiInputPaths(defaults.inputVariables, transformSpec, transform, transform.inputPaths); + } else { + var collector = defaults.collectInputPaths; + if (collector) { + var collected = fluid.makeArray(fluid.invokeGlobalFunction(collector, [transformSpec, transform])); + transform.inputPaths = transform.inputPaths.concat(collected); + } + } + }; + + fluid.model.transform.lookupType = function (typeName, transformSpec) { + if (!typeName) { + fluid.fail("Transformation record is missing a type name: ", transformSpec); + } + if (typeName.indexOf(".") === -1) { + typeName = "fluid.transforms." + typeName; + } + var defaults = fluid.defaults(typeName); + return { defaults: defaults, typeName: typeName}; + }; + + // A utility which is helpful in computing inverses involving compound values. + // For example, with the valueMapper, compound input values are accepted as literals implicitly, + // whereas as output values they must be escaped. This utility escapes a value if it is not primitive. + fluid.model.transform.literaliseValue = function (value) { + return fluid.isPrimitive(value) ? value : { + literalValue: value + }; + }; + + // unsupported, NON-API function + fluid.model.transform.processRule = function (rule, transform) { + if (typeof(rule) === "string") { + rule = fluid.model.transform.pathToRule(rule); + } + // special dispensation to allow "literalValue" to escape any value + else if (rule.literalValue !== undefined) { + rule = fluid.model.transform.literalValueToRule(rule.literalValue); + } + var togo; + if (rule.transform) { + var transformSpec, transformOpts; + + if (fluid.isArrayable(rule.transform)) { + // if the transform holds an array, each transformer within that is responsible for its own output + var transforms = rule.transform; + togo = undefined; + for (var i = 0; i < transforms.length; ++i) { + transformSpec = transforms[i]; + transformOpts = fluid.model.transform.lookupType(transformSpec.type); + transform.transformHandler(transformSpec, transform, transformOpts); + } + } else { + // else we just have a normal single transform which will return 'undefined' as a flag to defeat cascading output + transformSpec = rule.transform; + transformOpts = fluid.model.transform.lookupType(transformSpec.type); + togo = transform.transformHandler(transformSpec, transform, transformOpts); + } + } + // if rule is an array, save path for later use in schema strategy on final applier (so output will be interpreted as array) + if (fluid.isArrayable(rule)) { + transform.collectedFlatSchemaOpts = transform.collectedFlatSchemaOpts || {}; + transform.collectedFlatSchemaOpts[transform.outputPrefix] = "array"; + } + fluid.each(rule, function (value, key) { + if (key !== "transform") { + transform.outputPrefixOp.push(key); + var togo = transform.expand(value, transform); + // Value expanders and arrays as rules implicitly outputs, unless they have nothing (undefined) to output + if (togo !== undefined) { + fluid.model.transform.setValue(null, togo, transform); + // ensure that expanders further up does not try to output this value as well. + togo = undefined; + } + transform.outputPrefixOp.pop(); + } + }); + return togo; + }; + + // unsupported, NON-API function + // 3rd arg is disused by the framework and always defaults to fluid.model.transform.processRule + fluid.model.transform.makeStrategy = function (transform, handleFn, transformFn) { + transformFn = transformFn || fluid.model.transform.processRule; + transform.expand = function (rules) { + return transformFn(rules, transform); + }; + transform.outputPrefixOp = fluid.model.makePathStack(transform, "outputPrefix"); + transform.inputPrefixOp = fluid.model.makePathStack(transform, "inputPrefix"); + transform.transformHandler = handleFn; + }; + + fluid.model.transform.invertConfiguration = function (rules) { + var transform = { + inverted: [] + }; + fluid.model.transform.makeStrategy(transform, fluid.model.transform.handleInvertStrategy); + transform.expand(rules); + return { + transform: transform.inverted + }; + }; + + fluid.model.transform.collectInputPaths = function (rules) { + var transform = { + inputPaths: [] + }; + fluid.model.transform.makeStrategy(transform, fluid.model.transform.handleCollectStrategy); + transform.expand(rules); + return transform.inputPaths; + }; + + // unsupported, NON-API function + fluid.model.transform.flatSchemaStrategy = function (flatSchema, getConfig) { + var keys = fluid.model.sortByKeyLength(flatSchema); + return function (root, segment, index, segs) { + var path = getConfig.parser.compose.apply(null, segs.slice(0, index)); + // TODO: clearly this implementation could be much more efficient + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + if (fluid.pathUtil.matchPath(key, path, true) !== null) { + return flatSchema[key]; + } + } + }; + }; + + // unsupported, NON-API function + fluid.model.transform.defaultSchemaValue = function (schemaValue) { + var type = fluid.isPrimitive(schemaValue) ? schemaValue : schemaValue.type; + return type === "array" ? [] : {}; + }; + + // unsupported, NON-API function + fluid.model.transform.isomorphicSchemaStrategy = function (source, getConfig) { + return function (root, segment, index, segs) { + var existing = fluid.get(source, segs.slice(0, index), getConfig); + return fluid.isArrayable(existing) ? "array" : "object"; + }; + }; + + // unsupported, NON-API function + fluid.model.transform.decodeStrategy = function (source, options, getConfig) { + if (options.isomorphic) { + return fluid.model.transform.isomorphicSchemaStrategy(source, getConfig); + } + else if (options.flatSchema) { + return fluid.model.transform.flatSchemaStrategy(options.flatSchema, getConfig); + } + }; + + // unsupported, NON-API function + fluid.model.transform.schemaToCreatorStrategy = function (strategy) { + return function (root, segment, index, segs) { + if (root[segment] === undefined) { + var schemaValue = strategy(root, segment, index, segs); + root[segment] = fluid.model.transform.defaultSchemaValue(schemaValue); + return root[segment]; + } + }; + }; + + /** Transforms a model by a sequence of rules. Parameters as for fluid.model.transform, + * only with an array accepted for "rules" + */ + fluid.model.transform.sequence = function (source, rules, options) { + for (var i = 0; i < rules.length; ++i) { + source = fluid.model.transform(source, rules[i], options); + } + return source; + }; + + fluid.model.compareByPathLength = function (changea, changeb) { + var pdiff = changea.path.length - changeb.path.length; + return pdiff === 0 ? changea.sequence - changeb.sequence : pdiff; + }; + + /** Fires an accumulated set of change requests in increasing order of target pathlength + */ + fluid.model.fireSortedChanges = function (changes, applier) { + changes.sort(fluid.model.compareByPathLength); + fluid.requestChanges(applier, changes); + }; + + /** + * Transforms a model based on a specified expansion rules objects. + * Rules objects take the form of: + * { + * "target.path": "value.el.path" || { + * transform: { + * type: "transform.function.path", + * ... + * } + * } + * } + * + * @param {Object} source the model to transform + * @param {Object} rules a rules object containing instructions on how to transform the model + * @param {Object} options a set of rules governing the transformations. At present this may contain + * the values isomorphic: true indicating that the output model is to be governed by the + * same schema found in the input model, or flatSchema holding a flat schema object which + * consists of a hash of EL path specifications with wildcards, to the values "array"/"object" defining + * the schema to be used to construct missing trunk values. + */ + fluid.model.transformWithRules = function (source, rules, options) { + options = options || {}; + + var getConfig = fluid.model.escapedGetConfig; + + var schemaStrategy = fluid.model.transform.decodeStrategy(source, options, getConfig); + + var transform = { + source: source, + target: { + model: schemaStrategy ? fluid.model.transform.defaultSchemaValue(schemaStrategy(null, "", 0, [""])) : {} + }, + resolverGetConfig: getConfig, + collectedFlatSchemaOpts: undefined, // to hold options for flat schema collected during transforms + queuedChanges: [], + queuedTransforms: [] // TODO: This is used only by wildcard applier - explain its operation + }; + fluid.model.transform.makeStrategy(transform, fluid.model.transform.handleTransformStrategy); + transform.applier = { + fireChangeRequest: function (changeRequest) { + changeRequest.sequence = transform.queuedChanges.length; + transform.queuedChanges.push(changeRequest); + } + }; + fluid.bindRequestChange(transform.applier); + + transform.expand(rules); + + var setConfig = fluid.copy(fluid.model.escapedSetConfig); + // Modify schemaStrategy if we collected flat schema options for the setConfig of finalApplier + if (transform.collectedFlatSchemaOpts !== undefined) { + $.extend(transform.collectedFlatSchemaOpts, options.flatSchema); + schemaStrategy = fluid.model.transform.flatSchemaStrategy(transform.collectedFlatSchemaOpts, getConfig); + } + setConfig.strategies = [fluid.model.defaultFetchStrategy, schemaStrategy ? fluid.model.transform.schemaToCreatorStrategy(schemaStrategy) + : fluid.model.defaultCreatorStrategy]; + transform.finalApplier = options.finalApplier || fluid.makeHolderChangeApplier(transform.target, {resolverSetConfig: setConfig}); + + if (transform.queuedTransforms.length > 0) { + transform.typeStack = []; + transform.pathOp = fluid.model.makePathStack(transform, "path"); + fluid.model.transform.expandWildcards(transform, source); + } + fluid.model.fireSortedChanges(transform.queuedChanges, transform.finalApplier); + return transform.target.model; + }; + + $.extend(fluid.model.transformWithRules, fluid.model.transform); + fluid.model.transform = fluid.model.transformWithRules; + + /** Utility function to produce a standard options transformation record for a single set of rules **/ + fluid.transformOne = function (rules) { + return { + transformOptions: { + transformer: "fluid.model.transformWithRules", + config: rules + } + }; + }; + + /** Utility function to produce a standard options transformation record for multiple rules to be applied in sequence **/ + fluid.transformMany = function (rules) { + return { + transformOptions: { + transformer: "fluid.model.transform.sequence", + config: rules + } + }; + }; + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/ModelTransformationTransforms.js b/ppig-2015/example/lib/infusion/ModelTransformationTransforms.js new file mode 100644 index 0000000..c375971 --- /dev/null +++ b/ppig-2015/example/lib/infusion/ModelTransformationTransforms.js @@ -0,0 +1,676 @@ +/* +Copyright 2010 University of Toronto +Copyright 2010-2011 OCAD University +Copyright 2013 Raising the Floor - International + +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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; +var fluid = fluid || fluid_2_0; + +(function ($, fluid) { + "use strict"; + + fluid.registerNamespace("fluid.model.transform"); + fluid.registerNamespace("fluid.transforms"); + + /********************************** + * Standard transformer functions * + **********************************/ + + fluid.defaults("fluid.transforms.value", { + gradeNames: "fluid.standardTransformFunction", + invertConfiguration: "fluid.transforms.value.invert" + }); + + fluid.transforms.value = fluid.identity; + + fluid.transforms.value.invert = function (transformSpec, transformer) { + var togo = fluid.copy(transformSpec); + // TODO: this will not behave correctly in the face of compound "value" which contains + // further transforms + togo.inputPath = fluid.model.composePaths(transformer.outputPrefix, transformSpec.outputPath); + togo.outputPath = fluid.model.composePaths(transformer.inputPrefix, transformSpec.inputPath); + return togo; + }; + + // Export the use of the "value" transform under the "identity" name for FLUID-5293 + fluid.transforms.identity = fluid.transforms.value; + fluid.defaults("fluid.transforms.identity", { + gradeNames: "fluid.transforms.value" + }); + + fluid.defaults("fluid.transforms.literalValue", { + gradeNames: "fluid.standardOutputTransformFunction" + }); + + fluid.transforms.literalValue = function (transformSpec) { + return transformSpec.value; + }; + + + fluid.defaults("fluid.transforms.arrayValue", { + gradeNames: "fluid.standardTransformFunction" + }); + + fluid.transforms.arrayValue = fluid.makeArray; + + + fluid.defaults("fluid.transforms.stringToNumber", { + gradeNames: ["fluid.standardTransformFunction"] + }); + + fluid.transforms.stringToNumber = function (value) { + var newValue = Number(value); + return isNaN(newValue) ? undefined : newValue; + }; + + fluid.defaults("fluid.transforms.count", { + gradeNames: "fluid.standardTransformFunction" + }); + + fluid.transforms.count = function (value) { + return fluid.makeArray(value).length; + }; + + + fluid.defaults("fluid.transforms.round", { + gradeNames: "fluid.standardTransformFunction" + }); + + fluid.transforms.round = function (value) { + return Math.round(value); + }; + + + fluid.defaults("fluid.transforms.delete", { + gradeNames: "fluid.transformFunction" + }); + + fluid.transforms["delete"] = function (transformSpec, transformer) { + var outputPath = fluid.model.composePaths(transformer.outputPrefix, transformSpec.outputPath); + transformer.applier.requestChange(outputPath, null, "DELETE"); + }; + + + fluid.defaults("fluid.transforms.firstValue", { + gradeNames: "fluid.transformFunction" + }); + + fluid.transforms.firstValue = function (transformSpec, transformer) { + if (!transformSpec.values || !transformSpec.values.length) { + fluid.fail("firstValue transformer requires an array of values at path named \"values\", supplied", transformSpec); + } + for (var i = 0; i < transformSpec.values.length; i++) { + var value = transformSpec.values[i]; + // TODO: problem here - all of these transforms will have their side-effects (setValue) even if only one is chosen + var expanded = transformer.expand(value); + if (expanded !== undefined) { + return expanded; + } + } + }; + + fluid.defaults("fluid.transforms.linearScale", { + gradeNames: [ "fluid.multiInputTransformFunction", "fluid.standardOutputTransformFunction", "fluid.lens" ], + invertConfiguration: "fluid.transforms.linearScale.invert", + inputVariables: { + value: null, + factor: 1, + offset: 0 + } + }); + + /* simple linear transformation */ + fluid.transforms.linearScale = function (inputs) { + var value = inputs.value(); + var factor = inputs.factor(); + var offset = inputs.offset(); + + if (typeof(value) !== "number" || typeof(factor) !== "number" || typeof(offset) !== "number") { + return undefined; + } + return value * factor + offset; + }; + + /* TODO: This inversion doesn't work if the value and factors are given as paths in the source model */ + fluid.transforms.linearScale.invert = function (transformSpec, transformer) { + var togo = fluid.copy(transformSpec); + + if (togo.factor) { + togo.factor = (togo.factor === 0) ? 0 : 1 / togo.factor; + } + if (togo.offset) { + togo.offset = - togo.offset * (togo.factor !== undefined ? togo.factor : 1); + } + // TODO: This rubbish should be done by the inversion machinery by itself. We shouldn't have to repeat it in every + // inversion rule + togo.valuePath = fluid.model.composePaths(transformer.outputPrefix, transformSpec.outputPath); + togo.outputPath = fluid.model.composePaths(transformer.inputPrefix, transformSpec.valuePath); + return togo; + }; + + fluid.defaults("fluid.transforms.binaryOp", { + gradeNames: [ "fluid.multiInputTransformFunction", "fluid.standardOutputTransformFunction" ], + inputVariables: { + left: null, + right: null + } + }); + + fluid.transforms.binaryLookup = { + "===": function (a, b) { return a === b; }, + "!==": function (a, b) { return a !== b; }, + "<=": function (a, b) { return a <= b; }, + "<": function (a, b) { return a < b; }, + ">=": function (a, b) { return a >= b; }, + ">": function (a, b) { return a > b; }, + "+": function (a, b) { return a + b; }, + "-": function (a, b) { return a - b; }, + "*": function (a, b) { return a * b; }, + "/": function (a, b) { return a / b; }, + "%": function (a, b) { return a % b; }, + "&&": function (a, b) { return a && b; }, + "||": function (a, b) { return a || b; } + }; + + fluid.transforms.binaryOp = function (inputs, transformSpec, transformer) { + var left = inputs.left(); + var right = inputs.right(); + + var operator = fluid.model.transform.getValue(undefined, transformSpec.operator, transformer); + + var fun = fluid.transforms.binaryLookup[operator]; + return (fun === undefined || left === undefined || right === undefined) ? + undefined : fun(left, right); + }; + + fluid.defaults("fluid.transforms.condition", { + gradeNames: [ "fluid.multiInputTransformFunction", "fluid.standardOutputTransformFunction" ], + inputVariables: { + "true": null, + "false": null, + "condition": null + } + }); + + fluid.transforms.condition = function (inputs) { + var condition = inputs.condition(); + if (condition === null) { + return undefined; + } + + return inputs[condition ? "true" : "false"](); + }; + + fluid.defaults("fluid.transforms.valueMapper", { + gradeNames: ["fluid.transformFunction", "fluid.lens"], + invertConfiguration: "fluid.transforms.valueMapper.invert", + collectInputPaths: "fluid.transforms.valueMapper.collect" + }); + + // unsupported, NON-API function + fluid.model.transform.compareMatches = function (speca, specb) { + return specb.matchValue - speca.matchValue; + }; + + // unsupported, NON-API function + fluid.model.transform.matchValueMapperFull = function (outerValue, transformSpec, transformer) { + var o = transformSpec.options; + if (o.length === 0) { + fluid.fail("valueMapper supplied empty list of options: ", transformSpec); + } + var matchPower = []; + for (var i = 0; i < o.length; ++i) { + var option = o[i]; + var value = fluid.firstDefined(fluid.model.transform.getValue(option.inputPath, undefined, transformer), + outerValue); + var matchValue = fluid.model.transform.matchValue(option.undefinedInputValue ? undefined : + (option.inputValue === undefined ? transformSpec.defaultInputValue : option.inputValue), value, transformSpec.partialMatches || option.partialMatches); + matchPower[i] = {index: i, matchValue: matchValue}; + } + matchPower.sort(fluid.model.transform.compareMatches); + return (matchPower[0].matchValue <= 0 || o.length > 1 && matchPower[0].matchValue === matchPower[1].matchValue) ? -1 : matchPower[0].index; + }; + + fluid.transforms.valueMapper = function (transformSpec, transformer) { + if (!transformSpec.options) { + fluid.fail("valueMapper requires a list or hash of options at path named \"options\", supplied ", transformSpec); + } + var value = fluid.model.transform.getValue(transformSpec.inputPath, undefined, transformer); + var deref = fluid.isArrayable(transformSpec.options) ? // long form with list of records + function (testVal) { + var index = fluid.model.transform.matchValueMapperFull(testVal, transformSpec, transformer); + return index === -1 ? null : transformSpec.options[index]; + } : + function (testVal) { + return transformSpec.options[testVal]; + }; + + var indexed = deref(value); + if (!indexed) { + // if no branch matches, try again using this value - WARNING, this seriously + // threatens invertibility + indexed = deref(transformSpec.defaultInputValue); + } + if (!indexed) { + return; + } + + var outputPath = indexed.outputPath === undefined ? transformSpec.defaultOutputPath : indexed.outputPath; + transformer.outputPrefixOp.push(outputPath); + var outputValue; + if (fluid.isPrimitive(indexed)) { + outputValue = indexed; + } else { + // if undefinedOutputValue is set, outputValue should be undefined + if (indexed.undefinedOutputValue) { + outputValue = undefined; + } else { + // get value from outputValue or outputValuePath. If none is found set the outputValue to be that of defaultOutputValue (or undefined) + outputValue = fluid.model.transform.resolveParam(indexed, transformer, "outputValue", undefined); + outputValue = (outputValue === undefined) ? transformSpec.defaultOutputValue : outputValue; + } + } + // output if outputPath or defaultOutputPath have been specified and the relevant child hasn't done the outputting + if (typeof(outputPath) === "string" && outputValue !== undefined) { + fluid.model.transform.setValue(undefined, outputValue, transformer, transformSpec.merge); + outputValue = undefined; + } + transformer.outputPrefixOp.pop(); + return outputValue; + }; + + fluid.transforms.valueMapper.invert = function (transformSpec, transformer) { + var options = []; + var togo = { + type: "fluid.transforms.valueMapper", + options: options + }; + var isArray = fluid.isArrayable(transformSpec.options); + var findCustom = function (name) { + return fluid.find(transformSpec.options, function (option) { + if (option[name]) { + return true; + } + }); + }; + var anyCustomOutput = findCustom("outputPath"); + var anyCustomInput = findCustom("inputPath"); + if (!anyCustomOutput) { + togo.inputPath = fluid.model.composePaths(transformer.outputPrefix, transformSpec.defaultOutputPath); + } + if (!anyCustomInput) { + togo.defaultOutputPath = fluid.model.composePaths(transformer.inputPrefix, transformSpec.inputPath); + } + var def = fluid.firstDefined; + fluid.each(transformSpec.options, function (option, key) { + var outOption = {}; + var origInputValue = def(isArray ? option.inputValue : key, transformSpec.defaultInputValue); + if (origInputValue === undefined) { + fluid.fail("Failure inverting configuration for valueMapper - inputValue could not be resolved for record " + key + ": ", transformSpec); + } + outOption.outputValue = fluid.model.transform.literaliseValue(origInputValue); + var origOutputValue = def(option.outputValue, transformSpec.defaultOutputValue); + outOption.inputValue = fluid.model.transform.getValue(option.outputValuePath, origOutputValue, transformer); + if (anyCustomOutput) { + outOption.inputPath = fluid.model.composePaths(transformer.outputPrefix, def(option.outputPath, transformSpec.outputPath)); + } + if (anyCustomInput) { + outOption.outputPath = fluid.model.composePaths(transformer.inputPrefix, def(option.inputPath, transformSpec.inputPath)); + } + if (option.outputValuePath) { + outOption.inputValuePath = option.outputValuePath; + } + options.push(outOption); + }); + return togo; + }; + + fluid.transforms.valueMapper.collect = function (transformSpec, transformer) { + var togo = []; + fluid.model.transform.accumulateInputPath(transformSpec.inputPath, transformer, togo); + fluid.each(transformSpec.options, function (option) { + fluid.model.transform.accumulateInputPath(option.inputPath, transformer, togo); + }); + return togo; + }; + + /* -------- arrayToSetMembership and setMembershipToArray ---------------- */ + + fluid.defaults("fluid.transforms.arrayToSetMembership", { + gradeNames: ["fluid.standardInputTransformFunction", "fluid.lens"], + invertConfiguration: "fluid.transforms.arrayToSetMembership.invert" + }); + + + fluid.transforms.arrayToSetMembership = function (value, transformSpec, transformer) { + var options = transformSpec.options; + + if (!value || !fluid.isArrayable(value)) { + fluid.fail("arrayToSetMembership didn't find array at inputPath nor passed as value.", transformSpec); + } + if (!options) { + fluid.fail("arrayToSetMembership requires an options block set"); + } + + if (transformSpec.presentValue === undefined) { + transformSpec.presentValue = true; + } + + if (transformSpec.missingValue === undefined) { + transformSpec.missingValue = false; + } + + fluid.each(options, function (outPath, key) { + // write to output path given in options the value or depending on whether key is found in user input + var outVal = (value.indexOf(key) !== -1) ? transformSpec.presentValue : transformSpec.missingValue; + fluid.model.transform.setValue(outPath, outVal, transformer); + }); + // TODO: Why does this transform make no return? + }; + + fluid.transforms.arrayToSetMembership.invert = function (transformSpec, transformer) { + var togo = fluid.copy(transformSpec); + delete togo.inputPath; + togo.type = "fluid.transforms.setMembershipToArray"; + togo.outputPath = fluid.model.composePaths(transformer.inputPrefix, transformSpec.inputPath); + var newOptions = {}; + fluid.each(transformSpec.options, function (path, oldKey) { + var newKey = fluid.model.composePaths(transformer.outputPrefix, path); + newOptions[newKey] = oldKey; + }); + togo.options = newOptions; + return togo; + }; + + fluid.defaults("fluid.transforms.setMembershipToArray", { + gradeNames: ["fluid.standardOutputTransformFunction"] + }); + + fluid.transforms.setMembershipToArray = function (transformSpec, transformer) { + var options = transformSpec.options; + + if (!options) { + fluid.fail("setMembershipToArray requires an options block specified"); + } + + if (transformSpec.presentValue === undefined) { + transformSpec.presentValue = true; + } + + if (transformSpec.missingValue === undefined) { + transformSpec.missingValue = false; + } + + var outputArr = []; + fluid.each(options, function (arrVal, inPath) { + var val = fluid.model.transform.getValue(inPath, undefined, transformer); + if (val === transformSpec.presentValue) { + outputArr.push(arrVal); + } + }); + return outputArr; + }; + + /* -------- objectToArray and arrayToObject -------------------- */ + + /** + * Transforms the given array to an object. + * Uses the transformSpec.options.key values from each object within the array as new keys. + * + * For example, with transformSpec.key = "name" and an input object like this: + * + * { + * b: [ + * { name: b1, v: v1 }, + * { name: b2, v: v2 } + * ] + * } + * + * The output will be: + * { + * b: { + * b1: { + * v: v1 + * } + * }, + * { + * b2: { + * v: v2 + * } + * } + * } + */ + fluid.model.transform.applyPaths = function (operation, pathOp, paths) { + for (var i = 0; i < paths.length; ++i) { + if (operation === "push") { + pathOp.push(paths[i]); + } else { + pathOp.pop(); + } + } + }; + + fluid.model.transform.expandInnerValues = function (inputPath, outputPath, transformer, innerValues) { + var inputPrefixOp = transformer.inputPrefixOp; + var outputPrefixOp = transformer.outputPrefixOp; + var apply = fluid.model.transform.applyPaths; + + apply("push", inputPrefixOp, inputPath); + apply("push", outputPrefixOp, outputPath); + var expanded = {}; + fluid.each(innerValues, function (innerValue) { + var expandedInner = transformer.expand(innerValue); + if (!fluid.isPrimitive(expandedInner)) { + $.extend(true, expanded, expandedInner); + } else { + expanded = expandedInner; + } + }); + apply("pop", outputPrefixOp, outputPath); + apply("pop", inputPrefixOp, inputPath); + + return expanded; + }; + + + fluid.defaults("fluid.transforms.arrayToObject", { + gradeNames: ["fluid.standardTransformFunction", "fluid.lens" ], + invertConfiguration: "fluid.transforms.arrayToObject.invert" + }); + + /** Transforms an array of objects into an object of objects, by indexing using the option "key" which must be supplied within the transform specification. + * The key of each element will be taken from the value held in each each original object's member derived from the option value in "key" - this member should + * exist in each array element. The member with name agreeing with "key" and its value will be removed from each original object before inserting into the returned + * object. + * For example, + * fluid.transforms.arrayToObject([{k: "e1", b: 1, c: 2}, {k: "e2", b: 2: c: 3}], {key: "k"}) will output the object + * {e1: {b: 1, c: 2}, e2: {b: 2: c, 3} + * Note: This transform frequently arises in the context of data which arose in XML form, which often represents "morally indexed" data in repeating array-like + * constructs where the indexing key is held, for example, in an attribute. + */ + + fluid.transforms.arrayToObject = function (arr, transformSpec, transformer) { + if (transformSpec.key === undefined) { + fluid.fail("arrayToObject requires a 'key' option.", transformSpec); + } + if (!fluid.isArrayable(arr)) { + fluid.fail("arrayToObject didn't find array at inputPath.", transformSpec); + } + var newHash = {}; + var pivot = transformSpec.key; + + fluid.each(arr, function (v, k) { + // check that we have a pivot entry in the object and it's a valid type: + var newKey = v[pivot]; + var keyType = typeof(newKey); + if (keyType !== "string" && keyType !== "boolean" && keyType !== "number") { + fluid.fail("arrayToObject encountered untransformable array due to missing or invalid key", v); + } + // use the value of the key element as key and use the remaining content as value + var content = fluid.copy(v); + delete content[pivot]; + // fix sub Arrays if needed: + if (transformSpec.innerValue) { + content = fluid.model.transform.expandInnerValues([transformer.inputPrefix, transformSpec.inputPath, k.toString()], + [newKey], transformer, transformSpec.innerValue); + } + newHash[newKey] = content; + }); + return newHash; + }; + + fluid.transforms.arrayToObject.invert = function (transformSpec, transformer) { + var togo = fluid.copy(transformSpec); + togo.type = "fluid.transforms.objectToArray"; + togo.inputPath = fluid.model.composePaths(transformer.outputPrefix, transformSpec.outputPath); + togo.outputPath = fluid.model.composePaths(transformer.inputPrefix, transformSpec.inputPath); + // invert transforms from innerValue as well: + // TODO: The Model Transformations framework should be capable of this, but right now the + // issue is that we use a "private contract" to operate the "innerValue" slot. We need to + // spend time thinking of how this should be formalised + if (togo.innerValue) { + var innerValue = togo.innerValue; + for (var i = 0; i < innerValue.length; ++i) { + innerValue[i] = fluid.model.transform.invertConfiguration(innerValue[i]); + } + } + return togo; + }; + + + fluid.defaults("fluid.transforms.objectToArray", { + gradeNames: "fluid.standardTransformFunction" + }); + + /** + * Transforms an object of objects into an array of objects, by deindexing by the option "key" which must be supplied within the transform specification. + * The key of each object will become split out into a fresh value in each array element which will be given the key held in the transformSpec option "key". + * For example: + * fluid.transforms.objectToArray({e1: {b: 1, c: 2}, e2: {b: 2: c, 3}, {key: "k"}) will output the array + * [{k: "e1", b: 1, c: 2}, {k: "e2", b: 2: c: 3}] + * + * This performs the inverse transform of fluid.transforms.arrayToObject. + */ + fluid.transforms.objectToArray = function (hash, transformSpec, transformer) { + if (transformSpec.key === undefined) { + fluid.fail("objectToArray requires a \"key\" option.", transformSpec); + } + + var newArray = []; + var pivot = transformSpec.key; + + fluid.each(hash, function (v, k) { + var content = {}; + content[pivot] = k; + if (transformSpec.innerValue) { + v = fluid.model.transform.expandInnerValues([transformSpec.inputPath, k], [transformSpec.outputPath, newArray.length.toString()], + transformer, transformSpec.innerValue); + } + $.extend(true, content, v); + newArray.push(content); + }); + return newArray; + }; + + fluid.defaults("fluid.transforms.limitRange", { + gradeNames: "fluid.standardTransformFunction" + }); + + fluid.transforms.limitRange = function (value, transformSpec) { + var min = transformSpec.min; + if (min !== undefined) { + var excludeMin = transformSpec.excludeMin || 0; + min += excludeMin; + if (value < min) { + value = min; + } + } + var max = transformSpec.max; + if (max !== undefined) { + var excludeMax = transformSpec.excludeMax || 0; + max -= excludeMax; + if (value > max) { + value = max; + } + } + return value; + }; + + fluid.defaults("fluid.transforms.indexOf", { + gradeNames: ["fluid.standardTransformFunction", "fluid.lens"], + invertConfiguration: "fluid.transforms.indexOf.invert" + }); + + fluid.transforms.indexOf = function (value, transformSpec) { + var offset = fluid.transforms.parseIndexationOffset(transformSpec.offset, "indexOf"); + var array = fluid.makeArray(transformSpec.array); + var originalIndex = array.indexOf(value); + return originalIndex === -1 && transformSpec.notFound ? transformSpec.notFound : originalIndex + offset; + }; + + fluid.transforms.indexOf.invert = function (transformSpec, transformer) { + var togo = fluid.transforms.invertArrayIndexation(transformSpec, transformer); + togo.type = "fluid.transforms.dereference"; + return togo; + }; + + fluid.defaults("fluid.transforms.dereference", { + gradeNames: ["fluid.standardTransformFunction", "fluid.lens"], + invertConfiguration: "fluid.transforms.dereference.invert" + }); + + fluid.transforms.dereference = function (value, transformSpec) { + if (typeof (value) !== "number") { + fluid.fail("dereference requires \"value\" to be a number. " + value + " is invalid."); + } + var offset = fluid.transforms.parseIndexationOffset(transformSpec.offset, "dereference"); + var array = fluid.makeArray(transformSpec.array); + var index = value + offset; + return index === -1 && transformSpec.notFound ? transformSpec.notFound : array[index]; + }; + + fluid.transforms.dereference.invert = function (transformSpec, transformer) { + var togo = fluid.transforms.invertArrayIndexation(transformSpec, transformer); + togo.type = "fluid.transforms.indexOf"; + return togo; + }; + + fluid.transforms.parseIndexationOffset = function (offset, transformName) { + var parsedOffset = 0; + if (offset !== undefined) { + parsedOffset = fluid.parseInteger(offset); + if (isNaN(parsedOffset)) { + fluid.fail(transformName + " requires the value of \"offset\" to be an integer or a string that can be converted to an integer. " + offset + " is invalid."); + } + } + return parsedOffset; + }; + + fluid.transforms.invertArrayIndexation = function (transformSpec, transformer) { + var togo = fluid.copy(transformSpec); + togo.inputPath = fluid.model.composePaths(transformer.outputPrefix, transformSpec.outputPath); + togo.outputPath = fluid.model.composePaths(transformer.inputPrefix, transformSpec.inputPath); + if (!isNaN(Number(togo.offset))) { + togo.offset = Number(togo.offset) * (-1); + } + return togo; + }; + + fluid.defaults("fluid.transforms.free", { + gradeNames: "fluid.transformFunction" + }); + + fluid.transforms.free = function (transformSpec) { + var args = fluid.makeArray(transformSpec.args); + return fluid.invokeGlobalFunction(transformSpec.func, args); + }; + +})(jQuery, fluid_2_0); diff --git a/ppig-2015/example/lib/infusion/jquery.keyboard-a11y.js b/ppig-2015/example/lib/infusion/jquery.keyboard-a11y.js new file mode 100644 index 0000000..61653de --- /dev/null +++ b/ppig-2015/example/lib/infusion/jquery.keyboard-a11y.js @@ -0,0 +1,623 @@ +/* +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 +*/ + +var fluid_2_0 = fluid_2_0 || {}; +var fluid = fluid || fluid_2_0; + +(function ($, fluid) { + "use strict"; + + // $().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_2_0", fluid_2_0); + +/************************************************************************* + * 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 = (sc_that.options.noWrap ? elements.length - 1 : 0); + } + if (sc_that.activeItemIndex < 0 && sc_that.activeItemIndex !== NO_SELECTION) { + sc_that.activeItemIndex = (sc_that.options.noWrap ? 0 : 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) { + 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) { + fluid.fail("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, + noWrap: false + }; + + /******************************************************************** + * 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(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"); + $(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_2_0); diff --git a/ppig-2015/example/lib/infusion/jquery.standalone.js b/ppig-2015/example/lib/infusion/jquery.standalone.js new file mode 100644 index 0000000..f49f344 --- /dev/null +++ b/ppig-2015/example/lib/infusion/jquery.standalone.js @@ -0,0 +1,173 @@ +/* + * Definitions in this file taken from: + * + * jQuery JavaScript Library v1.6.1 + * http://jquery.com/ + * + * This implementation is only intended to be used in contexts where the Fluid Infusion framework + * is required to be used without a functioning DOM being available (node.js or other standalone contexts). + * It includes the minimum definitions taken from jQuery required to operate the core of Fluid.js + * without FluidView.js. Consult http://issues.fluidproject.org/browse/FLUID-4568 for more details. + * + * Copyright 2011, John Resig + * Copyright 2011- OCAD University + * + * Dual licensed under the MIT or GPL Version 2 licenses. + * http://jquery.org/license + * Date: Thu May 12 15:04:36 2011 -0400 + */ + +/* global jQuery:true */ +/* exported jQuery */ + +var fluid_2_0 = fluid_2_0 || {}; +var fluid = fluid || fluid_2_0; + +(function (fluid) { + "use strict"; + + // Save a reference to some core methods + var toString = Object.prototype.toString; + var hasOwn = Object.prototype.hasOwnProperty; + var indexOf = Array.prototype.indexOf; + // Map over jQuery in case of overwrite + var _jQuery = window.jQuery; + // Map over the $ in case of overwrite + var _$ = window.$; + // Used for trimming whitespace + var trimLeft = /^\s+/, + trimRight = /\s+$/, + trim = String.prototype.trim; + + var jQuery = fluid.jQueryStandalone = { + + // The current version of jQuery being used + jquery: "1.6.1-fluidStandalone", + + noConflict: function (deep) { + if (window.$ === jQuery) { + window.$ = _$; + } + if (deep && window.jQuery === jQuery) { + window.jQuery = _jQuery; + } + return jQuery; + }, + + isArray: Array.isArray || function (obj) { + return toString.call(obj) === "[object Array]"; + }, + + // Use native String.trim function wherever possible + trim: trim ? function( text ) { + return text === null ? "" : trim.call( text ); + } : + // Otherwise use our own trimming functionality + function( text ) { + return text === null ? "" : text.toString().replace( trimLeft, "" ).replace( trimRight, "" ); + }, + + // A crude way of determining if an object is a window + isWindow: function (obj) { + return obj && typeof obj === "object" && "setInterval" in obj; + }, + + isPlainObject: function (obj) { + // Must be an Object. + // Because of IE, we also have to check the presence of the constructor property. + // Make sure that DOM nodes and window objects don't pass through, as well + if ( !obj || toString.call(obj) !== "[object Object]" || obj.nodeType || jQuery.isWindow( obj ) ) { + return false; + } + + // Not own constructor property must be Object + if ( obj.constructor && + !hasOwn.call(obj, "constructor") && + !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) { + return false; + } + // Own properties are enumerated firstly, so to speed up, + // if last one is own, then all properties are own. + // TODO: Isn't this enormously expensive? + var key; + for (key in obj) {} + return key === undefined || hasOwn.call( obj, key ); + }, + + isEmptyObject: function (obj) { + var name; + for ( name in obj ) { + return false; + } + return true; + }, + + inArray: function (elem, array) { + if (indexOf) { + return indexOf.call( array, elem ); + } + for (var i = 0, length = array.length; i < length; i++) { + if (array[i] === elem) { + return i; + } + } + return -1; + }, + + extend: function () { + var options, + target = arguments[0] || {}, + i = 1, + length = arguments.length, + deep = false; + + // Handle a deep copy situation + if (typeof target === "boolean") { + deep = target; + target = arguments[1] || {}; + // skip the boolean and the target + i = 2; + } + + // Handle case when target is a string or something (possible in deep copy) + if (typeof target !== "object" && typeof(target) !== "function") { + target = {}; + } + + for ( ; i < length; i++ ) { + // Only deal with non-null/undefined values + if ( (options = arguments[ i ]) !== null ) { + // Extend the base object + for (var name in options) { + var src = target[ name ]; + var copy = options[ name ]; + + // Prevent never-ending loop + if ( target === copy ) { + continue; + } + var copyIsArray, clone; + // Recurse if we're merging plain objects or arrays + if (deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy))) ) { + if (copyIsArray) { + copyIsArray = false; + clone = src && jQuery.isArray(src) ? src : []; + } else { + clone = src && jQuery.isPlainObject(src) ? src : {}; + } + // Never move original objects, clone them + target[name] = jQuery.extend( deep, clone, copy ); + } else if (copy !== undefined) { + // Don't bring in undefined values + target[name] = copy; + } + } + } + } + return target; + } + }; + +})(fluid_2_0); + +var jQuery = fluid.jQueryStandalone; diff --git a/ppig-2015/example/lib/jQuery/jquery.js b/ppig-2015/example/lib/jQuery/jquery.js new file mode 100644 index 0000000..c5d7bd1 --- /dev/null +++ b/ppig-2015/example/lib/jQuery/jquery.js @@ -0,0 +1,10337 @@ +/*! + * jQuery JavaScript Library v1.11.0 + * http://jquery.com/ + * + * Includes Sizzle.js + * http://sizzlejs.com/ + * + * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors + * Released under the MIT license + * http://jquery.org/license + * + * Date: 2014-01-23T21:02Z + */ + +(function( global, factory ) { + + if ( typeof module === "object" && typeof module.exports === "object" ) { + // For CommonJS and CommonJS-like environments where a proper window is present, + // execute the factory and get jQuery + // For environments that do not inherently posses a window with a document + // (such as Node.js), expose a jQuery-making factory as module.exports + // This accentuates the need for the creation of a real window + // e.g. var jQuery = require("jquery")(window); + // See ticket #14549 for more info + module.exports = global.document ? + factory( global, true ) : + function( w ) { + if ( !w.document ) { + throw new Error( "jQuery requires a window with a document" ); + } + return factory( w ); + }; + } else { + factory( global ); + } + +// Pass this if window is not defined yet +}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) { + +// Can't do this because several apps including ASP.NET trace +// the stack via arguments.caller.callee and Firefox dies if +// you try to trace through "use strict" call chains. (#13335) +// Support: Firefox 18+ +// + +var deletedIds = []; + +var slice = deletedIds.slice; + +var concat = deletedIds.concat; + +var push = deletedIds.push; + +var indexOf = deletedIds.indexOf; + +var class2type = {}; + +var toString = class2type.toString; + +var hasOwn = class2type.hasOwnProperty; + +var trim = "".trim; + +var support = {}; + + + +var + version = "1.11.0", + + // Define a local copy of jQuery + jQuery = function( selector, context ) { + // The jQuery object is actually just the init constructor 'enhanced' + // Need init if jQuery is called (just allow error to be thrown if not included) + return new jQuery.fn.init( selector, context ); + }, + + // Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE) + rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, + + // Matches dashed string for camelizing + rmsPrefix = /^-ms-/, + rdashAlpha = /-([\da-z])/gi, + + // Used by jQuery.camelCase as callback to replace() + fcamelCase = function( all, letter ) { + return letter.toUpperCase(); + }; + +jQuery.fn = jQuery.prototype = { + // The current version of jQuery being used + jquery: version, + + constructor: jQuery, + + // Start with an empty selector + selector: "", + + // The default length of a jQuery object is 0 + length: 0, + + toArray: function() { + return slice.call( this ); + }, + + // Get the Nth element in the matched element set OR + // Get the whole matched element set as a clean array + get: function( num ) { + return num != null ? + + // Return a 'clean' array + ( num < 0 ? this[ num + this.length ] : this[ num ] ) : + + // Return just the object + slice.call( this ); + }, + + // Take an array of elements and push it onto the stack + // (returning the new matched element set) + pushStack: function( elems ) { + + // Build a new jQuery matched element set + var ret = jQuery.merge( this.constructor(), elems ); + + // Add the old object onto the stack (as a reference) + ret.prevObject = this; + ret.context = this.context; + + // Return the newly-formed element set + return ret; + }, + + // Execute a callback for every element in the matched set. + // (You can seed the arguments with an array of args, but this is + // only used internally.) + each: function( callback, args ) { + return jQuery.each( this, callback, args ); + }, + + map: function( callback ) { + return this.pushStack( jQuery.map(this, function( elem, i ) { + return callback.call( elem, i, elem ); + })); + }, + + slice: function() { + return this.pushStack( slice.apply( this, arguments ) ); + }, + + first: function() { + return this.eq( 0 ); + }, + + last: function() { + return this.eq( -1 ); + }, + + eq: function( i ) { + var len = this.length, + j = +i + ( i < 0 ? len : 0 ); + return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] ); + }, + + end: function() { + return this.prevObject || this.constructor(null); + }, + + // For internal use only. + // Behaves like an Array's method, not like a jQuery method. + push: push, + sort: deletedIds.sort, + splice: deletedIds.splice +}; + +jQuery.extend = jQuery.fn.extend = function() { + var src, copyIsArray, copy, name, options, clone, + target = arguments[0] || {}, + i = 1, + length = arguments.length, + deep = false; + + // Handle a deep copy situation + if ( typeof target === "boolean" ) { + deep = target; + + // skip the boolean and the target + target = arguments[ i ] || {}; + i++; + } + + // Handle case when target is a string or something (possible in deep copy) + if ( typeof target !== "object" && !jQuery.isFunction(target) ) { + target = {}; + } + + // extend jQuery itself if only one argument is passed + if ( i === length ) { + target = this; + i--; + } + + for ( ; i < length; i++ ) { + // Only deal with non-null/undefined values + if ( (options = arguments[ i ]) != null ) { + // Extend the base object + for ( name in options ) { + src = target[ name ]; + copy = options[ name ]; + + // Prevent never-ending loop + if ( target === copy ) { + continue; + } + + // Recurse if we're merging plain objects or arrays + if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) { + if ( copyIsArray ) { + copyIsArray = false; + clone = src && jQuery.isArray(src) ? src : []; + + } else { + clone = src && jQuery.isPlainObject(src) ? src : {}; + } + + // Never move original objects, clone them + target[ name ] = jQuery.extend( deep, clone, copy ); + + // Don't bring in undefined values + } else if ( copy !== undefined ) { + target[ name ] = copy; + } + } + } + } + + // Return the modified object + return target; +}; + +jQuery.extend({ + // Unique for each copy of jQuery on the page + expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ), + + // Assume jQuery is ready without the ready module + isReady: true, + + error: function( msg ) { + throw new Error( msg ); + }, + + noop: function() {}, + + // See test/unit/core.js for details concerning isFunction. + // Since version 1.3, DOM methods and functions like alert + // aren't supported. They return false on IE (#2968). + isFunction: function( obj ) { + return jQuery.type(obj) === "function"; + }, + + isArray: Array.isArray || function( obj ) { + return jQuery.type(obj) === "array"; + }, + + isWindow: function( obj ) { + /* jshint eqeqeq: false */ + return obj != null && obj == obj.window; + }, + + isNumeric: function( obj ) { + // parseFloat NaNs numeric-cast false positives (null|true|false|"") + // ...but misinterprets leading-number strings, particularly hex literals ("0x...") + // subtraction forces infinities to NaN + return obj - parseFloat( obj ) >= 0; + }, + + isEmptyObject: function( obj ) { + var name; + for ( name in obj ) { + return false; + } + return true; + }, + + isPlainObject: function( obj ) { + var key; + + // Must be an Object. + // Because of IE, we also have to check the presence of the constructor property. + // Make sure that DOM nodes and window objects don't pass through, as well + if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { + return false; + } + + try { + // Not own constructor property must be Object + if ( obj.constructor && + !hasOwn.call(obj, "constructor") && + !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) { + return false; + } + } catch ( e ) { + // IE8,9 Will throw exceptions on certain host objects #9897 + return false; + } + + // Support: IE<9 + // Handle iteration over inherited properties before own properties. + if ( support.ownLast ) { + for ( key in obj ) { + return hasOwn.call( obj, key ); + } + } + + // Own properties are enumerated firstly, so to speed up, + // if last one is own, then all properties are own. + for ( key in obj ) {} + + return key === undefined || hasOwn.call( obj, key ); + }, + + type: function( obj ) { + if ( obj == null ) { + return obj + ""; + } + return typeof obj === "object" || typeof obj === "function" ? + class2type[ toString.call(obj) ] || "object" : + typeof obj; + }, + + // Evaluates a script in a global context + // Workarounds based on findings by Jim Driscoll + // http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context + globalEval: function( data ) { + if ( data && jQuery.trim( data ) ) { + // We use execScript on Internet Explorer + // We use an anonymous function so that context is window + // rather than jQuery in Firefox + ( window.execScript || function( data ) { + window[ "eval" ].call( window, data ); + } )( data ); + } + }, + + // Convert dashed to camelCase; used by the css and data modules + // Microsoft forgot to hump their vendor prefix (#9572) + camelCase: function( string ) { + return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase ); + }, + + nodeName: function( elem, name ) { + return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); + }, + + // args is for internal usage only + each: function( obj, callback, args ) { + var value, + i = 0, + length = obj.length, + isArray = isArraylike( obj ); + + if ( args ) { + if ( isArray ) { + for ( ; i < length; i++ ) { + value = callback.apply( obj[ i ], args ); + + if ( value === false ) { + break; + } + } + } else { + for ( i in obj ) { + value = callback.apply( obj[ i ], args ); + + if ( value === false ) { + break; + } + } + } + + // A special, fast, case for the most common use of each + } else { + if ( isArray ) { + for ( ; i < length; i++ ) { + value = callback.call( obj[ i ], i, obj[ i ] ); + + if ( value === false ) { + break; + } + } + } else { + for ( i in obj ) { + value = callback.call( obj[ i ], i, obj[ i ] ); + + if ( value === false ) { + break; + } + } + } + } + + return obj; + }, + + // Use native String.trim function wherever possible + trim: trim && !trim.call("\uFEFF\xA0") ? + function( text ) { + return text == null ? + "" : + trim.call( text ); + } : + + // Otherwise use our own trimming functionality + function( text ) { + return text == null ? + "" : + ( text + "" ).replace( rtrim, "" ); + }, + + // results is for internal usage only + makeArray: function( arr, results ) { + var ret = results || []; + + if ( arr != null ) { + if ( isArraylike( Object(arr) ) ) { + jQuery.merge( ret, + typeof arr === "string" ? + [ arr ] : arr + ); + } else { + push.call( ret, arr ); + } + } + + return ret; + }, + + inArray: function( elem, arr, i ) { + var len; + + if ( arr ) { + if ( indexOf ) { + return indexOf.call( arr, elem, i ); + } + + len = arr.length; + i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0; + + for ( ; i < len; i++ ) { + // Skip accessing in sparse arrays + if ( i in arr && arr[ i ] === elem ) { + return i; + } + } + } + + return -1; + }, + + merge: function( first, second ) { + var len = +second.length, + j = 0, + i = first.length; + + while ( j < len ) { + first[ i++ ] = second[ j++ ]; + } + + // Support: IE<9 + // Workaround casting of .length to NaN on otherwise arraylike objects (e.g., NodeLists) + if ( len !== len ) { + while ( second[j] !== undefined ) { + first[ i++ ] = second[ j++ ]; + } + } + + first.length = i; + + return first; + }, + + grep: function( elems, callback, invert ) { + var callbackInverse, + matches = [], + i = 0, + length = elems.length, + callbackExpect = !invert; + + // Go through the array, only saving the items + // that pass the validator function + for ( ; i < length; i++ ) { + callbackInverse = !callback( elems[ i ], i ); + if ( callbackInverse !== callbackExpect ) { + matches.push( elems[ i ] ); + } + } + + return matches; + }, + + // arg is for internal usage only + map: function( elems, callback, arg ) { + var value, + i = 0, + length = elems.length, + isArray = isArraylike( elems ), + ret = []; + + // Go through the array, translating each of the items to their new values + if ( isArray ) { + for ( ; i < length; i++ ) { + value = callback( elems[ i ], i, arg ); + + if ( value != null ) { + ret.push( value ); + } + } + + // Go through every key on the object, + } else { + for ( i in elems ) { + value = callback( elems[ i ], i, arg ); + + if ( value != null ) { + ret.push( value ); + } + } + } + + // Flatten any nested arrays + return concat.apply( [], ret ); + }, + + // A global GUID counter for objects + guid: 1, + + // Bind a function to a context, optionally partially applying any + // arguments. + proxy: function( fn, context ) { + var args, proxy, tmp; + + if ( typeof context === "string" ) { + tmp = fn[ context ]; + context = fn; + fn = tmp; + } + + // Quick check to determine if target is callable, in the spec + // this throws a TypeError, but we will just return undefined. + if ( !jQuery.isFunction( fn ) ) { + return undefined; + } + + // Simulated bind + args = slice.call( arguments, 2 ); + proxy = function() { + return fn.apply( context || this, args.concat( slice.call( arguments ) ) ); + }; + + // Set the guid of unique handler to the same of original handler, so it can be removed + proxy.guid = fn.guid = fn.guid || jQuery.guid++; + + return proxy; + }, + + now: function() { + return +( new Date() ); + }, + + // jQuery.support is not used in Core but other projects attach their + // properties to it so it needs to exist. + support: support +}); + +// Populate the class2type map +jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { + class2type[ "[object " + name + "]" ] = name.toLowerCase(); +}); + +function isArraylike( obj ) { + var length = obj.length, + type = jQuery.type( obj ); + + if ( type === "function" || jQuery.isWindow( obj ) ) { + return false; + } + + if ( obj.nodeType === 1 && length ) { + return true; + } + + return type === "array" || length === 0 || + typeof length === "number" && length > 0 && ( length - 1 ) in obj; +} +var Sizzle = +/*! + * Sizzle CSS Selector Engine v1.10.16 + * http://sizzlejs.com/ + * + * Copyright 2013 jQuery Foundation, Inc. and other contributors + * Released under the MIT license + * http://jquery.org/license + * + * Date: 2014-01-13 + */ +(function( window ) { + +var i, + support, + Expr, + getText, + isXML, + compile, + outermostContext, + sortInput, + hasDuplicate, + + // Local document vars + setDocument, + document, + docElem, + documentIsHTML, + rbuggyQSA, + rbuggyMatches, + matches, + contains, + + // Instance-specific data + expando = "sizzle" + -(new Date()), + preferredDoc = window.document, + dirruns = 0, + done = 0, + classCache = createCache(), + tokenCache = createCache(), + compilerCache = createCache(), + sortOrder = function( a, b ) { + if ( a === b ) { + hasDuplicate = true; + } + return 0; + }, + + // General-purpose constants + strundefined = typeof undefined, + MAX_NEGATIVE = 1 << 31, + + // Instance methods + hasOwn = ({}).hasOwnProperty, + arr = [], + pop = arr.pop, + push_native = arr.push, + push = arr.push, + slice = arr.slice, + // Use a stripped-down indexOf if we can't use a native one + indexOf = arr.indexOf || function( elem ) { + var i = 0, + len = this.length; + for ( ; i < len; i++ ) { + if ( this[i] === elem ) { + return i; + } + } + return -1; + }, + + booleans = "checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped", + + // Regular expressions + + // Whitespace characters http://www.w3.org/TR/css3-selectors/#whitespace + whitespace = "[\\x20\\t\\r\\n\\f]", + // http://www.w3.org/TR/css3-syntax/#characters + characterEncoding = "(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+", + + // Loosely modeled on CSS identifier characters + // An unquoted value should be a CSS identifier http://www.w3.org/TR/css3-selectors/#attribute-selectors + // Proper syntax: http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier + identifier = characterEncoding.replace( "w", "w#" ), + + // Acceptable operators http://www.w3.org/TR/selectors/#attribute-selectors + attributes = "\\[" + whitespace + "*(" + characterEncoding + ")" + whitespace + + "*(?:([*^$|!~]?=)" + whitespace + "*(?:(['\"])((?:\\\\.|[^\\\\])*?)\\3|(" + identifier + ")|)|)" + whitespace + "*\\]", + + // Prefer arguments quoted, + // then not containing pseudos/brackets, + // then attribute selectors/non-parenthetical expressions, + // then anything else + // These preferences are here to reduce the number of selectors + // needing tokenize in the PSEUDO preFilter + pseudos = ":(" + characterEncoding + ")(?:\\(((['\"])((?:\\\\.|[^\\\\])*?)\\3|((?:\\\\.|[^\\\\()[\\]]|" + attributes.replace( 3, 8 ) + ")*)|.*)\\)|)", + + // Leading and non-escaped trailing whitespace, capturing some non-whitespace characters preceding the latter + rtrim = new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" ), + + rcomma = new RegExp( "^" + whitespace + "*," + whitespace + "*" ), + rcombinators = new RegExp( "^" + whitespace + "*([>+~]|" + whitespace + ")" + whitespace + "*" ), + + rattributeQuotes = new RegExp( "=" + whitespace + "*([^\\]'\"]*?)" + whitespace + "*\\]", "g" ), + + rpseudo = new RegExp( pseudos ), + ridentifier = new RegExp( "^" + identifier + "$" ), + + matchExpr = { + "ID": new RegExp( "^#(" + characterEncoding + ")" ), + "CLASS": new RegExp( "^\\.(" + characterEncoding + ")" ), + "TAG": new RegExp( "^(" + characterEncoding.replace( "w", "w*" ) + ")" ), + "ATTR": new RegExp( "^" + attributes ), + "PSEUDO": new RegExp( "^" + pseudos ), + "CHILD": new RegExp( "^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\(" + whitespace + + "*(even|odd|(([+-]|)(\\d*)n|)" + whitespace + "*(?:([+-]|)" + whitespace + + "*(\\d+)|))" + whitespace + "*\\)|)", "i" ), + "bool": new RegExp( "^(?:" + booleans + ")$", "i" ), + // For use in libraries implementing .is() + // We use this for POS matching in `select` + "needsContext": new RegExp( "^" + whitespace + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\(" + + whitespace + "*((?:-\\d)?\\d*)" + whitespace + "*\\)|)(?=[^-]|$)", "i" ) + }, + + rinputs = /^(?:input|select|textarea|button)$/i, + rheader = /^h\d$/i, + + rnative = /^[^{]+\{\s*\[native \w/, + + // Easily-parseable/retrievable ID or TAG or CLASS selectors + rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/, + + rsibling = /[+~]/, + rescape = /'|\\/g, + + // CSS escapes http://www.w3.org/TR/CSS21/syndata.html#escaped-characters + runescape = new RegExp( "\\\\([\\da-f]{1,6}" + whitespace + "?|(" + whitespace + ")|.)", "ig" ), + funescape = function( _, escaped, escapedWhitespace ) { + var high = "0x" + escaped - 0x10000; + // NaN means non-codepoint + // Support: Firefox + // Workaround erroneous numeric interpretation of +"0x" + return high !== high || escapedWhitespace ? + escaped : + high < 0 ? + // BMP codepoint + String.fromCharCode( high + 0x10000 ) : + // Supplemental Plane codepoint (surrogate pair) + String.fromCharCode( high >> 10 | 0xD800, high & 0x3FF | 0xDC00 ); + }; + +// Optimize for push.apply( _, NodeList ) +try { + push.apply( + (arr = slice.call( preferredDoc.childNodes )), + preferredDoc.childNodes + ); + // Support: Android<4.0 + // Detect silently failing push.apply + arr[ preferredDoc.childNodes.length ].nodeType; +} catch ( e ) { + push = { apply: arr.length ? + + // Leverage slice if possible + function( target, els ) { + push_native.apply( target, slice.call(els) ); + } : + + // Support: IE<9 + // Otherwise append directly + function( target, els ) { + var j = target.length, + i = 0; + // Can't trust NodeList.length + while ( (target[j++] = els[i++]) ) {} + target.length = j - 1; + } + }; +} + +function Sizzle( selector, context, results, seed ) { + var match, elem, m, nodeType, + // QSA vars + i, groups, old, nid, newContext, newSelector; + + if ( ( context ? context.ownerDocument || context : preferredDoc ) !== document ) { + setDocument( context ); + } + + context = context || document; + results = results || []; + + if ( !selector || typeof selector !== "string" ) { + return results; + } + + if ( (nodeType = context.nodeType) !== 1 && nodeType !== 9 ) { + return []; + } + + if ( documentIsHTML && !seed ) { + + // Shortcuts + if ( (match = rquickExpr.exec( selector )) ) { + // Speed-up: Sizzle("#ID") + if ( (m = match[1]) ) { + if ( nodeType === 9 ) { + elem = context.getElementById( m ); + // Check parentNode to catch when Blackberry 4.6 returns + // nodes that are no longer in the document (jQuery #6963) + if ( elem && elem.parentNode ) { + // Handle the case where IE, Opera, and Webkit return items + // by name instead of ID + if ( elem.id === m ) { + results.push( elem ); + return results; + } + } else { + return results; + } + } else { + // Context is not a document + if ( context.ownerDocument && (elem = context.ownerDocument.getElementById( m )) && + contains( context, elem ) && elem.id === m ) { + results.push( elem ); + return results; + } + } + + // Speed-up: Sizzle("TAG") + } else if ( match[2] ) { + push.apply( results, context.getElementsByTagName( selector ) ); + return results; + + // Speed-up: Sizzle(".CLASS") + } else if ( (m = match[3]) && support.getElementsByClassName && context.getElementsByClassName ) { + push.apply( results, context.getElementsByClassName( m ) ); + return results; + } + } + + // QSA path + if ( support.qsa && (!rbuggyQSA || !rbuggyQSA.test( selector )) ) { + nid = old = expando; + newContext = context; + newSelector = nodeType === 9 && selector; + + // qSA works strangely on Element-rooted queries + // We can work around this by specifying an extra ID on the root + // and working up from there (Thanks to Andrew Dupont for the technique) + // IE 8 doesn't work on object elements + if ( nodeType === 1 && context.nodeName.toLowerCase() !== "object" ) { + groups = tokenize( selector ); + + if ( (old = context.getAttribute("id")) ) { + nid = old.replace( rescape, "\\$&" ); + } else { + context.setAttribute( "id", nid ); + } + nid = "[id='" + nid + "'] "; + + i = groups.length; + while ( i-- ) { + groups[i] = nid + toSelector( groups[i] ); + } + newContext = rsibling.test( selector ) && testContext( context.parentNode ) || context; + newSelector = groups.join(","); + } + + if ( newSelector ) { + try { + push.apply( results, + newContext.querySelectorAll( newSelector ) + ); + return results; + } catch(qsaError) { + } finally { + if ( !old ) { + context.removeAttribute("id"); + } + } + } + } + } + + // All others + return select( selector.replace( rtrim, "$1" ), context, results, seed ); +} + +/** + * Create key-value caches of limited size + * @returns {Function(string, Object)} Returns the Object data after storing it on itself with + * property name the (space-suffixed) string and (if the cache is larger than Expr.cacheLength) + * deleting the oldest entry + */ +function createCache() { + var keys = []; + + function cache( key, value ) { + // Use (key + " ") to avoid collision with native prototype properties (see Issue #157) + if ( keys.push( key + " " ) > Expr.cacheLength ) { + // Only keep the most recent entries + delete cache[ keys.shift() ]; + } + return (cache[ key + " " ] = value); + } + return cache; +} + +/** + * Mark a function for special use by Sizzle + * @param {Function} fn The function to mark + */ +function markFunction( fn ) { + fn[ expando ] = true; + return fn; +} + +/** + * Support testing using an element + * @param {Function} fn Passed the created div and expects a boolean result + */ +function assert( fn ) { + var div = document.createElement("div"); + + try { + return !!fn( div ); + } catch (e) { + return false; + } finally { + // Remove from its parent by default + if ( div.parentNode ) { + div.parentNode.removeChild( div ); + } + // release memory in IE + div = null; + } +} + +/** + * Adds the same handler for all of the specified attrs + * @param {String} attrs Pipe-separated list of attributes + * @param {Function} handler The method that will be applied + */ +function addHandle( attrs, handler ) { + var arr = attrs.split("|"), + i = attrs.length; + + while ( i-- ) { + Expr.attrHandle[ arr[i] ] = handler; + } +} + +/** + * Checks document order of two siblings + * @param {Element} a + * @param {Element} b + * @returns {Number} Returns less than 0 if a precedes b, greater than 0 if a follows b + */ +function siblingCheck( a, b ) { + var cur = b && a, + diff = cur && a.nodeType === 1 && b.nodeType === 1 && + ( ~b.sourceIndex || MAX_NEGATIVE ) - + ( ~a.sourceIndex || MAX_NEGATIVE ); + + // Use IE sourceIndex if available on both nodes + if ( diff ) { + return diff; + } + + // Check if b follows a + if ( cur ) { + while ( (cur = cur.nextSibling) ) { + if ( cur === b ) { + return -1; + } + } + } + + return a ? 1 : -1; +} + +/** + * Returns a function to use in pseudos for input types + * @param {String} type + */ +function createInputPseudo( type ) { + return function( elem ) { + var name = elem.nodeName.toLowerCase(); + return name === "input" && elem.type === type; + }; +} + +/** + * Returns a function to use in pseudos for buttons + * @param {String} type + */ +function createButtonPseudo( type ) { + return function( elem ) { + var name = elem.nodeName.toLowerCase(); + return (name === "input" || name === "button") && elem.type === type; + }; +} + +/** + * Returns a function to use in pseudos for positionals + * @param {Function} fn + */ +function createPositionalPseudo( fn ) { + return markFunction(function( argument ) { + argument = +argument; + return markFunction(function( seed, matches ) { + var j, + matchIndexes = fn( [], seed.length, argument ), + i = matchIndexes.length; + + // Match elements found at the specified indexes + while ( i-- ) { + if ( seed[ (j = matchIndexes[i]) ] ) { + seed[j] = !(matches[j] = seed[j]); + } + } + }); + }); +} + +/** + * Checks a node for validity as a Sizzle context + * @param {Element|Object=} context + * @returns {Element|Object|Boolean} The input node if acceptable, otherwise a falsy value + */ +function testContext( context ) { + return context && typeof context.getElementsByTagName !== strundefined && context; +} + +// Expose support vars for convenience +support = Sizzle.support = {}; + +/** + * Detects XML nodes + * @param {Element|Object} elem An element or a document + * @returns {Boolean} True iff elem is a non-HTML XML node + */ +isXML = Sizzle.isXML = function( elem ) { + // documentElement is verified for cases where it doesn't yet exist + // (such as loading iframes in IE - #4833) + var documentElement = elem && (elem.ownerDocument || elem).documentElement; + return documentElement ? documentElement.nodeName !== "HTML" : false; +}; + +/** + * Sets document-related variables once based on the current document + * @param {Element|Object} [doc] An element or document object to use to set the document + * @returns {Object} Returns the current document + */ +setDocument = Sizzle.setDocument = function( node ) { + var hasCompare, + doc = node ? node.ownerDocument || node : preferredDoc, + parent = doc.defaultView; + + // If no document and documentElement is available, return + if ( doc === document || doc.nodeType !== 9 || !doc.documentElement ) { + return document; + } + + // Set our document + document = doc; + docElem = doc.documentElement; + + // Support tests + documentIsHTML = !isXML( doc ); + + // Support: IE>8 + // If iframe document is assigned to "document" variable and if iframe has been reloaded, + // IE will throw "permission denied" error when accessing "document" variable, see jQuery #13936 + // IE6-8 do not support the defaultView property so parent will be undefined + if ( parent && parent !== parent.top ) { + // IE11 does not have attachEvent, so all must suffer + if ( parent.addEventListener ) { + parent.addEventListener( "unload", function() { + setDocument(); + }, false ); + } else if ( parent.attachEvent ) { + parent.attachEvent( "onunload", function() { + setDocument(); + }); + } + } + + /* Attributes + ---------------------------------------------------------------------- */ + + // Support: IE<8 + // Verify that getAttribute really returns attributes and not properties (excepting IE8 booleans) + support.attributes = assert(function( div ) { + div.className = "i"; + return !div.getAttribute("className"); + }); + + /* getElement(s)By* + ---------------------------------------------------------------------- */ + + // Check if getElementsByTagName("*") returns only elements + support.getElementsByTagName = assert(function( div ) { + div.appendChild( doc.createComment("") ); + return !div.getElementsByTagName("*").length; + }); + + // Check if getElementsByClassName can be trusted + support.getElementsByClassName = rnative.test( doc.getElementsByClassName ) && assert(function( div ) { + div.innerHTML = "
"; + + // Support: Safari<4 + // Catch class over-caching + div.firstChild.className = "i"; + // Support: Opera<10 + // Catch gEBCN failure to find non-leading classes + return div.getElementsByClassName("i").length === 2; + }); + + // Support: IE<10 + // Check if getElementById returns elements by name + // The broken getElementById methods don't pick up programatically-set names, + // so use a roundabout getElementsByName test + support.getById = assert(function( div ) { + docElem.appendChild( div ).id = expando; + return !doc.getElementsByName || !doc.getElementsByName( expando ).length; + }); + + // ID find and filter + if ( support.getById ) { + Expr.find["ID"] = function( id, context ) { + if ( typeof context.getElementById !== strundefined && documentIsHTML ) { + var m = context.getElementById( id ); + // Check parentNode to catch when Blackberry 4.6 returns + // nodes that are no longer in the document #6963 + return m && m.parentNode ? [m] : []; + } + }; + Expr.filter["ID"] = function( id ) { + var attrId = id.replace( runescape, funescape ); + return function( elem ) { + return elem.getAttribute("id") === attrId; + }; + }; + } else { + // Support: IE6/7 + // getElementById is not reliable as a find shortcut + delete Expr.find["ID"]; + + Expr.filter["ID"] = function( id ) { + var attrId = id.replace( runescape, funescape ); + return function( elem ) { + var node = typeof elem.getAttributeNode !== strundefined && elem.getAttributeNode("id"); + return node && node.value === attrId; + }; + }; + } + + // Tag + Expr.find["TAG"] = support.getElementsByTagName ? + function( tag, context ) { + if ( typeof context.getElementsByTagName !== strundefined ) { + return context.getElementsByTagName( tag ); + } + } : + function( tag, context ) { + var elem, + tmp = [], + i = 0, + results = context.getElementsByTagName( tag ); + + // Filter out possible comments + if ( tag === "*" ) { + while ( (elem = results[i++]) ) { + if ( elem.nodeType === 1 ) { + tmp.push( elem ); + } + } + + return tmp; + } + return results; + }; + + // Class + Expr.find["CLASS"] = support.getElementsByClassName && function( className, context ) { + if ( typeof context.getElementsByClassName !== strundefined && documentIsHTML ) { + return context.getElementsByClassName( className ); + } + }; + + /* QSA/matchesSelector + ---------------------------------------------------------------------- */ + + // QSA and matchesSelector support + + // matchesSelector(:active) reports false when true (IE9/Opera 11.5) + rbuggyMatches = []; + + // qSa(:focus) reports false when true (Chrome 21) + // We allow this because of a bug in IE8/9 that throws an error + // whenever `document.activeElement` is accessed on an iframe + // So, we allow :focus to pass through QSA all the time to avoid the IE error + // See http://bugs.jquery.com/ticket/13378 + rbuggyQSA = []; + + if ( (support.qsa = rnative.test( doc.querySelectorAll )) ) { + // Build QSA regex + // Regex strategy adopted from Diego Perini + assert(function( div ) { + // Select is set to empty string on purpose + // This is to test IE's treatment of not explicitly + // setting a boolean content attribute, + // since its presence should be enough + // http://bugs.jquery.com/ticket/12359 + div.innerHTML = ""; + + // Support: IE8, Opera 10-12 + // Nothing should be selected when empty strings follow ^= or $= or *= + if ( div.querySelectorAll("[t^='']").length ) { + rbuggyQSA.push( "[*^$]=" + whitespace + "*(?:''|\"\")" ); + } + + // Support: IE8 + // Boolean attributes and "value" are not treated correctly + if ( !div.querySelectorAll("[selected]").length ) { + rbuggyQSA.push( "\\[" + whitespace + "*(?:value|" + booleans + ")" ); + } + + // Webkit/Opera - :checked should return selected option elements + // http://www.w3.org/TR/2011/REC-css3-selectors-20110929/#checked + // IE8 throws error here and will not see later tests + if ( !div.querySelectorAll(":checked").length ) { + rbuggyQSA.push(":checked"); + } + }); + + assert(function( div ) { + // Support: Windows 8 Native Apps + // The type and name attributes are restricted during .innerHTML assignment + var input = doc.createElement("input"); + input.setAttribute( "type", "hidden" ); + div.appendChild( input ).setAttribute( "name", "D" ); + + // Support: IE8 + // Enforce case-sensitivity of name attribute + if ( div.querySelectorAll("[name=d]").length ) { + rbuggyQSA.push( "name" + whitespace + "*[*^$|!~]?=" ); + } + + // FF 3.5 - :enabled/:disabled and hidden elements (hidden elements are still enabled) + // IE8 throws error here and will not see later tests + if ( !div.querySelectorAll(":enabled").length ) { + rbuggyQSA.push( ":enabled", ":disabled" ); + } + + // Opera 10-11 does not throw on post-comma invalid pseudos + div.querySelectorAll("*,:x"); + rbuggyQSA.push(",.*:"); + }); + } + + if ( (support.matchesSelector = rnative.test( (matches = docElem.webkitMatchesSelector || + docElem.mozMatchesSelector || + docElem.oMatchesSelector || + docElem.msMatchesSelector) )) ) { + + assert(function( div ) { + // Check to see if it's possible to do matchesSelector + // on a disconnected node (IE 9) + support.disconnectedMatch = matches.call( div, "div" ); + + // This should fail with an exception + // Gecko does not error, returns false instead + matches.call( div, "[s!='']:x" ); + rbuggyMatches.push( "!=", pseudos ); + }); + } + + rbuggyQSA = rbuggyQSA.length && new RegExp( rbuggyQSA.join("|") ); + rbuggyMatches = rbuggyMatches.length && new RegExp( rbuggyMatches.join("|") ); + + /* Contains + ---------------------------------------------------------------------- */ + hasCompare = rnative.test( docElem.compareDocumentPosition ); + + // Element contains another + // Purposefully does not implement inclusive descendent + // As in, an element does not contain itself + contains = hasCompare || rnative.test( docElem.contains ) ? + function( a, b ) { + var adown = a.nodeType === 9 ? a.documentElement : a, + bup = b && b.parentNode; + return a === bup || !!( bup && bup.nodeType === 1 && ( + adown.contains ? + adown.contains( bup ) : + a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16 + )); + } : + function( a, b ) { + if ( b ) { + while ( (b = b.parentNode) ) { + if ( b === a ) { + return true; + } + } + } + return false; + }; + + /* Sorting + ---------------------------------------------------------------------- */ + + // Document order sorting + sortOrder = hasCompare ? + function( a, b ) { + + // Flag for duplicate removal + if ( a === b ) { + hasDuplicate = true; + return 0; + } + + // Sort on method existence if only one input has compareDocumentPosition + var compare = !a.compareDocumentPosition - !b.compareDocumentPosition; + if ( compare ) { + return compare; + } + + // Calculate position if both inputs belong to the same document + compare = ( a.ownerDocument || a ) === ( b.ownerDocument || b ) ? + a.compareDocumentPosition( b ) : + + // Otherwise we know they are disconnected + 1; + + // Disconnected nodes + if ( compare & 1 || + (!support.sortDetached && b.compareDocumentPosition( a ) === compare) ) { + + // Choose the first element that is related to our preferred document + if ( a === doc || a.ownerDocument === preferredDoc && contains(preferredDoc, a) ) { + return -1; + } + if ( b === doc || b.ownerDocument === preferredDoc && contains(preferredDoc, b) ) { + return 1; + } + + // Maintain original order + return sortInput ? + ( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) : + 0; + } + + return compare & 4 ? -1 : 1; + } : + function( a, b ) { + // Exit early if the nodes are identical + if ( a === b ) { + hasDuplicate = true; + return 0; + } + + var cur, + i = 0, + aup = a.parentNode, + bup = b.parentNode, + ap = [ a ], + bp = [ b ]; + + // Parentless nodes are either documents or disconnected + if ( !aup || !bup ) { + return a === doc ? -1 : + b === doc ? 1 : + aup ? -1 : + bup ? 1 : + sortInput ? + ( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) : + 0; + + // If the nodes are siblings, we can do a quick check + } else if ( aup === bup ) { + return siblingCheck( a, b ); + } + + // Otherwise we need full lists of their ancestors for comparison + cur = a; + while ( (cur = cur.parentNode) ) { + ap.unshift( cur ); + } + cur = b; + while ( (cur = cur.parentNode) ) { + bp.unshift( cur ); + } + + // Walk down the tree looking for a discrepancy + while ( ap[i] === bp[i] ) { + i++; + } + + return i ? + // Do a sibling check if the nodes have a common ancestor + siblingCheck( ap[i], bp[i] ) : + + // Otherwise nodes in our document sort first + ap[i] === preferredDoc ? -1 : + bp[i] === preferredDoc ? 1 : + 0; + }; + + return doc; +}; + +Sizzle.matches = function( expr, elements ) { + return Sizzle( expr, null, null, elements ); +}; + +Sizzle.matchesSelector = function( elem, expr ) { + // Set document vars if needed + if ( ( elem.ownerDocument || elem ) !== document ) { + setDocument( elem ); + } + + // Make sure that attribute selectors are quoted + expr = expr.replace( rattributeQuotes, "='$1']" ); + + if ( support.matchesSelector && documentIsHTML && + ( !rbuggyMatches || !rbuggyMatches.test( expr ) ) && + ( !rbuggyQSA || !rbuggyQSA.test( expr ) ) ) { + + try { + var ret = matches.call( elem, expr ); + + // IE 9's matchesSelector returns false on disconnected nodes + if ( ret || support.disconnectedMatch || + // As well, disconnected nodes are said to be in a document + // fragment in IE 9 + elem.document && elem.document.nodeType !== 11 ) { + return ret; + } + } catch(e) {} + } + + return Sizzle( expr, document, null, [elem] ).length > 0; +}; + +Sizzle.contains = function( context, elem ) { + // Set document vars if needed + if ( ( context.ownerDocument || context ) !== document ) { + setDocument( context ); + } + return contains( context, elem ); +}; + +Sizzle.attr = function( elem, name ) { + // Set document vars if needed + if ( ( elem.ownerDocument || elem ) !== document ) { + setDocument( elem ); + } + + var fn = Expr.attrHandle[ name.toLowerCase() ], + // Don't get fooled by Object.prototype properties (jQuery #13807) + val = fn && hasOwn.call( Expr.attrHandle, name.toLowerCase() ) ? + fn( elem, name, !documentIsHTML ) : + undefined; + + return val !== undefined ? + val : + support.attributes || !documentIsHTML ? + elem.getAttribute( name ) : + (val = elem.getAttributeNode(name)) && val.specified ? + val.value : + null; +}; + +Sizzle.error = function( msg ) { + throw new Error( "Syntax error, unrecognized expression: " + msg ); +}; + +/** + * Document sorting and removing duplicates + * @param {ArrayLike} results + */ +Sizzle.uniqueSort = function( results ) { + var elem, + duplicates = [], + j = 0, + i = 0; + + // Unless we *know* we can detect duplicates, assume their presence + hasDuplicate = !support.detectDuplicates; + sortInput = !support.sortStable && results.slice( 0 ); + results.sort( sortOrder ); + + if ( hasDuplicate ) { + while ( (elem = results[i++]) ) { + if ( elem === results[ i ] ) { + j = duplicates.push( i ); + } + } + while ( j-- ) { + results.splice( duplicates[ j ], 1 ); + } + } + + // Clear input after sorting to release objects + // See https://github.com/jquery/sizzle/pull/225 + sortInput = null; + + return results; +}; + +/** + * Utility function for retrieving the text value of an array of DOM nodes + * @param {Array|Element} elem + */ +getText = Sizzle.getText = function( elem ) { + var node, + ret = "", + i = 0, + nodeType = elem.nodeType; + + if ( !nodeType ) { + // If no nodeType, this is expected to be an array + while ( (node = elem[i++]) ) { + // Do not traverse comment nodes + ret += getText( node ); + } + } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) { + // Use textContent for elements + // innerText usage removed for consistency of new lines (jQuery #11153) + if ( typeof elem.textContent === "string" ) { + return elem.textContent; + } else { + // Traverse its children + for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { + ret += getText( elem ); + } + } + } else if ( nodeType === 3 || nodeType === 4 ) { + return elem.nodeValue; + } + // Do not include comment or processing instruction nodes + + return ret; +}; + +Expr = Sizzle.selectors = { + + // Can be adjusted by the user + cacheLength: 50, + + createPseudo: markFunction, + + match: matchExpr, + + attrHandle: {}, + + find: {}, + + relative: { + ">": { dir: "parentNode", first: true }, + " ": { dir: "parentNode" }, + "+": { dir: "previousSibling", first: true }, + "~": { dir: "previousSibling" } + }, + + preFilter: { + "ATTR": function( match ) { + match[1] = match[1].replace( runescape, funescape ); + + // Move the given value to match[3] whether quoted or unquoted + match[3] = ( match[4] || match[5] || "" ).replace( runescape, funescape ); + + if ( match[2] === "~=" ) { + match[3] = " " + match[3] + " "; + } + + return match.slice( 0, 4 ); + }, + + "CHILD": function( match ) { + /* matches from matchExpr["CHILD"] + 1 type (only|nth|...) + 2 what (child|of-type) + 3 argument (even|odd|\d*|\d*n([+-]\d+)?|...) + 4 xn-component of xn+y argument ([+-]?\d*n|) + 5 sign of xn-component + 6 x of xn-component + 7 sign of y-component + 8 y of y-component + */ + match[1] = match[1].toLowerCase(); + + if ( match[1].slice( 0, 3 ) === "nth" ) { + // nth-* requires argument + if ( !match[3] ) { + Sizzle.error( match[0] ); + } + + // numeric x and y parameters for Expr.filter.CHILD + // remember that false/true cast respectively to 0/1 + match[4] = +( match[4] ? match[5] + (match[6] || 1) : 2 * ( match[3] === "even" || match[3] === "odd" ) ); + match[5] = +( ( match[7] + match[8] ) || match[3] === "odd" ); + + // other types prohibit arguments + } else if ( match[3] ) { + Sizzle.error( match[0] ); + } + + return match; + }, + + "PSEUDO": function( match ) { + var excess, + unquoted = !match[5] && match[2]; + + if ( matchExpr["CHILD"].test( match[0] ) ) { + return null; + } + + // Accept quoted arguments as-is + if ( match[3] && match[4] !== undefined ) { + match[2] = match[4]; + + // Strip excess characters from unquoted arguments + } else if ( unquoted && rpseudo.test( unquoted ) && + // Get excess from tokenize (recursively) + (excess = tokenize( unquoted, true )) && + // advance to the next closing parenthesis + (excess = unquoted.indexOf( ")", unquoted.length - excess ) - unquoted.length) ) { + + // excess is a negative index + match[0] = match[0].slice( 0, excess ); + match[2] = unquoted.slice( 0, excess ); + } + + // Return only captures needed by the pseudo filter method (type and argument) + return match.slice( 0, 3 ); + } + }, + + filter: { + + "TAG": function( nodeNameSelector ) { + var nodeName = nodeNameSelector.replace( runescape, funescape ).toLowerCase(); + return nodeNameSelector === "*" ? + function() { return true; } : + function( elem ) { + return elem.nodeName && elem.nodeName.toLowerCase() === nodeName; + }; + }, + + "CLASS": function( className ) { + var pattern = classCache[ className + " " ]; + + return pattern || + (pattern = new RegExp( "(^|" + whitespace + ")" + className + "(" + whitespace + "|$)" )) && + classCache( className, function( elem ) { + return pattern.test( typeof elem.className === "string" && elem.className || typeof elem.getAttribute !== strundefined && elem.getAttribute("class") || "" ); + }); + }, + + "ATTR": function( name, operator, check ) { + return function( elem ) { + var result = Sizzle.attr( elem, name ); + + if ( result == null ) { + return operator === "!="; + } + if ( !operator ) { + return true; + } + + result += ""; + + return operator === "=" ? result === check : + operator === "!=" ? result !== check : + operator === "^=" ? check && result.indexOf( check ) === 0 : + operator === "*=" ? check && result.indexOf( check ) > -1 : + operator === "$=" ? check && result.slice( -check.length ) === check : + operator === "~=" ? ( " " + result + " " ).indexOf( check ) > -1 : + operator === "|=" ? result === check || result.slice( 0, check.length + 1 ) === check + "-" : + false; + }; + }, + + "CHILD": function( type, what, argument, first, last ) { + var simple = type.slice( 0, 3 ) !== "nth", + forward = type.slice( -4 ) !== "last", + ofType = what === "of-type"; + + return first === 1 && last === 0 ? + + // Shortcut for :nth-*(n) + function( elem ) { + return !!elem.parentNode; + } : + + function( elem, context, xml ) { + var cache, outerCache, node, diff, nodeIndex, start, + dir = simple !== forward ? "nextSibling" : "previousSibling", + parent = elem.parentNode, + name = ofType && elem.nodeName.toLowerCase(), + useCache = !xml && !ofType; + + if ( parent ) { + + // :(first|last|only)-(child|of-type) + if ( simple ) { + while ( dir ) { + node = elem; + while ( (node = node[ dir ]) ) { + if ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) { + return false; + } + } + // Reverse direction for :only-* (if we haven't yet done so) + start = dir = type === "only" && !start && "nextSibling"; + } + return true; + } + + start = [ forward ? parent.firstChild : parent.lastChild ]; + + // non-xml :nth-child(...) stores cache data on `parent` + if ( forward && useCache ) { + // Seek `elem` from a previously-cached index + outerCache = parent[ expando ] || (parent[ expando ] = {}); + cache = outerCache[ type ] || []; + nodeIndex = cache[0] === dirruns && cache[1]; + diff = cache[0] === dirruns && cache[2]; + node = nodeIndex && parent.childNodes[ nodeIndex ]; + + while ( (node = ++nodeIndex && node && node[ dir ] || + + // Fallback to seeking `elem` from the start + (diff = nodeIndex = 0) || start.pop()) ) { + + // When found, cache indexes on `parent` and break + if ( node.nodeType === 1 && ++diff && node === elem ) { + outerCache[ type ] = [ dirruns, nodeIndex, diff ]; + break; + } + } + + // Use previously-cached element index if available + } else if ( useCache && (cache = (elem[ expando ] || (elem[ expando ] = {}))[ type ]) && cache[0] === dirruns ) { + diff = cache[1]; + + // xml :nth-child(...) or :nth-last-child(...) or :nth(-last)?-of-type(...) + } else { + // Use the same loop as above to seek `elem` from the start + while ( (node = ++nodeIndex && node && node[ dir ] || + (diff = nodeIndex = 0) || start.pop()) ) { + + if ( ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) && ++diff ) { + // Cache the index of each encountered element + if ( useCache ) { + (node[ expando ] || (node[ expando ] = {}))[ type ] = [ dirruns, diff ]; + } + + if ( node === elem ) { + break; + } + } + } + } + + // Incorporate the offset, then check against cycle size + diff -= last; + return diff === first || ( diff % first === 0 && diff / first >= 0 ); + } + }; + }, + + "PSEUDO": function( pseudo, argument ) { + // pseudo-class names are case-insensitive + // http://www.w3.org/TR/selectors/#pseudo-classes + // Prioritize by case sensitivity in case custom pseudos are added with uppercase letters + // Remember that setFilters inherits from pseudos + var args, + fn = Expr.pseudos[ pseudo ] || Expr.setFilters[ pseudo.toLowerCase() ] || + Sizzle.error( "unsupported pseudo: " + pseudo ); + + // The user may use createPseudo to indicate that + // arguments are needed to create the filter function + // just as Sizzle does + if ( fn[ expando ] ) { + return fn( argument ); + } + + // But maintain support for old signatures + if ( fn.length > 1 ) { + args = [ pseudo, pseudo, "", argument ]; + return Expr.setFilters.hasOwnProperty( pseudo.toLowerCase() ) ? + markFunction(function( seed, matches ) { + var idx, + matched = fn( seed, argument ), + i = matched.length; + while ( i-- ) { + idx = indexOf.call( seed, matched[i] ); + seed[ idx ] = !( matches[ idx ] = matched[i] ); + } + }) : + function( elem ) { + return fn( elem, 0, args ); + }; + } + + return fn; + } + }, + + pseudos: { + // Potentially complex pseudos + "not": markFunction(function( selector ) { + // Trim the selector passed to compile + // to avoid treating leading and trailing + // spaces as combinators + var input = [], + results = [], + matcher = compile( selector.replace( rtrim, "$1" ) ); + + return matcher[ expando ] ? + markFunction(function( seed, matches, context, xml ) { + var elem, + unmatched = matcher( seed, null, xml, [] ), + i = seed.length; + + // Match elements unmatched by `matcher` + while ( i-- ) { + if ( (elem = unmatched[i]) ) { + seed[i] = !(matches[i] = elem); + } + } + }) : + function( elem, context, xml ) { + input[0] = elem; + matcher( input, null, xml, results ); + return !results.pop(); + }; + }), + + "has": markFunction(function( selector ) { + return function( elem ) { + return Sizzle( selector, elem ).length > 0; + }; + }), + + "contains": markFunction(function( text ) { + return function( elem ) { + return ( elem.textContent || elem.innerText || getText( elem ) ).indexOf( text ) > -1; + }; + }), + + // "Whether an element is represented by a :lang() selector + // is based solely on the element's language value + // being equal to the identifier C, + // or beginning with the identifier C immediately followed by "-". + // The matching of C against the element's language value is performed case-insensitively. + // The identifier C does not have to be a valid language name." + // http://www.w3.org/TR/selectors/#lang-pseudo + "lang": markFunction( function( lang ) { + // lang value must be a valid identifier + if ( !ridentifier.test(lang || "") ) { + Sizzle.error( "unsupported lang: " + lang ); + } + lang = lang.replace( runescape, funescape ).toLowerCase(); + return function( elem ) { + var elemLang; + do { + if ( (elemLang = documentIsHTML ? + elem.lang : + elem.getAttribute("xml:lang") || elem.getAttribute("lang")) ) { + + elemLang = elemLang.toLowerCase(); + return elemLang === lang || elemLang.indexOf( lang + "-" ) === 0; + } + } while ( (elem = elem.parentNode) && elem.nodeType === 1 ); + return false; + }; + }), + + // Miscellaneous + "target": function( elem ) { + var hash = window.location && window.location.hash; + return hash && hash.slice( 1 ) === elem.id; + }, + + "root": function( elem ) { + return elem === docElem; + }, + + "focus": function( elem ) { + return elem === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(elem.type || elem.href || ~elem.tabIndex); + }, + + // Boolean properties + "enabled": function( elem ) { + return elem.disabled === false; + }, + + "disabled": function( elem ) { + return elem.disabled === true; + }, + + "checked": function( elem ) { + // In CSS3, :checked should return both checked and selected elements + // http://www.w3.org/TR/2011/REC-css3-selectors-20110929/#checked + var nodeName = elem.nodeName.toLowerCase(); + return (nodeName === "input" && !!elem.checked) || (nodeName === "option" && !!elem.selected); + }, + + "selected": function( elem ) { + // Accessing this property makes selected-by-default + // options in Safari work properly + if ( elem.parentNode ) { + elem.parentNode.selectedIndex; + } + + return elem.selected === true; + }, + + // Contents + "empty": function( elem ) { + // http://www.w3.org/TR/selectors/#empty-pseudo + // :empty is negated by element (1) or content nodes (text: 3; cdata: 4; entity ref: 5), + // but not by others (comment: 8; processing instruction: 7; etc.) + // nodeType < 6 works because attributes (2) do not appear as children + for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { + if ( elem.nodeType < 6 ) { + return false; + } + } + return true; + }, + + "parent": function( elem ) { + return !Expr.pseudos["empty"]( elem ); + }, + + // Element/input types + "header": function( elem ) { + return rheader.test( elem.nodeName ); + }, + + "input": function( elem ) { + return rinputs.test( elem.nodeName ); + }, + + "button": function( elem ) { + var name = elem.nodeName.toLowerCase(); + return name === "input" && elem.type === "button" || name === "button"; + }, + + "text": function( elem ) { + var attr; + return elem.nodeName.toLowerCase() === "input" && + elem.type === "text" && + + // Support: IE<8 + // New HTML5 attribute values (e.g., "search") appear with elem.type === "text" + ( (attr = elem.getAttribute("type")) == null || attr.toLowerCase() === "text" ); + }, + + // Position-in-collection + "first": createPositionalPseudo(function() { + return [ 0 ]; + }), + + "last": createPositionalPseudo(function( matchIndexes, length ) { + return [ length - 1 ]; + }), + + "eq": createPositionalPseudo(function( matchIndexes, length, argument ) { + return [ argument < 0 ? argument + length : argument ]; + }), + + "even": createPositionalPseudo(function( matchIndexes, length ) { + var i = 0; + for ( ; i < length; i += 2 ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "odd": createPositionalPseudo(function( matchIndexes, length ) { + var i = 1; + for ( ; i < length; i += 2 ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "lt": createPositionalPseudo(function( matchIndexes, length, argument ) { + var i = argument < 0 ? argument + length : argument; + for ( ; --i >= 0; ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "gt": createPositionalPseudo(function( matchIndexes, length, argument ) { + var i = argument < 0 ? argument + length : argument; + for ( ; ++i < length; ) { + matchIndexes.push( i ); + } + return matchIndexes; + }) + } +}; + +Expr.pseudos["nth"] = Expr.pseudos["eq"]; + +// Add button/input type pseudos +for ( i in { radio: true, checkbox: true, file: true, password: true, image: true } ) { + Expr.pseudos[ i ] = createInputPseudo( i ); +} +for ( i in { submit: true, reset: true } ) { + Expr.pseudos[ i ] = createButtonPseudo( i ); +} + +// Easy API for creating new setFilters +function setFilters() {} +setFilters.prototype = Expr.filters = Expr.pseudos; +Expr.setFilters = new setFilters(); + +function tokenize( selector, parseOnly ) { + var matched, match, tokens, type, + soFar, groups, preFilters, + cached = tokenCache[ selector + " " ]; + + if ( cached ) { + return parseOnly ? 0 : cached.slice( 0 ); + } + + soFar = selector; + groups = []; + preFilters = Expr.preFilter; + + while ( soFar ) { + + // Comma and first run + if ( !matched || (match = rcomma.exec( soFar )) ) { + if ( match ) { + // Don't consume trailing commas as valid + soFar = soFar.slice( match[0].length ) || soFar; + } + groups.push( (tokens = []) ); + } + + matched = false; + + // Combinators + if ( (match = rcombinators.exec( soFar )) ) { + matched = match.shift(); + tokens.push({ + value: matched, + // Cast descendant combinators to space + type: match[0].replace( rtrim, " " ) + }); + soFar = soFar.slice( matched.length ); + } + + // Filters + for ( type in Expr.filter ) { + if ( (match = matchExpr[ type ].exec( soFar )) && (!preFilters[ type ] || + (match = preFilters[ type ]( match ))) ) { + matched = match.shift(); + tokens.push({ + value: matched, + type: type, + matches: match + }); + soFar = soFar.slice( matched.length ); + } + } + + if ( !matched ) { + break; + } + } + + // Return the length of the invalid excess + // if we're just parsing + // Otherwise, throw an error or return tokens + return parseOnly ? + soFar.length : + soFar ? + Sizzle.error( selector ) : + // Cache the tokens + tokenCache( selector, groups ).slice( 0 ); +} + +function toSelector( tokens ) { + var i = 0, + len = tokens.length, + selector = ""; + for ( ; i < len; i++ ) { + selector += tokens[i].value; + } + return selector; +} + +function addCombinator( matcher, combinator, base ) { + var dir = combinator.dir, + checkNonElements = base && dir === "parentNode", + doneName = done++; + + return combinator.first ? + // Check against closest ancestor/preceding element + function( elem, context, xml ) { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + return matcher( elem, context, xml ); + } + } + } : + + // Check against all ancestor/preceding elements + function( elem, context, xml ) { + var oldCache, outerCache, + newCache = [ dirruns, doneName ]; + + // We can't set arbitrary data on XML nodes, so they don't benefit from dir caching + if ( xml ) { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + if ( matcher( elem, context, xml ) ) { + return true; + } + } + } + } else { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + outerCache = elem[ expando ] || (elem[ expando ] = {}); + if ( (oldCache = outerCache[ dir ]) && + oldCache[ 0 ] === dirruns && oldCache[ 1 ] === doneName ) { + + // Assign to newCache so results back-propagate to previous elements + return (newCache[ 2 ] = oldCache[ 2 ]); + } else { + // Reuse newcache so results back-propagate to previous elements + outerCache[ dir ] = newCache; + + // A match means we're done; a fail means we have to keep checking + if ( (newCache[ 2 ] = matcher( elem, context, xml )) ) { + return true; + } + } + } + } + } + }; +} + +function elementMatcher( matchers ) { + return matchers.length > 1 ? + function( elem, context, xml ) { + var i = matchers.length; + while ( i-- ) { + if ( !matchers[i]( elem, context, xml ) ) { + return false; + } + } + return true; + } : + matchers[0]; +} + +function condense( unmatched, map, filter, context, xml ) { + var elem, + newUnmatched = [], + i = 0, + len = unmatched.length, + mapped = map != null; + + for ( ; i < len; i++ ) { + if ( (elem = unmatched[i]) ) { + if ( !filter || filter( elem, context, xml ) ) { + newUnmatched.push( elem ); + if ( mapped ) { + map.push( i ); + } + } + } + } + + return newUnmatched; +} + +function setMatcher( preFilter, selector, matcher, postFilter, postFinder, postSelector ) { + if ( postFilter && !postFilter[ expando ] ) { + postFilter = setMatcher( postFilter ); + } + if ( postFinder && !postFinder[ expando ] ) { + postFinder = setMatcher( postFinder, postSelector ); + } + return markFunction(function( seed, results, context, xml ) { + var temp, i, elem, + preMap = [], + postMap = [], + preexisting = results.length, + + // Get initial elements from seed or context + elems = seed || multipleContexts( selector || "*", context.nodeType ? [ context ] : context, [] ), + + // Prefilter to get matcher input, preserving a map for seed-results synchronization + matcherIn = preFilter && ( seed || !selector ) ? + condense( elems, preMap, preFilter, context, xml ) : + elems, + + matcherOut = matcher ? + // If we have a postFinder, or filtered seed, or non-seed postFilter or preexisting results, + postFinder || ( seed ? preFilter : preexisting || postFilter ) ? + + // ...intermediate processing is necessary + [] : + + // ...otherwise use results directly + results : + matcherIn; + + // Find primary matches + if ( matcher ) { + matcher( matcherIn, matcherOut, context, xml ); + } + + // Apply postFilter + if ( postFilter ) { + temp = condense( matcherOut, postMap ); + postFilter( temp, [], context, xml ); + + // Un-match failing elements by moving them back to matcherIn + i = temp.length; + while ( i-- ) { + if ( (elem = temp[i]) ) { + matcherOut[ postMap[i] ] = !(matcherIn[ postMap[i] ] = elem); + } + } + } + + if ( seed ) { + if ( postFinder || preFilter ) { + if ( postFinder ) { + // Get the final matcherOut by condensing this intermediate into postFinder contexts + temp = []; + i = matcherOut.length; + while ( i-- ) { + if ( (elem = matcherOut[i]) ) { + // Restore matcherIn since elem is not yet a final match + temp.push( (matcherIn[i] = elem) ); + } + } + postFinder( null, (matcherOut = []), temp, xml ); + } + + // Move matched elements from seed to results to keep them synchronized + i = matcherOut.length; + while ( i-- ) { + if ( (elem = matcherOut[i]) && + (temp = postFinder ? indexOf.call( seed, elem ) : preMap[i]) > -1 ) { + + seed[temp] = !(results[temp] = elem); + } + } + } + + // Add elements to results, through postFinder if defined + } else { + matcherOut = condense( + matcherOut === results ? + matcherOut.splice( preexisting, matcherOut.length ) : + matcherOut + ); + if ( postFinder ) { + postFinder( null, results, matcherOut, xml ); + } else { + push.apply( results, matcherOut ); + } + } + }); +} + +function matcherFromTokens( tokens ) { + var checkContext, matcher, j, + len = tokens.length, + leadingRelative = Expr.relative[ tokens[0].type ], + implicitRelative = leadingRelative || Expr.relative[" "], + i = leadingRelative ? 1 : 0, + + // The foundational matcher ensures that elements are reachable from top-level context(s) + matchContext = addCombinator( function( elem ) { + return elem === checkContext; + }, implicitRelative, true ), + matchAnyContext = addCombinator( function( elem ) { + return indexOf.call( checkContext, elem ) > -1; + }, implicitRelative, true ), + matchers = [ function( elem, context, xml ) { + return ( !leadingRelative && ( xml || context !== outermostContext ) ) || ( + (checkContext = context).nodeType ? + matchContext( elem, context, xml ) : + matchAnyContext( elem, context, xml ) ); + } ]; + + for ( ; i < len; i++ ) { + if ( (matcher = Expr.relative[ tokens[i].type ]) ) { + matchers = [ addCombinator(elementMatcher( matchers ), matcher) ]; + } else { + matcher = Expr.filter[ tokens[i].type ].apply( null, tokens[i].matches ); + + // Return special upon seeing a positional matcher + if ( matcher[ expando ] ) { + // Find the next relative operator (if any) for proper handling + j = ++i; + for ( ; j < len; j++ ) { + if ( Expr.relative[ tokens[j].type ] ) { + break; + } + } + return setMatcher( + i > 1 && elementMatcher( matchers ), + i > 1 && toSelector( + // If the preceding token was a descendant combinator, insert an implicit any-element `*` + tokens.slice( 0, i - 1 ).concat({ value: tokens[ i - 2 ].type === " " ? "*" : "" }) + ).replace( rtrim, "$1" ), + matcher, + i < j && matcherFromTokens( tokens.slice( i, j ) ), + j < len && matcherFromTokens( (tokens = tokens.slice( j )) ), + j < len && toSelector( tokens ) + ); + } + matchers.push( matcher ); + } + } + + return elementMatcher( matchers ); +} + +function matcherFromGroupMatchers( elementMatchers, setMatchers ) { + var bySet = setMatchers.length > 0, + byElement = elementMatchers.length > 0, + superMatcher = function( seed, context, xml, results, outermost ) { + var elem, j, matcher, + matchedCount = 0, + i = "0", + unmatched = seed && [], + setMatched = [], + contextBackup = outermostContext, + // We must always have either seed elements or outermost context + elems = seed || byElement && Expr.find["TAG"]( "*", outermost ), + // Use integer dirruns iff this is the outermost matcher + dirrunsUnique = (dirruns += contextBackup == null ? 1 : Math.random() || 0.1), + len = elems.length; + + if ( outermost ) { + outermostContext = context !== document && context; + } + + // Add elements passing elementMatchers directly to results + // Keep `i` a string if there are no elements so `matchedCount` will be "00" below + // Support: IE<9, Safari + // Tolerate NodeList properties (IE: "length"; Safari: ) matching elements by id + for ( ; i !== len && (elem = elems[i]) != null; i++ ) { + if ( byElement && elem ) { + j = 0; + while ( (matcher = elementMatchers[j++]) ) { + if ( matcher( elem, context, xml ) ) { + results.push( elem ); + break; + } + } + if ( outermost ) { + dirruns = dirrunsUnique; + } + } + + // Track unmatched elements for set filters + if ( bySet ) { + // They will have gone through all possible matchers + if ( (elem = !matcher && elem) ) { + matchedCount--; + } + + // Lengthen the array for every element, matched or not + if ( seed ) { + unmatched.push( elem ); + } + } + } + + // Apply set filters to unmatched elements + matchedCount += i; + if ( bySet && i !== matchedCount ) { + j = 0; + while ( (matcher = setMatchers[j++]) ) { + matcher( unmatched, setMatched, context, xml ); + } + + if ( seed ) { + // Reintegrate element matches to eliminate the need for sorting + if ( matchedCount > 0 ) { + while ( i-- ) { + if ( !(unmatched[i] || setMatched[i]) ) { + setMatched[i] = pop.call( results ); + } + } + } + + // Discard index placeholder values to get only actual matches + setMatched = condense( setMatched ); + } + + // Add matches to results + push.apply( results, setMatched ); + + // Seedless set matches succeeding multiple successful matchers stipulate sorting + if ( outermost && !seed && setMatched.length > 0 && + ( matchedCount + setMatchers.length ) > 1 ) { + + Sizzle.uniqueSort( results ); + } + } + + // Override manipulation of globals by nested matchers + if ( outermost ) { + dirruns = dirrunsUnique; + outermostContext = contextBackup; + } + + return unmatched; + }; + + return bySet ? + markFunction( superMatcher ) : + superMatcher; +} + +compile = Sizzle.compile = function( selector, group /* Internal Use Only */ ) { + var i, + setMatchers = [], + elementMatchers = [], + cached = compilerCache[ selector + " " ]; + + if ( !cached ) { + // Generate a function of recursive functions that can be used to check each element + if ( !group ) { + group = tokenize( selector ); + } + i = group.length; + while ( i-- ) { + cached = matcherFromTokens( group[i] ); + if ( cached[ expando ] ) { + setMatchers.push( cached ); + } else { + elementMatchers.push( cached ); + } + } + + // Cache the compiled function + cached = compilerCache( selector, matcherFromGroupMatchers( elementMatchers, setMatchers ) ); + } + return cached; +}; + +function multipleContexts( selector, contexts, results ) { + var i = 0, + len = contexts.length; + for ( ; i < len; i++ ) { + Sizzle( selector, contexts[i], results ); + } + return results; +} + +function select( selector, context, results, seed ) { + var i, tokens, token, type, find, + match = tokenize( selector ); + + if ( !seed ) { + // Try to minimize operations if there is only one group + if ( match.length === 1 ) { + + // Take a shortcut and set the context if the root selector is an ID + tokens = match[0] = match[0].slice( 0 ); + if ( tokens.length > 2 && (token = tokens[0]).type === "ID" && + support.getById && context.nodeType === 9 && documentIsHTML && + Expr.relative[ tokens[1].type ] ) { + + context = ( Expr.find["ID"]( token.matches[0].replace(runescape, funescape), context ) || [] )[0]; + if ( !context ) { + return results; + } + selector = selector.slice( tokens.shift().value.length ); + } + + // Fetch a seed set for right-to-left matching + i = matchExpr["needsContext"].test( selector ) ? 0 : tokens.length; + while ( i-- ) { + token = tokens[i]; + + // Abort if we hit a combinator + if ( Expr.relative[ (type = token.type) ] ) { + break; + } + if ( (find = Expr.find[ type ]) ) { + // Search, expanding context for leading sibling combinators + if ( (seed = find( + token.matches[0].replace( runescape, funescape ), + rsibling.test( tokens[0].type ) && testContext( context.parentNode ) || context + )) ) { + + // If seed is empty or no tokens remain, we can return early + tokens.splice( i, 1 ); + selector = seed.length && toSelector( tokens ); + if ( !selector ) { + push.apply( results, seed ); + return results; + } + + break; + } + } + } + } + } + + // Compile and execute a filtering function + // Provide `match` to avoid retokenization if we modified the selector above + compile( selector, match )( + seed, + context, + !documentIsHTML, + results, + rsibling.test( selector ) && testContext( context.parentNode ) || context + ); + return results; +} + +// One-time assignments + +// Sort stability +support.sortStable = expando.split("").sort( sortOrder ).join("") === expando; + +// Support: Chrome<14 +// Always assume duplicates if they aren't passed to the comparison function +support.detectDuplicates = !!hasDuplicate; + +// Initialize against the default document +setDocument(); + +// Support: Webkit<537.32 - Safari 6.0.3/Chrome 25 (fixed in Chrome 27) +// Detached nodes confoundingly follow *each other* +support.sortDetached = assert(function( div1 ) { + // Should return 1, but returns 4 (following) + return div1.compareDocumentPosition( document.createElement("div") ) & 1; +}); + +// Support: IE<8 +// Prevent attribute/property "interpolation" +// http://msdn.microsoft.com/en-us/library/ms536429%28VS.85%29.aspx +if ( !assert(function( div ) { + div.innerHTML = ""; + return div.firstChild.getAttribute("href") === "#" ; +}) ) { + addHandle( "type|href|height|width", function( elem, name, isXML ) { + if ( !isXML ) { + return elem.getAttribute( name, name.toLowerCase() === "type" ? 1 : 2 ); + } + }); +} + +// Support: IE<9 +// Use defaultValue in place of getAttribute("value") +if ( !support.attributes || !assert(function( div ) { + div.innerHTML = ""; + div.firstChild.setAttribute( "value", "" ); + return div.firstChild.getAttribute( "value" ) === ""; +}) ) { + addHandle( "value", function( elem, name, isXML ) { + if ( !isXML && elem.nodeName.toLowerCase() === "input" ) { + return elem.defaultValue; + } + }); +} + +// Support: IE<9 +// Use getAttributeNode to fetch booleans when getAttribute lies +if ( !assert(function( div ) { + return div.getAttribute("disabled") == null; +}) ) { + addHandle( booleans, function( elem, name, isXML ) { + var val; + if ( !isXML ) { + return elem[ name ] === true ? name.toLowerCase() : + (val = elem.getAttributeNode( name )) && val.specified ? + val.value : + null; + } + }); +} + +return Sizzle; + +})( window ); + + + +jQuery.find = Sizzle; +jQuery.expr = Sizzle.selectors; +jQuery.expr[":"] = jQuery.expr.pseudos; +jQuery.unique = Sizzle.uniqueSort; +jQuery.text = Sizzle.getText; +jQuery.isXMLDoc = Sizzle.isXML; +jQuery.contains = Sizzle.contains; + + + +var rneedsContext = jQuery.expr.match.needsContext; + +var rsingleTag = (/^<(\w+)\s*\/?>(?:<\/\1>|)$/); + + + +var risSimple = /^.[^:#\[\.,]*$/; + +// Implement the identical functionality for filter and not +function winnow( elements, qualifier, not ) { + if ( jQuery.isFunction( qualifier ) ) { + return jQuery.grep( elements, function( elem, i ) { + /* jshint -W018 */ + return !!qualifier.call( elem, i, elem ) !== not; + }); + + } + + if ( qualifier.nodeType ) { + return jQuery.grep( elements, function( elem ) { + return ( elem === qualifier ) !== not; + }); + + } + + if ( typeof qualifier === "string" ) { + if ( risSimple.test( qualifier ) ) { + return jQuery.filter( qualifier, elements, not ); + } + + qualifier = jQuery.filter( qualifier, elements ); + } + + return jQuery.grep( elements, function( elem ) { + return ( jQuery.inArray( elem, qualifier ) >= 0 ) !== not; + }); +} + +jQuery.filter = function( expr, elems, not ) { + var elem = elems[ 0 ]; + + if ( not ) { + expr = ":not(" + expr + ")"; + } + + return elems.length === 1 && elem.nodeType === 1 ? + jQuery.find.matchesSelector( elem, expr ) ? [ elem ] : [] : + jQuery.find.matches( expr, jQuery.grep( elems, function( elem ) { + return elem.nodeType === 1; + })); +}; + +jQuery.fn.extend({ + find: function( selector ) { + var i, + ret = [], + self = this, + len = self.length; + + if ( typeof selector !== "string" ) { + return this.pushStack( jQuery( selector ).filter(function() { + for ( i = 0; i < len; i++ ) { + if ( jQuery.contains( self[ i ], this ) ) { + return true; + } + } + }) ); + } + + for ( i = 0; i < len; i++ ) { + jQuery.find( selector, self[ i ], ret ); + } + + // Needed because $( selector, context ) becomes $( context ).find( selector ) + ret = this.pushStack( len > 1 ? jQuery.unique( ret ) : ret ); + ret.selector = this.selector ? this.selector + " " + selector : selector; + return ret; + }, + filter: function( selector ) { + return this.pushStack( winnow(this, selector || [], false) ); + }, + not: function( selector ) { + return this.pushStack( winnow(this, selector || [], true) ); + }, + is: function( selector ) { + return !!winnow( + this, + + // If this is a positional/relative selector, check membership in the returned set + // so $("p:first").is("p:last") won't return true for a doc with two "p". + typeof selector === "string" && rneedsContext.test( selector ) ? + jQuery( selector ) : + selector || [], + false + ).length; + } +}); + + +// Initialize a jQuery object + + +// A central reference to the root jQuery(document) +var rootjQuery, + + // Use the correct document accordingly with window argument (sandbox) + document = window.document, + + // A simple way to check for HTML strings + // Prioritize #id over to avoid XSS via location.hash (#9521) + // Strict HTML recognition (#11290: must start with <) + rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/, + + init = jQuery.fn.init = function( selector, context ) { + var match, elem; + + // HANDLE: $(""), $(null), $(undefined), $(false) + if ( !selector ) { + return this; + } + + // Handle HTML strings + if ( typeof selector === "string" ) { + if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) { + // Assume that strings that start and end with <> are HTML and skip the regex check + match = [ null, selector, null ]; + + } else { + match = rquickExpr.exec( selector ); + } + + // Match html or make sure no context is specified for #id + if ( match && (match[1] || !context) ) { + + // HANDLE: $(html) -> $(array) + if ( match[1] ) { + context = context instanceof jQuery ? context[0] : context; + + // scripts is true for back-compat + // Intentionally let the error be thrown if parseHTML is not present + jQuery.merge( this, jQuery.parseHTML( + match[1], + context && context.nodeType ? context.ownerDocument || context : document, + true + ) ); + + // HANDLE: $(html, props) + if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) { + for ( match in context ) { + // Properties of context are called as methods if possible + if ( jQuery.isFunction( this[ match ] ) ) { + this[ match ]( context[ match ] ); + + // ...and otherwise set as attributes + } else { + this.attr( match, context[ match ] ); + } + } + } + + return this; + + // HANDLE: $(#id) + } else { + elem = document.getElementById( match[2] ); + + // Check parentNode to catch when Blackberry 4.6 returns + // nodes that are no longer in the document #6963 + if ( elem && elem.parentNode ) { + // Handle the case where IE and Opera return items + // by name instead of ID + if ( elem.id !== match[2] ) { + return rootjQuery.find( selector ); + } + + // Otherwise, we inject the element directly into the jQuery object + this.length = 1; + this[0] = elem; + } + + this.context = document; + this.selector = selector; + return this; + } + + // HANDLE: $(expr, $(...)) + } else if ( !context || context.jquery ) { + return ( context || rootjQuery ).find( selector ); + + // HANDLE: $(expr, context) + // (which is just equivalent to: $(context).find(expr) + } else { + return this.constructor( context ).find( selector ); + } + + // HANDLE: $(DOMElement) + } else if ( selector.nodeType ) { + this.context = this[0] = selector; + this.length = 1; + return this; + + // HANDLE: $(function) + // Shortcut for document ready + } else if ( jQuery.isFunction( selector ) ) { + return typeof rootjQuery.ready !== "undefined" ? + rootjQuery.ready( selector ) : + // Execute immediately if ready is not present + selector( jQuery ); + } + + if ( selector.selector !== undefined ) { + this.selector = selector.selector; + this.context = selector.context; + } + + return jQuery.makeArray( selector, this ); + }; + +// Give the init function the jQuery prototype for later instantiation +init.prototype = jQuery.fn; + +// Initialize central reference +rootjQuery = jQuery( document ); + + +var rparentsprev = /^(?:parents|prev(?:Until|All))/, + // methods guaranteed to produce a unique set when starting from a unique set + guaranteedUnique = { + children: true, + contents: true, + next: true, + prev: true + }; + +jQuery.extend({ + dir: function( elem, dir, until ) { + var matched = [], + cur = elem[ dir ]; + + while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) { + if ( cur.nodeType === 1 ) { + matched.push( cur ); + } + cur = cur[dir]; + } + return matched; + }, + + sibling: function( n, elem ) { + var r = []; + + for ( ; n; n = n.nextSibling ) { + if ( n.nodeType === 1 && n !== elem ) { + r.push( n ); + } + } + + return r; + } +}); + +jQuery.fn.extend({ + has: function( target ) { + var i, + targets = jQuery( target, this ), + len = targets.length; + + return this.filter(function() { + for ( i = 0; i < len; i++ ) { + if ( jQuery.contains( this, targets[i] ) ) { + return true; + } + } + }); + }, + + closest: function( selectors, context ) { + var cur, + i = 0, + l = this.length, + matched = [], + pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ? + jQuery( selectors, context || this.context ) : + 0; + + for ( ; i < l; i++ ) { + for ( cur = this[i]; cur && cur !== context; cur = cur.parentNode ) { + // Always skip document fragments + if ( cur.nodeType < 11 && (pos ? + pos.index(cur) > -1 : + + // Don't pass non-elements to Sizzle + cur.nodeType === 1 && + jQuery.find.matchesSelector(cur, selectors)) ) { + + matched.push( cur ); + break; + } + } + } + + return this.pushStack( matched.length > 1 ? jQuery.unique( matched ) : matched ); + }, + + // Determine the position of an element within + // the matched set of elements + index: function( elem ) { + + // No argument, return index in parent + if ( !elem ) { + return ( this[0] && this[0].parentNode ) ? this.first().prevAll().length : -1; + } + + // index in selector + if ( typeof elem === "string" ) { + return jQuery.inArray( this[0], jQuery( elem ) ); + } + + // Locate the position of the desired element + return jQuery.inArray( + // If it receives a jQuery object, the first element is used + elem.jquery ? elem[0] : elem, this ); + }, + + add: function( selector, context ) { + return this.pushStack( + jQuery.unique( + jQuery.merge( this.get(), jQuery( selector, context ) ) + ) + ); + }, + + addBack: function( selector ) { + return this.add( selector == null ? + this.prevObject : this.prevObject.filter(selector) + ); + } +}); + +function sibling( cur, dir ) { + do { + cur = cur[ dir ]; + } while ( cur && cur.nodeType !== 1 ); + + return cur; +} + +jQuery.each({ + parent: function( elem ) { + var parent = elem.parentNode; + return parent && parent.nodeType !== 11 ? parent : null; + }, + parents: function( elem ) { + return jQuery.dir( elem, "parentNode" ); + }, + parentsUntil: function( elem, i, until ) { + return jQuery.dir( elem, "parentNode", until ); + }, + next: function( elem ) { + return sibling( elem, "nextSibling" ); + }, + prev: function( elem ) { + return sibling( elem, "previousSibling" ); + }, + nextAll: function( elem ) { + return jQuery.dir( elem, "nextSibling" ); + }, + prevAll: function( elem ) { + return jQuery.dir( elem, "previousSibling" ); + }, + nextUntil: function( elem, i, until ) { + return jQuery.dir( elem, "nextSibling", until ); + }, + prevUntil: function( elem, i, until ) { + return jQuery.dir( elem, "previousSibling", until ); + }, + siblings: function( elem ) { + return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem ); + }, + children: function( elem ) { + return jQuery.sibling( elem.firstChild ); + }, + contents: function( elem ) { + return jQuery.nodeName( elem, "iframe" ) ? + elem.contentDocument || elem.contentWindow.document : + jQuery.merge( [], elem.childNodes ); + } +}, function( name, fn ) { + jQuery.fn[ name ] = function( until, selector ) { + var ret = jQuery.map( this, fn, until ); + + if ( name.slice( -5 ) !== "Until" ) { + selector = until; + } + + if ( selector && typeof selector === "string" ) { + ret = jQuery.filter( selector, ret ); + } + + if ( this.length > 1 ) { + // Remove duplicates + if ( !guaranteedUnique[ name ] ) { + ret = jQuery.unique( ret ); + } + + // Reverse order for parents* and prev-derivatives + if ( rparentsprev.test( name ) ) { + ret = ret.reverse(); + } + } + + return this.pushStack( ret ); + }; +}); +var rnotwhite = (/\S+/g); + + + +// String to Object options format cache +var optionsCache = {}; + +// Convert String-formatted options into Object-formatted ones and store in cache +function createOptions( options ) { + var object = optionsCache[ options ] = {}; + jQuery.each( options.match( rnotwhite ) || [], function( _, flag ) { + object[ flag ] = true; + }); + return object; +} + +/* + * Create a callback list using the following parameters: + * + * options: an optional list of space-separated options that will change how + * the callback list behaves or a more traditional option object + * + * By default a callback list will act like an event callback list and can be + * "fired" multiple times. + * + * Possible options: + * + * once: will ensure the callback list can only be fired once (like a Deferred) + * + * memory: will keep track of previous values and will call any callback added + * after the list has been fired right away with the latest "memorized" + * values (like a Deferred) + * + * unique: will ensure a callback can only be added once (no duplicate in the list) + * + * stopOnFalse: interrupt callings when a callback returns false + * + */ +jQuery.Callbacks = function( options ) { + + // Convert options from String-formatted to Object-formatted if needed + // (we check in cache first) + options = typeof options === "string" ? + ( optionsCache[ options ] || createOptions( options ) ) : + jQuery.extend( {}, options ); + + var // Flag to know if list is currently firing + firing, + // Last fire value (for non-forgettable lists) + memory, + // Flag to know if list was already fired + fired, + // End of the loop when firing + firingLength, + // Index of currently firing callback (modified by remove if needed) + firingIndex, + // First callback to fire (used internally by add and fireWith) + firingStart, + // Actual callback list + list = [], + // Stack of fire calls for repeatable lists + stack = !options.once && [], + // Fire callbacks + fire = function( data ) { + memory = options.memory && data; + fired = true; + firingIndex = firingStart || 0; + firingStart = 0; + firingLength = list.length; + firing = true; + for ( ; list && firingIndex < firingLength; firingIndex++ ) { + if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) { + memory = false; // To prevent further calls using add + break; + } + } + firing = false; + if ( list ) { + if ( stack ) { + if ( stack.length ) { + fire( stack.shift() ); + } + } else if ( memory ) { + list = []; + } else { + self.disable(); + } + } + }, + // Actual Callbacks object + self = { + // Add a callback or a collection of callbacks to the list + add: function() { + if ( list ) { + // First, we save the current length + var start = list.length; + (function add( args ) { + jQuery.each( args, function( _, arg ) { + var type = jQuery.type( arg ); + if ( type === "function" ) { + if ( !options.unique || !self.has( arg ) ) { + list.push( arg ); + } + } else if ( arg && arg.length && type !== "string" ) { + // Inspect recursively + add( arg ); + } + }); + })( arguments ); + // Do we need to add the callbacks to the + // current firing batch? + if ( firing ) { + firingLength = list.length; + // With memory, if we're not firing then + // we should call right away + } else if ( memory ) { + firingStart = start; + fire( memory ); + } + } + return this; + }, + // Remove a callback from the list + remove: function() { + if ( list ) { + jQuery.each( arguments, function( _, arg ) { + var index; + while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) { + list.splice( index, 1 ); + // Handle firing indexes + if ( firing ) { + if ( index <= firingLength ) { + firingLength--; + } + if ( index <= firingIndex ) { + firingIndex--; + } + } + } + }); + } + return this; + }, + // Check if a given callback is in the list. + // If no argument is given, return whether or not list has callbacks attached. + has: function( fn ) { + return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length ); + }, + // Remove all callbacks from the list + empty: function() { + list = []; + firingLength = 0; + return this; + }, + // Have the list do nothing anymore + disable: function() { + list = stack = memory = undefined; + return this; + }, + // Is it disabled? + disabled: function() { + return !list; + }, + // Lock the list in its current state + lock: function() { + stack = undefined; + if ( !memory ) { + self.disable(); + } + return this; + }, + // Is it locked? + locked: function() { + return !stack; + }, + // Call all callbacks with the given context and arguments + fireWith: function( context, args ) { + if ( list && ( !fired || stack ) ) { + args = args || []; + args = [ context, args.slice ? args.slice() : args ]; + if ( firing ) { + stack.push( args ); + } else { + fire( args ); + } + } + return this; + }, + // Call all the callbacks with the given arguments + fire: function() { + self.fireWith( this, arguments ); + return this; + }, + // To know if the callbacks have already been called at least once + fired: function() { + return !!fired; + } + }; + + return self; +}; + + +jQuery.extend({ + + Deferred: function( func ) { + var tuples = [ + // action, add listener, listener list, final state + [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ], + [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ], + [ "notify", "progress", jQuery.Callbacks("memory") ] + ], + state = "pending", + promise = { + state: function() { + return state; + }, + always: function() { + deferred.done( arguments ).fail( arguments ); + return this; + }, + then: function( /* fnDone, fnFail, fnProgress */ ) { + var fns = arguments; + return jQuery.Deferred(function( newDefer ) { + jQuery.each( tuples, function( i, tuple ) { + var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ]; + // deferred[ done | fail | progress ] for forwarding actions to newDefer + deferred[ tuple[1] ](function() { + var returned = fn && fn.apply( this, arguments ); + if ( returned && jQuery.isFunction( returned.promise ) ) { + returned.promise() + .done( newDefer.resolve ) + .fail( newDefer.reject ) + .progress( newDefer.notify ); + } else { + newDefer[ tuple[ 0 ] + "With" ]( this === promise ? newDefer.promise() : this, fn ? [ returned ] : arguments ); + } + }); + }); + fns = null; + }).promise(); + }, + // Get a promise for this deferred + // If obj is provided, the promise aspect is added to the object + promise: function( obj ) { + return obj != null ? jQuery.extend( obj, promise ) : promise; + } + }, + deferred = {}; + + // Keep pipe for back-compat + promise.pipe = promise.then; + + // Add list-specific methods + jQuery.each( tuples, function( i, tuple ) { + var list = tuple[ 2 ], + stateString = tuple[ 3 ]; + + // promise[ done | fail | progress ] = list.add + promise[ tuple[1] ] = list.add; + + // Handle state + if ( stateString ) { + list.add(function() { + // state = [ resolved | rejected ] + state = stateString; + + // [ reject_list | resolve_list ].disable; progress_list.lock + }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock ); + } + + // deferred[ resolve | reject | notify ] + deferred[ tuple[0] ] = function() { + deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments ); + return this; + }; + deferred[ tuple[0] + "With" ] = list.fireWith; + }); + + // Make the deferred a promise + promise.promise( deferred ); + + // Call given func if any + if ( func ) { + func.call( deferred, deferred ); + } + + // All done! + return deferred; + }, + + // Deferred helper + when: function( subordinate /* , ..., subordinateN */ ) { + var i = 0, + resolveValues = slice.call( arguments ), + length = resolveValues.length, + + // the count of uncompleted subordinates + remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0, + + // the master Deferred. If resolveValues consist of only a single Deferred, just use that. + deferred = remaining === 1 ? subordinate : jQuery.Deferred(), + + // Update function for both resolve and progress values + updateFunc = function( i, contexts, values ) { + return function( value ) { + contexts[ i ] = this; + values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value; + if ( values === progressValues ) { + deferred.notifyWith( contexts, values ); + + } else if ( !(--remaining) ) { + deferred.resolveWith( contexts, values ); + } + }; + }, + + progressValues, progressContexts, resolveContexts; + + // add listeners to Deferred subordinates; treat others as resolved + if ( length > 1 ) { + progressValues = new Array( length ); + progressContexts = new Array( length ); + resolveContexts = new Array( length ); + for ( ; i < length; i++ ) { + if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) { + resolveValues[ i ].promise() + .done( updateFunc( i, resolveContexts, resolveValues ) ) + .fail( deferred.reject ) + .progress( updateFunc( i, progressContexts, progressValues ) ); + } else { + --remaining; + } + } + } + + // if we're not waiting on anything, resolve the master + if ( !remaining ) { + deferred.resolveWith( resolveContexts, resolveValues ); + } + + return deferred.promise(); + } +}); + + +// The deferred used on DOM ready +var readyList; + +jQuery.fn.ready = function( fn ) { + // Add the callback + jQuery.ready.promise().done( fn ); + + return this; +}; + +jQuery.extend({ + // Is the DOM ready to be used? Set to true once it occurs. + isReady: false, + + // A counter to track how many items to wait for before + // the ready event fires. See #6781 + readyWait: 1, + + // Hold (or release) the ready event + holdReady: function( hold ) { + if ( hold ) { + jQuery.readyWait++; + } else { + jQuery.ready( true ); + } + }, + + // Handle when the DOM is ready + ready: function( wait ) { + + // Abort if there are pending holds or we're already ready + if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) { + return; + } + + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). + if ( !document.body ) { + return setTimeout( jQuery.ready ); + } + + // Remember that the DOM is ready + jQuery.isReady = true; + + // If a normal DOM Ready event fired, decrement, and wait if need be + if ( wait !== true && --jQuery.readyWait > 0 ) { + return; + } + + // If there are functions bound, to execute + readyList.resolveWith( document, [ jQuery ] ); + + // Trigger any bound ready events + if ( jQuery.fn.trigger ) { + jQuery( document ).trigger("ready").off("ready"); + } + } +}); + +/** + * Clean-up method for dom ready events + */ +function detach() { + if ( document.addEventListener ) { + document.removeEventListener( "DOMContentLoaded", completed, false ); + window.removeEventListener( "load", completed, false ); + + } else { + document.detachEvent( "onreadystatechange", completed ); + window.detachEvent( "onload", completed ); + } +} + +/** + * The ready event handler and self cleanup method + */ +function completed() { + // readyState === "complete" is good enough for us to call the dom ready in oldIE + if ( document.addEventListener || event.type === "load" || document.readyState === "complete" ) { + detach(); + jQuery.ready(); + } +} + +jQuery.ready.promise = function( obj ) { + if ( !readyList ) { + + readyList = jQuery.Deferred(); + + // Catch cases where $(document).ready() is called after the browser event has already occurred. + // we once tried to use readyState "interactive" here, but it caused issues like the one + // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 + if ( document.readyState === "complete" ) { + // Handle it asynchronously to allow scripts the opportunity to delay ready + setTimeout( jQuery.ready ); + + // Standards-based browsers support DOMContentLoaded + } else if ( document.addEventListener ) { + // Use the handy event callback + document.addEventListener( "DOMContentLoaded", completed, false ); + + // A fallback to window.onload, that will always work + window.addEventListener( "load", completed, false ); + + // If IE event model is used + } else { + // Ensure firing before onload, maybe late but safe also for iframes + document.attachEvent( "onreadystatechange", completed ); + + // A fallback to window.onload, that will always work + window.attachEvent( "onload", completed ); + + // If IE and not a frame + // continually check to see if the document is ready + var top = false; + + try { + top = window.frameElement == null && document.documentElement; + } catch(e) {} + + if ( top && top.doScroll ) { + (function doScrollCheck() { + if ( !jQuery.isReady ) { + + try { + // Use the trick by Diego Perini + // http://javascript.nwbox.com/IEContentLoaded/ + top.doScroll("left"); + } catch(e) { + return setTimeout( doScrollCheck, 50 ); + } + + // detach all dom ready events + detach(); + + // and execute any waiting functions + jQuery.ready(); + } + })(); + } + } + } + return readyList.promise( obj ); +}; + + +var strundefined = typeof undefined; + + + +// Support: IE<9 +// Iteration over object's inherited properties before its own +var i; +for ( i in jQuery( support ) ) { + break; +} +support.ownLast = i !== "0"; + +// Note: most support tests are defined in their respective modules. +// false until the test is run +support.inlineBlockNeedsLayout = false; + +jQuery(function() { + // We need to execute this one support test ASAP because we need to know + // if body.style.zoom needs to be set. + + var container, div, + body = document.getElementsByTagName("body")[0]; + + if ( !body ) { + // Return for frameset docs that don't have a body + return; + } + + // Setup + container = document.createElement( "div" ); + container.style.cssText = "border:0;width:0;height:0;position:absolute;top:0;left:-9999px;margin-top:1px"; + + div = document.createElement( "div" ); + body.appendChild( container ).appendChild( div ); + + if ( typeof div.style.zoom !== strundefined ) { + // Support: IE<8 + // Check if natively block-level elements act like inline-block + // elements when setting their display to 'inline' and giving + // them layout + div.style.cssText = "border:0;margin:0;width:1px;padding:1px;display:inline;zoom:1"; + + if ( (support.inlineBlockNeedsLayout = ( div.offsetWidth === 3 )) ) { + // Prevent IE 6 from affecting layout for positioned elements #11048 + // Prevent IE from shrinking the body in IE 7 mode #12869 + // Support: IE<8 + body.style.zoom = 1; + } + } + + body.removeChild( container ); + + // Null elements to avoid leaks in IE + container = div = null; +}); + + + + +(function() { + var div = document.createElement( "div" ); + + // Execute the test only if not already executed in another module. + if (support.deleteExpando == null) { + // Support: IE<9 + support.deleteExpando = true; + try { + delete div.test; + } catch( e ) { + support.deleteExpando = false; + } + } + + // Null elements to avoid leaks in IE. + div = null; +})(); + + +/** + * Determines whether an object can have data + */ +jQuery.acceptData = function( elem ) { + var noData = jQuery.noData[ (elem.nodeName + " ").toLowerCase() ], + nodeType = +elem.nodeType || 1; + + // Do not set data on non-element DOM nodes because it will not be cleared (#8335). + return nodeType !== 1 && nodeType !== 9 ? + false : + + // Nodes accept data unless otherwise specified; rejection can be conditional + !noData || noData !== true && elem.getAttribute("classid") === noData; +}; + + +var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/, + rmultiDash = /([A-Z])/g; + +function dataAttr( elem, key, data ) { + // If nothing was found internally, try to fetch any + // data from the HTML5 data-* attribute + if ( data === undefined && elem.nodeType === 1 ) { + + var name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase(); + + data = elem.getAttribute( name ); + + if ( typeof data === "string" ) { + try { + data = data === "true" ? true : + data === "false" ? false : + data === "null" ? null : + // Only convert to a number if it doesn't change the string + +data + "" === data ? +data : + rbrace.test( data ) ? jQuery.parseJSON( data ) : + data; + } catch( e ) {} + + // Make sure we set the data so it isn't changed later + jQuery.data( elem, key, data ); + + } else { + data = undefined; + } + } + + return data; +} + +// checks a cache object for emptiness +function isEmptyDataObject( obj ) { + var name; + for ( name in obj ) { + + // if the public data object is empty, the private is still empty + if ( name === "data" && jQuery.isEmptyObject( obj[name] ) ) { + continue; + } + if ( name !== "toJSON" ) { + return false; + } + } + + return true; +} + +function internalData( elem, name, data, pvt /* Internal Use Only */ ) { + if ( !jQuery.acceptData( elem ) ) { + return; + } + + var ret, thisCache, + internalKey = jQuery.expando, + + // We have to handle DOM nodes and JS objects differently because IE6-7 + // can't GC object references properly across the DOM-JS boundary + isNode = elem.nodeType, + + // Only DOM nodes need the global jQuery cache; JS object data is + // attached directly to the object so GC can occur automatically + cache = isNode ? jQuery.cache : elem, + + // Only defining an ID for JS objects if its cache already exists allows + // the code to shortcut on the same path as a DOM node with no cache + id = isNode ? elem[ internalKey ] : elem[ internalKey ] && internalKey; + + // Avoid doing any more work than we need to when trying to get data on an + // object that has no data at all + if ( (!id || !cache[id] || (!pvt && !cache[id].data)) && data === undefined && typeof name === "string" ) { + return; + } + + if ( !id ) { + // Only DOM nodes need a new unique ID for each element since their data + // ends up in the global cache + if ( isNode ) { + id = elem[ internalKey ] = deletedIds.pop() || jQuery.guid++; + } else { + id = internalKey; + } + } + + if ( !cache[ id ] ) { + // Avoid exposing jQuery metadata on plain JS objects when the object + // is serialized using JSON.stringify + cache[ id ] = isNode ? {} : { toJSON: jQuery.noop }; + } + + // An object can be passed to jQuery.data instead of a key/value pair; this gets + // shallow copied over onto the existing cache + if ( typeof name === "object" || typeof name === "function" ) { + if ( pvt ) { + cache[ id ] = jQuery.extend( cache[ id ], name ); + } else { + cache[ id ].data = jQuery.extend( cache[ id ].data, name ); + } + } + + thisCache = cache[ id ]; + + // jQuery data() is stored in a separate object inside the object's internal data + // cache in order to avoid key collisions between internal data and user-defined + // data. + if ( !pvt ) { + if ( !thisCache.data ) { + thisCache.data = {}; + } + + thisCache = thisCache.data; + } + + if ( data !== undefined ) { + thisCache[ jQuery.camelCase( name ) ] = data; + } + + // Check for both converted-to-camel and non-converted data property names + // If a data property was specified + if ( typeof name === "string" ) { + + // First Try to find as-is property data + ret = thisCache[ name ]; + + // Test for null|undefined property data + if ( ret == null ) { + + // Try to find the camelCased property + ret = thisCache[ jQuery.camelCase( name ) ]; + } + } else { + ret = thisCache; + } + + return ret; +} + +function internalRemoveData( elem, name, pvt ) { + if ( !jQuery.acceptData( elem ) ) { + return; + } + + var thisCache, i, + isNode = elem.nodeType, + + // See jQuery.data for more information + cache = isNode ? jQuery.cache : elem, + id = isNode ? elem[ jQuery.expando ] : jQuery.expando; + + // If there is already no cache entry for this object, there is no + // purpose in continuing + if ( !cache[ id ] ) { + return; + } + + if ( name ) { + + thisCache = pvt ? cache[ id ] : cache[ id ].data; + + if ( thisCache ) { + + // Support array or space separated string names for data keys + if ( !jQuery.isArray( name ) ) { + + // try the string as a key before any manipulation + if ( name in thisCache ) { + name = [ name ]; + } else { + + // split the camel cased version by spaces unless a key with the spaces exists + name = jQuery.camelCase( name ); + if ( name in thisCache ) { + name = [ name ]; + } else { + name = name.split(" "); + } + } + } else { + // If "name" is an array of keys... + // When data is initially created, via ("key", "val") signature, + // keys will be converted to camelCase. + // Since there is no way to tell _how_ a key was added, remove + // both plain key and camelCase key. #12786 + // This will only penalize the array argument path. + name = name.concat( jQuery.map( name, jQuery.camelCase ) ); + } + + i = name.length; + while ( i-- ) { + delete thisCache[ name[i] ]; + } + + // If there is no data left in the cache, we want to continue + // and let the cache object itself get destroyed + if ( pvt ? !isEmptyDataObject(thisCache) : !jQuery.isEmptyObject(thisCache) ) { + return; + } + } + } + + // See jQuery.data for more information + if ( !pvt ) { + delete cache[ id ].data; + + // Don't destroy the parent cache unless the internal data object + // had been the only thing left in it + if ( !isEmptyDataObject( cache[ id ] ) ) { + return; + } + } + + // Destroy the cache + if ( isNode ) { + jQuery.cleanData( [ elem ], true ); + + // Use delete when supported for expandos or `cache` is not a window per isWindow (#10080) + /* jshint eqeqeq: false */ + } else if ( support.deleteExpando || cache != cache.window ) { + /* jshint eqeqeq: true */ + delete cache[ id ]; + + // When all else fails, null + } else { + cache[ id ] = null; + } +} + +jQuery.extend({ + cache: {}, + + // The following elements (space-suffixed to avoid Object.prototype collisions) + // throw uncatchable exceptions if you attempt to set expando properties + noData: { + "applet ": true, + "embed ": true, + // ...but Flash objects (which have this classid) *can* handle expandos + "object ": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" + }, + + hasData: function( elem ) { + elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ]; + return !!elem && !isEmptyDataObject( elem ); + }, + + data: function( elem, name, data ) { + return internalData( elem, name, data ); + }, + + removeData: function( elem, name ) { + return internalRemoveData( elem, name ); + }, + + // For internal use only. + _data: function( elem, name, data ) { + return internalData( elem, name, data, true ); + }, + + _removeData: function( elem, name ) { + return internalRemoveData( elem, name, true ); + } +}); + +jQuery.fn.extend({ + data: function( key, value ) { + var i, name, data, + elem = this[0], + attrs = elem && elem.attributes; + + // Special expections of .data basically thwart jQuery.access, + // so implement the relevant behavior ourselves + + // Gets all values + if ( key === undefined ) { + if ( this.length ) { + data = jQuery.data( elem ); + + if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) { + i = attrs.length; + while ( i-- ) { + name = attrs[i].name; + + if ( name.indexOf("data-") === 0 ) { + name = jQuery.camelCase( name.slice(5) ); + + dataAttr( elem, name, data[ name ] ); + } + } + jQuery._data( elem, "parsedAttrs", true ); + } + } + + return data; + } + + // Sets multiple values + if ( typeof key === "object" ) { + return this.each(function() { + jQuery.data( this, key ); + }); + } + + return arguments.length > 1 ? + + // Sets one value + this.each(function() { + jQuery.data( this, key, value ); + }) : + + // Gets one value + // Try to fetch any internally stored data first + elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : undefined; + }, + + removeData: function( key ) { + return this.each(function() { + jQuery.removeData( this, key ); + }); + } +}); + + +jQuery.extend({ + queue: function( elem, type, data ) { + var queue; + + if ( elem ) { + type = ( type || "fx" ) + "queue"; + queue = jQuery._data( elem, type ); + + // Speed up dequeue by getting out quickly if this is just a lookup + if ( data ) { + if ( !queue || jQuery.isArray(data) ) { + queue = jQuery._data( elem, type, jQuery.makeArray(data) ); + } else { + queue.push( data ); + } + } + return queue || []; + } + }, + + dequeue: function( elem, type ) { + type = type || "fx"; + + var queue = jQuery.queue( elem, type ), + startLength = queue.length, + fn = queue.shift(), + hooks = jQuery._queueHooks( elem, type ), + next = function() { + jQuery.dequeue( elem, type ); + }; + + // If the fx queue is dequeued, always remove the progress sentinel + if ( fn === "inprogress" ) { + fn = queue.shift(); + startLength--; + } + + if ( fn ) { + + // Add a progress sentinel to prevent the fx queue from being + // automatically dequeued + if ( type === "fx" ) { + queue.unshift( "inprogress" ); + } + + // clear up the last queue stop function + delete hooks.stop; + fn.call( elem, next, hooks ); + } + + if ( !startLength && hooks ) { + hooks.empty.fire(); + } + }, + + // not intended for public consumption - generates a queueHooks object, or returns the current one + _queueHooks: function( elem, type ) { + var key = type + "queueHooks"; + return jQuery._data( elem, key ) || jQuery._data( elem, key, { + empty: jQuery.Callbacks("once memory").add(function() { + jQuery._removeData( elem, type + "queue" ); + jQuery._removeData( elem, key ); + }) + }); + } +}); + +jQuery.fn.extend({ + queue: function( type, data ) { + var setter = 2; + + if ( typeof type !== "string" ) { + data = type; + type = "fx"; + setter--; + } + + if ( arguments.length < setter ) { + return jQuery.queue( this[0], type ); + } + + return data === undefined ? + this : + this.each(function() { + var queue = jQuery.queue( this, type, data ); + + // ensure a hooks for this queue + jQuery._queueHooks( this, type ); + + if ( type === "fx" && queue[0] !== "inprogress" ) { + jQuery.dequeue( this, type ); + } + }); + }, + dequeue: function( type ) { + return this.each(function() { + jQuery.dequeue( this, type ); + }); + }, + clearQueue: function( type ) { + return this.queue( type || "fx", [] ); + }, + // Get a promise resolved when queues of a certain type + // are emptied (fx is the type by default) + promise: function( type, obj ) { + var tmp, + count = 1, + defer = jQuery.Deferred(), + elements = this, + i = this.length, + resolve = function() { + if ( !( --count ) ) { + defer.resolveWith( elements, [ elements ] ); + } + }; + + if ( typeof type !== "string" ) { + obj = type; + type = undefined; + } + type = type || "fx"; + + while ( i-- ) { + tmp = jQuery._data( elements[ i ], type + "queueHooks" ); + if ( tmp && tmp.empty ) { + count++; + tmp.empty.add( resolve ); + } + } + resolve(); + return defer.promise( obj ); + } +}); +var pnum = (/[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/).source; + +var cssExpand = [ "Top", "Right", "Bottom", "Left" ]; + +var isHidden = function( elem, el ) { + // isHidden might be called from jQuery#filter function; + // in that case, element will be second argument + elem = el || elem; + return jQuery.css( elem, "display" ) === "none" || !jQuery.contains( elem.ownerDocument, elem ); + }; + + + +// Multifunctional method to get and set values of a collection +// The value/s can optionally be executed if it's a function +var access = jQuery.access = function( elems, fn, key, value, chainable, emptyGet, raw ) { + var i = 0, + length = elems.length, + bulk = key == null; + + // Sets many values + if ( jQuery.type( key ) === "object" ) { + chainable = true; + for ( i in key ) { + jQuery.access( elems, fn, i, key[i], true, emptyGet, raw ); + } + + // Sets one value + } else if ( value !== undefined ) { + chainable = true; + + if ( !jQuery.isFunction( value ) ) { + raw = true; + } + + if ( bulk ) { + // Bulk operations run against the entire set + if ( raw ) { + fn.call( elems, value ); + fn = null; + + // ...except when executing function values + } else { + bulk = fn; + fn = function( elem, key, value ) { + return bulk.call( jQuery( elem ), value ); + }; + } + } + + if ( fn ) { + for ( ; i < length; i++ ) { + fn( elems[i], key, raw ? value : value.call( elems[i], i, fn( elems[i], key ) ) ); + } + } + } + + return chainable ? + elems : + + // Gets + bulk ? + fn.call( elems ) : + length ? fn( elems[0], key ) : emptyGet; +}; +var rcheckableType = (/^(?:checkbox|radio)$/i); + + + +(function() { + var fragment = document.createDocumentFragment(), + div = document.createElement("div"), + input = document.createElement("input"); + + // Setup + div.setAttribute( "className", "t" ); + div.innerHTML = "
a"; + + // IE strips leading whitespace when .innerHTML is used + support.leadingWhitespace = div.firstChild.nodeType === 3; + + // Make sure that tbody elements aren't automatically inserted + // IE will insert them into empty tables + support.tbody = !div.getElementsByTagName( "tbody" ).length; + + // Make sure that link elements get serialized correctly by innerHTML + // This requires a wrapper element in IE + support.htmlSerialize = !!div.getElementsByTagName( "link" ).length; + + // Makes sure cloning an html5 element does not cause problems + // Where outerHTML is undefined, this still works + support.html5Clone = + document.createElement( "nav" ).cloneNode( true ).outerHTML !== "<:nav>"; + + // Check if a disconnected checkbox will retain its checked + // value of true after appended to the DOM (IE6/7) + input.type = "checkbox"; + input.checked = true; + fragment.appendChild( input ); + support.appendChecked = input.checked; + + // Make sure textarea (and checkbox) defaultValue is properly cloned + // Support: IE6-IE11+ + div.innerHTML = ""; + support.noCloneChecked = !!div.cloneNode( true ).lastChild.defaultValue; + + // #11217 - WebKit loses check when the name is after the checked attribute + fragment.appendChild( div ); + div.innerHTML = ""; + + // Support: Safari 5.1, iOS 5.1, Android 4.x, Android 2.3 + // old WebKit doesn't clone checked state correctly in fragments + support.checkClone = div.cloneNode( true ).cloneNode( true ).lastChild.checked; + + // Support: IE<9 + // Opera does not clone events (and typeof div.attachEvent === undefined). + // IE9-10 clones events bound via attachEvent, but they don't trigger with .click() + support.noCloneEvent = true; + if ( div.attachEvent ) { + div.attachEvent( "onclick", function() { + support.noCloneEvent = false; + }); + + div.cloneNode( true ).click(); + } + + // Execute the test only if not already executed in another module. + if (support.deleteExpando == null) { + // Support: IE<9 + support.deleteExpando = true; + try { + delete div.test; + } catch( e ) { + support.deleteExpando = false; + } + } + + // Null elements to avoid leaks in IE. + fragment = div = input = null; +})(); + + +(function() { + var i, eventName, + div = document.createElement( "div" ); + + // Support: IE<9 (lack submit/change bubble), Firefox 23+ (lack focusin event) + for ( i in { submit: true, change: true, focusin: true }) { + eventName = "on" + i; + + if ( !(support[ i + "Bubbles" ] = eventName in window) ) { + // Beware of CSP restrictions (https://developer.mozilla.org/en/Security/CSP) + div.setAttribute( eventName, "t" ); + support[ i + "Bubbles" ] = div.attributes[ eventName ].expando === false; + } + } + + // Null elements to avoid leaks in IE. + div = null; +})(); + + +var rformElems = /^(?:input|select|textarea)$/i, + rkeyEvent = /^key/, + rmouseEvent = /^(?:mouse|contextmenu)|click/, + rfocusMorph = /^(?:focusinfocus|focusoutblur)$/, + rtypenamespace = /^([^.]*)(?:\.(.+)|)$/; + +function returnTrue() { + return true; +} + +function returnFalse() { + return false; +} + +function safeActiveElement() { + try { + return document.activeElement; + } catch ( err ) { } +} + +/* + * Helper functions for managing events -- not part of the public interface. + * Props to Dean Edwards' addEvent library for many of the ideas. + */ +jQuery.event = { + + global: {}, + + add: function( elem, types, handler, data, selector ) { + var tmp, events, t, handleObjIn, + special, eventHandle, handleObj, + handlers, type, namespaces, origType, + elemData = jQuery._data( elem ); + + // Don't attach events to noData or text/comment nodes (but allow plain objects) + if ( !elemData ) { + return; + } + + // Caller can pass in an object of custom data in lieu of the handler + if ( handler.handler ) { + handleObjIn = handler; + handler = handleObjIn.handler; + selector = handleObjIn.selector; + } + + // Make sure that the handler has a unique ID, used to find/remove it later + if ( !handler.guid ) { + handler.guid = jQuery.guid++; + } + + // Init the element's event structure and main handler, if this is the first + if ( !(events = elemData.events) ) { + events = elemData.events = {}; + } + if ( !(eventHandle = elemData.handle) ) { + eventHandle = elemData.handle = function( e ) { + // Discard the second event of a jQuery.event.trigger() and + // when an event is called after a page has unloaded + return typeof jQuery !== strundefined && (!e || jQuery.event.triggered !== e.type) ? + jQuery.event.dispatch.apply( eventHandle.elem, arguments ) : + undefined; + }; + // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events + eventHandle.elem = elem; + } + + // Handle multiple events separated by a space + types = ( types || "" ).match( rnotwhite ) || [ "" ]; + t = types.length; + while ( t-- ) { + tmp = rtypenamespace.exec( types[t] ) || []; + type = origType = tmp[1]; + namespaces = ( tmp[2] || "" ).split( "." ).sort(); + + // There *must* be a type, no attaching namespace-only handlers + if ( !type ) { + continue; + } + + // If event changes its type, use the special event handlers for the changed type + special = jQuery.event.special[ type ] || {}; + + // If selector defined, determine special event api type, otherwise given type + type = ( selector ? special.delegateType : special.bindType ) || type; + + // Update special based on newly reset type + special = jQuery.event.special[ type ] || {}; + + // handleObj is passed to all event handlers + handleObj = jQuery.extend({ + type: type, + origType: origType, + data: data, + handler: handler, + guid: handler.guid, + selector: selector, + needsContext: selector && jQuery.expr.match.needsContext.test( selector ), + namespace: namespaces.join(".") + }, handleObjIn ); + + // Init the event handler queue if we're the first + if ( !(handlers = events[ type ]) ) { + handlers = events[ type ] = []; + handlers.delegateCount = 0; + + // Only use addEventListener/attachEvent if the special events handler returns false + if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) { + // Bind the global event handler to the element + if ( elem.addEventListener ) { + elem.addEventListener( type, eventHandle, false ); + + } else if ( elem.attachEvent ) { + elem.attachEvent( "on" + type, eventHandle ); + } + } + } + + if ( special.add ) { + special.add.call( elem, handleObj ); + + if ( !handleObj.handler.guid ) { + handleObj.handler.guid = handler.guid; + } + } + + // Add to the element's handler list, delegates in front + if ( selector ) { + handlers.splice( handlers.delegateCount++, 0, handleObj ); + } else { + handlers.push( handleObj ); + } + + // Keep track of which events have ever been used, for event optimization + jQuery.event.global[ type ] = true; + } + + // Nullify elem to prevent memory leaks in IE + elem = null; + }, + + // Detach an event or set of events from an element + remove: function( elem, types, handler, selector, mappedTypes ) { + var j, handleObj, tmp, + origCount, t, events, + special, handlers, type, + namespaces, origType, + elemData = jQuery.hasData( elem ) && jQuery._data( elem ); + + if ( !elemData || !(events = elemData.events) ) { + return; + } + + // Once for each type.namespace in types; type may be omitted + types = ( types || "" ).match( rnotwhite ) || [ "" ]; + t = types.length; + while ( t-- ) { + tmp = rtypenamespace.exec( types[t] ) || []; + type = origType = tmp[1]; + namespaces = ( tmp[2] || "" ).split( "." ).sort(); + + // Unbind all events (on this namespace, if provided) for the element + if ( !type ) { + for ( type in events ) { + jQuery.event.remove( elem, type + types[ t ], handler, selector, true ); + } + continue; + } + + special = jQuery.event.special[ type ] || {}; + type = ( selector ? special.delegateType : special.bindType ) || type; + handlers = events[ type ] || []; + tmp = tmp[2] && new RegExp( "(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)" ); + + // Remove matching events + origCount = j = handlers.length; + while ( j-- ) { + handleObj = handlers[ j ]; + + if ( ( mappedTypes || origType === handleObj.origType ) && + ( !handler || handler.guid === handleObj.guid ) && + ( !tmp || tmp.test( handleObj.namespace ) ) && + ( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) { + handlers.splice( j, 1 ); + + if ( handleObj.selector ) { + handlers.delegateCount--; + } + if ( special.remove ) { + special.remove.call( elem, handleObj ); + } + } + } + + // Remove generic event handler if we removed something and no more handlers exist + // (avoids potential for endless recursion during removal of special event handlers) + if ( origCount && !handlers.length ) { + if ( !special.teardown || special.teardown.call( elem, namespaces, elemData.handle ) === false ) { + jQuery.removeEvent( elem, type, elemData.handle ); + } + + delete events[ type ]; + } + } + + // Remove the expando if it's no longer used + if ( jQuery.isEmptyObject( events ) ) { + delete elemData.handle; + + // removeData also checks for emptiness and clears the expando if empty + // so use it instead of delete + jQuery._removeData( elem, "events" ); + } + }, + + trigger: function( event, data, elem, onlyHandlers ) { + var handle, ontype, cur, + bubbleType, special, tmp, i, + eventPath = [ elem || document ], + type = hasOwn.call( event, "type" ) ? event.type : event, + namespaces = hasOwn.call( event, "namespace" ) ? event.namespace.split(".") : []; + + cur = tmp = elem = elem || document; + + // Don't do events on text and comment nodes + if ( elem.nodeType === 3 || elem.nodeType === 8 ) { + return; + } + + // focus/blur morphs to focusin/out; ensure we're not firing them right now + if ( rfocusMorph.test( type + jQuery.event.triggered ) ) { + return; + } + + if ( type.indexOf(".") >= 0 ) { + // Namespaced trigger; create a regexp to match event type in handle() + namespaces = type.split("."); + type = namespaces.shift(); + namespaces.sort(); + } + ontype = type.indexOf(":") < 0 && "on" + type; + + // Caller can pass in a jQuery.Event object, Object, or just an event type string + event = event[ jQuery.expando ] ? + event : + new jQuery.Event( type, typeof event === "object" && event ); + + // Trigger bitmask: & 1 for native handlers; & 2 for jQuery (always true) + event.isTrigger = onlyHandlers ? 2 : 3; + event.namespace = namespaces.join("."); + event.namespace_re = event.namespace ? + new RegExp( "(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)" ) : + null; + + // Clean up the event in case it is being reused + event.result = undefined; + if ( !event.target ) { + event.target = elem; + } + + // Clone any incoming data and prepend the event, creating the handler arg list + data = data == null ? + [ event ] : + jQuery.makeArray( data, [ event ] ); + + // Allow special events to draw outside the lines + special = jQuery.event.special[ type ] || {}; + if ( !onlyHandlers && special.trigger && special.trigger.apply( elem, data ) === false ) { + return; + } + + // Determine event propagation path in advance, per W3C events spec (#9951) + // Bubble up to document, then to window; watch for a global ownerDocument var (#9724) + if ( !onlyHandlers && !special.noBubble && !jQuery.isWindow( elem ) ) { + + bubbleType = special.delegateType || type; + if ( !rfocusMorph.test( bubbleType + type ) ) { + cur = cur.parentNode; + } + for ( ; cur; cur = cur.parentNode ) { + eventPath.push( cur ); + tmp = cur; + } + + // Only add window if we got to document (e.g., not plain obj or detached DOM) + if ( tmp === (elem.ownerDocument || document) ) { + eventPath.push( tmp.defaultView || tmp.parentWindow || window ); + } + } + + // Fire handlers on the event path + i = 0; + while ( (cur = eventPath[i++]) && !event.isPropagationStopped() ) { + + event.type = i > 1 ? + bubbleType : + special.bindType || type; + + // jQuery handler + handle = ( jQuery._data( cur, "events" ) || {} )[ event.type ] && jQuery._data( cur, "handle" ); + if ( handle ) { + handle.apply( cur, data ); + } + + // Native handler + handle = ontype && cur[ ontype ]; + if ( handle && handle.apply && jQuery.acceptData( cur ) ) { + event.result = handle.apply( cur, data ); + if ( event.result === false ) { + event.preventDefault(); + } + } + } + event.type = type; + + // If nobody prevented the default action, do it now + if ( !onlyHandlers && !event.isDefaultPrevented() ) { + + if ( (!special._default || special._default.apply( eventPath.pop(), data ) === false) && + jQuery.acceptData( elem ) ) { + + // Call a native DOM method on the target with the same name name as the event. + // Can't use an .isFunction() check here because IE6/7 fails that test. + // Don't do default actions on window, that's where global variables be (#6170) + if ( ontype && elem[ type ] && !jQuery.isWindow( elem ) ) { + + // Don't re-trigger an onFOO event when we call its FOO() method + tmp = elem[ ontype ]; + + if ( tmp ) { + elem[ ontype ] = null; + } + + // Prevent re-triggering of the same event, since we already bubbled it above + jQuery.event.triggered = type; + try { + elem[ type ](); + } catch ( e ) { + // IE<9 dies on focus/blur to hidden element (#1486,#12518) + // only reproducible on winXP IE8 native, not IE9 in IE8 mode + } + jQuery.event.triggered = undefined; + + if ( tmp ) { + elem[ ontype ] = tmp; + } + } + } + } + + return event.result; + }, + + dispatch: function( event ) { + + // Make a writable jQuery.Event from the native event object + event = jQuery.event.fix( event ); + + var i, ret, handleObj, matched, j, + handlerQueue = [], + args = slice.call( arguments ), + handlers = ( jQuery._data( this, "events" ) || {} )[ event.type ] || [], + special = jQuery.event.special[ event.type ] || {}; + + // Use the fix-ed jQuery.Event rather than the (read-only) native event + args[0] = event; + event.delegateTarget = this; + + // Call the preDispatch hook for the mapped type, and let it bail if desired + if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) { + return; + } + + // Determine handlers + handlerQueue = jQuery.event.handlers.call( this, event, handlers ); + + // Run delegates first; they may want to stop propagation beneath us + i = 0; + while ( (matched = handlerQueue[ i++ ]) && !event.isPropagationStopped() ) { + event.currentTarget = matched.elem; + + j = 0; + while ( (handleObj = matched.handlers[ j++ ]) && !event.isImmediatePropagationStopped() ) { + + // Triggered event must either 1) have no namespace, or + // 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace). + if ( !event.namespace_re || event.namespace_re.test( handleObj.namespace ) ) { + + event.handleObj = handleObj; + event.data = handleObj.data; + + ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler ) + .apply( matched.elem, args ); + + if ( ret !== undefined ) { + if ( (event.result = ret) === false ) { + event.preventDefault(); + event.stopPropagation(); + } + } + } + } + } + + // Call the postDispatch hook for the mapped type + if ( special.postDispatch ) { + special.postDispatch.call( this, event ); + } + + return event.result; + }, + + handlers: function( event, handlers ) { + var sel, handleObj, matches, i, + handlerQueue = [], + delegateCount = handlers.delegateCount, + cur = event.target; + + // Find delegate handlers + // Black-hole SVG instance trees (#13180) + // Avoid non-left-click bubbling in Firefox (#3861) + if ( delegateCount && cur.nodeType && (!event.button || event.type !== "click") ) { + + /* jshint eqeqeq: false */ + for ( ; cur != this; cur = cur.parentNode || this ) { + /* jshint eqeqeq: true */ + + // Don't check non-elements (#13208) + // Don't process clicks on disabled elements (#6911, #8165, #11382, #11764) + if ( cur.nodeType === 1 && (cur.disabled !== true || event.type !== "click") ) { + matches = []; + for ( i = 0; i < delegateCount; i++ ) { + handleObj = handlers[ i ]; + + // Don't conflict with Object.prototype properties (#13203) + sel = handleObj.selector + " "; + + if ( matches[ sel ] === undefined ) { + matches[ sel ] = handleObj.needsContext ? + jQuery( sel, this ).index( cur ) >= 0 : + jQuery.find( sel, this, null, [ cur ] ).length; + } + if ( matches[ sel ] ) { + matches.push( handleObj ); + } + } + if ( matches.length ) { + handlerQueue.push({ elem: cur, handlers: matches }); + } + } + } + } + + // Add the remaining (directly-bound) handlers + if ( delegateCount < handlers.length ) { + handlerQueue.push({ elem: this, handlers: handlers.slice( delegateCount ) }); + } + + return handlerQueue; + }, + + fix: function( event ) { + if ( event[ jQuery.expando ] ) { + return event; + } + + // Create a writable copy of the event object and normalize some properties + var i, prop, copy, + type = event.type, + originalEvent = event, + fixHook = this.fixHooks[ type ]; + + if ( !fixHook ) { + this.fixHooks[ type ] = fixHook = + rmouseEvent.test( type ) ? this.mouseHooks : + rkeyEvent.test( type ) ? this.keyHooks : + {}; + } + copy = fixHook.props ? this.props.concat( fixHook.props ) : this.props; + + event = new jQuery.Event( originalEvent ); + + i = copy.length; + while ( i-- ) { + prop = copy[ i ]; + event[ prop ] = originalEvent[ prop ]; + } + + // Support: IE<9 + // Fix target property (#1925) + if ( !event.target ) { + event.target = originalEvent.srcElement || document; + } + + // Support: Chrome 23+, Safari? + // Target should not be a text node (#504, #13143) + if ( event.target.nodeType === 3 ) { + event.target = event.target.parentNode; + } + + // Support: IE<9 + // For mouse/key events, metaKey==false if it's undefined (#3368, #11328) + event.metaKey = !!event.metaKey; + + return fixHook.filter ? fixHook.filter( event, originalEvent ) : event; + }, + + // Includes some event props shared by KeyEvent and MouseEvent + props: "altKey bubbles cancelable ctrlKey currentTarget eventPhase metaKey relatedTarget shiftKey target timeStamp view which".split(" "), + + fixHooks: {}, + + keyHooks: { + props: "char charCode key keyCode".split(" "), + filter: function( event, original ) { + + // Add which for key events + if ( event.which == null ) { + event.which = original.charCode != null ? original.charCode : original.keyCode; + } + + return event; + } + }, + + mouseHooks: { + props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" "), + filter: function( event, original ) { + var body, eventDoc, doc, + button = original.button, + fromElement = original.fromElement; + + // Calculate pageX/Y if missing and clientX/Y available + if ( event.pageX == null && original.clientX != null ) { + eventDoc = event.target.ownerDocument || document; + doc = eventDoc.documentElement; + body = eventDoc.body; + + event.pageX = original.clientX + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - ( doc && doc.clientLeft || body && body.clientLeft || 0 ); + event.pageY = original.clientY + ( doc && doc.scrollTop || body && body.scrollTop || 0 ) - ( doc && doc.clientTop || body && body.clientTop || 0 ); + } + + // Add relatedTarget, if necessary + if ( !event.relatedTarget && fromElement ) { + event.relatedTarget = fromElement === event.target ? original.toElement : fromElement; + } + + // Add which for click: 1 === left; 2 === middle; 3 === right + // Note: button is not normalized, so don't use it + if ( !event.which && button !== undefined ) { + event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) ); + } + + return event; + } + }, + + special: { + load: { + // Prevent triggered image.load events from bubbling to window.load + noBubble: true + }, + focus: { + // Fire native event if possible so blur/focus sequence is correct + trigger: function() { + if ( this !== safeActiveElement() && this.focus ) { + try { + this.focus(); + return false; + } catch ( e ) { + // Support: IE<9 + // If we error on focus to hidden element (#1486, #12518), + // let .trigger() run the handlers + } + } + }, + delegateType: "focusin" + }, + blur: { + trigger: function() { + if ( this === safeActiveElement() && this.blur ) { + this.blur(); + return false; + } + }, + delegateType: "focusout" + }, + click: { + // For checkbox, fire native event so checked state will be right + trigger: function() { + if ( jQuery.nodeName( this, "input" ) && this.type === "checkbox" && this.click ) { + this.click(); + return false; + } + }, + + // For cross-browser consistency, don't fire native .click() on links + _default: function( event ) { + return jQuery.nodeName( event.target, "a" ); + } + }, + + beforeunload: { + postDispatch: function( event ) { + + // Even when returnValue equals to undefined Firefox will still show alert + if ( event.result !== undefined ) { + event.originalEvent.returnValue = event.result; + } + } + } + }, + + simulate: function( type, elem, event, bubble ) { + // Piggyback on a donor event to simulate a different one. + // Fake originalEvent to avoid donor's stopPropagation, but if the + // simulated event prevents default then we do the same on the donor. + var e = jQuery.extend( + new jQuery.Event(), + event, + { + type: type, + isSimulated: true, + originalEvent: {} + } + ); + if ( bubble ) { + jQuery.event.trigger( e, null, elem ); + } else { + jQuery.event.dispatch.call( elem, e ); + } + if ( e.isDefaultPrevented() ) { + event.preventDefault(); + } + } +}; + +jQuery.removeEvent = document.removeEventListener ? + function( elem, type, handle ) { + if ( elem.removeEventListener ) { + elem.removeEventListener( type, handle, false ); + } + } : + function( elem, type, handle ) { + var name = "on" + type; + + if ( elem.detachEvent ) { + + // #8545, #7054, preventing memory leaks for custom events in IE6-8 + // detachEvent needed property on element, by name of that event, to properly expose it to GC + if ( typeof elem[ name ] === strundefined ) { + elem[ name ] = null; + } + + elem.detachEvent( name, handle ); + } + }; + +jQuery.Event = function( src, props ) { + // Allow instantiation without the 'new' keyword + if ( !(this instanceof jQuery.Event) ) { + return new jQuery.Event( src, props ); + } + + // Event object + if ( src && src.type ) { + this.originalEvent = src; + this.type = src.type; + + // Events bubbling up the document may have been marked as prevented + // by a handler lower down the tree; reflect the correct value. + this.isDefaultPrevented = src.defaultPrevented || + src.defaultPrevented === undefined && ( + // Support: IE < 9 + src.returnValue === false || + // Support: Android < 4.0 + src.getPreventDefault && src.getPreventDefault() ) ? + returnTrue : + returnFalse; + + // Event type + } else { + this.type = src; + } + + // Put explicitly provided properties onto the event object + if ( props ) { + jQuery.extend( this, props ); + } + + // Create a timestamp if incoming event doesn't have one + this.timeStamp = src && src.timeStamp || jQuery.now(); + + // Mark it as fixed + this[ jQuery.expando ] = true; +}; + +// jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding +// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html +jQuery.Event.prototype = { + isDefaultPrevented: returnFalse, + isPropagationStopped: returnFalse, + isImmediatePropagationStopped: returnFalse, + + preventDefault: function() { + var e = this.originalEvent; + + this.isDefaultPrevented = returnTrue; + if ( !e ) { + return; + } + + // If preventDefault exists, run it on the original event + if ( e.preventDefault ) { + e.preventDefault(); + + // Support: IE + // Otherwise set the returnValue property of the original event to false + } else { + e.returnValue = false; + } + }, + stopPropagation: function() { + var e = this.originalEvent; + + this.isPropagationStopped = returnTrue; + if ( !e ) { + return; + } + // If stopPropagation exists, run it on the original event + if ( e.stopPropagation ) { + e.stopPropagation(); + } + + // Support: IE + // Set the cancelBubble property of the original event to true + e.cancelBubble = true; + }, + stopImmediatePropagation: function() { + this.isImmediatePropagationStopped = returnTrue; + this.stopPropagation(); + } +}; + +// Create mouseenter/leave events using mouseover/out and event-time checks +jQuery.each({ + mouseenter: "mouseover", + mouseleave: "mouseout" +}, function( orig, fix ) { + jQuery.event.special[ orig ] = { + delegateType: fix, + bindType: fix, + + handle: function( event ) { + var ret, + target = this, + related = event.relatedTarget, + handleObj = event.handleObj; + + // For mousenter/leave call the handler if related is outside the target. + // NB: No relatedTarget if the mouse left/entered the browser window + if ( !related || (related !== target && !jQuery.contains( target, related )) ) { + event.type = handleObj.origType; + ret = handleObj.handler.apply( this, arguments ); + event.type = fix; + } + return ret; + } + }; +}); + +// IE submit delegation +if ( !support.submitBubbles ) { + + jQuery.event.special.submit = { + setup: function() { + // Only need this for delegated form submit events + if ( jQuery.nodeName( this, "form" ) ) { + return false; + } + + // Lazy-add a submit handler when a descendant form may potentially be submitted + jQuery.event.add( this, "click._submit keypress._submit", function( e ) { + // Node name check avoids a VML-related crash in IE (#9807) + var elem = e.target, + form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined; + if ( form && !jQuery._data( form, "submitBubbles" ) ) { + jQuery.event.add( form, "submit._submit", function( event ) { + event._submit_bubble = true; + }); + jQuery._data( form, "submitBubbles", true ); + } + }); + // return undefined since we don't need an event listener + }, + + postDispatch: function( event ) { + // If form was submitted by the user, bubble the event up the tree + if ( event._submit_bubble ) { + delete event._submit_bubble; + if ( this.parentNode && !event.isTrigger ) { + jQuery.event.simulate( "submit", this.parentNode, event, true ); + } + } + }, + + teardown: function() { + // Only need this for delegated form submit events + if ( jQuery.nodeName( this, "form" ) ) { + return false; + } + + // Remove delegated handlers; cleanData eventually reaps submit handlers attached above + jQuery.event.remove( this, "._submit" ); + } + }; +} + +// IE change delegation and checkbox/radio fix +if ( !support.changeBubbles ) { + + jQuery.event.special.change = { + + setup: function() { + + if ( rformElems.test( this.nodeName ) ) { + // IE doesn't fire change on a check/radio until blur; trigger it on click + // after a propertychange. Eat the blur-change in special.change.handle. + // This still fires onchange a second time for check/radio after blur. + if ( this.type === "checkbox" || this.type === "radio" ) { + jQuery.event.add( this, "propertychange._change", function( event ) { + if ( event.originalEvent.propertyName === "checked" ) { + this._just_changed = true; + } + }); + jQuery.event.add( this, "click._change", function( event ) { + if ( this._just_changed && !event.isTrigger ) { + this._just_changed = false; + } + // Allow triggered, simulated change events (#11500) + jQuery.event.simulate( "change", this, event, true ); + }); + } + return false; + } + // Delegated event; lazy-add a change handler on descendant inputs + jQuery.event.add( this, "beforeactivate._change", function( e ) { + var elem = e.target; + + if ( rformElems.test( elem.nodeName ) && !jQuery._data( elem, "changeBubbles" ) ) { + jQuery.event.add( elem, "change._change", function( event ) { + if ( this.parentNode && !event.isSimulated && !event.isTrigger ) { + jQuery.event.simulate( "change", this.parentNode, event, true ); + } + }); + jQuery._data( elem, "changeBubbles", true ); + } + }); + }, + + handle: function( event ) { + var elem = event.target; + + // Swallow native change events from checkbox/radio, we already triggered them above + if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) { + return event.handleObj.handler.apply( this, arguments ); + } + }, + + teardown: function() { + jQuery.event.remove( this, "._change" ); + + return !rformElems.test( this.nodeName ); + } + }; +} + +// Create "bubbling" focus and blur events +if ( !support.focusinBubbles ) { + jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) { + + // Attach a single capturing handler on the document while someone wants focusin/focusout + var handler = function( event ) { + jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true ); + }; + + jQuery.event.special[ fix ] = { + setup: function() { + var doc = this.ownerDocument || this, + attaches = jQuery._data( doc, fix ); + + if ( !attaches ) { + doc.addEventListener( orig, handler, true ); + } + jQuery._data( doc, fix, ( attaches || 0 ) + 1 ); + }, + teardown: function() { + var doc = this.ownerDocument || this, + attaches = jQuery._data( doc, fix ) - 1; + + if ( !attaches ) { + doc.removeEventListener( orig, handler, true ); + jQuery._removeData( doc, fix ); + } else { + jQuery._data( doc, fix, attaches ); + } + } + }; + }); +} + +jQuery.fn.extend({ + + on: function( types, selector, data, fn, /*INTERNAL*/ one ) { + var type, origFn; + + // Types can be a map of types/handlers + if ( typeof types === "object" ) { + // ( types-Object, selector, data ) + if ( typeof selector !== "string" ) { + // ( types-Object, data ) + data = data || selector; + selector = undefined; + } + for ( type in types ) { + this.on( type, selector, data, types[ type ], one ); + } + return this; + } + + if ( data == null && fn == null ) { + // ( types, fn ) + fn = selector; + data = selector = undefined; + } else if ( fn == null ) { + if ( typeof selector === "string" ) { + // ( types, selector, fn ) + fn = data; + data = undefined; + } else { + // ( types, data, fn ) + fn = data; + data = selector; + selector = undefined; + } + } + if ( fn === false ) { + fn = returnFalse; + } else if ( !fn ) { + return this; + } + + if ( one === 1 ) { + origFn = fn; + fn = function( event ) { + // Can use an empty set, since event contains the info + jQuery().off( event ); + return origFn.apply( this, arguments ); + }; + // Use same guid so caller can remove using origFn + fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ ); + } + return this.each( function() { + jQuery.event.add( this, types, fn, data, selector ); + }); + }, + one: function( types, selector, data, fn ) { + return this.on( types, selector, data, fn, 1 ); + }, + off: function( types, selector, fn ) { + var handleObj, type; + if ( types && types.preventDefault && types.handleObj ) { + // ( event ) dispatched jQuery.Event + handleObj = types.handleObj; + jQuery( types.delegateTarget ).off( + handleObj.namespace ? handleObj.origType + "." + handleObj.namespace : handleObj.origType, + handleObj.selector, + handleObj.handler + ); + return this; + } + if ( typeof types === "object" ) { + // ( types-object [, selector] ) + for ( type in types ) { + this.off( type, selector, types[ type ] ); + } + return this; + } + if ( selector === false || typeof selector === "function" ) { + // ( types [, fn] ) + fn = selector; + selector = undefined; + } + if ( fn === false ) { + fn = returnFalse; + } + return this.each(function() { + jQuery.event.remove( this, types, fn, selector ); + }); + }, + + trigger: function( type, data ) { + return this.each(function() { + jQuery.event.trigger( type, data, this ); + }); + }, + triggerHandler: function( type, data ) { + var elem = this[0]; + if ( elem ) { + return jQuery.event.trigger( type, data, elem, true ); + } + } +}); + + +function createSafeFragment( document ) { + var list = nodeNames.split( "|" ), + safeFrag = document.createDocumentFragment(); + + if ( safeFrag.createElement ) { + while ( list.length ) { + safeFrag.createElement( + list.pop() + ); + } + } + return safeFrag; +} + +var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figcaption|figure|footer|" + + "header|hgroup|mark|meter|nav|output|progress|section|summary|time|video", + rinlinejQuery = / jQuery\d+="(?:null|\d+)"/g, + rnoshimcache = new RegExp("<(?:" + nodeNames + ")[\\s/>]", "i"), + rleadingWhitespace = /^\s+/, + rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi, + rtagName = /<([\w:]+)/, + rtbody = /\s*$/g, + + // We have to close these tags to support XHTML (#13200) + wrapMap = { + option: [ 1, "" ], + legend: [ 1, "
", "
" ], + area: [ 1, "", "" ], + param: [ 1, "", "" ], + thead: [ 1, "", "
" ], + tr: [ 2, "", "
" ], + col: [ 2, "", "
" ], + td: [ 3, "", "
" ], + + // IE6-8 can't serialize link, script, style, or any html5 (NoScope) tags, + // unless wrapped in a div with non-breaking characters in front of it. + _default: support.htmlSerialize ? [ 0, "", "" ] : [ 1, "X
", "
" ] + }, + safeFragment = createSafeFragment( document ), + fragmentDiv = safeFragment.appendChild( document.createElement("div") ); + +wrapMap.optgroup = wrapMap.option; +wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead; +wrapMap.th = wrapMap.td; + +function getAll( context, tag ) { + var elems, elem, + i = 0, + found = typeof context.getElementsByTagName !== strundefined ? context.getElementsByTagName( tag || "*" ) : + typeof context.querySelectorAll !== strundefined ? context.querySelectorAll( tag || "*" ) : + undefined; + + if ( !found ) { + for ( found = [], elems = context.childNodes || context; (elem = elems[i]) != null; i++ ) { + if ( !tag || jQuery.nodeName( elem, tag ) ) { + found.push( elem ); + } else { + jQuery.merge( found, getAll( elem, tag ) ); + } + } + } + + return tag === undefined || tag && jQuery.nodeName( context, tag ) ? + jQuery.merge( [ context ], found ) : + found; +} + +// Used in buildFragment, fixes the defaultChecked property +function fixDefaultChecked( elem ) { + if ( rcheckableType.test( elem.type ) ) { + elem.defaultChecked = elem.checked; + } +} + +// Support: IE<8 +// Manipulating tables requires a tbody +function manipulationTarget( elem, content ) { + return jQuery.nodeName( elem, "table" ) && + jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ? + + elem.getElementsByTagName("tbody")[0] || + elem.appendChild( elem.ownerDocument.createElement("tbody") ) : + elem; +} + +// Replace/restore the type attribute of script elements for safe DOM manipulation +function disableScript( elem ) { + elem.type = (jQuery.find.attr( elem, "type" ) !== null) + "/" + elem.type; + return elem; +} +function restoreScript( elem ) { + var match = rscriptTypeMasked.exec( elem.type ); + if ( match ) { + elem.type = match[1]; + } else { + elem.removeAttribute("type"); + } + return elem; +} + +// Mark scripts as having already been evaluated +function setGlobalEval( elems, refElements ) { + var elem, + i = 0; + for ( ; (elem = elems[i]) != null; i++ ) { + jQuery._data( elem, "globalEval", !refElements || jQuery._data( refElements[i], "globalEval" ) ); + } +} + +function cloneCopyEvent( src, dest ) { + + if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) { + return; + } + + var type, i, l, + oldData = jQuery._data( src ), + curData = jQuery._data( dest, oldData ), + events = oldData.events; + + if ( events ) { + delete curData.handle; + curData.events = {}; + + for ( type in events ) { + for ( i = 0, l = events[ type ].length; i < l; i++ ) { + jQuery.event.add( dest, type, events[ type ][ i ] ); + } + } + } + + // make the cloned public data object a copy from the original + if ( curData.data ) { + curData.data = jQuery.extend( {}, curData.data ); + } +} + +function fixCloneNodeIssues( src, dest ) { + var nodeName, e, data; + + // We do not need to do anything for non-Elements + if ( dest.nodeType !== 1 ) { + return; + } + + nodeName = dest.nodeName.toLowerCase(); + + // IE6-8 copies events bound via attachEvent when using cloneNode. + if ( !support.noCloneEvent && dest[ jQuery.expando ] ) { + data = jQuery._data( dest ); + + for ( e in data.events ) { + jQuery.removeEvent( dest, e, data.handle ); + } + + // Event data gets referenced instead of copied if the expando gets copied too + dest.removeAttribute( jQuery.expando ); + } + + // IE blanks contents when cloning scripts, and tries to evaluate newly-set text + if ( nodeName === "script" && dest.text !== src.text ) { + disableScript( dest ).text = src.text; + restoreScript( dest ); + + // IE6-10 improperly clones children of object elements using classid. + // IE10 throws NoModificationAllowedError if parent is null, #12132. + } else if ( nodeName === "object" ) { + if ( dest.parentNode ) { + dest.outerHTML = src.outerHTML; + } + + // This path appears unavoidable for IE9. When cloning an object + // element in IE9, the outerHTML strategy above is not sufficient. + // If the src has innerHTML and the destination does not, + // copy the src.innerHTML into the dest.innerHTML. #10324 + if ( support.html5Clone && ( src.innerHTML && !jQuery.trim(dest.innerHTML) ) ) { + dest.innerHTML = src.innerHTML; + } + + } else if ( nodeName === "input" && rcheckableType.test( src.type ) ) { + // IE6-8 fails to persist the checked state of a cloned checkbox + // or radio button. Worse, IE6-7 fail to give the cloned element + // a checked appearance if the defaultChecked value isn't also set + + dest.defaultChecked = dest.checked = src.checked; + + // IE6-7 get confused and end up setting the value of a cloned + // checkbox/radio button to an empty string instead of "on" + if ( dest.value !== src.value ) { + dest.value = src.value; + } + + // IE6-8 fails to return the selected option to the default selected + // state when cloning options + } else if ( nodeName === "option" ) { + dest.defaultSelected = dest.selected = src.defaultSelected; + + // IE6-8 fails to set the defaultValue to the correct value when + // cloning other types of input fields + } else if ( nodeName === "input" || nodeName === "textarea" ) { + dest.defaultValue = src.defaultValue; + } +} + +jQuery.extend({ + clone: function( elem, dataAndEvents, deepDataAndEvents ) { + var destElements, node, clone, i, srcElements, + inPage = jQuery.contains( elem.ownerDocument, elem ); + + if ( support.html5Clone || jQuery.isXMLDoc(elem) || !rnoshimcache.test( "<" + elem.nodeName + ">" ) ) { + clone = elem.cloneNode( true ); + + // IE<=8 does not properly clone detached, unknown element nodes + } else { + fragmentDiv.innerHTML = elem.outerHTML; + fragmentDiv.removeChild( clone = fragmentDiv.firstChild ); + } + + if ( (!support.noCloneEvent || !support.noCloneChecked) && + (elem.nodeType === 1 || elem.nodeType === 11) && !jQuery.isXMLDoc(elem) ) { + + // We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2 + destElements = getAll( clone ); + srcElements = getAll( elem ); + + // Fix all IE cloning issues + for ( i = 0; (node = srcElements[i]) != null; ++i ) { + // Ensure that the destination node is not null; Fixes #9587 + if ( destElements[i] ) { + fixCloneNodeIssues( node, destElements[i] ); + } + } + } + + // Copy the events from the original to the clone + if ( dataAndEvents ) { + if ( deepDataAndEvents ) { + srcElements = srcElements || getAll( elem ); + destElements = destElements || getAll( clone ); + + for ( i = 0; (node = srcElements[i]) != null; i++ ) { + cloneCopyEvent( node, destElements[i] ); + } + } else { + cloneCopyEvent( elem, clone ); + } + } + + // Preserve script evaluation history + destElements = getAll( clone, "script" ); + if ( destElements.length > 0 ) { + setGlobalEval( destElements, !inPage && getAll( elem, "script" ) ); + } + + destElements = srcElements = node = null; + + // Return the cloned set + return clone; + }, + + buildFragment: function( elems, context, scripts, selection ) { + var j, elem, contains, + tmp, tag, tbody, wrap, + l = elems.length, + + // Ensure a safe fragment + safe = createSafeFragment( context ), + + nodes = [], + i = 0; + + for ( ; i < l; i++ ) { + elem = elems[ i ]; + + if ( elem || elem === 0 ) { + + // Add nodes directly + if ( jQuery.type( elem ) === "object" ) { + jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem ); + + // Convert non-html into a text node + } else if ( !rhtml.test( elem ) ) { + nodes.push( context.createTextNode( elem ) ); + + // Convert html into DOM nodes + } else { + tmp = tmp || safe.appendChild( context.createElement("div") ); + + // Deserialize a standard representation + tag = (rtagName.exec( elem ) || [ "", "" ])[ 1 ].toLowerCase(); + wrap = wrapMap[ tag ] || wrapMap._default; + + tmp.innerHTML = wrap[1] + elem.replace( rxhtmlTag, "<$1>" ) + wrap[2]; + + // Descend through wrappers to the right content + j = wrap[0]; + while ( j-- ) { + tmp = tmp.lastChild; + } + + // Manually add leading whitespace removed by IE + if ( !support.leadingWhitespace && rleadingWhitespace.test( elem ) ) { + nodes.push( context.createTextNode( rleadingWhitespace.exec( elem )[0] ) ); + } + + // Remove IE's autoinserted from table fragments + if ( !support.tbody ) { + + // String was a , *may* have spurious + elem = tag === "table" && !rtbody.test( elem ) ? + tmp.firstChild : + + // String was a bare or + wrap[1] === "
" && !rtbody.test( elem ) ? + tmp : + 0; + + j = elem && elem.childNodes.length; + while ( j-- ) { + if ( jQuery.nodeName( (tbody = elem.childNodes[j]), "tbody" ) && !tbody.childNodes.length ) { + elem.removeChild( tbody ); + } + } + } + + jQuery.merge( nodes, tmp.childNodes ); + + // Fix #12392 for WebKit and IE > 9 + tmp.textContent = ""; + + // Fix #12392 for oldIE + while ( tmp.firstChild ) { + tmp.removeChild( tmp.firstChild ); + } + + // Remember the top-level container for proper cleanup + tmp = safe.lastChild; + } + } + } + + // Fix #11356: Clear elements from fragment + if ( tmp ) { + safe.removeChild( tmp ); + } + + // Reset defaultChecked for any radios and checkboxes + // about to be appended to the DOM in IE 6/7 (#8060) + if ( !support.appendChecked ) { + jQuery.grep( getAll( nodes, "input" ), fixDefaultChecked ); + } + + i = 0; + while ( (elem = nodes[ i++ ]) ) { + + // #4087 - If origin and destination elements are the same, and this is + // that element, do not do anything + if ( selection && jQuery.inArray( elem, selection ) !== -1 ) { + continue; + } + + contains = jQuery.contains( elem.ownerDocument, elem ); + + // Append to fragment + tmp = getAll( safe.appendChild( elem ), "script" ); + + // Preserve script evaluation history + if ( contains ) { + setGlobalEval( tmp ); + } + + // Capture executables + if ( scripts ) { + j = 0; + while ( (elem = tmp[ j++ ]) ) { + if ( rscriptType.test( elem.type || "" ) ) { + scripts.push( elem ); + } + } + } + } + + tmp = null; + + return safe; + }, + + cleanData: function( elems, /* internal */ acceptData ) { + var elem, type, id, data, + i = 0, + internalKey = jQuery.expando, + cache = jQuery.cache, + deleteExpando = support.deleteExpando, + special = jQuery.event.special; + + for ( ; (elem = elems[i]) != null; i++ ) { + if ( acceptData || jQuery.acceptData( elem ) ) { + + id = elem[ internalKey ]; + data = id && cache[ id ]; + + if ( data ) { + if ( data.events ) { + for ( type in data.events ) { + if ( special[ type ] ) { + jQuery.event.remove( elem, type ); + + // This is a shortcut to avoid jQuery.event.remove's overhead + } else { + jQuery.removeEvent( elem, type, data.handle ); + } + } + } + + // Remove cache only if it was not already removed by jQuery.event.remove + if ( cache[ id ] ) { + + delete cache[ id ]; + + // IE does not allow us to delete expando properties from nodes, + // nor does it have a removeAttribute function on Document nodes; + // we must handle all of these cases + if ( deleteExpando ) { + delete elem[ internalKey ]; + + } else if ( typeof elem.removeAttribute !== strundefined ) { + elem.removeAttribute( internalKey ); + + } else { + elem[ internalKey ] = null; + } + + deletedIds.push( id ); + } + } + } + } + } +}); + +jQuery.fn.extend({ + text: function( value ) { + return access( this, function( value ) { + return value === undefined ? + jQuery.text( this ) : + this.empty().append( ( this[0] && this[0].ownerDocument || document ).createTextNode( value ) ); + }, null, value, arguments.length ); + }, + + append: function() { + return this.domManip( arguments, function( elem ) { + if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { + var target = manipulationTarget( this, elem ); + target.appendChild( elem ); + } + }); + }, + + prepend: function() { + return this.domManip( arguments, function( elem ) { + if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { + var target = manipulationTarget( this, elem ); + target.insertBefore( elem, target.firstChild ); + } + }); + }, + + before: function() { + return this.domManip( arguments, function( elem ) { + if ( this.parentNode ) { + this.parentNode.insertBefore( elem, this ); + } + }); + }, + + after: function() { + return this.domManip( arguments, function( elem ) { + if ( this.parentNode ) { + this.parentNode.insertBefore( elem, this.nextSibling ); + } + }); + }, + + remove: function( selector, keepData /* Internal Use Only */ ) { + var elem, + elems = selector ? jQuery.filter( selector, this ) : this, + i = 0; + + for ( ; (elem = elems[i]) != null; i++ ) { + + if ( !keepData && elem.nodeType === 1 ) { + jQuery.cleanData( getAll( elem ) ); + } + + if ( elem.parentNode ) { + if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) { + setGlobalEval( getAll( elem, "script" ) ); + } + elem.parentNode.removeChild( elem ); + } + } + + return this; + }, + + empty: function() { + var elem, + i = 0; + + for ( ; (elem = this[i]) != null; i++ ) { + // Remove element nodes and prevent memory leaks + if ( elem.nodeType === 1 ) { + jQuery.cleanData( getAll( elem, false ) ); + } + + // Remove any remaining nodes + while ( elem.firstChild ) { + elem.removeChild( elem.firstChild ); + } + + // If this is a select, ensure that it displays empty (#12336) + // Support: IE<9 + if ( elem.options && jQuery.nodeName( elem, "select" ) ) { + elem.options.length = 0; + } + } + + return this; + }, + + clone: function( dataAndEvents, deepDataAndEvents ) { + dataAndEvents = dataAndEvents == null ? false : dataAndEvents; + deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents; + + return this.map(function() { + return jQuery.clone( this, dataAndEvents, deepDataAndEvents ); + }); + }, + + html: function( value ) { + return access( this, function( value ) { + var elem = this[ 0 ] || {}, + i = 0, + l = this.length; + + if ( value === undefined ) { + return elem.nodeType === 1 ? + elem.innerHTML.replace( rinlinejQuery, "" ) : + undefined; + } + + // See if we can take a shortcut and just use innerHTML + if ( typeof value === "string" && !rnoInnerhtml.test( value ) && + ( support.htmlSerialize || !rnoshimcache.test( value ) ) && + ( support.leadingWhitespace || !rleadingWhitespace.test( value ) ) && + !wrapMap[ (rtagName.exec( value ) || [ "", "" ])[ 1 ].toLowerCase() ] ) { + + value = value.replace( rxhtmlTag, "<$1>" ); + + try { + for (; i < l; i++ ) { + // Remove element nodes and prevent memory leaks + elem = this[i] || {}; + if ( elem.nodeType === 1 ) { + jQuery.cleanData( getAll( elem, false ) ); + elem.innerHTML = value; + } + } + + elem = 0; + + // If using innerHTML throws an exception, use the fallback method + } catch(e) {} + } + + if ( elem ) { + this.empty().append( value ); + } + }, null, value, arguments.length ); + }, + + replaceWith: function() { + var arg = arguments[ 0 ]; + + // Make the changes, replacing each context element with the new content + this.domManip( arguments, function( elem ) { + arg = this.parentNode; + + jQuery.cleanData( getAll( this ) ); + + if ( arg ) { + arg.replaceChild( elem, this ); + } + }); + + // Force removal if there was no new content (e.g., from empty arguments) + return arg && (arg.length || arg.nodeType) ? this : this.remove(); + }, + + detach: function( selector ) { + return this.remove( selector, true ); + }, + + domManip: function( args, callback ) { + + // Flatten any nested arrays + args = concat.apply( [], args ); + + var first, node, hasScripts, + scripts, doc, fragment, + i = 0, + l = this.length, + set = this, + iNoClone = l - 1, + value = args[0], + isFunction = jQuery.isFunction( value ); + + // We can't cloneNode fragments that contain checked, in WebKit + if ( isFunction || + ( l > 1 && typeof value === "string" && + !support.checkClone && rchecked.test( value ) ) ) { + return this.each(function( index ) { + var self = set.eq( index ); + if ( isFunction ) { + args[0] = value.call( this, index, self.html() ); + } + self.domManip( args, callback ); + }); + } + + if ( l ) { + fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this ); + first = fragment.firstChild; + + if ( fragment.childNodes.length === 1 ) { + fragment = first; + } + + if ( first ) { + scripts = jQuery.map( getAll( fragment, "script" ), disableScript ); + hasScripts = scripts.length; + + // Use the original fragment for the last item instead of the first because it can end up + // being emptied incorrectly in certain situations (#8070). + for ( ; i < l; i++ ) { + node = fragment; + + if ( i !== iNoClone ) { + node = jQuery.clone( node, true, true ); + + // Keep references to cloned scripts for later restoration + if ( hasScripts ) { + jQuery.merge( scripts, getAll( node, "script" ) ); + } + } + + callback.call( this[i], node, i ); + } + + if ( hasScripts ) { + doc = scripts[ scripts.length - 1 ].ownerDocument; + + // Reenable scripts + jQuery.map( scripts, restoreScript ); + + // Evaluate executable scripts on first document insertion + for ( i = 0; i < hasScripts; i++ ) { + node = scripts[ i ]; + if ( rscriptType.test( node.type || "" ) && + !jQuery._data( node, "globalEval" ) && jQuery.contains( doc, node ) ) { + + if ( node.src ) { + // Optional AJAX dependency, but won't run scripts if not present + if ( jQuery._evalUrl ) { + jQuery._evalUrl( node.src ); + } + } else { + jQuery.globalEval( ( node.text || node.textContent || node.innerHTML || "" ).replace( rcleanScript, "" ) ); + } + } + } + } + + // Fix #11809: Avoid leaking memory + fragment = first = null; + } + } + + return this; + } +}); + +jQuery.each({ + appendTo: "append", + prependTo: "prepend", + insertBefore: "before", + insertAfter: "after", + replaceAll: "replaceWith" +}, function( name, original ) { + jQuery.fn[ name ] = function( selector ) { + var elems, + i = 0, + ret = [], + insert = jQuery( selector ), + last = insert.length - 1; + + for ( ; i <= last; i++ ) { + elems = i === last ? this : this.clone(true); + jQuery( insert[i] )[ original ]( elems ); + + // Modern browsers can apply jQuery collections as arrays, but oldIE needs a .get() + push.apply( ret, elems.get() ); + } + + return this.pushStack( ret ); + }; +}); + + +var iframe, + elemdisplay = {}; + +/** + * Retrieve the actual display of a element + * @param {String} name nodeName of the element + * @param {Object} doc Document object + */ +// Called only from within defaultDisplay +function actualDisplay( name, doc ) { + var elem = jQuery( doc.createElement( name ) ).appendTo( doc.body ), + + // getDefaultComputedStyle might be reliably used only on attached element + display = window.getDefaultComputedStyle ? + + // Use of this method is a temporary fix (more like optmization) until something better comes along, + // since it was removed from specification and supported only in FF + window.getDefaultComputedStyle( elem[ 0 ] ).display : jQuery.css( elem[ 0 ], "display" ); + + // We don't have any data stored on the element, + // so use "detach" method as fast way to get rid of the element + elem.detach(); + + return display; +} + +/** + * Try to determine the default display value of an element + * @param {String} nodeName + */ +function defaultDisplay( nodeName ) { + var doc = document, + display = elemdisplay[ nodeName ]; + + if ( !display ) { + display = actualDisplay( nodeName, doc ); + + // If the simple way fails, read from inside an iframe + if ( display === "none" || !display ) { + + // Use the already-created iframe if possible + iframe = (iframe || jQuery( "