Skip to content

Commit

Permalink
more test
Browse files Browse the repository at this point in the history
  • Loading branch information
syarul committed Oct 9, 2017
1 parent 4d05e2f commit 1053df8
Show file tree
Hide file tree
Showing 10 changed files with 816 additions and 133 deletions.
16 changes: 10 additions & 6 deletions copy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module.exports = function(argv) {
var cop = function(v){
var o = {}
for(var attr in v){
o[attr] = v[attr]
if(typeof v !== 'object'){
o.copy = v
return o.copy
}else {
for(var attr in v){
o[attr] = v[attr]
}
}
return o
}
var clone = function(v) {
return typeof v == 'object' && cop(v)
}
return clone(argv)
return Array.isArray(argv) ? argv.map(function(v) {
return v
}) : cop(argv)
}
52 changes: 25 additions & 27 deletions dist/keet.debug.js

Large diffs are not rendered by default.

71 changes: 32 additions & 39 deletions dist/keet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
module.exports = function(argv) {
var cop = function(v){
var o = {}
for(var attr in v){
o[attr] = v[attr]
if(typeof v !== 'object'){
o.copy = v
return o.copy
}else {
for(var attr in v){
o[attr] = v[attr]
}
}
return o
}
var clone = function(v) {
return typeof v == 'object' && cop(v)
}
return clone(argv)
return Array.isArray(argv) ? argv.map(function(v) {
return v
}) : cop(argv)
}
},{}],2:[function(require,module,exports){
/**
Expand Down Expand Up @@ -53,7 +57,7 @@ function Keet(tagName, context) {
delete cloneChild[attr]
}
}
var s = tag(child.tag, child.template ? child.template : '', cloneChild, child.style)
var s = child.tag ? tag(child.tag, child.template ? child.template : '', cloneChild, child.style) : child.template
tempDiv.innerHTML = s
if(child.tag === 'input'){
if (child.checked)
Expand Down Expand Up @@ -220,8 +224,9 @@ function Keet(tagName, context) {
this.update = function(appObj){
var ele = getId(ctx.el)
var elArr = parseStr(appObj, true)
ele.innerHTML = ''
for (var i = 0; i < elArr.length; i++) {
ele.replaceChild(elArr[i], ele.childNodes[i])
ele.appendChild(elArr[i])
if(i === elArr.length - 1){
document.addEventListener('_update', window._update && typeof window._update === 'function' ? window._update(ctx.el) : null, false)
}
Expand Down Expand Up @@ -292,17 +297,15 @@ function Keet(tagName, context) {

op.forEach(function(f, i, r){
instance[f] = function() {
if(op.length > 0) {
var fargv = [].slice.call(arguments)
// if(!pristineLen[fargv[0]]) return false
if(f === 'update')
fargv[1] = Object.assign(pristineLen[fargv[0]], fargv[1])
Array.prototype[f].apply(this, fargv)
//propagate splice with single arguments
if(fargv.length === 1 && f === 'splice')
fargv.push(pristineLen.length - fargv[0])
query(f, fargv)
}
var fargv = [].slice.call(arguments)
// if(!pristineLen[fargv[0]]) return false
if(f === 'update')
fargv[1] = Object.assign(pristineLen[fargv[0]], fargv[1])
Array.prototype[f].apply(this, fargv)
//propagate splice with single arguments
if(fargv.length === 1 && f === 'splice')
fargv.push(pristineLen.length - fargv[0])
query(f, fargv)
}
})
}
Expand All @@ -314,16 +317,12 @@ function Keet(tagName, context) {

var arrProtoPop = function(){
var ele = getId(ctx.el)
if(ele.childNodes.length) {
ele.removeChild(ele.lastChild)
}
ele.removeChild(ele.lastChild)
}

var arrProtoShift = function(){
var ele = getId(ctx.el)
if(ele.childNodes.length) {
ele.removeChild(ele.firstChild)
}
ele.removeChild(ele.firstChild)
}

var arrProtoUnShift = function(){
Expand All @@ -348,11 +347,7 @@ function Keet(tagName, context) {
, tempDiv
, argv = [].slice.call(arguments)
, start = [].shift.call(argv)
, count
if(typeof argv[0] === 'number'){
count = [].shift.call(argv)
}

, count = [].shift.call(argv)
tempDiv = document.createElement('div')
if(argv.length){
i = 0
Expand All @@ -361,7 +356,6 @@ function Keet(tagName, context) {
i++
}
}

childLen = copy(ele.childNodes.length)
tempDivChildLen = copy(tempDiv.childNodes.length)
if (count && count > 0) {
Expand Down Expand Up @@ -420,7 +414,6 @@ function Keet(tagName, context) {
}

var nodeUpdate = function(newNode, oldNode) {
if(!newNode) return false
var oAttr = newNode.attributes
var output = {};
if(oAttr){
Expand All @@ -433,7 +426,7 @@ function Keet(tagName, context) {
oldNode.setAttribute(iAttr, output[iAttr])
}
}
if(oldNode.textContent === "" && newNode.textContent ){
if(oldNode.textContent === '' && newNode.textContent || oldNode.textContent != newNode.textContent){
oldNode.textContent = newNode.textContent
}
output = {}
Expand Down Expand Up @@ -520,9 +513,6 @@ Keet.prototype.link = function(id, value) {

this.el = argv[0]
if (argv.length === 2){
if(!argv[1].tag){
argv[1].tag = document.getElementById(id).tagName.toLowerCase()
}
this.base = argv[1]
}
this.render()
Expand Down Expand Up @@ -559,22 +549,24 @@ Keet.prototype.getBase = function(child, attribute, newProp) {
}

Keet.prototype.addClass = function(child, newClass) {
var self = this
var b = this.getBase(child, 'class')

var isArr = function() {
b.push(newClass)
this.getBase(child, 'class', b)
self.getBase(child, 'class', b)
}

return Array.isArray(b) && isArr()
}

Keet.prototype.removeClass = function(child, oldClass) {
var self = this
var b = this.getBase(child, 'class')

var hIdx = function(idx) {
b.splice(idx, 1)
this.getBase(child, 'class', b)
self.getBase(child, 'class', b)
}

var isArr = function() {
Expand All @@ -586,13 +578,14 @@ Keet.prototype.removeClass = function(child, oldClass) {
}

Keet.prototype.swapClass = function(child, condition, classesArray) {
var self = this
var b = this.getBase(child, 'class')

if (condition) classesArray.reverse()

var hIdx = function(idx) {
b.splice(idx, 1, classesArray[1])
this.getBase(child, 'class', b)
self.getBase(child, 'class', b)
}

var isArr = function() {
Expand Down
2 changes: 1 addition & 1 deletion dist/keet.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gulp.task('main', function () {
})
gulp.task('watch', function() {
livereload.listen()
gulp.watch('./index.js', ['main'])
gulp.watch('./*.js', ['main'])
})

gulp.task('default', ['watch'])
55 changes: 22 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function Keet(tagName, context) {
delete cloneChild[attr]
}
}
var s = tag(child.tag, child.template ? child.template : '', cloneChild, child.style)
var s = child.tag ? tag(child.tag, child.template ? child.template : '', cloneChild, child.style) : child.template
tempDiv.innerHTML = s
if(child.tag === 'input'){
if (child.checked)
Expand Down Expand Up @@ -205,8 +205,9 @@ function Keet(tagName, context) {
this.update = function(appObj){
var ele = getId(ctx.el)
var elArr = parseStr(appObj, true)
ele.innerHTML = ''
for (var i = 0; i < elArr.length; i++) {
ele.replaceChild(elArr[i], ele.childNodes[i])
ele.appendChild(elArr[i])
if(i === elArr.length - 1){
document.addEventListener('_update', window._update && typeof window._update === 'function' ? window._update(ctx.el) : null, false)
}
Expand Down Expand Up @@ -277,17 +278,15 @@ function Keet(tagName, context) {

op.forEach(function(f, i, r){
instance[f] = function() {
if(op.length > 0) {
var fargv = [].slice.call(arguments)
// if(!pristineLen[fargv[0]]) return false
if(f === 'update')
fargv[1] = Object.assign(pristineLen[fargv[0]], fargv[1])
Array.prototype[f].apply(this, fargv)
//propagate splice with single arguments
if(fargv.length === 1 && f === 'splice')
fargv.push(pristineLen.length - fargv[0])
query(f, fargv)
}
var fargv = [].slice.call(arguments)
// if(!pristineLen[fargv[0]]) return false
if(f === 'update')
fargv[1] = Object.assign(pristineLen[fargv[0]], fargv[1])
Array.prototype[f].apply(this, fargv)
//propagate splice with single arguments
if(fargv.length === 1 && f === 'splice')
fargv.push(pristineLen.length - fargv[0])
query(f, fargv)
}
})
}
Expand All @@ -299,16 +298,12 @@ function Keet(tagName, context) {

var arrProtoPop = function(){
var ele = getId(ctx.el)
if(ele.childNodes.length) {
ele.removeChild(ele.lastChild)
}
ele.removeChild(ele.lastChild)
}

var arrProtoShift = function(){
var ele = getId(ctx.el)
if(ele.childNodes.length) {
ele.removeChild(ele.firstChild)
}
ele.removeChild(ele.firstChild)
}

var arrProtoUnShift = function(){
Expand All @@ -333,11 +328,7 @@ function Keet(tagName, context) {
, tempDiv
, argv = [].slice.call(arguments)
, start = [].shift.call(argv)
, count
if(typeof argv[0] === 'number'){
count = [].shift.call(argv)
}

, count = [].shift.call(argv)
tempDiv = document.createElement('div')
if(argv.length){
i = 0
Expand All @@ -346,7 +337,6 @@ function Keet(tagName, context) {
i++
}
}

childLen = copy(ele.childNodes.length)
tempDivChildLen = copy(tempDiv.childNodes.length)
if (count && count > 0) {
Expand Down Expand Up @@ -405,7 +395,6 @@ function Keet(tagName, context) {
}

var nodeUpdate = function(newNode, oldNode) {
if(!newNode) return false
var oAttr = newNode.attributes
var output = {};
if(oAttr){
Expand All @@ -418,7 +407,7 @@ function Keet(tagName, context) {
oldNode.setAttribute(iAttr, output[iAttr])
}
}
if(oldNode.textContent === "" && newNode.textContent ){
if(oldNode.textContent === '' && newNode.textContent || oldNode.textContent != newNode.textContent){
oldNode.textContent = newNode.textContent
}
output = {}
Expand Down Expand Up @@ -505,9 +494,6 @@ Keet.prototype.link = function(id, value) {

this.el = argv[0]
if (argv.length === 2){
if(!argv[1].tag){
argv[1].tag = document.getElementById(id).tagName.toLowerCase()
}
this.base = argv[1]
}
this.render()
Expand Down Expand Up @@ -544,22 +530,24 @@ Keet.prototype.getBase = function(child, attribute, newProp) {
}

Keet.prototype.addClass = function(child, newClass) {
var self = this
var b = this.getBase(child, 'class')

var isArr = function() {
b.push(newClass)
this.getBase(child, 'class', b)
self.getBase(child, 'class', b)
}

return Array.isArray(b) && isArr()
}

Keet.prototype.removeClass = function(child, oldClass) {
var self = this
var b = this.getBase(child, 'class')

var hIdx = function(idx) {
b.splice(idx, 1)
this.getBase(child, 'class', b)
self.getBase(child, 'class', b)
}

var isArr = function() {
Expand All @@ -571,13 +559,14 @@ Keet.prototype.removeClass = function(child, oldClass) {
}

Keet.prototype.swapClass = function(child, condition, classesArray) {
var self = this
var b = this.getBase(child, 'class')

if (condition) classesArray.reverse()

var hIdx = function(idx) {
b.splice(idx, 1, classesArray[1])
this.getBase(child, 'class', b)
self.getBase(child, 'class', b)
}

var isArr = function() {
Expand Down
13 changes: 7 additions & 6 deletions sample/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ app.link('app', c)
// }, 4000)

setTimeout(function() {
// c['k-list'].push({first: 'awddo', last: 'awdwriw'})
// c['k-list'].shift()
// c.list.unshift({first: 'john', last: 'doe'}, {first: 'rocky', last: 'bulba'})
// c.list.splice(0, 2)
c.list.splice(1, 1, {first: 'awil', last: 'awile'})
// c.list.push({first: 'awddo', last: 'awdwriw', bg: 'red'})
// c.list.shift()
// c.list.unshift({first: 'em', last: 'woo', bg: 'grey'}, {first: 'foo', last: 'dafoo', bg: 'magenta'})
// c.list.splice(1)
// c.list.splice(1, 1, {first: 'awil', last: 'awile', bg: 'cyan'})
c.list.splice(1, 0, {first: 'awil', last: 'awile', bg: 'cyan'})
// c.list.update(2, {
// bg: 'red'
// })
// c.list.shift()
}, 2000)
}, 1000)
Loading

0 comments on commit 1053df8

Please sign in to comment.