aurelia-bootstrap
Version:
Bootstrap components written in Aurelia.
667 lines (540 loc) • 25.7 kB
JavaScript
/* */
define(['exports', 'aurelia-logging', 'aurelia-binding', 'aurelia-templating'], function (exports, _aureliaLogging, _aureliaBinding, _aureliaTemplating) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TemplatingBindingLanguage = exports.SyntaxInterpreter = exports.ChildInterpolationBinding = exports.InterpolationBinding = exports.InterpolationBindingExpression = exports.AttributeMap = undefined;
exports.configure = configure;
var LogManager = _interopRequireWildcard(_aureliaLogging);
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {};
if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key];
}
}
newObj.default = obj;
return newObj;
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
var _class, _temp, _dec, _class2, _class3, _temp2, _class4, _temp3;
var AttributeMap = exports.AttributeMap = (_temp = _class = function () {
function AttributeMap(svg) {
this.elements = Object.create(null);
this.allElements = Object.create(null);
this.svg = svg;
this.registerUniversal('accesskey', 'accessKey');
this.registerUniversal('contenteditable', 'contentEditable');
this.registerUniversal('tabindex', 'tabIndex');
this.registerUniversal('textcontent', 'textContent');
this.registerUniversal('innerhtml', 'innerHTML');
this.registerUniversal('scrolltop', 'scrollTop');
this.registerUniversal('scrollleft', 'scrollLeft');
this.registerUniversal('readonly', 'readOnly');
this.register('label', 'for', 'htmlFor');
this.register('input', 'maxlength', 'maxLength');
this.register('input', 'minlength', 'minLength');
this.register('input', 'formaction', 'formAction');
this.register('input', 'formenctype', 'formEncType');
this.register('input', 'formmethod', 'formMethod');
this.register('input', 'formnovalidate', 'formNoValidate');
this.register('input', 'formtarget', 'formTarget');
this.register('textarea', 'maxlength', 'maxLength');
this.register('td', 'rowspan', 'rowSpan');
this.register('td', 'colspan', 'colSpan');
this.register('th', 'rowspan', 'rowSpan');
this.register('th', 'colspan', 'colSpan');
}
AttributeMap.prototype.register = function register(elementName, attributeName, propertyName) {
elementName = elementName.toLowerCase();
attributeName = attributeName.toLowerCase();
var element = this.elements[elementName] = this.elements[elementName] || Object.create(null);
element[attributeName] = propertyName;
};
AttributeMap.prototype.registerUniversal = function registerUniversal(attributeName, propertyName) {
attributeName = attributeName.toLowerCase();
this.allElements[attributeName] = propertyName;
};
AttributeMap.prototype.map = function map(elementName, attributeName) {
if (this.svg.isStandardSvgAttribute(elementName, attributeName)) {
return attributeName;
}
elementName = elementName.toLowerCase();
attributeName = attributeName.toLowerCase();
var element = this.elements[elementName];
if (element !== undefined && attributeName in element) {
return element[attributeName];
}
if (attributeName in this.allElements) {
return this.allElements[attributeName];
}
if (/(^data-)|(^aria-)|:/.test(attributeName)) {
return attributeName;
}
return (0, _aureliaBinding.camelCase)(attributeName);
};
return AttributeMap;
}(), _class.inject = [_aureliaBinding.SVGAnalyzer], _temp);
var InterpolationBindingExpression = exports.InterpolationBindingExpression = function () {
function InterpolationBindingExpression(observerLocator, targetProperty, parts, mode, lookupFunctions, attribute) {
this.observerLocator = observerLocator;
this.targetProperty = targetProperty;
this.parts = parts;
this.mode = mode;
this.lookupFunctions = lookupFunctions;
this.attribute = this.attrToRemove = attribute;
this.discrete = false;
}
InterpolationBindingExpression.prototype.createBinding = function createBinding(target) {
if (this.parts.length === 3) {
return new ChildInterpolationBinding(target, this.observerLocator, this.parts[1], this.mode, this.lookupFunctions, this.targetProperty, this.parts[0], this.parts[2]);
}
return new InterpolationBinding(this.observerLocator, this.parts, target, this.targetProperty, this.mode, this.lookupFunctions);
};
return InterpolationBindingExpression;
}();
function validateTarget(target, propertyName) {
if (propertyName === 'style') {
LogManager.getLogger('templating-binding').info('Internet Explorer does not support interpolation in "style" attributes. Use the style attribute\'s alias, "css" instead.');
} else if (target.parentElement && target.parentElement.nodeName === 'TEXTAREA' && propertyName === 'textContent') {
throw new Error('Interpolation binding cannot be used in the content of a textarea element. Use <textarea value.bind="expression"></textarea> instead.');
}
}
var InterpolationBinding = exports.InterpolationBinding = function () {
function InterpolationBinding(observerLocator, parts, target, targetProperty, mode, lookupFunctions) {
validateTarget(target, targetProperty);
this.observerLocator = observerLocator;
this.parts = parts;
this.target = target;
this.targetProperty = targetProperty;
this.targetAccessor = observerLocator.getAccessor(target, targetProperty);
this.mode = mode;
this.lookupFunctions = lookupFunctions;
}
InterpolationBinding.prototype.interpolate = function interpolate() {
if (this.isBound) {
var value = '';
var parts = this.parts;
for (var i = 0, ii = parts.length; i < ii; i++) {
value += i % 2 === 0 ? parts[i] : this['childBinding' + i].value;
}
this.targetAccessor.setValue(value, this.target, this.targetProperty);
}
};
InterpolationBinding.prototype.updateOneTimeBindings = function updateOneTimeBindings() {
for (var i = 1, ii = this.parts.length; i < ii; i += 2) {
var child = this['childBinding' + i];
if (child.mode === _aureliaBinding.bindingMode.oneTime) {
child.call();
}
}
};
InterpolationBinding.prototype.bind = function bind(source) {
if (this.isBound) {
if (this.source === source) {
return;
}
this.unbind();
}
this.source = source;
var parts = this.parts;
for (var i = 1, ii = parts.length; i < ii; i += 2) {
var binding = new ChildInterpolationBinding(this, this.observerLocator, parts[i], this.mode, this.lookupFunctions);
binding.bind(source);
this['childBinding' + i] = binding;
}
this.isBound = true;
this.interpolate();
};
InterpolationBinding.prototype.unbind = function unbind() {
if (!this.isBound) {
return;
}
this.isBound = false;
this.source = null;
var parts = this.parts;
for (var i = 1, ii = parts.length; i < ii; i += 2) {
var name = 'childBinding' + i;
this[name].unbind();
}
};
return InterpolationBinding;
}();
var ChildInterpolationBinding = exports.ChildInterpolationBinding = (_dec = (0, _aureliaBinding.connectable)(), _dec(_class2 = function () {
function ChildInterpolationBinding(target, observerLocator, sourceExpression, mode, lookupFunctions, targetProperty, left, right) {
if (target instanceof InterpolationBinding) {
this.parent = target;
} else {
validateTarget(target, targetProperty);
this.target = target;
this.targetProperty = targetProperty;
this.targetAccessor = observerLocator.getAccessor(target, targetProperty);
}
this.observerLocator = observerLocator;
this.sourceExpression = sourceExpression;
this.mode = mode;
this.lookupFunctions = lookupFunctions;
this.left = left;
this.right = right;
}
ChildInterpolationBinding.prototype.updateTarget = function updateTarget(value) {
value = value === null || value === undefined ? '' : value.toString();
if (value !== this.value) {
this.value = value;
if (this.parent) {
this.parent.interpolate();
} else {
this.targetAccessor.setValue(this.left + value + this.right, this.target, this.targetProperty);
}
}
};
ChildInterpolationBinding.prototype.call = function call() {
if (!this.isBound) {
return;
}
this.rawValue = this.sourceExpression.evaluate(this.source, this.lookupFunctions);
this.updateTarget(this.rawValue);
if (this.mode !== _aureliaBinding.bindingMode.oneTime) {
this._version++;
this.sourceExpression.connect(this, this.source);
if (this.rawValue instanceof Array) {
this.observeArray(this.rawValue);
}
this.unobserve(false);
}
};
ChildInterpolationBinding.prototype.bind = function bind(source) {
if (this.isBound) {
if (this.source === source) {
return;
}
this.unbind();
}
this.isBound = true;
this.source = source;
var sourceExpression = this.sourceExpression;
if (sourceExpression.bind) {
sourceExpression.bind(this, source, this.lookupFunctions);
}
this.rawValue = sourceExpression.evaluate(source, this.lookupFunctions);
this.updateTarget(this.rawValue);
if (this.mode === _aureliaBinding.bindingMode.oneWay) {
(0, _aureliaBinding.enqueueBindingConnect)(this);
}
};
ChildInterpolationBinding.prototype.unbind = function unbind() {
if (!this.isBound) {
return;
}
this.isBound = false;
var sourceExpression = this.sourceExpression;
if (sourceExpression.unbind) {
sourceExpression.unbind(this, this.source);
}
this.source = null;
this.value = null;
this.rawValue = null;
this.unobserve(true);
};
ChildInterpolationBinding.prototype.connect = function connect(evaluate) {
if (!this.isBound) {
return;
}
if (evaluate) {
this.rawValue = this.sourceExpression.evaluate(this.source, this.lookupFunctions);
this.updateTarget(this.rawValue);
}
this.sourceExpression.connect(this, this.source);
if (this.rawValue instanceof Array) {
this.observeArray(this.rawValue);
}
};
return ChildInterpolationBinding;
}()) || _class2);
var SyntaxInterpreter = exports.SyntaxInterpreter = (_temp2 = _class3 = function () {
function SyntaxInterpreter(parser, observerLocator, eventManager, attributeMap) {
this.parser = parser;
this.observerLocator = observerLocator;
this.eventManager = eventManager;
this.attributeMap = attributeMap;
}
SyntaxInterpreter.prototype.interpret = function interpret(resources, element, info, existingInstruction, context) {
if (info.command in this) {
return this[info.command](resources, element, info, existingInstruction, context);
}
return this.handleUnknownCommand(resources, element, info, existingInstruction, context);
};
SyntaxInterpreter.prototype.handleUnknownCommand = function handleUnknownCommand(resources, element, info, existingInstruction, context) {
LogManager.getLogger('templating-binding').warn('Unknown binding command.', info);
return existingInstruction;
};
SyntaxInterpreter.prototype.determineDefaultBindingMode = function determineDefaultBindingMode(element, attrName, context) {
var tagName = element.tagName.toLowerCase();
if (tagName === 'input' && (attrName === 'value' || attrName === 'files') && element.type !== 'checkbox' && element.type !== 'radio' || tagName === 'input' && attrName === 'checked' && (element.type === 'checkbox' || element.type === 'radio') || (tagName === 'textarea' || tagName === 'select') && attrName === 'value' || (attrName === 'textcontent' || attrName === 'innerhtml') && element.contentEditable === 'true' || attrName === 'scrolltop' || attrName === 'scrollleft') {
return _aureliaBinding.bindingMode.twoWay;
}
if (context && attrName in context.attributes && context.attributes[attrName] && context.attributes[attrName].defaultBindingMode >= _aureliaBinding.bindingMode.oneTime) {
return context.attributes[attrName].defaultBindingMode;
}
return _aureliaBinding.bindingMode.oneWay;
};
SyntaxInterpreter.prototype.bind = function bind(resources, element, info, existingInstruction, context) {
var instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(info.attrName);
instruction.attributes[info.attrName] = new _aureliaBinding.BindingExpression(this.observerLocator, this.attributeMap.map(element.tagName, info.attrName), this.parser.parse(info.attrValue), info.defaultBindingMode || this.determineDefaultBindingMode(element, info.attrName, context), resources.lookupFunctions);
return instruction;
};
SyntaxInterpreter.prototype.trigger = function trigger(resources, element, info) {
return new _aureliaBinding.ListenerExpression(this.eventManager, info.attrName, this.parser.parse(info.attrValue), false, true, resources.lookupFunctions);
};
SyntaxInterpreter.prototype.delegate = function delegate(resources, element, info) {
return new _aureliaBinding.ListenerExpression(this.eventManager, info.attrName, this.parser.parse(info.attrValue), true, true, resources.lookupFunctions);
};
SyntaxInterpreter.prototype.call = function call(resources, element, info, existingInstruction) {
var instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(info.attrName);
instruction.attributes[info.attrName] = new _aureliaBinding.CallExpression(this.observerLocator, info.attrName, this.parser.parse(info.attrValue), resources.lookupFunctions);
return instruction;
};
SyntaxInterpreter.prototype.options = function options(resources, element, info, existingInstruction, context) {
var instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(info.attrName);
var attrValue = info.attrValue;
var language = this.language;
var name = null;
var target = '';
var current = void 0;
var i = void 0;
var ii = void 0;
var inString = false;
var inEscape = false;
for (i = 0, ii = attrValue.length; i < ii; ++i) {
current = attrValue[i];
if (current === ';' && !inString) {
info = language.inspectAttribute(resources, '?', name, target.trim());
language.createAttributeInstruction(resources, element, info, instruction, context);
if (!instruction.attributes[info.attrName]) {
instruction.attributes[info.attrName] = info.attrValue;
}
target = '';
name = null;
} else if (current === ':' && name === null) {
name = target.trim();
target = '';
} else if (current === '\\') {
target += current;
inEscape = true;
continue;
} else {
target += current;
if (name !== null && inEscape === false && current === '\'') {
inString = !inString;
}
}
inEscape = false;
}
if (name !== null) {
info = language.inspectAttribute(resources, '?', name, target.trim());
language.createAttributeInstruction(resources, element, info, instruction, context);
if (!instruction.attributes[info.attrName]) {
instruction.attributes[info.attrName] = info.attrValue;
}
}
return instruction;
};
SyntaxInterpreter.prototype['for'] = function _for(resources, element, info, existingInstruction) {
var parts = void 0;
var keyValue = void 0;
var instruction = void 0;
var attrValue = void 0;
var isDestructuring = void 0;
attrValue = info.attrValue;
isDestructuring = attrValue.match(/^ *[[].+[\]]/);
parts = isDestructuring ? attrValue.split('of ') : attrValue.split(' of ');
if (parts.length !== 2) {
throw new Error('Incorrect syntax for "for". The form is: "$local of $items" or "[$key, $value] of $items".');
}
instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(info.attrName);
if (isDestructuring) {
keyValue = parts[0].replace(/[[\]]/g, '').replace(/,/g, ' ').replace(/\s+/g, ' ').trim().split(' ');
instruction.attributes.key = keyValue[0];
instruction.attributes.value = keyValue[1];
} else {
instruction.attributes.local = parts[0];
}
instruction.attributes.items = new _aureliaBinding.BindingExpression(this.observerLocator, 'items', this.parser.parse(parts[1]), _aureliaBinding.bindingMode.oneWay, resources.lookupFunctions);
return instruction;
};
SyntaxInterpreter.prototype['two-way'] = function twoWay(resources, element, info, existingInstruction) {
var instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(info.attrName);
instruction.attributes[info.attrName] = new _aureliaBinding.BindingExpression(this.observerLocator, this.attributeMap.map(element.tagName, info.attrName), this.parser.parse(info.attrValue), _aureliaBinding.bindingMode.twoWay, resources.lookupFunctions);
return instruction;
};
SyntaxInterpreter.prototype['one-way'] = function oneWay(resources, element, info, existingInstruction) {
var instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(info.attrName);
instruction.attributes[info.attrName] = new _aureliaBinding.BindingExpression(this.observerLocator, this.attributeMap.map(element.tagName, info.attrName), this.parser.parse(info.attrValue), _aureliaBinding.bindingMode.oneWay, resources.lookupFunctions);
return instruction;
};
SyntaxInterpreter.prototype['one-time'] = function oneTime(resources, element, info, existingInstruction) {
var instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(info.attrName);
instruction.attributes[info.attrName] = new _aureliaBinding.BindingExpression(this.observerLocator, this.attributeMap.map(element.tagName, info.attrName), this.parser.parse(info.attrValue), _aureliaBinding.bindingMode.oneTime, resources.lookupFunctions);
return instruction;
};
return SyntaxInterpreter;
}(), _class3.inject = [_aureliaBinding.Parser, _aureliaBinding.ObserverLocator, _aureliaBinding.EventManager, AttributeMap], _temp2);
var info = {};
var TemplatingBindingLanguage = exports.TemplatingBindingLanguage = (_temp3 = _class4 = function (_BindingLanguage) {
_inherits(TemplatingBindingLanguage, _BindingLanguage);
function TemplatingBindingLanguage(parser, observerLocator, syntaxInterpreter, attributeMap) {
var _this = _possibleConstructorReturn(this, _BindingLanguage.call(this));
_this.parser = parser;
_this.observerLocator = observerLocator;
_this.syntaxInterpreter = syntaxInterpreter;
_this.emptyStringExpression = _this.parser.parse('\'\'');
syntaxInterpreter.language = _this;
_this.attributeMap = attributeMap;
return _this;
}
TemplatingBindingLanguage.prototype.inspectAttribute = function inspectAttribute(resources, elementName, attrName, attrValue) {
var parts = attrName.split('.');
info.defaultBindingMode = null;
if (parts.length === 2) {
info.attrName = parts[0].trim();
info.attrValue = attrValue;
info.command = parts[1].trim();
if (info.command === 'ref') {
info.expression = new _aureliaBinding.NameExpression(this.parser.parse(attrValue), info.attrName, resources.lookupFunctions);
info.command = null;
info.attrName = 'ref';
} else {
info.expression = null;
}
} else if (attrName === 'ref') {
info.attrName = attrName;
info.attrValue = attrValue;
info.command = null;
info.expression = new _aureliaBinding.NameExpression(this.parser.parse(attrValue), 'element', resources.lookupFunctions);
} else {
info.attrName = attrName;
info.attrValue = attrValue;
info.command = null;
var interpolationParts = this.parseInterpolation(resources, attrValue);
if (interpolationParts === null) {
info.expression = null;
} else {
info.expression = new InterpolationBindingExpression(this.observerLocator, this.attributeMap.map(elementName, attrName), interpolationParts, _aureliaBinding.bindingMode.oneWay, resources.lookupFunctions, attrName);
}
}
return info;
};
TemplatingBindingLanguage.prototype.createAttributeInstruction = function createAttributeInstruction(resources, element, theInfo, existingInstruction, context) {
var instruction = void 0;
if (theInfo.expression) {
if (theInfo.attrName === 'ref') {
return theInfo.expression;
}
instruction = existingInstruction || _aureliaTemplating.BehaviorInstruction.attribute(theInfo.attrName);
instruction.attributes[theInfo.attrName] = theInfo.expression;
} else if (theInfo.command) {
instruction = this.syntaxInterpreter.interpret(resources, element, theInfo, existingInstruction, context);
}
return instruction;
};
TemplatingBindingLanguage.prototype.inspectTextContent = function inspectTextContent(resources, value) {
var parts = this.parseInterpolation(resources, value);
if (parts === null) {
return null;
}
return new InterpolationBindingExpression(this.observerLocator, 'textContent', parts, _aureliaBinding.bindingMode.oneWay, resources.lookupFunctions, 'textContent');
};
TemplatingBindingLanguage.prototype.parseInterpolation = function parseInterpolation(resources, value) {
var i = value.indexOf('${', 0);
var ii = value.length;
var char = void 0;
var pos = 0;
var open = 0;
var quote = null;
var interpolationStart = void 0;
var parts = void 0;
var partIndex = 0;
while (i >= 0 && i < ii - 2) {
open = 1;
interpolationStart = i;
i += 2;
do {
char = value[i];
i++;
if (char === "'" || char === '"') {
if (quote === null) {
quote = char;
} else if (quote === char) {
quote = null;
}
continue;
}
if (char === '\\') {
i++;
continue;
}
if (quote !== null) {
continue;
}
if (char === '{') {
open++;
} else if (char === '}') {
open--;
}
} while (open > 0 && i < ii);
if (open === 0) {
parts = parts || [];
if (value[interpolationStart - 1] === '\\' && value[interpolationStart - 2] !== '\\') {
parts[partIndex] = value.substring(pos, interpolationStart - 1) + value.substring(interpolationStart, i);
partIndex++;
parts[partIndex] = this.emptyStringExpression;
partIndex++;
} else {
parts[partIndex] = value.substring(pos, interpolationStart);
partIndex++;
parts[partIndex] = this.parser.parse(value.substring(interpolationStart + 2, i - 1));
partIndex++;
}
pos = i;
i = value.indexOf('${', i);
} else {
break;
}
}
if (partIndex === 0) {
return null;
}
parts[partIndex] = value.substr(pos);
return parts;
};
return TemplatingBindingLanguage;
}(_aureliaTemplating.BindingLanguage), _class4.inject = [_aureliaBinding.Parser, _aureliaBinding.ObserverLocator, SyntaxInterpreter, AttributeMap], _temp3);
function configure(config) {
config.container.registerSingleton(_aureliaTemplating.BindingLanguage, TemplatingBindingLanguage);
config.container.registerAlias(_aureliaTemplating.BindingLanguage, TemplatingBindingLanguage);
}
});