Skip to content

Commit

Permalink
bug fix and all done
Browse files Browse the repository at this point in the history
  • Loading branch information
qstufie committed Mar 5, 2016
1 parent 85cf88f commit 75de3fe
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 21 deletions.
22 changes: 17 additions & 5 deletions dist/simple.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
});
};
/**
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down
10 changes: 6 additions & 4 deletions dist/simple.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]}
Expand Down
76 changes: 70 additions & 6 deletions html/apps/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

})();
22 changes: 17 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
});
};
/**
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down
1 change: 0 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down

0 comments on commit 75de3fe

Please sign in to comment.