/***************************** BEGIN LICENSE BLOCK ***************************
The contents of this file are subject to the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
Copyright (C) 2015-2017 Mathieu Dhainaut. All Rights Reserved.
Author: Mathieu Dhainaut <mathieu.dhainaut@gmail.com>
******************************* END LICENSE BLOCK ***************************/
var MAX_LONG = Math.pow(2, 53) + 1;
/**
*
* @constructor
*/
OSH.Utils = function() {};
/**
*
* @returns {string}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.randomUUID = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};
/**
* This function stamps/embeds a UUID into an object and returns the UUID generated for it
* @returns {string}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.stampUUID = function(obj) {
obj._osh_id = obj._osh_id || OSH.Utils.randomUUID();
return obj._osh_id;
};
/**
*
* @param xmlStr
* @returns {*}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.jsonix_XML2JSON = function (xmlStr) {
var module = SOS_2_0_Module_Factory();
var context = new Jsonix.Context([XLink_1_0, IC_2_0, SMIL_2_0, SMIL_2_0_Language, GML_3_1_1, SWE_1_0_1, GML_3_2_1, OWS_1_1_0, SWE_2_0, SWES_2_0, WSN_T_1, WS_Addr_1_0_Core, OM_2_0, ISO19139_GMD_20070417, ISO19139_GCO_20070417, ISO19139_GSS_20070417, ISO19139_GTS_20070417, ISO19139_GSR_20070417, Filter_2_0, SensorML_2_0, SOS_2_0]);
var unmarshaller = context.createUnmarshaller();
var jsonData = unmarshaller.unmarshalString(xmlStr);
return jsonData;
};
OSH.Utils.jsonix_JSON2XML = function (jsonStr) {
var module = SOS_2_0_Module_Factory();
var context = new Jsonix.Context([XLink_1_0, IC_2_0, SMIL_2_0, SMIL_2_0_Language, GML_3_1_1, SWE_1_0_1, GML_3_2_1, OWS_1_1_0, SWE_2_0, SWES_2_0, WSN_T_1, WS_Addr_1_0_Core, OM_2_0, ISO19139_GMD_20070417, ISO19139_GCO_20070417, ISO19139_GSS_20070417, ISO19139_GTS_20070417, ISO19139_GSR_20070417, Filter_2_0, SensorML_2_0, SOS_2_0]);
var marshaller = context.createMarshaller();
var xmlData = marshaller.marshalString(jsonStr);
return xmlData;
};
//buffer is an ArrayBuffer object, the offset if specified in bytes, and the type is a string
//corresponding to an OGC data type.
//See http://def.seegrid.csiro.au/sissvoc/ogc-def/resource?uri=http://www.opengis.net/def/dataType/OGC/0/
/**
*
* @param buffer
* @param offset
* @param type
* @returns {*}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.ParseBytes = function (buffer, offset, type) {
var view = new DataView(buffer);
//Note: There exist types not listed in the map below that have OGC definitions, but no appropriate
//methods or corresponding types available for parsing in javascript. They are float128, float16, signedLong,
//and unsignedLong
var typeMap = {
double: function (offset) {
return {val: view.getFloat64(offset), bytes: 8};
},
float64: function (offset) {
return {val: view.getFloat64(offset), bytes: 8};
},
float32: function (offset) {
return {val: view.getFloat32(offset), bytes: 4};
},
signedByte: function (offset) {
return {val: view.getInt8(offset), bytes: 1};
},
signedInt: function (offset) {
return {val: view.getInt32(offset), bytes: 4};
},
signedShort: function (offset) {
return {val: view.getInt16(offset), bytes: 2};
},
unsignedByte: function (offset) {
return {val: view.getUint8(offset), bytes: 1};
},
unsignedInt: function (offset) {
return {val: view.getUint32(offset), bytes: 4};
},
unsignedShort: function (offset) {
return {val: view.getUint16(offset), bytes: 2};
},
//TODO: string-utf-8:
};
return typeMap[type](offset);
};
//This function recursivley iterates over the resultStructure to fill in
//values read from data which should be an ArrayBuffer containing the payload from a websocket
/**
*
* @param struct
* @param data
* @param offsetBytes
* @returns {*}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.ReadData = function(struct, data, offsetBytes) {
var offset = offsetBytes;
for(var i = 0 ; i < struct.fields.length; i++) {
var currFieldStruct = struct.fields[i];
if(typeof currFieldStruct.type != 'undefined' && currFieldStruct.type !== null) {
var ret = OSH.Utils.ParseBytes(data, offset, currFieldStruct.type);
currFieldStruct.val = ret.val;
offset += ret.bytes;
} else if(typeof currFieldStruct.count != 'undefined' && currFieldStruct.count !== null) {
//check if count is a reference to another variable
if(isNaN(currFieldStruct.count))
{
var id = currFieldStruct.count;
var fieldName = struct.id2FieldMap[id];
currFieldStruct.count = struct.findFieldByName(fieldName).val;
}
for(var c = 0; c < currFieldStruct.count; c++) {
for(var j = 0 ; j < currFieldStruct.fields.length; j++) {
var field = JSON.parse(JSON.stringify(currFieldStruct.fields[j]));
offset = OSH.Utils.ReadData(field, data, offset);
currFieldStruct.val.push(field);
}
}
}
}
return offset;
};
/**
*
* @param resultStructure
* @returns {{}}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.GetResultObject = function(resultStructure) {
//TODO: handle cases for nested arrays / matrix data types
var result = {};
for(var i = 0; i < resultStructure.fields.length; i++) {
if(typeof resultStructure.fields[i].count != 'undefined') {
result[resultStructure.fields[i].name] = [];
for(var c = 0; c < resultStructure.fields[i].count; c++) {
var item = {};
for(var k = 0; k < resultStructure.fields[i].val[c].fields.length; k++) {
item[resultStructure.fields[i].val[c].fields[k].name] = resultStructure.fields[i].val[c].fields[k].val;
}
result[resultStructure.fields[i].name].push(item);
}
} else {
result[resultStructure.fields[i].name] = resultStructure.fields[i].val;
}
}
return result;
};
/**
*
* @returns {boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isOpera = function () {
return (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
};
/**
*
* @returns {boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isFirefox = function() {
return typeof InstallTrigger !== 'undefined';
};
/**
*
* @returns {boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isSafari = function() {
return Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
};
/**
*
* @returns {boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isIE = function() {
return /*@cc_on!@*/false || !!document.documentMode;
};
/**
*
* @returns {boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isChrome = function() {
return !!window.chrome && !!window.chrome.webstore;
};
/**
*
* @returns {*|boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isBlink = function() {
return (isChrome || isOpera) && !!window.CSS;
};
//------- GET X,Y absolute cursor position ---//
var absoluteXposition = null;
var absoluteYposition = null;
document.addEventListener('mousemove', onMouseUpdate, false);
document.addEventListener('mouseenter', onMouseUpdate, false);
function onMouseUpdate(e) {
absoluteXposition = e.pageX;
absoluteYposition = e.pageY;
}
/**
*
* @returns {*}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.getXCursorPosition = function() {
return absoluteXposition;
};
/**
*
* @returns {*}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.getYCursorPosition = function() {
return absoluteYposition;
};
/**
*
* @param a
* @param b
* @returns {boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isArrayIntersect = function(a, b) {
return a.filter(function(element){
return b.indexOf(element) > -1;
}).length > 0;
};
/**
*
* @param o
* @returns {boolean}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isElement = function isElement(o) {
return (
typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName==="string"
);
};
/**
*
* @returns {*}
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.isWebWorker = function() {
if (typeof(Worker) !== "undefined") {
return true;
}
return false;
};
/**
*
* @param div
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.takeScreeshot = function(div) {
};
/**
* Remove a css class from a the div given as argument.
* @param div the div to remove the class from
* @param css the css class to remove
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.removeCss = function(div,css) {
var divCss = div.className;
css = divCss.replace(css,"");
div.className = css;
};
/**
* Add a css class to a the div given as argument.
* @param div the div to add the class to
* @param css the css class to add
* @instance
* @memberof OSH.Utils
*/
OSH.Utils.addCss = function(div,css) {
div.setAttribute("class",div.className+" "+css);
};
OSH.Utils.removeLastCharIfExist = function(value,char) {
if(typeof value === undefined || value === null || value.length === 0 || !value.endsWith("/")) {
return value;
}
return value.substring(0,value.length-1);
};