UNPKG

can

Version:

MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.

1,362 lines (1,354 loc) 1.38 MB
/*[process-shim]*/ (function(global, env) { // jshint ignore:line if (typeof process === "undefined") { global.process = { argv: [], cwd: function() { return ""; }, browser: true, env: { NODE_ENV: env || "development" }, version: "", platform: global.navigator && global.navigator.userAgent && /Windows/.test(global.navigator.userAgent) ? "win" : "" }; } })( typeof self == "object" && self.Object == Object ? self : typeof process === "object" && Object.prototype.toString.call(process) === "[object process]" ? global : window, "development" ); /*[global-shim-start]*/ (function(exports, global, doEval) { // jshint ignore:line var origDefine = global.define; var get = function(name) { var parts = name.split("."), cur = global, i; for (i = 0; i < parts.length; i++) { if (!cur) { break; } cur = cur[parts[i]]; } return cur; }; var set = function(name, val) { var parts = name.split("."), cur = global, i, part, next; for (i = 0; i < parts.length - 1; i++) { part = parts[i]; next = cur[part]; if (!next) { next = cur[part] = {}; } cur = next; } part = parts[parts.length - 1]; cur[part] = val; }; var useDefault = function(mod) { if (!mod || !mod.__esModule) return false; var esProps = { __esModule: true, default: true }; for (var p in mod) { if (!esProps[p]) return false; } return true; }; var hasCjsDependencies = function(deps) { return ( deps[0] === "require" && deps[1] === "exports" && deps[2] === "module" ); }; var modules = (global.define && global.define.modules) || (global._define && global._define.modules) || {}; var ourDefine = (global.define = function(moduleName, deps, callback) { var module; if (typeof deps === "function") { callback = deps; deps = []; } var args = [], i; for (i = 0; i < deps.length; i++) { args.push( exports[deps[i]] ? get(exports[deps[i]]) : modules[deps[i]] || get(deps[i]) ); } // CJS has no dependencies but 3 callback arguments if (hasCjsDependencies(deps) || (!deps.length && callback.length)) { module = { exports: {} }; args[0] = function(name) { return exports[name] ? get(exports[name]) : modules[name]; }; args[1] = module.exports; args[2] = module; } // Babel uses the exports and module object. else if (!args[0] && deps[0] === "exports") { module = { exports: {} }; args[0] = module.exports; if (deps[1] === "module") { args[1] = module; } } else if (!args[0] && deps[0] === "module") { args[0] = { id: moduleName }; } global.define = origDefine; var result = callback ? callback.apply(null, args) : undefined; global.define = ourDefine; // Favor CJS module.exports over the return value result = module && module.exports ? module.exports : result; modules[moduleName] = result; // Set global exports var globalExport = exports[moduleName]; if (globalExport && !get(globalExport)) { if (useDefault(result)) { result = result["default"]; } set(globalExport, result); } }); global.define.orig = origDefine; global.define.modules = modules; global.define.amd = true; ourDefine("@loader", [], function() { // shim for @@global-helpers var noop = function() {}; return { get: function() { return { prepareGlobal: noop, retrieveGlobal: noop }; }, global: global, __exec: function(__load) { doEval(__load.source, global); } }; }); })( { jquery: "jQuery", "can-namespace": "can", kefir: "Kefir", "validate.js": "validate", react: "React" }, typeof self == "object" && self.Object == Object ? self : typeof process === "object" && Object.prototype.toString.call(process) === "[object process]" ? global : window, function(__$source__, __$global__) { // jshint ignore:line eval("(function() { " + __$source__ + " \n }).call(__$global__);"); } ); /*can-namespace@1.0.0#can-namespace*/ define('can-namespace', function (require, exports, module) { module.exports = {}; }); /*can-symbol@1.7.0#can-symbol*/ define('can-symbol', [ 'require', 'exports', 'module', 'can-namespace' ], function (require, exports, module) { (function (global, require, exports, module) { 'use strict'; var namespace = require('can-namespace'); var supportsNativeSymbols = function () { var symbolExists = typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'; if (!symbolExists) { return false; } var symbol = Symbol('a symbol for testing symbols'); return typeof symbol === 'symbol'; }(); var CanSymbol; if (supportsNativeSymbols) { CanSymbol = Symbol; } else { var symbolNum = 0; CanSymbol = function CanSymbolPolyfill(description) { var symbolValue = '@@symbol' + symbolNum++ + description; var symbol = {}; Object.defineProperties(symbol, { toString: { value: function () { return symbolValue; } } }); return symbol; }; var descriptionToSymbol = {}; var symbolToDescription = {}; CanSymbol.for = function (description) { var symbol = descriptionToSymbol[description]; if (!symbol) { symbol = descriptionToSymbol[description] = CanSymbol(description); symbolToDescription[symbol] = description; } return symbol; }; CanSymbol.keyFor = function (symbol) { return symbolToDescription[symbol]; }; [ 'hasInstance', 'isConcatSpreadable', 'iterator', 'match', 'prototype', 'replace', 'search', 'species', 'split', 'toPrimitive', 'toStringTag', 'unscopables' ].forEach(function (name) { CanSymbol[name] = CanSymbol('Symbol.' + name); }); } [ 'isMapLike', 'isListLike', 'isValueLike', 'isFunctionLike', 'isScopeLike', 'getOwnKeys', 'getOwnKeyDescriptor', 'proto', 'getOwnEnumerableKeys', 'hasOwnKey', 'hasKey', 'size', 'getName', 'getIdentity', 'assignDeep', 'updateDeep', 'getValue', 'setValue', 'getKeyValue', 'setKeyValue', 'updateValues', 'addValue', 'removeValues', 'apply', 'new', 'onValue', 'offValue', 'onKeyValue', 'offKeyValue', 'getKeyDependencies', 'getValueDependencies', 'keyHasDependencies', 'valueHasDependencies', 'onKeys', 'onKeysAdded', 'onKeysRemoved', 'onPatches' ].forEach(function (name) { CanSymbol.for('can.' + name); }); module.exports = namespace.Symbol = CanSymbol; }(function () { return this; }(), require, exports, module)); }); /*can-reflect@1.19.2#reflections/helpers*/ define('can-reflect/reflections/helpers', [ 'require', 'exports', 'module', 'can-symbol' ], function (require, exports, module) { 'use strict'; var canSymbol = require('can-symbol'); module.exports = { makeGetFirstSymbolValue: function (symbolNames) { var symbols = symbolNames.map(function (name) { return canSymbol.for(name); }); var length = symbols.length; return function getFirstSymbol(obj) { var index = -1; while (++index < length) { if (obj[symbols[index]] !== undefined) { return obj[symbols[index]]; } } }; }, hasLength: function (list) { var type = typeof list; if (type === 'string' || Array.isArray(list)) { return true; } var length = list && (type !== 'boolean' && type !== 'number' && 'length' in list) && list.length; return typeof list !== 'function' && (length === 0 || typeof length === 'number' && length > 0 && length - 1 in list); } }; }); /*can-reflect@1.19.2#reflections/type/type*/ define('can-reflect/reflections/type/type', [ 'require', 'exports', 'module', 'can-symbol', 'can-reflect/reflections/helpers' ], function (require, exports, module) { 'use strict'; var canSymbol = require('can-symbol'); var helpers = require('can-reflect/reflections/helpers'); var plainFunctionPrototypePropertyNames = Object.getOwnPropertyNames(function () { }.prototype); var plainFunctionPrototypeProto = Object.getPrototypeOf(function () { }.prototype); function isConstructorLike(func) { var value = func[canSymbol.for('can.new')]; if (value !== undefined) { return value; } if (typeof func !== 'function') { return false; } var prototype = func.prototype; if (!prototype) { return false; } if (plainFunctionPrototypeProto !== Object.getPrototypeOf(prototype)) { return true; } var propertyNames = Object.getOwnPropertyNames(prototype); if (propertyNames.length === plainFunctionPrototypePropertyNames.length) { for (var i = 0, len = propertyNames.length; i < len; i++) { if (propertyNames[i] !== plainFunctionPrototypePropertyNames[i]) { return true; } } return false; } else { return true; } } var getNewOrApply = helpers.makeGetFirstSymbolValue([ 'can.new', 'can.apply' ]); function isFunctionLike(obj) { var result, symbolValue = !!obj && obj[canSymbol.for('can.isFunctionLike')]; if (symbolValue !== undefined) { return symbolValue; } result = getNewOrApply(obj); if (result !== undefined) { return !!result; } return typeof obj === 'function'; } function isPrimitive(obj) { var type = typeof obj; if (obj == null || type !== 'function' && type !== 'object') { return true; } else { return false; } } var coreHasOwn = Object.prototype.hasOwnProperty; var funcToString = Function.prototype.toString; var objectCtorString = funcToString.call(Object); function isPlainObject(obj) { if (!obj || typeof obj !== 'object') { return false; } var proto = Object.getPrototypeOf(obj); if (proto === Object.prototype || proto === null) { return true; } var Constructor = coreHasOwn.call(proto, 'constructor') && proto.constructor; return typeof Constructor === 'function' && Constructor instanceof Constructor && funcToString.call(Constructor) === objectCtorString; } function isBuiltIn(obj) { if (isPrimitive(obj) || Array.isArray(obj) || isPlainObject(obj) || Object.prototype.toString.call(obj) !== '[object Object]' && Object.prototype.toString.call(obj).indexOf('[object ') !== -1) { return true; } else { return false; } } function isValueLike(obj) { var symbolValue; if (isPrimitive(obj)) { return true; } symbolValue = obj[canSymbol.for('can.isValueLike')]; if (typeof symbolValue !== 'undefined') { return symbolValue; } var value = obj[canSymbol.for('can.getValue')]; if (value !== undefined) { return !!value; } } function isMapLike(obj) { if (isPrimitive(obj)) { return false; } var isMapLike = obj[canSymbol.for('can.isMapLike')]; if (typeof isMapLike !== 'undefined') { return !!isMapLike; } var value = obj[canSymbol.for('can.getKeyValue')]; if (value !== undefined) { return !!value; } return true; } var onValueSymbol = canSymbol.for('can.onValue'), onKeyValueSymbol = canSymbol.for('can.onKeyValue'), onPatchesSymbol = canSymbol.for('can.onPatches'); function isObservableLike(obj) { if (isPrimitive(obj)) { return false; } return Boolean(obj[onValueSymbol] || obj[onKeyValueSymbol] || obj[onPatchesSymbol]); } function isListLike(list) { var symbolValue, type = typeof list; if (type === 'string') { return true; } if (isPrimitive(list)) { return false; } symbolValue = list[canSymbol.for('can.isListLike')]; if (typeof symbolValue !== 'undefined') { return symbolValue; } var value = list[canSymbol.iterator]; if (value !== undefined) { return !!value; } if (Array.isArray(list)) { return true; } return helpers.hasLength(list); } var supportsNativeSymbols = function () { var symbolExists = typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'; if (!symbolExists) { return false; } var symbol = Symbol('a symbol for testing symbols'); return typeof symbol === 'symbol'; }(); var isSymbolLike; if (supportsNativeSymbols) { isSymbolLike = function (symbol) { return typeof symbol === 'symbol'; }; } else { var symbolStart = '@@symbol'; isSymbolLike = function (symbol) { if (typeof symbol === 'object' && !Array.isArray(symbol)) { return symbol.toString().substr(0, symbolStart.length) === symbolStart; } else { return false; } }; } var fnKeys = [ 'get', 'set', 'peek', 'computeData', 'add', 'getScope', 'getHelperOrPartial', 'getTemplateContext', 'cloneFromRef' ]; function isScopeLike(obj) { if (isPrimitive(obj)) { return false; } var isScopeLike = obj[canSymbol.for('can.isScopeLike')]; if (typeof isScopeLike !== 'undefined') { return !!isScopeLike; } return fnKeys.every(function (key) { return typeof obj[key] === 'function'; }) && '_context' in obj && obj._meta && typeof obj._meta === 'object'; } module.exports = { isConstructorLike: isConstructorLike, isFunctionLike: isFunctionLike, isListLike: isListLike, isMapLike: isMapLike, isObservableLike: isObservableLike, isScopeLike: isScopeLike, isPrimitive: isPrimitive, isBuiltIn: isBuiltIn, isValueLike: isValueLike, isSymbolLike: isSymbolLike, isMoreListLikeThanMapLike: function (obj) { if (Array.isArray(obj)) { return true; } if (obj instanceof Array) { return true; } if (obj == null) { return false; } var value = obj[canSymbol.for('can.isMoreListLikeThanMapLike')]; if (value !== undefined) { return value; } var isListLike = this.isListLike(obj), isMapLike = this.isMapLike(obj); if (isListLike && !isMapLike) { return true; } else if (!isListLike && isMapLike) { return false; } }, isIteratorLike: function (obj) { return obj && typeof obj === 'object' && typeof obj.next === 'function' && obj.next.length === 0; }, isPromise: function (obj) { return obj instanceof Promise || Object.prototype.toString.call(obj) === '[object Promise]'; }, isPlainObject: isPlainObject }; }); /*can-reflect@1.19.2#reflections/call/call*/ define('can-reflect/reflections/call/call', [ 'require', 'exports', 'module', 'can-symbol', 'can-reflect/reflections/type/type' ], function (require, exports, module) { 'use strict'; var canSymbol = require('can-symbol'); var typeReflections = require('can-reflect/reflections/type/type'); module.exports = { call: function (func, context) { var args = [].slice.call(arguments, 2); var apply = func[canSymbol.for('can.apply')]; if (apply) { return apply.call(func, context, args); } else { return func.apply(context, args); } }, apply: function (func, context, args) { var apply = func[canSymbol.for('can.apply')]; if (apply) { return apply.call(func, context, args); } else { return func.apply(context, args); } }, 'new': function (func) { var args = [].slice.call(arguments, 1); var makeNew = func[canSymbol.for('can.new')]; if (makeNew) { return makeNew.apply(func, args); } else { var context = Object.create(func.prototype); var ret = func.apply(context, args); if (typeReflections.isPrimitive(ret)) { return context; } else { return ret; } } } }; }); /*can-reflect@1.19.2#reflections/get-set/get-set*/ define('can-reflect/reflections/get-set/get-set', [ 'require', 'exports', 'module', 'can-symbol', 'can-reflect/reflections/type/type' ], function (require, exports, module) { 'use strict'; var canSymbol = require('can-symbol'); var typeReflections = require('can-reflect/reflections/type/type'); var setKeyValueSymbol = canSymbol.for('can.setKeyValue'), getKeyValueSymbol = canSymbol.for('can.getKeyValue'), getValueSymbol = canSymbol.for('can.getValue'), setValueSymbol = canSymbol.for('can.setValue'); var reflections = { setKeyValue: function (obj, key, value) { if (typeReflections.isSymbolLike(key)) { if (typeof key === 'symbol') { obj[key] = value; } else { Object.defineProperty(obj, key, { enumerable: false, configurable: true, value: value, writable: true }); } return; } var setKeyValue = obj[setKeyValueSymbol]; if (setKeyValue !== undefined) { return setKeyValue.call(obj, key, value); } else { obj[key] = value; } }, getKeyValue: function (obj, key) { var getKeyValue = obj[getKeyValueSymbol]; if (getKeyValue) { return getKeyValue.call(obj, key); } return obj[key]; }, deleteKeyValue: function (obj, key) { var deleteKeyValue = obj[canSymbol.for('can.deleteKeyValue')]; if (deleteKeyValue) { return deleteKeyValue.call(obj, key); } delete obj[key]; }, getValue: function (value) { if (typeReflections.isPrimitive(value)) { return value; } var getValue = value[getValueSymbol]; if (getValue) { return getValue.call(value); } return value; }, setValue: function (item, value) { var setValue = item && item[setValueSymbol]; if (setValue) { return setValue.call(item, value); } else { throw new Error('can-reflect.setValue - Can not set value.'); } }, splice: function (obj, index, removing, adding) { var howMany; if (typeof removing !== 'number') { var updateValues = obj[canSymbol.for('can.updateValues')]; if (updateValues) { return updateValues.call(obj, index, removing, adding); } howMany = removing.length; } else { howMany = removing; } if (arguments.length <= 3) { adding = []; } var splice = obj[canSymbol.for('can.splice')]; if (splice) { return splice.call(obj, index, howMany, adding); } return [].splice.apply(obj, [ index, howMany ].concat(adding)); }, addValues: function (obj, adding, index) { var add = obj[canSymbol.for('can.addValues')]; if (add) { return add.call(obj, adding, index); } if (Array.isArray(obj) && index === undefined) { return obj.push.apply(obj, adding); } return reflections.splice(obj, index, [], adding); }, removeValues: function (obj, removing, index) { var removeValues = obj[canSymbol.for('can.removeValues')]; if (removeValues) { return removeValues.call(obj, removing, index); } if (Array.isArray(obj) && index === undefined) { removing.forEach(function (item) { var index = obj.indexOf(item); if (index >= 0) { obj.splice(index, 1); } }); return; } return reflections.splice(obj, index, removing, []); } }; reflections.get = reflections.getKeyValue; reflections.set = reflections.setKeyValue; reflections['delete'] = reflections.deleteKeyValue; module.exports = reflections; }); /*can-reflect@1.19.2#reflections/observe/observe*/ define('can-reflect/reflections/observe/observe', [ 'require', 'exports', 'module', 'can-symbol' ], function (require, exports, module) { 'use strict'; var canSymbol = require('can-symbol'); var slice = [].slice; function makeFallback(symbolName, fallbackName) { return function (obj, event, handler, queueName) { var method = obj[canSymbol.for(symbolName)]; if (method !== undefined) { return method.call(obj, event, handler, queueName); } return this[fallbackName].apply(this, arguments); }; } function makeErrorIfMissing(symbolName, errorMessage) { return function (obj) { var method = obj[canSymbol.for(symbolName)]; if (method !== undefined) { var args = slice.call(arguments, 1); return method.apply(obj, args); } throw new Error(errorMessage); }; } module.exports = { onKeyValue: makeFallback('can.onKeyValue', 'onEvent'), offKeyValue: makeFallback('can.offKeyValue', 'offEvent'), onKeys: makeErrorIfMissing('can.onKeys', 'can-reflect: can not observe an onKeys event'), onKeysAdded: makeErrorIfMissing('can.onKeysAdded', 'can-reflect: can not observe an onKeysAdded event'), onKeysRemoved: makeErrorIfMissing('can.onKeysRemoved', 'can-reflect: can not unobserve an onKeysRemoved event'), getKeyDependencies: makeErrorIfMissing('can.getKeyDependencies', 'can-reflect: can not determine dependencies'), getWhatIChange: makeErrorIfMissing('can.getWhatIChange', 'can-reflect: can not determine dependencies'), getChangesDependencyRecord: function getChangesDependencyRecord(handler) { var fn = handler[canSymbol.for('can.getChangesDependencyRecord')]; if (typeof fn === 'function') { return fn(); } }, keyHasDependencies: makeErrorIfMissing('can.keyHasDependencies', 'can-reflect: can not determine if this has key dependencies'), onValue: makeErrorIfMissing('can.onValue', 'can-reflect: can not observe value change'), offValue: makeErrorIfMissing('can.offValue', 'can-reflect: can not unobserve value change'), getValueDependencies: makeErrorIfMissing('can.getValueDependencies', 'can-reflect: can not determine dependencies'), valueHasDependencies: makeErrorIfMissing('can.valueHasDependencies', 'can-reflect: can not determine if value has dependencies'), onPatches: makeErrorIfMissing('can.onPatches', 'can-reflect: can not observe patches on object'), offPatches: makeErrorIfMissing('can.offPatches', 'can-reflect: can not unobserve patches on object'), onInstancePatches: makeErrorIfMissing('can.onInstancePatches', 'can-reflect: can not observe onInstancePatches on Type'), offInstancePatches: makeErrorIfMissing('can.offInstancePatches', 'can-reflect: can not unobserve onInstancePatches on Type'), onInstanceBoundChange: makeErrorIfMissing('can.onInstanceBoundChange', 'can-reflect: can not observe bound state change in instances.'), offInstanceBoundChange: makeErrorIfMissing('can.offInstanceBoundChange', 'can-reflect: can not unobserve bound state change'), isBound: makeErrorIfMissing('can.isBound', 'can-reflect: cannot determine if object is bound'), onEvent: function (obj, eventName, callback, queue) { if (obj) { var onEvent = obj[canSymbol.for('can.onEvent')]; if (onEvent !== undefined) { return onEvent.call(obj, eventName, callback, queue); } else if (obj.addEventListener) { obj.addEventListener(eventName, callback, queue); } } }, offEvent: function (obj, eventName, callback, queue) { if (obj) { var offEvent = obj[canSymbol.for('can.offEvent')]; if (offEvent !== undefined) { return offEvent.call(obj, eventName, callback, queue); } else if (obj.removeEventListener) { obj.removeEventListener(eventName, callback, queue); } } }, setPriority: function (obj, priority) { if (obj) { var setPriority = obj[canSymbol.for('can.setPriority')]; if (setPriority !== undefined) { setPriority.call(obj, priority); return true; } } return false; }, getPriority: function (obj) { if (obj) { var getPriority = obj[canSymbol.for('can.getPriority')]; if (getPriority !== undefined) { return getPriority.call(obj); } } return undefined; } }; }); /*can-reflect@1.19.2#reflections/shape/shape*/ define('can-reflect/reflections/shape/shape', [ 'require', 'exports', 'module', 'can-symbol', 'can-reflect/reflections/get-set/get-set', 'can-reflect/reflections/type/type', 'can-reflect/reflections/helpers' ], function (require, exports, module) { 'use strict'; var canSymbol = require('can-symbol'); var getSetReflections = require('can-reflect/reflections/get-set/get-set'); var typeReflections = require('can-reflect/reflections/type/type'); var helpers = require('can-reflect/reflections/helpers'); var getPrototypeOfWorksWithPrimitives = true; try { Object.getPrototypeOf(1); } catch (e) { getPrototypeOfWorksWithPrimitives = false; } var ArrayMap; if (typeof Map === 'function') { ArrayMap = Map; } else { var isEven = function isEven(num) { return num % 2 === 0; }; ArrayMap = function () { this.contents = []; }; ArrayMap.prototype = { _getIndex: function (key) { var idx; do { idx = this.contents.indexOf(key, idx); } while (idx !== -1 && !isEven(idx)); return idx; }, has: function (key) { return this._getIndex(key) !== -1; }, get: function (key) { var idx = this._getIndex(key); if (idx !== -1) { return this.contents[idx + 1]; } }, set: function (key, value) { var idx = this._getIndex(key); if (idx !== -1) { this.contents[idx + 1] = value; } else { this.contents.push(key); this.contents.push(value); } }, 'delete': function (key) { var idx = this._getIndex(key); if (idx !== -1) { this.contents.splice(idx, 2); } } }; } var hasOwnProperty = Object.prototype.hasOwnProperty; var shapeReflections; var shiftFirstArgumentToThis = function (func) { return function () { var args = [this]; args.push.apply(args, arguments); return func.apply(null, args); }; }; var getKeyValueSymbol = canSymbol.for('can.getKeyValue'); var shiftedGetKeyValue = shiftFirstArgumentToThis(getSetReflections.getKeyValue); var setKeyValueSymbol = canSymbol.for('can.setKeyValue'); var shiftedSetKeyValue = shiftFirstArgumentToThis(getSetReflections.setKeyValue); var sizeSymbol = canSymbol.for('can.size'); var hasUpdateSymbol = helpers.makeGetFirstSymbolValue([ 'can.updateDeep', 'can.assignDeep', 'can.setKeyValue' ]); var shouldUpdateOrAssign = function (obj) { return typeReflections.isPlainObject(obj) || Array.isArray(obj) || !!hasUpdateSymbol(obj); }; function isSerializedHelper(obj) { if (typeReflections.isPrimitive(obj)) { return true; } if (hasUpdateSymbol(obj)) { return false; } return typeReflections.isBuiltIn(obj) && !typeReflections.isPlainObject(obj) && !Array.isArray(obj) && !typeReflections.isObservableLike(obj); } var Object_Keys; try { Object.keys(1); Object_Keys = Object.keys; } catch (e) { Object_Keys = function (obj) { if (typeReflections.isPrimitive(obj)) { return []; } else { return Object.keys(obj); } }; } function createSerializeMap(Type) { var MapType = Type || ArrayMap; return { unwrap: new MapType(), serialize: new MapType(), isSerializing: { unwrap: new MapType(), serialize: new MapType() }, circularReferenceIsSerializing: { unwrap: new MapType(), serialize: new MapType() } }; } function makeSerializer(methodName, symbolsToCheck) { var serializeMap = null; function SerializeOperation(MapType) { this.first = !serializeMap; if (this.first) { serializeMap = createSerializeMap(MapType); } this.map = serializeMap; this.result = null; } SerializeOperation.prototype.end = function () { if (this.first) { serializeMap = null; } return this.result; }; return function serializer(value, MapType) { if (isSerializedHelper(value)) { return value; } var operation = new SerializeOperation(MapType); if (typeReflections.isValueLike(value)) { operation.result = this[methodName](getSetReflections.getValue(value)); } else { var isListLike = typeReflections.isIteratorLike(value) || typeReflections.isMoreListLikeThanMapLike(value); operation.result = isListLike ? [] : {}; if (operation.map[methodName].has(value)) { if (operation.map.isSerializing[methodName].has(value)) { operation.map.circularReferenceIsSerializing[methodName].set(value, true); } return operation.map[methodName].get(value); } else { operation.map[methodName].set(value, operation.result); } for (var i = 0, len = symbolsToCheck.length; i < len; i++) { var serializer = value[symbolsToCheck[i]]; if (serializer) { operation.map.isSerializing[methodName].set(value, true); var oldResult = operation.result; operation.result = serializer.call(value, oldResult); operation.map.isSerializing[methodName].delete(value); if (operation.result !== oldResult) { if (operation.map.circularReferenceIsSerializing[methodName].has(value)) { operation.end(); throw new Error('Cannot serialize cirular reference!'); } operation.map[methodName].set(value, operation.result); } return operation.end(); } } if (typeof obj === 'function') { operation.map[methodName].set(value, value); operation.result = value; } else if (isListLike) { this.eachIndex(value, function (childValue, index) { operation.result[index] = this[methodName](childValue); }, this); } else { this.eachKey(value, function (childValue, prop) { operation.result[prop] = this[methodName](childValue); }, this); } } return operation.end(); }; } var makeMap; if (typeof Map !== 'undefined') { makeMap = function (keys) { var map = new Map(); shapeReflections.eachIndex(keys, function (key) { map.set(key, true); }); return map; }; } else { makeMap = function (keys) { var map = {}; keys.forEach(function (key) { map[key] = true; }); return { get: function (key) { return map[key]; }, set: function (key, value) { map[key] = value; }, keys: function () { return keys; } }; }; } var fastHasOwnKey = function (obj) { var hasOwnKey = obj[canSymbol.for('can.hasOwnKey')]; if (hasOwnKey) { return hasOwnKey.bind(obj); } else { var map = makeMap(shapeReflections.getOwnEnumerableKeys(obj)); return function (key) { return map.get(key); }; } }; function addPatch(patches, patch) { var lastPatch = patches[patches.length - 1]; if (lastPatch) { if (lastPatch.deleteCount === lastPatch.insert.length && patch.index - lastPatch.index === lastPatch.deleteCount) { lastPatch.insert.push.apply(lastPatch.insert, patch.insert); lastPatch.deleteCount += patch.deleteCount; return; } } patches.push(patch); } function updateDeepList(target, source, isAssign) { var sourceArray = this.toArray(source); var patches = [], lastIndex = -1; this.eachIndex(target, function (curVal, index) { lastIndex = index; if (index >= sourceArray.length) { if (!isAssign) { addPatch(patches, { index: index, deleteCount: target.length - index + 1, insert: [] }); } return false; } var newVal = sourceArray[index]; if (typeReflections.isPrimitive(curVal) || typeReflections.isPrimitive(newVal) || shouldUpdateOrAssign(curVal) === false) { addPatch(patches, { index: index, deleteCount: 1, insert: [newVal] }); } else { if (isAssign === true) { this.assignDeep(curVal, newVal); } else { this.updateDeep(curVal, newVal); } } }, this); if (sourceArray.length > lastIndex) { addPatch(patches, { index: lastIndex + 1, deleteCount: 0, insert: sourceArray.slice(lastIndex + 1) }); } for (var i = 0, patchLen = patches.length; i < patchLen; i++) { var patch = patches[i]; getSetReflections.splice(target, patch.index, patch.deleteCount, patch.insert); } return target; } shapeReflections = { each: function (obj, callback, context) { if (typeReflections.isIteratorLike(obj) || typeReflections.isMoreListLikeThanMapLike(obj)) { return shapeReflections.eachIndex(obj, callback, context); } else { return shapeReflections.eachKey(obj, callback, context); } }, eachIndex: function (list, callback, context) { if (Array.isArray(list)) { return shapeReflections.eachListLike(list, callback, context); } else { var iter, iterator = list[canSymbol.iterator]; if (typeReflections.isIteratorLike(list)) { iter = list; } else if (iterator) { iter = iterator.call(list); } if (iter) { var res, index = 0; while (!(res = iter.next()).done) { if (callback.call(context || list, res.value, index++, list) === false) { break; } } } else { shapeReflections.eachListLike(list, callback, context); } } return list; }, eachListLike: function (list, callback, context) { var index = -1; var length = list.length; if (length === undefined) { var size = list[sizeSymbol]; if (size) { length = size.call(list); } else { throw new Error('can-reflect: unable to iterate.'); } } while (++index < length) { var item = list[index]; if (callback.call(context || item, item, index, list) === false) { break; } } return list; }, toArray: function (obj) { var arr = []; shapeReflections.each(obj, function (value) { arr.push(value); }); return arr; }, eachKey: function (obj, callback, context) { if (obj) { var enumerableKeys = shapeReflections.getOwnEnumerableKeys(obj); var getKeyValue = obj[getKeyValueSymbol] || shiftedGetKeyValue; return shapeReflections.eachIndex(enumerableKeys, function (key) { var value = getKeyValue.call(obj, key); return callback.call(context || obj, value, key, obj); }); } return obj; }, 'hasOwnKey': function (obj, key) { var hasOwnKey = obj[canSymbol.for('can.hasOwnKey')]; if (hasOwnKey) { return hasOwnKey.call(obj, key); } var getOwnKeys = obj[canSymbol.for('can.getOwnKeys')]; if (getOwnKeys) { var found = false; shapeReflections.eachIndex(getOwnKeys.call(obj), function (objKey) { if (objKey === key) { found = true; return false; } }); return found; } return hasOwnProperty.call(obj, key); }, getOwnEnumerableKeys: function (obj) { var getOwnEnumerableKeys = obj[canSymbol.for('can.getOwnEnumerableKeys')]; if (getOwnEnumerableKeys) { return getOwnEnumerableKeys.call(obj); } if (obj[canSymbol.for('can.getOwnKeys')] && obj[canSymbol.for('can.getOwnKeyDescriptor')]) { var keys = []; shapeReflections.eachIndex(shapeReflections.getOwnKeys(obj), function (key) { var descriptor = shapeReflections.getOwnKeyDescriptor(obj, key); if (descriptor.enumerable) { keys.push(key); } }, this); return keys; } else { return Object_Keys(obj); } }, getOwnKeys: function (obj) { var getOwnKeys = obj[canSymbol.for('can.getOwnKeys')]; if (getOwnKeys) { return getOwnKeys.call(obj); } else { return Object.getOwnPropertyNames(obj); } }, getOwnKeyDescriptor: function (obj, key) { var getOwnKeyDescriptor = obj[canSymbol.for('can.getOwnKeyDescriptor')]; if (getOwnKeyDescriptor) { return getOwnKeyDescriptor.call(obj, key); } else { return Object.getOwnPropertyDescriptor(obj, key); } }, unwrap: makeSerializer('unwrap', [canSymbol.for('can.unwrap')]), serialize: makeSerializer('serialize', [ canSymbol.for('can.serialize'), canSymbol.for('can.unwrap') ]), assignMap: function (target, source) { var hasOwnKey = fastHasOwnKey(target); var getKeyValue = target[getKeyValueSymbol] || shiftedGetKeyValue; var setKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; shapeReflections.eachKey(source, function (value, key) { if (!hasOwnKey(key) || getKeyValue.call(target, key) !== value) { setKeyValue.call(target, key, value); } }); return target; }, assignList: function (target, source) { var inserting = shapeReflections.toArray(source); getSetReflections.splice(target, 0, inserting, inserting); return target; }, assign: function (target, source) { if (typeReflections.isIteratorLike(source) || typeReflections.isMoreListLikeThanMapLike(source)) { shapeReflections.assignList(target, source); } else { shapeReflections.assignMap(target, source); } return target; }, assignDeepMap: function (target, source) { var hasOwnKey = fastHasOwnKey(target); var getKeyValue = target[getKeyValueSymbol] || shiftedGetKeyValue; var setKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; shapeReflections.eachKey(source, function (newVal, key) { if (!hasOwnKey(key)) { getSetReflections.setKeyValue(target, key, newVal); } else { var curVal = getKeyValue.call(target, key); if (newVal === curVal) { } else if (typeReflections.isPrimitive(curVal) || typeReflections.isPrimitive(newVal) || shouldUpdateOrAssign(curVal) === false) { setKeyValue.call(target, key, newVal); } else { shapeReflections.assignDeep(curVal, newVal); } } }, this); return target; }, assignDeepList: function (target, source) { return updateDeepList.call(this, target, source, true); }, assignDeep: function (target, source) { var assignDeep = target[canSymbol.for('can.assignDeep')]; if (assignDeep) { assignDeep.call(target, source); } else if (typeReflections.isMoreListLikeThanMapLike(source)) { shapeReflections.assignDeepList(target, source); } else { shapeReflections.assignDeepMap(target, source); } return target; }, updateMap: function (target, source) { var sourceKeyMap = makeMap(shapeReflections.getOwnEnumerableKeys(source)); var sourceGetKeyValue = source[getKeyValueSymbol] || shiftedGetKeyValue; var targetSetKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; shapeReflections.eachKey(target, function (curVal, key) { if (!sourceKeyMap.get(key)) { getSetReflections.deleteKeyValue(target, key); return; } sourceKeyMap.set(key, false); var newVal = sourceGetKeyValue.call(source, key); if (newVal !== curVal) { targetSetKeyValue.call(target, key, newVal); } }, this); shapeReflections.eachIndex(sourceKeyMap.keys(), function (key) { if (sourceKeyMap.get(key)) { targetSetKeyValue.call(target, key, sourceGetKeyValue.call(source, key)); } }); return target; }, updateList: function (target, source) { var inserting = shapeReflections.toArray(source); getSetReflections.splice(target, 0, target, inserting); return target; }, update: function (target, source) { if (typeReflections.isIteratorLike(source) || typeReflections.isMoreListLikeThanMapLike(source)) { shapeReflections.updateList(target, source); } else { shapeReflections.updateMap(target, source); } return target; }, updateDeepMap: function (target, source) { var sourceKeyMap = makeMap(shapeReflections.getOwnEnumerableKeys(source)); var sourceGetKeyValue = source[getKeyValueSymbol] || shiftedGetKeyValue; var targetSetKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; shapeReflections.eachKey(target, function (curVal, key) { if (!sourceKeyMap.get(key)) { getSetReflections.deleteKeyValue(target, key); return; } sourceKeyMap.set(key, false); var newVal = sourceGetKeyValue.call(source, key); if (typeReflections.isPrimitive(curVal) || typeReflections.isPrimitive(newVal) || shouldUpdateOrAssign(curVal) === false) { targetSetKeyValue.call(target, key, newVal); } else { shapeReflections.updateDeep(curVal, newVal); } }, this); shapeReflections.eachIndex(sourceKeyMap.keys(), function (key) { if (sourceKeyMap.get(key)) { targetSetKeyValue.call(target, key, sourceGetKeyValue.call(source, key)); } }); return target; }, updateDeepList: function (target, source) { return updateDeepList.call(this, target, source); }, updateDeep: function (target, source) { var updateDeep = target[canSymbol.for('can.updateDeep')]; if (updateDeep) { updateDeep.call(target, source); } else if (typeReflections.isMoreListLikeThanMapLike(source)) { shapeReflections.updateDeepList(target, source); } else { shapeReflections.updateDeepMap(target, source); } return target; }, hasKey: function (obj, key) { if (obj == null) { return false; } if (typeReflections.isPrimitive(obj)) { if (hasOwnProperty.call(obj, key)) { return true; } else { var pr