UNPKG

14.2 kBJavaScriptView Raw
1(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.armature = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2'use strict';
3/* eslint-disable no-unused-vars */
4var hasOwnProperty = Object.prototype.hasOwnProperty;
5var propIsEnumerable = Object.prototype.propertyIsEnumerable;
6function toObject(val) {
7 if (val === null || val === undefined) {
8 throw new TypeError('Object.assign cannot be called with null or undefined');
9 }
10 return Object(val);
11}
12function shouldUseNative() {
13 try {
14 if (!Object.assign) {
15 return false;
16 }
17 // Detect buggy property enumeration order in older V8 versions.
18 // https://bugs.chromium.org/p/v8/issues/detail?id=4118
19 var test1 = new String('abc'); // eslint-disable-line
20 test1[5] = 'de';
21 if (Object.getOwnPropertyNames(test1)[0] === '5') {
22 return false;
23 }
24 // https://bugs.chromium.org/p/v8/issues/detail?id=3056
25 var test2 = {};
26 for (var i = 0; i < 10; i++) {
27 test2['_' + String.fromCharCode(i)] = i;
28 }
29 var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
30 return test2[n];
31 });
32 if (order2.join('') !== '0123456789') {
33 return false;
34 }
35 // https://bugs.chromium.org/p/v8/issues/detail?id=3056
36 var test3 = {};
37 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
38 test3[letter] = letter;
39 });
40 if (Object.keys(Object.assign({}, test3)).join('') !==
41 'abcdefghijklmnopqrst') {
42 return false;
43 }
44 return true;
45 }
46 catch (e) {
47 // We don't expect any of the above to throw, but better to be safe.
48 return false;
49 }
50}
51module.exports = shouldUseNative() ? Object.assign : function (target, source) {
52 var from;
53 var to = toObject(target);
54 var symbols;
55 for (var s = 1; s < arguments.length; s++) {
56 from = Object(arguments[s]);
57 for (var key in from) {
58 if (hasOwnProperty.call(from, key)) {
59 to[key] = from[key];
60 }
61 }
62 if (Object.getOwnPropertySymbols) {
63 symbols = Object.getOwnPropertySymbols(from);
64 for (var i = 0; i < symbols.length; i++) {
65 if (propIsEnumerable.call(from, symbols[i])) {
66 to[symbols[i]] = from[symbols[i]];
67 }
68 }
69 }
70 }
71 return to;
72};
73
74},{}],2:[function(require,module,exports){
75
76},{}],3:[function(require,module,exports){
77"use strict";
78var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
79 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
80 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
81 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
82 return c > 3 && r && Object.defineProperty(target, key, r), r;
83};
84var UUID_1 = require("./UUID");
85var Decorators_1 = require("./Decorators");
86var ComponentStore_1 = require("./ComponentStore");
87var assign = require("object-assign");
88/**
89 * The base for all Armature components.
90 */
91var Component = (function () {
92 /**
93 * Creates a new component with the given data.
94 *
95 * @param data The data to initialize the component with
96 */
97 function Component(data) {
98 /**
99 * The components this component contains.
100 */
101 this.$children = [];
102 this.$data = data;
103 this.$id = UUID_1.default.get();
104 }
105 /**
106 * Creates or retrieves a component that is attached to another component.
107 * Using the same parent and 'label' parameters will retrieve the same object.
108 * Pass null to the data attribute to throw if a new component is created.
109 *
110 * @param parent The component to attach to
111 * @param label The label to use when creating or retrieving the component
112 * @param data The data to initialize the component with if it hasn't been created
113 */
114 Component.$for = function (parent, label, data) {
115 var identifier = this.$getIdentifier(label);
116 var existing = parent.$children.find(function (v) { return v.$getIdentifier() === identifier; });
117 if (existing) {
118 return existing;
119 }
120 if (data === null) {
121 throw new Error("Subcomponent was created with no data!");
122 }
123 var inst = new this(data);
124 inst.$label = label;
125 inst.$parent = parent;
126 parent.$children.push(inst);
127 return inst;
128 };
129 /**
130 * Serializes the component's data for potential recreation.
131 */
132 Component.prototype.$serializeData = function () {
133 return assign({}, this.$data);
134 };
135 /**
136 * Returns a way to uniquely identify this component type.
137 */
138 Component.$getTypeName = function () {
139 var classString = this.$classNames.map(function (v) { return "." + v; }).join("");
140 return this.$tagName + classString;
141 };
142 /**
143 * Creates an object that can be used to recreate this component.
144 */
145 Component.prototype.$deflate = function () {
146 var thisClass = this.constructor;
147 return {
148 type: thisClass.$getTypeName(),
149 data: this.$serializeData(),
150 id: this.$id,
151 label: this.$label,
152 children: this.$children.map(function (v) { return v.$deflate(); })
153 };
154 };
155 /**
156 * Creates an instance of a component from data generated by $deflate.
157 *
158 * @param deflated The deflated component data to use
159 * @param inst An optional existing object to inflate
160 */
161 Component.$inflate = function (deflated, inst) {
162 if (!inst) {
163 inst = new this(deflated.data);
164 }
165 inst.$id = deflated.id;
166 inst.$label = deflated.label;
167 if (deflated.children) {
168 for (var _i = 0, _a = deflated.children; _i < _a.length; _i++) {
169 var child = _a[_i];
170 var thatClass = ComponentStore_1.default.get(child.type);
171 var childInst = thatClass.$for(inst, child.label, child.data);
172 thatClass.$inflate(child, childInst);
173 }
174 }
175 return inst;
176 };
177 /**
178 * Returns an identifier that should be unique among a component's siblings.
179 *
180 * @param label The label of the component to create the label for
181 */
182 Component.$getIdentifier = function (label) {
183 return this.$getTypeName() + "__" + label;
184 };
185 /**
186 * Returns an identifier that should be unique among a component's siblings.
187 */
188 Component.prototype.$getIdentifier = function () {
189 var thisClass = this.constructor;
190 return thisClass.$getIdentifier(this.$label);
191 };
192 Component.prototype.toString = function () {
193 return this.$getHTML();
194 };
195 /**
196 * Builds the HTML associated with this component.
197 */
198 Component.prototype.$getHTML = function () {
199 var thisClass = this.constructor;
200 return "<" + thisClass.$tagName + "\n\t\t\tclass=\"" + thisClass.$classNames.join(" ") + "\"\n\t\t\tdata-armid=\"" + this.$id + "\"\n\t\t\t" + (this.$label ? "data-label=\"" + this.$label + "\"" : "") + "\n\t\t\t>" + this.$template(this) + "</" + thisClass.$tagName + ">";
201 };
202 /**
203 * Creates an HTML element for this component if one does not exist.
204 */
205 Component.prototype.$ensureElement = function () {
206 if (!this.$element) {
207 var thisClass = this.constructor;
208 var el = document.createElement(thisClass.$tagName);
209 (_a = el.classList).add.apply(_a, thisClass.$classNames);
210 this.$attachTo(el);
211 }
212 var _a;
213 };
214 /**
215 * Attaches this component to an existing HTML element.
216 *
217 * @param element The element to attach to
218 */
219 Component.prototype.$attachTo = function (element) {
220 this.$element = element;
221 element.setAttribute("data-armid", this.$id.toString());
222 if (this.$label) {
223 element.setAttribute("data-label", this.$label);
224 }
225 };
226 /**
227 * Attempts to locate this component's element from a parent element.
228 *
229 * @param parent The element to search in
230 */
231 Component.prototype.$locate = function (parent) {
232 if (parent === void 0) { parent = document.body; }
233 var thisClass = this.constructor;
234 var el = parent.querySelector(thisClass.$tagName + "[data-armid=\"" + this.$id + "\"]");
235 if (el) {
236 this.$element = el;
237 this.$children.forEach(function (child) {
238 child.$locate(el);
239 });
240 return el;
241 }
242 };
243 /**
244 * Evaluates the component's template and puts the result into the component's element
245 */
246 Component.prototype.$render = function () {
247 this.$element.innerHTML = this.$template(this);
248 };
249 /**
250 * Attaches event handlers for the component and its children.
251 * Often used after $render, or as part of $reify.
252 */
253 Component.prototype.$hydrate = function () {
254 for (var _i = 0, _a = this.$children; _i < _a.length; _i++) {
255 var child = _a[_i];
256 if (child.$locate(this.$element)) {
257 child.$hydrate();
258 }
259 }
260 };
261 /**
262 * Ensures this component's element exists, then renders and hydrates it.
263 */
264 Component.prototype.$reify = function () {
265 this.$ensureElement();
266 this.$render();
267 this.$hydrate();
268 };
269 /**
270 * A list of class names to apply to the component's root element.
271 */
272 Component.$classNames = [];
273 Component = __decorate([
274 Decorators_1.TagName("arm-component"),
275 Decorators_1.Template(function () { return ""; })
276 ], Component);
277 return Component;
278}());
279exports.Component = Component;
280
281},{"./ComponentStore":4,"./Decorators":5,"./UUID":7,"object-assign":1}],4:[function(require,module,exports){
282"use strict";
283var ComponentStore = {
284 store: new Map(),
285 register: function (componentType) {
286 this.store.set(componentType.$getTypeName(), componentType);
287 },
288 get: function (typeName) {
289 return this.store.get(typeName);
290 }
291};
292Object.defineProperty(exports, "__esModule", { value: true });
293exports.default = ComponentStore;
294
295},{}],5:[function(require,module,exports){
296"use strict";
297/**
298 * Attaches a template to a Component
299 * @param template The template to attach
300 */
301function Template(template) {
302 return function (target) {
303 target.prototype.$template = template;
304 };
305}
306exports.Template = Template;
307/**
308 * Sets the HTML tag name of a Component
309 * @param name The tag name to attach
310 */
311function TagName(name) {
312 return function (target) {
313 target.$tagName = name;
314 };
315}
316exports.TagName = TagName;
317/**
318 * Attaches HTML class names to a Component's element
319 * @param names The names to attach
320 */
321function ClassNames() {
322 var names = [];
323 for (var _i = 0; _i < arguments.length; _i++) {
324 names[_i - 0] = arguments[_i];
325 }
326 return function (target) {
327 (_a = target.$classNames).push.apply(_a, names);
328 var _a;
329 };
330}
331exports.ClassNames = ClassNames;
332
333},{}],6:[function(require,module,exports){
334"use strict";
335var ComponentStore_1 = require("./ComponentStore");
336function Properties(data) {
337 return function (target) {
338 target.prototype.$template = data.template;
339 target.$tagName = data.tag;
340 if (data.classes) {
341 target.$classNames = target.$classNames.concat(data.classes);
342 }
343 ComponentStore_1.default.register(target);
344 };
345}
346exports.Properties = Properties;
347
348},{"./ComponentStore":4}],7:[function(require,module,exports){
349"use strict";
350var randomBuffer = new Uint16Array(1);
351/**
352 * Returns a random number on [0, 1)
353 * Uses window.crypto if available
354 */
355function rand() {
356 if (typeof crypto !== "undefined") {
357 crypto.getRandomValues(randomBuffer);
358 return randomBuffer[0] / Math.pow(2, 16);
359 }
360 else {
361 return Math.random();
362 }
363}
364var UUID = {
365 /**
366 * Generates a v4 UUID.
367 */
368 get: function () {
369 var template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
370 var value = template.replace(/[xy]/g, function (c) {
371 var v = Math.floor(rand() * 16);
372 if (c !== "x") {
373 v = v & 0x3 | 0x8;
374 }
375 return v.toString(16);
376 });
377 return value;
378 }
379};
380Object.defineProperty(exports, "__esModule", { value: true });
381exports.default = UUID;
382
383},{}],8:[function(require,module,exports){
384"use strict";
385function __export(m) {
386 for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
387}
388__export(require("./Component"));
389var ComponentStore_1 = require("./ComponentStore");
390exports.ComponentStore = ComponentStore_1.default;
391var UUID_1 = require("./UUID");
392exports.UUID = UUID_1.default;
393__export(require("./Decorators"));
394__export(require("./Properties"));
395
396},{"./Component":3,"./ComponentStore":4,"./Decorators":5,"./Properties":6,"./UUID":7}],9:[function(require,module,exports){
397arguments[4][2][0].apply(exports,arguments)
398},{"dup":2}]},{},[8,2,9])(9)
399});
400
401
402//# sourceMappingURL=bundle.js.map