0
0
mirror of https://github.com/Doodle3D/doodle3d-connect.git synced 2024-12-27 02:53:48 +01:00
doodle3d-connect/js/main.js

146 lines
4.1 KiB
JavaScript
Raw Normal View History

/*
* This file is part of the Doodle3D project (http://doodle3d.com).
*
* Copyright (c) 2013, Doodle3D
* This software is licensed under the terms of the GNU GPL v2 or later.
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
*/
2013-10-02 17:24:40 +02:00
2014-04-24 12:46:26 +02:00
var d3d = {};
d3d.util = {
// Helper function that splits a URL just the way we want it
parseURL:function(url) {
var parsed = $.mobile.path.parseUrl( url );
var hashParts = parsed.hash.split( "?" );
2014-05-01 18:37:29 +02:00
var parameters;
2014-04-24 12:46:26 +02:00
// Assemble query parameters object from the query string
if (hashParts.length > 1) {
2014-05-01 18:37:29 +02:00
parameters = {};
jQuery.each(hashParts[1].split( "&" ), function( index, value ) {
2014-04-24 12:46:26 +02:00
var pair = value.split( "=" );
if ( pair.length > 0 && pair[ 0 ] ) {
parameters[ pair[ 0 ] ] =
2014-04-25 16:52:02 +02:00
( pair.length > 1 ? decodeURIComponent(pair[ 1 ]) : true );
2014-04-24 12:46:26 +02:00
}
});
}
return {
parsed: parsed,
hash: ( hashParts.length > 0 ? hashParts[ 0 ] : "" ),
parameters: parameters
};
},
2017-06-20 21:44:14 +02:00
2014-04-24 12:46:26 +02:00
getPageParams:function(pageID) {
return d3d.pageParams[pageID];
},
replaceURLParameters:function(href,parameters){
href = href.split("?")[0];
var i = 0;
jQuery.each(parameters, function (key,value) {
2014-04-25 16:22:55 +02:00
value = encodeURIComponent(value);
2014-04-24 12:46:26 +02:00
href += (i===0)? "?" : "&";
href += key+"="+value;
i++;
});
2014-04-24 12:46:26 +02:00
return href;
},
showLoader:function(autoHide) {
setTimeout(function(){
2014-05-07 15:18:27 +02:00
var loader = $.mobile.loading('show'); //.addClass("fadePrepare");
loader.addClass("fadeIn");
/*setTimeout(function() {
loader.addClass("fadeIn");
},500);*/
2014-04-24 12:46:26 +02:00
if(autoHide) {
setTimeout(function() {
$.mobile.loading('hide');
},1000);
}
}, 1);
},
hideLoader:function() {
$.mobile.loading('hide');
},
getFormData:function(form) {
var formData = {};
jQuery.each(form.serializeArray(), function(index,field) {
formData[field['name']] = field['value'];
});
return formData;
2014-05-07 15:18:08 +02:00
},
enableRefreshPrevention:function() {
$(document).on("keydown",d3d.util.preventRefresh);
},
disableRefreshPrevention:function() {
$(document).off("keydown",d3d.util.preventRefresh);
},
preventRefresh:function(event) {
if((event.which === 82 && (event.ctrlKey || event.metaKey)) || // ctrl+r
event.which === 116) { // F5
event.preventDefault();
event.stopImmediatePropagation();
return false;
}
},
disableLeaveWarning:function() {
window.onbeforeunload = null;
},
enableLeaveWarning:function(warning) {
if(warning === undefined) {
warning = "Are you sure you want to leave?";
}
window.onbeforeunload = function() {
return warning;
};
2014-05-08 22:40:08 +02:00
},
autofocus:function(form) {
if (!("autofocus" in document.createElement("input"))) {
var target = form.find("input[autofocus]:visible");
target.focus();
2017-06-20 21:44:14 +02:00
}
},
getQueryParam:function(param) {
var result = window.location.search.match(new RegExp("(\\?|&)" + param + "(\\[\\])?=([^&]*)"));
return result ? result[3] : false;
},
formatBytes:function(a,b) {
if (0===a) {
return "0 Bytes";
} else {
var c=1e3,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"];
var f=Math.floor(Math.log(a)/Math.log(c));
return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f];
}
},
formatPercentage:function(cur,total) {
console.log("formatPercentage",cur,total);
return Math.round(cur/total*100) + "%";
},
2014-04-24 12:46:26 +02:00
};
2014-04-24 12:46:26 +02:00
(function (w) {
2014-05-12 14:44:21 +02:00
// To get to url parameters we need the url
2014-05-01 18:37:29 +02:00
// only pagecontainer events contain url's
// we parse the parameters and store them in a global object
d3d.pageParams = {};
2017-06-20 21:44:14 +02:00
d3d.pageParams.uuid = d3d.util.getQueryParam("uuid");
2014-05-01 18:37:29 +02:00
$.mobile.document.on( "pagebeforechange", function( event, data ) {
2014-05-12 14:44:21 +02:00
if (typeof data.toPage !== "string") { return; }
2014-05-01 18:37:29 +02:00
var url = d3d.util.parseURL(data.toPage);
if(url.parameters === undefined) { return; }
2014-04-24 12:46:26 +02:00
d3d.pageParams[url.hash] = url.parameters;
2017-06-20 21:44:14 +02:00
2014-05-01 18:37:29 +02:00
// let jQuery mobile navigate to page (providing only the page id so it understands)
$.mobile.changePage(url.hash, data.options);
// replace the history item with a url including parameters
window.history.replaceState(null, null, url.parsed.href);
// make sure that the parameters are not removed from the visible url
event.preventDefault();
});
2014-04-24 12:46:26 +02:00
})(window);