{"version":3,"file":"Model.js","names":["defineDs","m","Cleanup","model","modelName","constructor","generateId","replace","c","r","Math","random","v","toString","cache","construct","content","state","save","record","id","prop","timestamp","Date","getTime","init","prototype","data","JSON","stringify","toJSON","localStorage","setItem","e","console","warn","update","updatedRecord","Object","keys","forEach","key","get","query","type","cached","val","getItem","json","parse","bulk","i","length","push","filter","match","first","second","test","delete","removeItem","new","remove"],"sources":["Shared/Framework/Mithril/Scripts/Core/Model.js"],"sourcesContent":["defineDs('Shared/Framework/Mithril/Scripts/Core/Model',\r\n [\r\n 'Shared/Framework/Mithril/Scripts/Core/Mithril',\r\n 'Shared/Framework/Mithril/Scripts/Helpers/Cleanup'\r\n ],\r\n function (m, Cleanup) {\r\n\r\n // Functions:\r\n var model = function (modelName, constructor) {\r\n modelName = 'Model-' + modelName;\r\n\r\n constructor.generateId = function () {\r\n return 'xxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {\r\n /* eslint-disable no-bitwise, no-mixed-operators, one-var */\r\n var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);\r\n /* eslint-enable no-bitwise, no-mixed-operators, one-var */\r\n return v.toString(16);\r\n });\r\n };\r\n\r\n constructor.cache = {};\r\n\r\n constructor.construct = function (content, state, save) {\r\n content = content || {};\r\n\r\n var record = new this(content);\r\n\r\n record.id = m.prop(content.id ? content.id : this.generateId());\r\n record.state = m.prop(state ? state : 'clean');\r\n record.timestamp = m.prop(content.timestamp || (new Date()).getTime());\r\n\r\n if (typeof record.init === 'function') {\r\n record.init(content);\r\n }\r\n\r\n if (typeof save === 'undefined' || save) {\r\n record.save();\r\n }\r\n\r\n return record;\r\n };\r\n\r\n constructor.prototype.save = function () {\r\n this.state('clean');\r\n this.timestamp((new Date()).getTime());\r\n\r\n var data = JSON.stringify(this.toJSON());\r\n\r\n try {\r\n localStorage.setItem(modelName + '-' + this.id(), data);\r\n } catch (e) {\r\n console.warn('Model.js Cannot write to localStorage');\r\n }\r\n\r\n constructor.cache[this.id()] = this;\r\n };\r\n\r\n constructor.update = function (record, content) {\r\n\r\n var id = record.id();\r\n\r\n content.id = id;\r\n\r\n var updatedRecord = this.construct(content, 'new');\r\n\r\n Object.keys(updatedRecord).forEach(function (key) {\r\n record[key] = updatedRecord[key];\r\n });\r\n\r\n return record;\r\n\r\n };\r\n\r\n constructor.get = function (query) {\r\n var type = typeof query;\r\n\r\n if (type === 'undefined') {\r\n return null;\r\n }\r\n\r\n // Get by ID:\r\n if (type === 'number' || type === 'string') {\r\n var cached = constructor.cache[query];\r\n\r\n if (cached) {\r\n return cached;\r\n }\r\n\r\n try {\r\n var val = localStorage.getItem(modelName + '-' + query);\r\n } catch (e) {\r\n console.warn('Model.js Cannot read from localStorage');\r\n }\r\n\r\n if (!val) {\r\n return null;\r\n }\r\n\r\n var json = JSON.parse(val);\r\n var record = this.construct(json);\r\n\r\n return record;\r\n }\r\n\r\n var bulk = [];\r\n var keys = Object.keys(constructor.cache);\r\n\r\n for (var i = 0; i < keys.length; i++) {\r\n bulk.push(constructor.cache[keys[i]]);\r\n }\r\n\r\n // Query:\r\n if (type === 'object') {\r\n keys = Object.keys(query);\r\n var length = keys.length;\r\n\r\n bulk = bulk.filter(function (record) {\r\n var match = true;\r\n\r\n for (var i = 0; i < length; i++) {\r\n var first = typeof first === 'function' ? query[keys[i]]() : query[keys[i]];\r\n var second = record[keys[i]]();\r\n\r\n if (/delete|remove/.test(record.state()) || first !== second) {\r\n match = false;\r\n\r\n break;\r\n }\r\n }\r\n\r\n return match;\r\n });\r\n\r\n }\r\n\r\n // Bulk:\r\n return bulk;\r\n\r\n };\r\n\r\n constructor.delete = function (query) {\r\n if (!query) {\r\n return;\r\n }\r\n\r\n try {\r\n localStorage.removeItem(modelName + '-' + query);\r\n delete constructor.cache[query];\r\n } catch (e) {\r\n console.warn('Model.js Cannot remove from localStorage');\r\n }\r\n };\r\n\r\n constructor.new = function (content, state, save) {\r\n return this.construct(content, state || 'new', save);\r\n };\r\n\r\n constructor.prototype.remove = function () {\r\n constructor.delete(this.id());\r\n };\r\n\r\n return constructor;\r\n };\r\n\r\n // Cleanup\r\n Cleanup();\r\n\r\n // Public functions:\r\n return model;\r\n\r\n });\r\n"],"mappings":"AAAAA,QAAQ,CAAC,6CAA6C,EACpD,CACE,+CAA+C,EAC/C,kDAAkD,CACnD,EACD,UAAUC,CAAC,EAAEC,OAAO,EAAE;EAEpB;EACA,IAAIC,KAAK,GAAG,SAAAA,CAAUC,SAAS,EAAEC,WAAW,EAAE;IAC5CD,SAAS,GAAG,QAAQ,GAAGA,SAAS;IAEhCC,WAAW,CAACC,UAAU,GAAG,YAAY;MACnC,OAAO,sBAAsB,CAACC,OAAO,CAAC,OAAO,EAAE,UAAUC,CAAC,EAAE;QAC1D;QACA,IAAIC,CAAC,GAAGC,IAAI,CAACC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC;UAAEC,CAAC,GAAGJ,CAAC,IAAI,GAAG,GAAGC,CAAC,GAAIA,CAAC,GAAG,GAAG,GAAG,GAAI;QAClE;QACA,OAAOG,CAAC,CAACC,QAAQ,CAAC,EAAE,CAAC;MACvB,CAAC,CAAC;IACJ,CAAC;IAEDR,WAAW,CAACS,KAAK,GAAG,CAAC,CAAC;IAEtBT,WAAW,CAACU,SAAS,GAAG,UAAUC,OAAO,EAAEC,KAAK,EAAEC,IAAI,EAAE;MACtDF,OAAO,GAAGA,OAAO,IAAI,CAAC,CAAC;MAEvB,IAAIG,MAAM,GAAG,IAAI,IAAI,CAACH,OAAO,CAAC;MAE9BG,MAAM,CAACC,EAAE,GAAGnB,CAAC,CAACoB,IAAI,CAACL,OAAO,CAACI,EAAE,GAAGJ,OAAO,CAACI,EAAE,GAAG,IAAI,CAACd,UAAU,CAAC,CAAC,CAAC;MAC/Da,MAAM,CAACF,KAAK,GAAGhB,CAAC,CAACoB,IAAI,CAACJ,KAAK,GAAGA,KAAK,GAAG,OAAO,CAAC;MAC9CE,MAAM,CAACG,SAAS,GAA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