Skip to content

Commit

Permalink
refactoring: pushed creation of instances to respective elements (jso…
Browse files Browse the repository at this point in the history
…n, xml)
  • Loading branch information
JoernT committed Jan 13, 2014
1 parent 0e666eb commit 3fedb22
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 29 deletions.
13 changes: 13 additions & 0 deletions fore/app/elements/fore-instance.html
Expand Up @@ -26,6 +26,7 @@
Polymer('fore-instance', {
id: '',
states:[],
model:null,


/*
Expand All @@ -35,12 +36,16 @@
*/
createInitialInstance:function(){
//todo: implement initial loading of instances (handling of 'src' versus inline instance
console.log("#### FORE-INSTANCE ::createInitialInstance : ");


},

getInstanceId: function () {
return this.id;
},


delete:function(path){
//todo: implement deletion of an item
},
Expand Down Expand Up @@ -90,12 +95,20 @@
* COMPONENT LIFECYCLE
* ###########################
*/

created:function(){
},

ready: function () {
this.data=null;
this.itemStates = [];
},

enteredView: function () {

console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
console.log("#### FORE-INSTANCE :: my model: ", this.model);

this.id = this.getAttribute('id');
console.log("#### FORE-INSTANCE :: enteredView() ID:" , this.id);
}
Expand Down
52 changes: 52 additions & 0 deletions fore/app/elements/fore-json-instance.html
Expand Up @@ -7,6 +7,58 @@
Polymer('fore-json-instance', {
dataformat:'json',

createInitialInstance:function(){
console.log("llllllllllllllllllllllllllll createInitialInstance",this.model);
var item = this.model.ownerForm;
this.setAttribute('dataformat', 'json');
//this._buildJSONInstance(item);
this.data = JSON.parse('{' + this._buildJSONInstance(item) + '}');
},


_buildJSONInstance: function (item) {
//console.log("Name: ", node.getAttribute('name'));
//console.log("Name: ", node.tagName);
if (item.hasAttribute('name')) {
var nameName = item.getAttribute('name');
var value = item.getAttribute('value');

//console.log("Name: ", node.getAttribute('name'));
//console.log("Value: ", node.getAttribute('value'));

var childNodes = item.children;
//console.log("L: ", childNodes.length);

if (childNodes.length === 0) {
if (value === null) {
value = '';
}

return '"' + nameName + '":"' + value + '"';
} else {
// console.log("loop");
var result = '"' + nameName+ '":{';
for (var n = 0; n < childNodes.length; n++) {
if(childNodes[n].hasAttribute('name')) {
result += this._buildJSONInstance(childNodes[n]);
if (n < childNodes.length -1) {
result += ',';
}
}
}

return result += '}';
}
} else {
var childNodes = item.children; F
var result = '';
for (var n = 0; n < childNodes.length; n++) {
result += this._buildJSONInstance(childNodes[n]);
}
return result;
}

},

ready: function () {
console.log("#### FORE-JSON-INSTANCE :: ready()");
Expand Down
62 changes: 33 additions & 29 deletions fore/app/elements/fore-model.html
Expand Up @@ -82,17 +82,21 @@
if(this.ownerForm.dataformat === 'xml') {
inst = document.createElement('fore-instance', 'fore-xml-instance');
//var data = document.createElement('data');
var data = document.createElement(rootElemName);
inst.appendChild(data);
inst.setAttribute('dataformat', 'xml');
this.buildXMLInstance(this.ownerForm, data, this);
//var data = document.createElement(rootElemName);
//inst.appendChild(data);
//inst.setAttribute('dataformat', 'xml');
inst.model= this;
inst.createInitialInstance();
//this.buildXMLInstance(this.ownerForm, data, this);
} else if(this.ownerForm.dataformat === 'json') {
inst = document.createElement('fore-instance', 'fore-json-instance');
inst.setAttribute('dataformat', 'json');
inst.data = JSON.parse('{' + this.buildJSONInstance(this.ownerForm, this) + '}');
//inst.setAttribute('dataformat', 'json');
inst.model= this;
// inst.data = JSON.parse('{' + this.buildJSONInstance(this.ownerForm, this) + '}');
inst.createInitialInstance();
}
inst.setAttribute('id', instId);

this._buildForeBinds(this.ownerForm, this);
this.shadowRoot.appendChild(inst);

this.instances['defaultInstance'] = inst;
Expand Down Expand Up @@ -133,7 +137,7 @@
var value = node.getAttribute('value');
var instanceElement = document.createElement(name);

var bind = this.buildForeBind(node);
var bind = this._buildForeBind(node);
//console.log("Name: ", node.getAttribute('name'));
//console.log("Value: ", node.getAttribute('value'));

Expand Down Expand Up @@ -232,45 +236,45 @@

},

buildForeBinds: function (node, model) {
if (node.hasAttribute('name')) {
var bind = this.buildForeBind(node);
var childNodes = node.children;
_buildForeBinds: function (item, parent) {
if (item.hasAttribute('name')) {
var bind = this._buildForeBind(item);
var childNodes = item.children;
for (var n = 0; n < childNodes.length; n++) {
this.buildForeBinds(childNodes[n], bind);
this._buildForeBinds(childNodes[n], bind);
}
model.shadowRoot.appendChild(bind);
parent.shadowRoot.appendChild(bind);
} else {
var childNodes = node.children;
var childNodes = item.children;
for (var n = 0; n < childNodes.length; n++) {
this.buildForeBinds(childNodes[n], model);
this._buildForeBinds(childNodes[n], parent);
}
}
},

buildForeBind: function (node) {
_buildForeBind: function (item) {
var bind = new ForeBind();
bind.ref = node.getAttribute('name');
bind.ref = item.getAttribute('name');
bind.debug = false;

if(node.hasAttribute('type')) {
if( node.getAttribute('type') === 'text') {
if(item.hasAttribute('type')) {
if( item.getAttribute('type') === 'text') {
bind.datatype = 'string';
} else {
bind.datatype = node.getAttribute('type');
bind.datatype = item.getAttribute('type');
}
}
if(node.hasAttribute('calculate')) {
bind.calculate = node.getAttribute('calculate');
if(item.hasAttribute('calculate')) {
bind.calculate = item.getAttribute('calculate');
}
if(node.hasAttribute('readonly')) {
bind.readonly = node.getAttribute('readonly');
if(item.hasAttribute('readonly')) {
bind.readonly = item.getAttribute('readonly');
}
if(node.hasAttribute('required')) {
bind.required = node.getAttribute('required');
if(item.hasAttribute('required')) {
bind.required = item.getAttribute('required');
}
if(node.hasAttribute('relevant')) {
bind.relevant = node.getAttribute('relevant');
if(item.hasAttribute('relevant')) {
bind.relevant = item.getAttribute('relevant');
}
return bind;
},
Expand Down
70 changes: 70 additions & 0 deletions fore/app/elements/fore-xml-instance.html
Expand Up @@ -26,6 +26,76 @@
Polymer('fore-xml-instance', {
dataformat:'xml',

createInitialInstance:function(){
var rootElemName =this.model.ownerForm.getAttribute('name');
var item = this.model.ownerForm;
var parent = document.createElement(rootElemName);
this.setAttribute('dataformat', 'xml');
this.appendChild(parent);

this._buildXMLInstance(item, parent);
},

_buildXMLInstance: function (item, parent) {
//console.log("hazName: ", node.hasAttribute('name'));
if (item.hasAttribute('name')) {
var name = item.getAttribute('name');
var value = item.getAttribute('value');
var instanceElement = document.createElement(name);


//console.log("Name: ", node.getAttribute('name'));
//console.log("Value: ", node.getAttribute('value'));

var childNodes = item.children;
//console.log("L: ", childNodes.length);

if(item.hasAttribute('type')) {
if( item.getAttribute('type') === 'text') {
instanceElement.setAttribute('type', 'string');
} else {
instanceElement.setAttribute('type', item.getAttribute('type'));
}
}
if(item.hasAttribute('calculate')) {
instanceElement.setAttribute('calculate', item.getAttribute('calculate'));
}
if(item.hasAttribute('readonly')) {
instanceElement.setAttribute('readonly', item.getAttribute('readonly'));
}
if(item.hasAttribute('required')) {
instanceElement.setAttribute('required', item.getAttribute('required'));
}
if(item.hasAttribute('relevant')) {
instanceElement.setAttribute('relevant', item.getAttribute('relevant'));
}

if (childNodes.length === 0) {
if (value === null) {
value = '';
}
instanceElement.appendChild(document.createTextNode(value));
} else {
//console.log("loop");
for (var n = 0; n < childNodes.length; n++) {
this._buildXMLInstance(childNodes[n], instanceElement);
}

}
parent.appendChild(instanceElement);

} else {
var childNodes = item.children;

for (var n = 0; n < childNodes.length; n++) {
this._buildXMLInstance(childNodes[n], parent);
}
}

},
created:function(){
},

ready: function () {
console.log("#### FORE-XML-INSTANCE :: ready()");
this.super();
Expand Down

0 comments on commit 3fedb22

Please sign in to comment.