Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load existing schema with tag attribute using schema builder tool #1133

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
define([
"jquery" , "underscore" , "backbone", "helper/pubsub"
, "models/snippet"
, "collections/snippets"
, "views/my-form-snippet"
, "text!data/n2attributeboolean.json"
, "text!data/n2.json" , "text!data/n2attributes.json"
], function(
$, _, Backbone,PubSub
"jquery", "underscore", "backbone", "helper/pubsub"
, "models/snippet"
, "collections/snippets"
, "views/my-form-snippet"
, "text!data/n2attributeboolean.json"
, "text!data/n2.json", "text!data/n2attributes.json"
], function (
$, _, Backbone, PubSub
, SnippetModel
, SnippetsCollection
, MyFormSnippetView
, n2AttrBoolean
,n2mandatoryJSON , attributesJSON
){
, MyFormSnippetView
, n2AttrBoolean
, n2mandatoryJSON, attributesJSON

) {
return SnippetsCollection.extend({
model: SnippetModel
, initialize: function() {
, initialize: function () {
this.counter = {};
this.on("add", this.giveUniqueIdandN2boolean);

}
, giveUniqueIdandN2boolean: function(snippet){
if(!snippet.get("fresh")) {
, giveUniqueIdandN2boolean: function (snippet) {
if (!snippet.get("fresh")) {
return;
}
snippet.set("fresh", false);
var snippetType = snippet.attributes.fields.type.value;

if(typeof this.counter[snippetType] === "undefined") {
if (typeof this.counter[snippetType] === "undefined") {
this.counter[snippetType] = 0;
} else {
this.counter[snippetType] += 1;
}

snippet.setField("n2id", "nunaliit-" + snippetType + "-" + this.counter[snippetType]);
if(typeof snippet.get("fields")["id2"] !== "undefined") {
if (typeof snippet.get("fields")["id2"] !== "undefined") {
snippet.setField("id2", snippetType + "2-" + this.counter[snippetType]);
}
snippet.mergeField(new Backbone.Model(JSON.parse(n2AttrBoolean)[0]))
snippet.mergeField(new Backbone.Model(JSON.parse(n2AttrBoolean)[0]))
}
, giveUniqueId: function(snippet){
if(!snippet.get("fresh")) {
, giveUniqueId: function (snippet) {
if (!snippet.get("fresh")) {
return;
}
snippet.set("fresh", false);
var snippetType = snippet.attributes.fields.type.value;

if(typeof this.counter[snippetType] === "undefined") {
if (typeof this.counter[snippetType] === "undefined") {
this.counter[snippetType] = 0;
} else {
this.counter[snippetType] += 1;
}

snippet.setField("n2id", "nunaliit-" + snippetType + "-" + this.counter[snippetType]);

if(typeof snippet.get("fields")["id2"] !== "undefined") {
if (typeof snippet.get("fields")["id2"] !== "undefined") {
snippet.setField("id2", snippetType + "2-" + this.counter[snippetType]);
}
}
, containsFileType: function(){
return !(typeof this.find(function(snippet){
, containsFileType: function () {
return !(typeof this.find(function (snippet) {
return snippet.attributes.title === "File Button"
}) === "undefined");
}
, readRapeSnippets: function(modelJSON){
, readRapeSnippets: function (modelJSON) {

this.reset();
this.reset();
var rapeSnippets = modelJSON;
var infoSnippetJson = JSON.parse(n2mandatoryJSON);
var attrSnippetJson = JSON.parse(attributesJSON);
var attrBoolSnippetInstance = JSON.parse(n2AttrBoolean) [0]
var attrBoolSnippetInstance = JSON.parse(n2AttrBoolean)[0]
var that = this;
//adding info-snippet
_.each( infoSnippetJson, function(infoSnippetInstance){
_.each(infoSnippetJson, function (infoSnippetInstance) {
var infoSnippet = new SnippetModel(infoSnippetInstance)
infoSnippet.set("fresh", false);
infoSnippet.set("fromDb", true);
_.each(_.keys(rapeSnippets), function(fieldname){
if(fieldname !== "attributes"){
fieldnameDecorated = fieldname === "id"? "n2id" : fieldname;
infoSnippet.set("fresh", false);
infoSnippet.set("fromDb", true);
_.each(_.keys(rapeSnippets), function (fieldname) {
if (fieldname !== "attributes") {
fieldnameDecorated = fieldname === "id" ? "n2id" : fieldname;
infoSnippet.setFieldFromJson(fieldnameDecorated, rapeSnippets[fieldname])
}
})
Expand All @@ -88,29 +88,29 @@ define([
});
//adding attributes-Snippets
var attrs = rapeSnippets["attributes"];
_.each( attrs, function(attr){
var candidateSnippetModel = _.find(attrSnippetJson, function(n2attr) {
_.each(attrs, function (attr) {
var candidateSnippetModel = _.find(attrSnippetJson, function (n2attr) {
return n2attr["fields"]["type"]["value"] === attr["type"]
})
if(typeof candidateSnippetModel !== "undefined"){
if (typeof candidateSnippetModel !== "undefined") {

var newCandidateSnippetModel =JSON.parse(JSON.stringify(candidateSnippetModel))
var newCandidateSnippetModel = JSON.parse(JSON.stringify(candidateSnippetModel))
var candidateSnippetInstance = new SnippetModel(newCandidateSnippetModel);
candidateSnippetInstance.set("fresh", false);
candidateSnippetInstance.set("fromDb", true);
var newAttrBoolSnippetInstance = JSON.parse(JSON.stringify(attrBoolSnippetInstance))
candidateSnippetInstance.mergeField(new Backbone.Model(newAttrBoolSnippetInstance));
_.each(_.keys(attr), function(fieldname){
if(typeof candidateSnippetInstance.get("fields")[fieldname] !== "undefined" ||
fieldname === "id"
){
fieldnameDecorated = fieldname === "id"? "n2id" : fieldname;
candidateSnippetInstance.setFieldFromJson( fieldnameDecorated, attr[fieldname]);
_.each(_.keys(attr), function (fieldname) {
if (typeof candidateSnippetInstance.get("fields")[fieldname] !== "undefined" ||
fieldname === "id"
) {
fieldnameDecorated = fieldname === "id" ? "n2id" : fieldname;
candidateSnippetInstance.setFieldFromJson(fieldnameDecorated, attr[fieldname]);
}
})
}else{
alert("CAUSION: the type: " + attr["type"] +" is not a valid type.\n" +
" The cause can be either a deprecated type or an error in the database.");
} else {
alert("CAUSION: the type: " + attr["type"] + " is not a valid type.\n" +
" The cause can be either a deprecated type or an error in the database.");
}
that.push(candidateSnippetInstance);
PubSub.trigger("rapeSnippetsDecre");
Expand All @@ -122,15 +122,15 @@ define([
}


, renderAll: function(){
, renderAll: function () {

return this.map(function(snippet){
return new MyFormSnippetView({model: snippet}).render(true);
return this.map(function (snippet) {
return new MyFormSnippetView({ model: snippet }).render(true);
})
}
, renderAllClean: function(){
return this.map(function(snippet){
return new MyFormSnippetView({model: snippet}).render(false);
, renderAllClean: function () {
return this.map(function (snippet) {
return new MyFormSnippetView({ model: snippet }).render(false);
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,25 @@
"value": "Label"
}
}
},
{
"title": "n2 attribute tag",
"fields": {
"type": {
"label": "type",
"type": "string",
"value": "tag"
},
"n2id": {
"label": "Id",
"type": "input",
"value": null
},
"label": {
"label": "label",
"type": "input",
"value": "Label"
}
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,22 @@
n2id:
label : Id
type : input
value :
value :
label:
label : label
type : input
value : Label
-
title: n2 attribute tag
fields:
type:
label : type
type : string
value : tag
n2id:
label : Id
type : input
value :
label:
label : label
type : input
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
define([
'jquery', 'underscore', 'backbone'
], function($, _, Backbone) {
'jquery', 'underscore', 'backbone'
], function ($, _, Backbone) {
return Backbone.Model.extend({
initialize: function() {
initialize: function () {
this.set("fresh", true);
this.set("fromDb", false);
}
, getValues: function(){
return _.reduce(this.get("fields"), function(o, v, k){
, getValues: function () {
return _.reduce(this.get("fields"), function (o, v, k) {
if (v["type"] == "select") {
o[k] = _.find(v["value"], function(o){return o.selected})["value"];
o[k] = _.find(v["value"], function (o) { return o.selected })["value"];
} else {
o[k] = v["value"];
}
return o;
}, {});
}

, idFriendlyTitle: function(){
return this.get("title").replace(/\W/g,'').toLowerCase();
, idFriendlyTitle: function () {
return this.get("title").replace(/\W/g, '').toLowerCase();
}
, setField: function(name, value) {
, setField: function (name, value) {
var fields = this.get("fields")
fields[name]["value"] = value;
this.set("fields", fields);
}
, setFieldFromJson: function(name, value){
, setFieldFromJson: function (name, value) {

var fields = this.get("fields")
if(typeof fields[name] === "undefined") {
if (typeof fields[name] === "undefined") {
console.log("CAUTION: one of the field is missing in ", name)
return;
}
var type = fields[name]["type"]
switch(type) {
switch (type) {
case "string":
fields[name]["value"] = value
this.set("fields", fields);
Expand All @@ -43,33 +43,39 @@ define([
this.set("fields", fields);
break;
case "input":
if(fields[name]["hide"] !== "undefined" && fields[name]["hide"]) {
if (fields[name]["hide"] !== "undefined" && fields[name]["hide"]) {

} else {
fields[name]["value"] = value
this.set("fields", fields);
fields[name]["value"] = value
this.set("fields", fields);
}
break;
case "textarea":
fields[name]["value"] = JSON.parse(JSON.stringify(value))
this.set("fields", fields);
break;
case "tag":
fields[name]["value"] = value
this.set("fields", fields);
break;
case "textarea-split":
var checkboxvalarr = _.map(value, function(t){return $.trim(t["label"])})
var checkboxvalarr = _.map(value, function (t) { return $.trim(t["label"]) })
fields[name]["value"] = checkboxvalarr
this.set("fields", fields);
break;
case "select":
var valmatch = _.find(fields[name]["value"], function(v){
var valmatch = _.find(fields[name]["value"], function (v) {
return value.startsWith(v["value"])
});
var valarr = _.map(fields[name]["value"], function(v){
return {value: v["value"], label: v["label"], selected: value.startsWith(v["value"])
,needExtra : v["needExtra"] }
var valarr = _.map(fields[name]["value"], function (v) {
return {
value: v["value"], label: v["label"], selected: value.startsWith(v["value"])
, needExtra: v["needExtra"]
}
});

if(typeof valmatch["needExtra"] !=="undefined" && valmatch["needExtra"]) {
fields["customField"]["value"] = value.substring(value.indexOf("(")+2, value.lastIndexOf(")")-1)
if (typeof valmatch["needExtra"] !== "undefined" && valmatch["needExtra"]) {
fields["customField"]["value"] = value.substring(value.indexOf("(") + 2, value.lastIndexOf(")") - 1)
}
fields[name]["value"] = valarr;
this.set("fields", fields);
Expand All @@ -78,12 +84,12 @@ define([


}
, mergeField: function(snippet){
var fields = this.get("fields")
var thatFields = snippet.get("fields")
this.set("fields", _.extend(fields, thatFields))
, mergeField: function (snippet) {
var fields = this.get("fields")
var thatFields = snippet.get("fields")
this.set("fields", _.extend(fields, thatFields))

}
}

});
});