diff --git a/dist/simple.dev.js b/dist/simple.dev.js index 6275221..f85ddb4 100644 --- a/dist/simple.dev.js +++ b/dist/simple.dev.js @@ -222,7 +222,6 @@ app = function (name, cnf) { value = valueOrNull; this.state[elementOrName] = valueOrNull; } else if (typeof elementOrName == 'object' && typeof elementOrName.nodeName == 'string') { - console.log('=> update state by element', elementOrName); // must be object el = elementOrName; var nodeName = el.nodeName; @@ -264,14 +263,25 @@ app = function (name, cnf) { } // save local if necessary this.store(); + // get current data (by index/name if possible) + var data = this.data[name], dataset = {}; + if (el && typeof el.dataset === 'object') { + dataset = el.dataset; + // find data... + if (typeof data === 'object' && typeof data.element === 'object' && dataset.index) { + data = data.element[dataset.index]; + } + } + console.log('Update obj: ', el, ' data: ', data); // notify callback this.stateIsUpdated({ name: name, value: value, element: el, // double check as el may not exist - we can just call updateState(k,v) from anywhere, really - dataset: (el && typeof el.dataset === 'object') ? el.dataset : {}, - state: this.state + dataset: dataset, + state: this.state, + data: data }); }; /** @@ -735,11 +745,13 @@ app = function (name, cnf) { if (node) { if (src) { vn = new vNode(src, nodeParent); + console.log('Replace: ' + src + ' with: ', node); vn.replace(node); } } else { // add some more to it if (nodeParent) { + console.log('Append: ' + src + ' to: ' + nodeParent.id); vn = new vNode(src, nodeParent); vn.right(); } else { @@ -757,9 +769,9 @@ app = function (name, cnf) { if (nodeParent && prevElementData.length > elData.length) { prevElementData.map(function (el, item) { if (item >= m) { - var node = self.node(elName + '_' + item); + var node = self.node(elName + '_' + (item + 1)); // remove childnode - console.log('Remove child: ' + item); + console.log('Remove child: ' + item + ' from: ' + nodeParent.id); if (node) nodeParent.removeChild(node); } } diff --git a/dist/simple.min.js b/dist/simple.min.js index a6f2c94..71492fa 100644 --- a/dist/simple.min.js +++ b/dist/simple.min.js @@ -24,7 +24,9 @@ case"checkbox":if(this.state[a]||(this.state[a]=[]),n.checked&&i.length>0)this.s else{var h=this.state[a].indexOf(i) h>=0&&this.state[a].splice(h,1)}break case"button":case"form":break -default:this.state[a]=i}}this.store(),this.stateIsUpdated({name:a,value:i,element:n,dataset:n&&"object"==typeof n.dataset?n.dataset:{},state:this.state})},this.stateIsUpdated=function(t){var e=this.getCallback("siu",t.name) +default:this.state[a]=i}}this.store() +var o=this.data[a],l={} +n&&"object"==typeof n.dataset&&(l=n.dataset,"object"==typeof o&&"object"==typeof o.element&&l.index&&(o=o.element[l.index])),this.stateIsUpdated({name:a,value:i,element:n,dataset:l,state:this.state,data:o})},this.stateIsUpdated=function(t){var e=this.getCallback("siu",t.name) return"function"==typeof e?e(t):t},this.htpl=function(t,e,a){a=a===!0 try{var i=t for(var n in e)if(n.length>0){var s=new RegExp("{"+n+"}","g"),r=e[n] @@ -72,13 +74,13 @@ else{var u=v-1,m=f.pData[t].element[u],y=f.data[t].element[u],_=null,S=f.node(t+ if(!g&&S&&(g=S.parentNode),n(m)==n(y))return _=f.htpl(p,d) var k -g&&(S?_&&(k=new h(_,g),k.replace(S)):g&&(k=new h(_,g),k.right()))}}),g&&y.length>u.length&&y.map(function(e,a){if(a>=v){var i=f.node(t+"_"+a) +g&&(S?_&&(k=new h(_,g),k.replace(S)):g&&(k=new h(_,g),k.right()))}}),g&&y.length>u.length&&y.map(function(e,a){if(a>=v){var i=f.node(t+"_"+(a+1)) i&&g.removeChild(i)}}),!("string"==typeof o&&o.length>1||0===v))return!1 o=f.htpl(c._wrapper[0],d)+o+c._wrapper[1]}else{var _=this.els(t,r,i),S=this.template.sub[t][_],k=this.parseElementData(t,r,i,c._type) S||(S=this.template.sub[t]["default"]),o=this.htpl(S,k)}if(this.template.sub[t]._sibling){var w=this.node(this.template.sub[t]._sibling) if(w){var j=new h(o,w.parentNode) -return j.right(),!1}}if(!e&&this.cnf.partialRender===!0&&o){var E=this.node(t),D=null -if(E)return D=new h(o),D.replace(E),!1}return o},this.init=function(t,e){t&&(this.container=t),this.load(),e!==!1&&(this._f("sta"),this.render(),this._f("fin"))},this.toQuerystring=function(){var t=[] +return j.right(),!1}}if(!e&&this.cnf.partialRender===!0&&o){var E=this.node(t),x=null +if(E)return x=new h(o),x.replace(E),!1}return o},this.init=function(t,e){t&&(this.container=t),this.load(),e!==!1&&(this._f("sta"),this.render(),this._f("fin"))},this.toQuerystring=function(){var t=[] for(var e in this.state){var a=this.state[e] a&&t.push(e+"="+a)}return t.join("&")},this.d=function(t,e){return e?"object"==typeof this.data[t].element&&this.data[t].element[e]?this.data[t].element[e]:void 0:this.data[t]}} var l={},p=function(t,e){return l[t]||(l[t]=new o(t,e)),l[t]} diff --git a/html/apps/todo.js b/html/apps/todo.js index a02e287..0b75349 100644 --- a/html/apps/todo.js +++ b/html/apps/todo.js @@ -57,23 +57,87 @@ } }; + /* the little storage thingy */ + var itemsKey = 'data-todo-items'; + // little thing to store in localstorage + var addItem = function (lbl) { + app.state[itemsKey].count++; + app.state[itemsKey].cache[app.state[itemsKey].count] = { + label: lbl, + done: false + }; + }, + updateItem = function (index, lbl, done) { + if (lbl) app.state[itemsKey].cache[index].lable = lbl; + if (typeof done !== 'undefined') app.state[itemsKey].cache[index].done = (done === true); + }, + removeItem = function (index) { + delete app.state[itemsKey].cache[index]; + }; + + // use app will render to check the current state + app.on(SimpleAppWillRender, 'loadState', function () { + // we load the state here and do render + console.log('=> App starts', app.state); + if (typeof app.state[itemsKey] === 'object') { + app.data.list.element = []; + // it should be in format of [{label: 'blah blah', done: true|false}] + for (var i in app.state[itemsKey].cache) { + var item = app.state[itemsKey].cache[i]; + var tmp = { + _lbl: item.label, + _item_key: i + }; + if (item.done === true) { + tmp.done = { + checked: 'checked' + }; + } + // insert + app.data.list.element.push(tmp); + } + } else { + app.state[itemsKey] = { + count: 0, + cache: {} + }; + } + // ^ really that's just it, no need for anything more + }); + + app.init(document.getElementById('apps'), false); app.render(true); // callback: add item app.on(SimpleAppStateIsUpdated, 'btnAdd', function (obj) { - console.log('add ' + app.state.itemEntry); if (app.state.itemEntry.length > 0) { - // add to data, then render - app.data.list.element.push({ - _lbl: app.state.itemEntry - }); + // add to state storage + addItem(app.state.itemEntry); + // store in local storage + app.store(); + // render app app.render(); + } else { + alert('Please enter the item entry'); } }); app.on(SimpleAppStateIsUpdated, 'list', function (obj) { - console.log(obj); + if (!obj.dataset.action || !obj.dataset.name || !obj.dataset.index) return; // no action + switch (obj.dataset.action) { + case 'done': + // update state + updateItem(obj.data._item_key, null, obj.element.checked); + app.store(); + break; + case 'trash': + // remove it + removeItem(obj.data._item_key); + app.store(); + app.render(false); + break; + } }); })(); \ No newline at end of file diff --git a/src/app.js b/src/app.js index ea5df87..adaf2d7 100644 --- a/src/app.js +++ b/src/app.js @@ -221,7 +221,6 @@ app = function (name, cnf) { value = valueOrNull; this.state[elementOrName] = valueOrNull; } else if (typeof elementOrName == 'object' && typeof elementOrName.nodeName == 'string') { - console.log('=> update state by element', elementOrName); // must be object el = elementOrName; var nodeName = el.nodeName; @@ -263,14 +262,25 @@ app = function (name, cnf) { } // save local if necessary this.store(); + // get current data (by index/name if possible) + var data = this.data[name], dataset = {}; + if (el && typeof el.dataset === 'object') { + dataset = el.dataset; + // find data... + if (typeof data === 'object' && typeof data.element === 'object' && dataset.index) { + data = data.element[dataset.index]; + } + } + console.log('Update obj: ', el, ' data: ', data); // notify callback this.stateIsUpdated({ name: name, value: value, element: el, // double check as el may not exist - we can just call updateState(k,v) from anywhere, really - dataset: (el && typeof el.dataset === 'object') ? el.dataset : {}, - state: this.state + dataset: dataset, + state: this.state, + data: data }); }; /** @@ -734,11 +744,13 @@ app = function (name, cnf) { if (node) { if (src) { vn = new vNode(src, nodeParent); + console.log('Replace: ' + src + ' with: ', node); vn.replace(node); } } else { // add some more to it if (nodeParent) { + console.log('Append: ' + src + ' to: ' + nodeParent.id); vn = new vNode(src, nodeParent); vn.right(); } else { @@ -756,9 +768,9 @@ app = function (name, cnf) { if (nodeParent && prevElementData.length > elData.length) { prevElementData.map(function (el, item) { if (item >= m) { - var node = self.node(elName + '_' + item); + var node = self.node(elName + '_' + (item + 1)); // remove childnode - console.log('Remove child: ' + item); + console.log('Remove child: ' + item + ' from: ' + nodeParent.id); if (node) nodeParent.removeChild(node); } } diff --git a/test/spec.js b/test/spec.js index a261e2e..6744d9e 100644 --- a/test/spec.js +++ b/test/spec.js @@ -17,7 +17,6 @@ describe("Localstorage Test", function () { app.state = {}; // load and make sure v is not in app.load(); - console.log(app.state); expect(app.state[k]).to.be.undefined; });