-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standardize formatting in EMLDistribution model
Issue #1380
- Loading branch information
Showing
1 changed file
with
120 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,134 @@ | ||
/* global define */ | ||
define(['jquery', 'underscore', 'backbone', 'models/DataONEObject'], | ||
function($, _, Backbone, DataONEObject) { | ||
|
||
var EMLDistribution = Backbone.Model.extend({ | ||
|
||
defaults: { | ||
objectXML: null, | ||
objectDOM: null, | ||
mediumName: null, | ||
mediumVolume: null, | ||
mediumFormat: null, | ||
mediumNote: null, | ||
onlineDescription: null, | ||
parentModel: null | ||
}, | ||
|
||
initialize: function(attributes){ | ||
if(attributes.objectDOM) this.parse(attributes.objectDOM); | ||
|
||
this.on("change:mediumName change:mediumVolume change:mediumFormat " + | ||
"change:mediumNote change:onlineDescription", this.trickleUpChange); | ||
}, | ||
|
||
/* | ||
* Maps the lower-case EML node names (valid in HTML DOM) to the camel-cased EML node names (valid in EML). | ||
* Used during parse() and serialize() | ||
*/ | ||
nodeNameMap: function(){ | ||
return{ | ||
"authsystem" : "authSystem", | ||
"connectiondefinition" : "connectionDefinition", | ||
"mediumdensity" : "mediumDensity", | ||
"mediumdensityunits" : "mediumDensityUnits", | ||
"mediumformat" : "mediumFormat", | ||
"mediumname" : "mediumName", | ||
"mediumnote" : "mediumNote", | ||
"mediumvolume" : "mediumVolume", | ||
"onlinedescription" : "onlineDescription" | ||
} | ||
}, | ||
|
||
parse: function(objectDOM){ | ||
if(!objectDOM) | ||
var xml = this.get("objectDOM"); | ||
|
||
var offline = $(xml).find("offline"), | ||
online = $(xml).find("online"); | ||
|
||
if(offline.length){ | ||
if($(offline).children("mediumname").length) this.parseNode($(offline).children("mediumname")); | ||
if($(offline).children("mediumvolume").length) this.parseNode($(offline).children("mediumvolume")); | ||
if($(offline).children("mediumformat").length) this.parseNode($(offline).children("mediumformat")); | ||
if($(offline).children("mediumnote").length) this.parseNode($(offline).children("mediumnote")); | ||
} | ||
|
||
if(online.length){ | ||
if($(online).children("onlinedescription").length) this.parseNode($(online).children("onlinedescription")); | ||
} | ||
}, | ||
|
||
parseNode: function(node){ | ||
if(!node || (Array.isArray(node) && !node.length)) | ||
return; | ||
|
||
this.set($(node)[0].localName, $(node).text()); | ||
}, | ||
|
||
serialize: function(){ | ||
var objectDOM = this.updateDOM(), | ||
xmlString = objectDOM.outerHTML; | ||
|
||
//Camel-case the XML | ||
xmlString = this.formatXML(xmlString); | ||
|
||
return xmlString; | ||
}, | ||
|
||
/* | ||
* Makes a copy of the original XML DOM and updates it with the new values from the model. | ||
*/ | ||
updateDOM: function(){ | ||
var objectDOM = this.get("objectDOM").cloneNode(true); | ||
|
||
// Remove empty (zero-length or whitespace-only) nodes | ||
$(objectDOM).find("*").filter(function() { return $.trim(this.innerHTML) === ""; } ).remove(); | ||
|
||
return objectDOM; | ||
}, | ||
define(["jquery", "underscore", "backbone", "models/DataONEObject"], function ( | ||
$, | ||
_, | ||
Backbone, | ||
DataONEObject | ||
) { | ||
var EMLDistribution = Backbone.Model.extend({ | ||
defaults: { | ||
objectXML: null, | ||
objectDOM: null, | ||
mediumName: null, | ||
mediumVolume: null, | ||
mediumFormat: null, | ||
mediumNote: null, | ||
onlineDescription: null, | ||
parentModel: null, | ||
}, | ||
|
||
initialize: function (attributes) { | ||
if (attributes.objectDOM) this.parse(attributes.objectDOM); | ||
|
||
this.on( | ||
"change:mediumName change:mediumVolume change:mediumFormat " + | ||
"change:mediumNote change:onlineDescription", | ||
this.trickleUpChange | ||
); | ||
}, | ||
|
||
/* | ||
* Climbs up the model heirarchy until it finds the EML model | ||
* | ||
* @return {EML211 or false} - Returns the EML 211 Model or false if not found | ||
*/ | ||
getParentEML: function(){ | ||
* Maps the lower-case EML node names (valid in HTML DOM) to the camel-cased | ||
* EML node names (valid in EML). Used during parse() and serialize() | ||
*/ | ||
nodeNameMap: function () { | ||
return { | ||
authsystem: "authSystem", | ||
connectiondefinition: "connectionDefinition", | ||
mediumdensity: "mediumDensity", | ||
mediumdensityunits: "mediumDensityUnits", | ||
mediumformat: "mediumFormat", | ||
mediumname: "mediumName", | ||
mediumnote: "mediumNote", | ||
mediumvolume: "mediumVolume", | ||
onlinedescription: "onlineDescription", | ||
}; | ||
}, | ||
|
||
parse: function (objectDOM) { | ||
if (!objectDOM) var xml = this.get("objectDOM"); | ||
|
||
var offline = $(xml).find("offline"), | ||
online = $(xml).find("online"); | ||
|
||
if (offline.length) { | ||
if ($(offline).children("mediumname").length) | ||
this.parseNode($(offline).children("mediumname")); | ||
if ($(offline).children("mediumvolume").length) | ||
this.parseNode($(offline).children("mediumvolume")); | ||
if ($(offline).children("mediumformat").length) | ||
this.parseNode($(offline).children("mediumformat")); | ||
if ($(offline).children("mediumnote").length) | ||
this.parseNode($(offline).children("mediumnote")); | ||
} | ||
|
||
if (online.length) { | ||
if ($(online).children("onlinedescription").length) | ||
this.parseNode($(online).children("onlinedescription")); | ||
} | ||
}, | ||
|
||
parseNode: function (node) { | ||
if (!node || (Array.isArray(node) && !node.length)) return; | ||
|
||
this.set($(node)[0].localName, $(node).text()); | ||
}, | ||
|
||
serialize: function () { | ||
var objectDOM = this.updateDOM(), | ||
xmlString = objectDOM.outerHTML; | ||
|
||
//Camel-case the XML | ||
xmlString = this.formatXML(xmlString); | ||
|
||
return xmlString; | ||
}, | ||
|
||
/* | ||
* Makes a copy of the original XML DOM and updates it with the new values | ||
* from the model. | ||
*/ | ||
updateDOM: function () { | ||
var objectDOM = this.get("objectDOM").cloneNode(true); | ||
|
||
// Remove empty (zero-length or whitespace-only) nodes | ||
$(objectDOM) | ||
.find("*") | ||
.filter(function () { | ||
return $.trim(this.innerHTML) === ""; | ||
}) | ||
.remove(); | ||
|
||
return objectDOM; | ||
}, | ||
|
||
/* | ||
* Climbs up the model heirarchy until it finds the EML model | ||
* | ||
* @return {EML211 or false} - Returns the EML 211 Model or false if not | ||
* found | ||
*/ | ||
getParentEML: function () { | ||
var emlModel = this.get("parentModel"), | ||
tries = 0; | ||
tries = 0; | ||
|
||
while (emlModel.type !== "EML" && tries < 6){ | ||
while (emlModel.type !== "EML" && tries < 6) { | ||
emlModel = emlModel.get("parentModel"); | ||
tries++; | ||
} | ||
|
||
if( emlModel && emlModel.type == "EML") | ||
return emlModel; | ||
else | ||
return false; | ||
|
||
if (emlModel && emlModel.type == "EML") return emlModel; | ||
else return false; | ||
}, | ||
|
||
trickleUpChange: function(){ | ||
MetacatUI.rootDataPackage.packageModel.set("changed", true); | ||
}, | ||
trickleUpChange: function () { | ||
MetacatUI.rootDataPackage.packageModel.set("changed", true); | ||
}, | ||
|
||
formatXML: function(xmlString){ | ||
return DataONEObject.prototype.formatXML.call(this, xmlString); | ||
} | ||
}); | ||
formatXML: function (xmlString) { | ||
return DataONEObject.prototype.formatXML.call(this, xmlString); | ||
}, | ||
}); | ||
|
||
return EMLDistribution; | ||
return EMLDistribution; | ||
}); |