mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-18 05:51:26 +02:00
391 lines
9.5 KiB
JavaScript
391 lines
9.5 KiB
JavaScript
window.addEvent('domready', function(){
|
|
parse_menubar('topmenubar');
|
|
parse_menubar('menubar');
|
|
|
|
infobox = new InfoBox({
|
|
parent : $("menubar"),
|
|
message : "Loading"
|
|
});
|
|
});
|
|
|
|
var media_url = "";
|
|
callbacks = {};
|
|
|
|
/*
|
|
* A simple function to initialize the value of the global variable
|
|
* 'media_url' (corresponding to django's MEDIA_URL variable).
|
|
*/
|
|
function set_media_url(url) {
|
|
media_url = url;
|
|
}
|
|
|
|
/*
|
|
* Shortcut function that construct an url from the media_url and the
|
|
* given value.
|
|
*/
|
|
function static_url(value) {
|
|
return media_url + value;
|
|
}
|
|
|
|
function register_callback(name, callback) {
|
|
callbacks[name] = callback;
|
|
};
|
|
|
|
get_callback = function(name) {
|
|
return callbacks[name];
|
|
};
|
|
|
|
/*
|
|
* AJAX anchor based navigation
|
|
*/
|
|
var current_anchor = null;
|
|
|
|
function check_anchor() {
|
|
if (current_anchor.serialized == location.hash
|
|
&& !$defined(current_anchor.force)) {
|
|
return;
|
|
}
|
|
delete(current_anchor.force);
|
|
current_anchor.from_string(location.hash);
|
|
if (!current_anchor.serialized) {
|
|
location.hash = current_anchor.deflocation;
|
|
return;
|
|
}
|
|
|
|
var query = current_anchor.serialized.substring(1);
|
|
|
|
infobox.show(current_anchor.loading_message, {
|
|
profile: current_anchor.loading_color,
|
|
spinner: true
|
|
});
|
|
new Request.JSON({
|
|
url: query,
|
|
noCache : true,
|
|
onSuccess: function(resp) {
|
|
var callback = ($defined(resp.callback)) ? resp.callback : "default";
|
|
callbacks[callback](resp);
|
|
infobox.info(gettext("Done"));
|
|
infobox.hide(1);
|
|
current_anchor.reset_loading_infos();
|
|
},
|
|
onFailure: function(xhr) {
|
|
$(document.body).set("html", xhr.responseText);
|
|
$(document.body).setStyle("overflow", "auto");
|
|
}
|
|
}).get();
|
|
};
|
|
|
|
/*
|
|
* Initialize the AJAX anchor based navigation
|
|
*/
|
|
function ajaxListener(deflocation, defcallback) {
|
|
current_anchor = new HashWrapper(deflocation);
|
|
register_callback("default", defcallback);
|
|
check_anchor.periodical(300);
|
|
};
|
|
|
|
/*
|
|
*
|
|
*/
|
|
function HashWrapper(deflocation) {
|
|
this.deflocation = deflocation;
|
|
this.params = $H();
|
|
this.base = '';
|
|
this.serialized = null;
|
|
|
|
this.reset_loading_infos = function() {
|
|
this.loading_message = gettext("Loading...");
|
|
this.loading_color = "gray";
|
|
};
|
|
this.reset_loading_infos();
|
|
|
|
this.reset = function() {
|
|
this.base = null;
|
|
this.params.empty();
|
|
return this;
|
|
};
|
|
|
|
this.parse_string = function(value, reset) {
|
|
var splits = (value.charAt(0) == '#')
|
|
? value.substring(1).split('?') : value.split('?');
|
|
|
|
if (splits.length == 0) {
|
|
return this;
|
|
}
|
|
if (!$defined(reset)) {
|
|
reset = false;
|
|
}
|
|
if (reset) {
|
|
this.reset();
|
|
}
|
|
this.base = (splits[0] == "") ? this.deflocation : splits[0];
|
|
|
|
var re = new RegExp("/$");
|
|
if (!this.base.match(re)) {
|
|
this.base += '/';
|
|
}
|
|
if (splits.length > 1) {
|
|
var params = splits[1].split('&');
|
|
for (var i = 0; i < params.length; i++) {
|
|
var tmp = params[i].split('=');
|
|
this.setparam(tmp[0], tmp[1]);
|
|
}
|
|
}
|
|
return this;
|
|
};
|
|
|
|
this.from_string = function(value, reset) {
|
|
this.parse_string(value);
|
|
this.serialized = value;
|
|
return this;
|
|
};
|
|
|
|
this.serialize = function() {
|
|
var res = this.base;
|
|
|
|
if (this.params.getLength() != 0) {
|
|
res += "?" + this.params.toQueryString();
|
|
}
|
|
return res;
|
|
};
|
|
|
|
this.update = function(force) {
|
|
window.fireEvent("pageRefresh");
|
|
location.hash = this.serialize();
|
|
if ($defined(force)) {
|
|
this.force = force;
|
|
}
|
|
};
|
|
|
|
this.deleteParam = function(str) {
|
|
this.params.erase(str);
|
|
};
|
|
|
|
this.updateparams = function(str) {
|
|
if (str.charAt(0) == '?') {
|
|
str = str.substring(1);
|
|
}
|
|
var elems = str.split('&');
|
|
for (var i = 0; i < elems.length; i++) {
|
|
this.setparamfromstring(elems[i]);
|
|
}
|
|
};
|
|
|
|
this.setparamfromstring = function(str) {
|
|
var def = str.split('=');
|
|
this.params.set(def[0], def[1]);
|
|
};
|
|
|
|
this.setparam = function(name, value) {
|
|
this.params.set(name, value);
|
|
return this;
|
|
};
|
|
|
|
this.setparams = function(params) {
|
|
this.params.extend(params);
|
|
return this;
|
|
};
|
|
|
|
this.getparam = function(name) {
|
|
if (!this.params.has(name)) {
|
|
return "";
|
|
}
|
|
return this.params.get(name);
|
|
};
|
|
|
|
this.baseurl = function(value, noreset) {
|
|
if (!$defined(noreset) || noreset == 0) {
|
|
this.reset();
|
|
}
|
|
this.base = value;
|
|
var re = new RegExp("/$");
|
|
if (!this.base.match(re)) {
|
|
this.base += '/';
|
|
}
|
|
return this;
|
|
};
|
|
|
|
this.addbaseurl = function(value) {
|
|
var newbase = this.base + value;
|
|
return this.baseurl(newbase);
|
|
};
|
|
|
|
this.getbaseurl = function() {
|
|
return this.base.substr(0, this.base.length - 1);
|
|
};
|
|
|
|
this.delparam = function(name) {
|
|
if (!this.params.has(name)) {
|
|
return false;
|
|
}
|
|
this.params.erase(name);
|
|
return this;
|
|
};
|
|
};
|
|
|
|
function parse_menubar(id) {
|
|
if ($(id)) {
|
|
$(id).getElements('li.dropdown').each(function(elem){
|
|
var list = elem.getElement('ul.links');
|
|
var myFx = new Fx.Slide(list, {
|
|
duration: 200
|
|
});
|
|
|
|
myFx.wrapper.setStyles({
|
|
"position" : "absolute"
|
|
});
|
|
myFx.hide();
|
|
|
|
elem.addEvents({
|
|
'mouseenter' : function(evt) {
|
|
myFx.cancel();
|
|
myFx.slideIn();
|
|
},
|
|
'mouseleave' : function() {
|
|
myFx.cancel();
|
|
myFx.slideOut();
|
|
}
|
|
});
|
|
});
|
|
SqueezeBox.assign($(id).getElements('a[class=boxed]'), {
|
|
parse: 'rel'
|
|
});
|
|
}
|
|
};
|
|
|
|
get_iframe_body = function(id) {
|
|
var saf = navigator.userAgent.match(/Safari/i);
|
|
var safver = (saf ? parseFloat(navigator.userAgent.match(/[\d\.]+Safari/i)) : 0);
|
|
var targetif = $(id);
|
|
var data;
|
|
|
|
if (targetif.contentDocument && (!saf || (saf && safver >= 3))) {
|
|
// NS6 & Gecko & Opera & IE7+
|
|
if (!saf || (saf && safver >= 3)) {
|
|
data = targetif.contentDocument.defaultView.document.body;
|
|
} else {
|
|
data = targetif.document.body;
|
|
}
|
|
} else if (targetif.contentWindow && !saf) {
|
|
// IE 5.5 & 6.x
|
|
data = targetif.contentWindow.document.body;
|
|
}
|
|
return (data);
|
|
};
|
|
|
|
setDivHeight = function(id, extrah, modulo) {
|
|
var contentsize = $(document.body).getSize().y
|
|
- $("topmenubar").getSize().y
|
|
- $("header").getSize().y
|
|
- $("footer").getSize().y
|
|
- extrah;
|
|
|
|
if (modulo) {
|
|
contentsize -= contentsize % modulo;
|
|
}
|
|
$(id).setStyle("height", contentsize + "px");
|
|
};
|
|
|
|
bindRows = function(rooturl, page) {
|
|
$$("tr").addEvent("dblclick", function(event) {
|
|
if (event.target.parentNode.id) {
|
|
var url = rooturl + "?domid=" + event.target.parentNode.id;
|
|
if (page != "") {
|
|
url += "?page=" + page;
|
|
}
|
|
event.target.parentNode.setStyle("background", "#ffffcc");
|
|
location.href = url;
|
|
}
|
|
});
|
|
};
|
|
|
|
toggleSelection = function(id, value) {
|
|
$$("input[name=" + id + "]").each(function(obj) {
|
|
obj.checked = value;
|
|
});
|
|
};
|
|
|
|
checkTableSelection = function(text) {
|
|
if (!$$("tr[class*=table-tr-selected]").length) {
|
|
return false;
|
|
}
|
|
return confirm(text);
|
|
};
|
|
|
|
searchbox_init = function() {
|
|
var myFx = new Fx.Slide($("searchparams"));
|
|
myFx.hide();
|
|
$("searchopts").addEvent("click", function(event) {
|
|
myFx.cancel();
|
|
myFx.toggle();
|
|
});
|
|
$$("input[name=scriteria]").addEvent("click", function(event) {
|
|
myFx.cancel();
|
|
myFx.toggle();
|
|
});
|
|
$("searchfield").addEvent("change", function(event) {
|
|
var criteria = "";
|
|
$$("input[name=scriteria]:checked").each(function(obj) {
|
|
criteria = obj.value;
|
|
});
|
|
var pattern = $("searchfield").get("value");
|
|
if (pattern != "") {
|
|
var params = "pattern=" + pattern + "&criteria=" + criteria;
|
|
current_anchor.updateparams(params);
|
|
} else {
|
|
current_anchor.deleteParam("pattern");
|
|
current_anchor.deleteParam("criteria");
|
|
}
|
|
current_anchor.update();
|
|
});
|
|
$("searchfield").addEvent("click", function(event) {
|
|
$("searchfield").select();
|
|
});
|
|
|
|
$("searchfield").addEvent("blur", function(event) {
|
|
if ($("searchfield").value == "") {
|
|
$("searchfield").set("value", gettext("Search..."));
|
|
}
|
|
});
|
|
$("searchbox").setStyle("float", "left");
|
|
var frag = new URI(location.href).get("fragment");
|
|
if (frag.indexOf('?') != -1) {
|
|
var params = frag.substring(frag.indexOf('?') + 1).parseQueryString();
|
|
if ($defined(params.pattern)) {
|
|
$("searchfield").set("value", params.pattern);
|
|
}
|
|
if ($defined(params.criteria)) {
|
|
$("crit_" + params.criteria).checked = true;
|
|
}
|
|
}
|
|
};
|
|
|
|
// The following code prevents a bug under IE7 because fullpath is
|
|
// returned instead of a relative one. (even if mootools uses
|
|
// getAttribute("href", 2), this is not working for AJAX requests)
|
|
gethref = function(obj) {
|
|
var url = obj.get("href");
|
|
var re = new RegExp("^(https?):");
|
|
var scheme = re.exec(url);
|
|
|
|
if (scheme != null) {
|
|
var baseurl = scheme[0] + "://" + location.host + location.pathname;
|
|
return url.replace(baseurl, "");
|
|
}
|
|
return url;
|
|
};
|
|
|
|
saveparams = function(id, message) {
|
|
$$("input[name=update]").addEvent("click", function(evt) {
|
|
evt.stop();
|
|
$(id).set("send", {
|
|
onSuccess: function(response) {
|
|
var decode = JSON.decode(response);
|
|
if (decode.status == "ok") {
|
|
infobox.info(message);
|
|
infobox.hide(1);
|
|
}
|
|
}
|
|
}).send();
|
|
});
|
|
}; |