Author: ygrego Date: 2015-06-09 20:40:34 +0000 (Tue, 09 Jun 2015) New Revision: 1676 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1676 Log: Transformation of theses classes in ES6 classes. Modified: oipf/lib/js/utils/Timeout.js oipf/lib/js/utils/TimerManager.js oipf/lib/js/utils/TimerRecordingManager.js oipf/lib/js/utils/XmlParser.js oipf/lib/js/utils/XmlToJson.js Modified: oipf/lib/js/utils/Timeout.js =================================================================== --- oipf/lib/js/utils/Timeout.js 2015-06-09 19:40:16 UTC (rev 1675) +++ oipf/lib/js/utils/Timeout.js 2015-06-09 20:40:34 UTC (rev 1676) @@ -1,37 +1,39 @@ -/* +/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ -var Timeout = Class.extend({ - timerId: null, - - promise: null, - - init: function(time) { +class Timeout { + + constructor(time) { + this.timerId = null; + + this.promise = null; + var self = this; + this.promise = new Promise(function(resolve, reject) { self.timerId = setTimeout(function() { resolve(); - }, time || 0); + }, time || 0); }); - }, - - then: function(callback) { + } + + then(callback) { this.promise.then(callback); return this; - }, - - catch: function(callback) { + } + + catch(callback) { this.promise.catch(callback); return this; - }, - - cancel: function() { + } + + cancel() { clearTimeout(this.timerId); this.timerId = null; this.promise = null; } - -}); +} + Modified: oipf/lib/js/utils/TimerManager.js =================================================================== --- oipf/lib/js/utils/TimerManager.js 2015-06-09 19:40:16 UTC (rev 1675) +++ oipf/lib/js/utils/TimerManager.js 2015-06-09 20:40:34 UTC (rev 1676) @@ -1,25 +1,26 @@ -/* +/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ -var TimerManager = Class.extend({ - - timer: null, +class TimerManager { - createTimer: function(time) { + constructor() { + this.timer = null; + } + + createTimer(time) { this.clearTimer(); this.timer = new Timeout(time); console.log("createTimer", this.timer.timerId); return this.timer; - }, + } - clearTimer: function() { + clearTimer() { if (this.timer) { this.timer.cancel(); console.log("cancelTimer", this.timer.timerId); } } - -}); +} Modified: oipf/lib/js/utils/TimerRecordingManager.js =================================================================== --- oipf/lib/js/utils/TimerRecordingManager.js 2015-06-09 19:40:16 UTC (rev 1675) +++ oipf/lib/js/utils/TimerRecordingManager.js 2015-06-09 20:40:34 UTC (rev 1676) @@ -1,29 +1,28 @@ -/* +/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ -var TimerRecordingManager = Class.extend({ - - init: function() { +class TimerRecordingManager { + + constructor() { this.recordingTimers = {}; - }, - - createTimer: function(time) { + } + + createTimer(time) { var timer = new Timeout(time); console.log("createTimer", timer.timerId); return timer; - }, + } - clearTimer: function(recordingID) { + clearTimer(recordingID) { console.log("Method clearTimer called."); var timers = this.recordingTimers[recordingID]; - timers.start && timers.start.cancel() && + timers.start && timers.start.cancel() && console.log("cancelTimer", "Start timer: ", timers.start.timerId); timers.end && timers.end.cancel() && console.log("cancelTimer", "End timer: ", timers.end.timerId); } - -}); +} Modified: oipf/lib/js/utils/XmlParser.js =================================================================== --- oipf/lib/js/utils/XmlParser.js 2015-06-09 19:40:16 UTC (rev 1675) +++ oipf/lib/js/utils/XmlParser.js 2015-06-09 20:40:34 UTC (rev 1676) @@ -1,40 +1,40 @@ -/* +/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ -var XmlParser = Class.extend({ - - init: function() { - +class XmlParser { + + constructor() { + this.initParser(); this.xmlDocument; - }, - - initParser: function() { + } + + initParser() { if (window.DOMParser) { - this.parser=new DOMParser(); + this.parser = new DOMParser(); } - }, - - createAXmlStringDocument: function(defaultPropertiesObject) { + } + + createAXmlStringDocument(defaultPropertiesObject) { console.log("Method createAXmlStringDocument called"); var defaultProperties = defaultPropertiesObject.rearrangeDefaultProperties(); var objectsNames = Object.keys(defaultProperties); var xmlStringDocument = "<" + defaultPropertiesObject.roots + ">"; for (var i = 0, l = objectsNames.length; i < l; i++) { - + var currentObjectName = objectsNames[i]; xmlStringDocument += "<" + currentObjectName; var currentObjectData = defaultProperties[currentObjectName]; var currentAttributes = currentObjectData.attributes; - + if (currentAttributes) { xmlStringDocument = this.createAttributes(currentAttributes, xmlStringDocument); } - + var currentObjectValue = currentObjectData.value; xmlStringDocument += ">" + currentObjectValue; xmlStringDocument += "</" + currentObjectName + ">"; @@ -42,32 +42,32 @@ xmlStringDocument += "</" + defaultPropertiesObject.roots + ">"; console.log("Method createAXmlStringDocument completed"); return xmlStringDocument; - }, - - createAttributes: function(currentAttributes, xmlStringDocument) { + } + + createAttributes(currentAttributes, xmlStringDocument) { var currentAttributesNames = Object.keys(currentAttributes); for (var i = 0, l = currentAttributesNames.length; i < l; i++) { var currentAttributeName = currentAttributesNames[i]; var currentAttributeValue = currentAttributes[currentAttributeName]; - xmlStringDocument += " " + currentAttributeName + xmlStringDocument += " " + currentAttributeName + "='" + currentAttributeValue + "'"; } return xmlStringDocument; - }, - - getXmlDocument: function(defaultProperties) { + } + + getXmlDocument(defaultProperties) { if (!this.parser) { this.initParser(); } - var xmlStringDocument = + var xmlStringDocument = this.createAXmlStringDocument(defaultProperties); - - this.xmlDocument = + + this.xmlDocument = this.parser.parseFromString(xmlStringDocument, "text/xml"); - + return this.xmlDocument; } - -}); +} + Modified: oipf/lib/js/utils/XmlToJson.js =================================================================== --- oipf/lib/js/utils/XmlToJson.js 2015-06-09 19:40:16 UTC (rev 1675) +++ oipf/lib/js/utils/XmlToJson.js 2015-06-09 20:40:34 UTC (rev 1676) @@ -1,97 +1,98 @@ -/* +/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ -var XmlToJson = Class.extend({ - unwantedNodeName: { - "#text": true, - "#comment": true - }, - - mappingValue: { - "true": true, - "false": false - }, - - wantedNode: "channel", - - init: function(xmlDocument) { +class XmlToJson { + + constructor(xmlDocument) { xmlDocument && (this.xmlDocument = xmlDocument) && (this.json = {}) && this.initJson(); - }, - - initJson: function() { + + this.unwantedNodeName = { + "#text": true, + "#comment": true + }; + + this.mappingValue = { + "true": true, + "false": false + }; + + this.wantedNode = "channel"; + } + + initJson() { if (!this.xmlDocument) { return null; } - + this.rootsNode = this.xmlDocument.firstChild; - + //We already know that document contains at least one child. var rootsName = this.rootsNode.nodeName; this.rootsElementJson = this.json[rootsName] = {}; - + var rootsObject = this.json[rootsName]; - + this.convertAttributesToProperties(rootsObject, this.rootsNode); - }, - - convertAttributesToProperties: function(object, node) { + } + + convertAttributesToProperties(object, node) { var attributesList = node.attributes; - + for (var i = 0, li = attributesList.length; i < li; i++) { var currentAttributes = attributesList[i]; var name = currentAttributes.name; - var value = currentAttributes.value; + var value = currentAttributes.value; object[name] = value; } return object; - }, - - getJson: function() { + } + + getJson() { if (!this.json) { console.log("Your xml document is not valid or null."); return null; } var data = this.rootsNode.childNodes; var dataStoredByKind = {}; - + for (var i = 0, li = data.length; i < li; i++) { var currentNode = data[i]; this.setPropertiesFromDomNodeList(currentNode, this.rootsElementJson); } return this.json; - }, - - setPropertiesFromDomNodeList: function(currentNode, parentContainer) { - + } + + setPropertiesFromDomNodeList(currentNode, parentContainer) { + if (this.unwantedNodeName[currentNode.nodeName]) { return; } - + var childNodes = currentNode.childNodes; var childNodeName; var currentChildNode; var nodeDataMap = {}; if (currentNode.hasAttributes()) { - nodeDataMap = + nodeDataMap = this.convertAttributesToProperties(nodeDataMap, currentNode); } - + for (var i = 0, li = childNodes.length; i < li; i++) { currentChildNode = childNodes[i]; childNodeName = currentChildNode.nodeName; - + if (!this.unwantedNodeName[childNodeName]) { nodeDataMap = this.setPropertiesFromDomNodeList (currentChildNode, nodeDataMap); } - - if (/\w+/.exec(currentChildNode.nodeValue) && + + if (/\w+/.exec(currentChildNode.nodeValue) && (currentChildNode.childNodes.length == 0)) { - + if (!this.isMapNotEmpty(nodeDataMap)) { var currentNodeName = currentNode.nodeName; var key = currentNodeName; @@ -99,8 +100,8 @@ this.addPropertiesToMap(parentContainer, key, value, currentNode.parentNode); } - - //This add concern only the nodes which have an atomic content + + //This add concern only the nodes which have an atomic content if (this.isMapNotEmpty(nodeDataMap) && childNodes.length == 1) { var key = "#text"; var value = currentChildNode.nodeValue; @@ -109,14 +110,14 @@ } } } - + //Addition of node in parent container even if this one have not a value - if (!this.unwantedNodeName[currentNode.nodeName] && + if (!this.unwantedNodeName[currentNode.nodeName] && !currentChildNode) { - + var currentNodeName = currentNode.nodeName; var key = currentNodeName; - this.addPropertiesToMap(parentContainer, key, null, + this.addPropertiesToMap(parentContainer, key, null, currentNode.parentNode); } @@ -124,46 +125,46 @@ if (this.isMapNotEmpty(nodeDataMap)) { var key = currentNodeName; // parentContainer[key] = nodeDataMap; - this.addPropertiesToMap(parentContainer, key, nodeDataMap, + this.addPropertiesToMap(parentContainer, key, nodeDataMap, currentNode.parentNode); } - + return parentContainer; - }, - - isMapNotEmpty: function(map) { + } + + isMapNotEmpty(map) { return Object.keys(map).length > 0; - }, - + } + /* - * + * */ - addPropertiesToMap: function(map, key, value, parentNode) { + addPropertiesToMap(map, key, value, parentNode) { // map[key] = value; - + var childNodesWithSpecificName = parentNode.getElementsByTagName(key); - var specificNodesNumber = childNodesWithSpecificName.length; - + var specificNodesNumber = childNodesWithSpecificName.length; + if ((specificNodesNumber > 1) && !map[key]) { - map[key] = []; + map[key] = []; } - + var nodeValue = map[key]; - + var value = this.setValue(value); - + if (nodeValue && (nodeValue instanceof Array)) { map[key].push(value); } else { map[key] = value; } - }, - - setValue: function(value) { + } + + setValue(value) { var newValue = this.mappingValue[value]; if ((typeof value) == "string" && newValue != undefined) { return newValue; } return value; } -}); +}
participants (1)
-
ygrego@users.nuiton.org