File size: 3,343 Bytes
2fcec7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | /** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* wire/cola plugin
*
* wire is part of the cujo.js family of libraries (http://cujojs.com/)
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*/
(function(define) {
define(['when', './relational/propertiesKey', './comparator/byProperty'],
function(when, propertiesKey, byProperty) {
var defaultComparator, defaultQuerySelector, defaultQuerySelectorAll,
defaultOn, excludeOptions;
defaultComparator = byProperty('id');
defaultQuerySelector = { $ref: 'dom.first!' };
defaultQuerySelectorAll = { $ref: 'dom.all!' };
defaultOn = { $ref: 'on!' };
function initBindOptions(incomingOptions, pluginOptions, resolver) {
var options, identifier, comparator;
if(resolver.isRef(incomingOptions)) {
incomingOptions = { to: incomingOptions };
}
options = copyOwnProps(incomingOptions, pluginOptions);
if(!options.querySelector) {
options.querySelector = defaultQuerySelector;
}
if(!options.querySelectorAll) {
options.querySelectorAll = defaultQuerySelectorAll;
}
if(!options.on) {
options.on = defaultOn;
}
// TODO: Extend syntax for identifier and comparator
// to allow more fields, and more complex expressions
identifier = options.identifier;
options.identifier = typeof identifier == 'string' || Array.isArray(identifier)
? propertiesKey(identifier)
: identifier;
comparator = options.comparator || defaultComparator;
options.comparator = typeof comparator == 'string'
? byProperty(comparator)
: comparator;
return options;
}
function doBind(facet, options, wire) {
var target = facet.target;
return when(wire(initBindOptions(facet.options, options, wire.resolver)),
function(options) {
var to = options.to;
if (!to) throw new Error('wire/cola: "to" must be specified');
to.addSource(target, copyOwnProps(options));
return target;
}
);
}
/**
* We don't want to copy the module property from the plugin options, and
* wire adds the id property, so we need to filter that out too.
* @type {Object}
*/
excludeOptions = {
id: 1,
module: 1
};
return {
wire$plugin: function(pluginOptions) {
var options, p;
options = {};
if(arguments.length) {
pluginOptions = arguments[arguments.length-1];
for(p in pluginOptions) {
if(!(p in excludeOptions)) {
options[p] = pluginOptions[p];
}
}
}
function bindFacet(resolver, facet, wire) {
resolver.resolve(doBind(facet, options, wire));
}
return {
facets: {
bind: {
ready: bindFacet
}
}
};
}
};
/**
* Copies own properties from each src object in the arguments list
* to a new object and returns it. Properties further to the right
* win.
*
* @return {Object} a new object with own properties from all srcs.
*/
function copyOwnProps(/*srcs...*/) {
var i, len, p, src, dst;
dst = {};
for(i = 0, len = arguments.length; i < len; i++) {
src = arguments[i];
if(src) {
for(p in src) {
if(src.hasOwnProperty(p)) {
dst[p] = src[p];
}
}
}
}
return dst;
}
});
})(typeof define == 'function'
// use define for AMD if available
? define
: function(deps, factory) { module.exports = factory.apply(this, deps.map(require)); }
);
|