/**
* Disable console output unless DEBUG mode is enabled.
* Add
* define('DEBUG', true);
* To the end of config/config.php to enable debug mode.
* The undefined checks fix the broken ie8 console
*/
var oc_debug;
var oc_webroot;
var oc_current_user = document.getElementsByTagName('head')[0].getAttribute('data-user');
var oc_requesttoken = document.getElementsByTagName('head')[0].getAttribute('data-requesttoken');
window.oc_config = window.oc_config || {};
if (typeof oc_webroot === "undefined") {
oc_webroot = location.pathname;
var pos = oc_webroot.indexOf('/index.php/');
if (pos !== -1) {
oc_webroot = oc_webroot.substr(0, pos);
}
else {
oc_webroot = oc_webroot.substr(0, oc_webroot.lastIndexOf('/'));
}
}
if (
oc_debug !== true || typeof console === "undefined" ||
typeof console.log === "undefined"
) {
if (!window.console) {
window.console = {};
}
var noOp = function() { };
var methods = ['log', 'debug', 'warn', 'info', 'error', 'assert', 'time', 'timeEnd'];
for (var i = 0; i < methods.length; i++) {
console[methods[i]] = noOp;
}
}
function initL10N(app) {
if (!( t.cache[app] )) {
$.ajax(OC.filePath('core', 'ajax', 'translations.php'), {
// TODO a proper solution for this without sync ajax calls
async: false,
data: {'app': app},
type: 'POST',
success: function (jsondata) {
t.cache[app] = jsondata.data;
t.plural_form = jsondata.plural_form;
}
});
// Bad answer ...
if (!( t.cache[app] )) {
t.cache[app] = [];
}
}
if (typeof t.plural_function[app] === 'undefined') {
t.plural_function[app] = function (n) {
var p = (n !== 1) ? 1 : 0;
return { 'nplural' : 2, 'plural' : p };
};
/**
* code below has been taken from jsgettext - which is LGPL licensed
* https://developer.berlios.de/projects/jsgettext/
* http://cvs.berlios.de/cgi-bin/viewcvs.cgi/jsgettext/jsgettext/lib/Gettext.js
*/
var pf_re = new RegExp('^(\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;a-zA-Z0-9_\\(\\)])+)', 'm');
if (pf_re.test(t.plural_form)) {
//ex english: "Plural-Forms: nplurals=2; plural=(n != 1);\n"
//pf = "nplurals=2; plural=(n != 1);";
//ex russian: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10< =4 && (n%100<10 or n%100>=20) ? 1 : 2)
//pf = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)";
var pf = t.plural_form;
if (! /;\s*$/.test(pf)) {
pf = pf.concat(';');
}
/* We used to use eval, but it seems IE has issues with it.
* We now use "new Function", though it carries a slightly
* bigger performance hit.
var code = 'function (n) { var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) }; };';
Gettext._locale_data[domain].head.plural_func = eval("("+code+")");
*/
var code = 'var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) };';
t.plural_function[app] = new Function("n", code);
} else {
console.log("Syntax error in language file. Plural-Forms header is invalid ["+t.plural_forms+"]");
}
}
}
/**
* translate a string
* @param {string} app the id of the app for which to translate the string
* @param {string} text the string to translate
* @param [vars] FIXME
* @param {number} [count] number to replace %n with
* @return {string}
*/
function t(app, text, vars, count){
initL10N(app);
var _build = function (text, vars, count) {
return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
function (a, b) {
var r = vars[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
var translation = text;
if( typeof( t.cache[app][text] ) !== 'undefined' ){
translation = t.cache[app][text];
}
if(typeof vars === 'object' || count !== undefined ) {
return _build(translation, vars, count);
} else {
return translation;
}
}
t.cache = {};
// different apps might or might not redefine the nplurals function correctly
// this is to make sure that a "broken" app doesn't mess up with the
// other app's plural function
t.plural_function = {};
/**
* translate a string
* @param {string} app the id of the app for which to translate the string
* @param {string} text_singular the string to translate for exactly one object
* @param {string} text_plural the string to translate for n objects
* @param {number} count number to determine whether to use singular or plural
* @param [vars] FIXME
* @return {string} Translated string
*/
function n(app, text_singular, text_plural, count, vars) {
initL10N(app);
var identifier = '_' + text_singular + '_::_' + text_plural + '_';
if( typeof( t.cache[app][identifier] ) !== 'undefined' ){
var translation = t.cache[app][identifier];
if ($.isArray(translation)) {
var plural = t.plural_function[app](count);
return t(app, translation[plural.plural], vars, count);
}
}
if(count === 1) {
return t(app, text_singular, vars, count);
}
else{
return t(app, text_plural, vars, count);
}
}
/**
* Sanitizes a HTML string by replacing all potential dangerous characters with HTML entities
* @param {string} s String to sanitize
* @return {string} Sanitized string
*/
function escapeHTML(s) {
return s.toString().split('&').join('&').split('<').join('<').split('>').join('>').split('"').join('"').split('\'').join(''');
}
/**
* Get the path to download a file
* @param {string} file The filename
* @param {string} dir The directory the file is in - e.g. $('#dir').val()
* @return {string} Path to download the file
* @deprecated use Files.getDownloadURL() instead
*/
function fileDownloadPath(dir, file) {
return OC.filePath('files', 'ajax', 'download.php')+'?files='+encodeURIComponent(file)+'&dir='+encodeURIComponent(dir);
}
var OC={
PERMISSION_CREATE:4,
PERMISSION_READ:1,
PERMISSION_UPDATE:2,
PERMISSION_DELETE:8,
PERMISSION_SHARE:16,
PERMISSION_ALL:31,
/* jshint camelcase: false */
webroot:oc_webroot,
appswebroots:(typeof oc_appswebroots !== 'undefined') ? oc_appswebroots:false,
currentUser:(typeof oc_current_user!=='undefined')?oc_current_user:false,
config: window.oc_config,
appConfig: window.oc_appconfig || {},
theme: window.oc_defaults || {},
coreApps:['', 'admin','log','search','settings','core','3rdparty'],
menuSpeed: 100,
/**
* Get an absolute url to a file in an app
* @param {string} app the id of the app the file belongs to
* @param {string} file the file path relative to the app folder
* @return {string} Absolute URL to a file
*/
linkTo:function(app,file){
return OC.filePath(app,'',file);
},
/**
* Creates a relative url for remote use
* @param {string} service id
* @return {string} the url
*/
linkToRemoteBase:function(service) {
return OC.webroot + '/remote.php/' + service;
},
/**
* @brief Creates an absolute url for remote use
* @param {string} service id
* @return {string} the url
*/
linkToRemote:function(service) {
return window.location.protocol + '//' + window.location.host + OC.linkToRemoteBase(service);
},
/**
* Gets the base path for the given OCS API service.
* @param {string} service name
* @return {string} OCS API base path
*/
linkToOCS: function(service) {
return window.location.protocol + '//' + window.location.host + OC.webroot + '/ocs/v1.php/' + service + '/';
},
/**
* Generates the absolute url for the given relative url, which can contain parameters.
* @param {string} url
* @param params
* @return {string} Absolute URL for the given relative URL
*/
generateUrl: function(url, params) {
var _build = function (text, vars) {
var vars = vars || [];
return text.replace(/{([^{}]*)}/g,
function (a, b) {
var r = vars[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
if (url.charAt(0) !== '/') {
url = '/' + url;
}
return OC.webroot + '/index.php' + _build(url, params);
},
/**
* Get the absolute url for a file in an app
* @param {string} app the id of the app
* @param {string} type the type of the file to link to (e.g. css,img,ajax.template)
* @param {string} file the filename
* @return {string} Absolute URL for a file in an app
*/
filePath:function(app,type,file){
var isCore=OC.coreApps.indexOf(app)!==-1,
link=OC.webroot;
if((file.substring(file.length-3) === 'php' || file.substring(file.length-3) === 'css') && !isCore){
link+='/index.php/apps/' + app;
if (file != 'index.php') {
link+='/';
if(type){
link+=encodeURI(type + '/');
}
link+= file;
}
}else if(file.substring(file.length-3) !== 'php' && !isCore){
link=OC.appswebroots[app];
if(type){
link+= '/'+type+'/';
}
if(link.substring(link.length-1) !== '/'){
link+='/';
}
link+=file;
}else{
if ((app == 'settings' || app == 'core' || app == 'search') && type == 'ajax') {
link+='/index.php/';
}
else {
link+='/';
}
if(!isCore){
link+='apps/';
}
if (app !== '') {
app+='/';
link+=app;
}
if(type){
link+=type+'/';
}
link+=file;
}
return link;
},
/**
* Redirect to the target URL, can also be used for downloads.
* @param {string} targetURL URL to redirect to
*/
redirect: function(targetURL) {
window.location = targetURL;
},
/**
* get the absolute path to an image file
* if no extension is given for the image, it will automatically decide
* between .png and .svg based on what the browser supports
* @param {string} app the app id to which the image belongs
* @param {string} file the name of the image file
* @return {string}
*/
imagePath:function(app,file){
if(file.indexOf('.')==-1){//if no extension is given, use png or svg depending on browser support
file+=(OC.Util.hasSVGSupport())?'.svg':'.png';
}
return OC.filePath(app,'img',file);
},
/**
* Load a script for the server and load it. If the script is already loaded,
* the event handler will be called directly
* @param {string} app the app id to which the script belongs
* @param {string} script the filename of the script
* @param ready event handler to be called when the script is loaded
*/
addScript:function(app,script,ready){
var deferred, path=OC.filePath(app,'js',script+'.js');
if(!OC.addScript.loaded[path]){
if(ready){
deferred=$.getScript(path,ready);
}else{
deferred=$.getScript(path);
}
OC.addScript.loaded[path]=deferred;
}else{
if(ready){
ready();
}
}
return OC.addScript.loaded[path];
},
/**
* Loads a CSS file
* @param {string} app the app id to which the css style belongs
* @param {string} style the filename of the css file
*/
addStyle:function(app,style){
var path=OC.filePath(app,'css',style+'.css');
if(OC.addStyle.loaded.indexOf(path)===-1){
OC.addStyle.loaded.push(path);
if (document.createStyleSheet) {
document.createStyleSheet(path);
} else {
style=$('');
$('head').append(style);
}
}
},
/**
* @todo Write the documentation
*/
basename: function(path) {
return path.replace(/\\/g,'/').replace( /.*\//, '' );
},
/**
* @todo Write the documentation
*/
dirname: function(path) {
return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
},
/**
* Do a search query and display the results
* @param {string} query the search query
*/
search: _.debounce(function(query){
if(query){
OC.addStyle('search','results');
$.getJSON(OC.filePath('search','ajax','search.php')+'?query='+encodeURIComponent(query), function(results){
OC.search.lastResults=results;
OC.search.showResults(results);
});
}
}, 500),
dialogs:OCdialogs,
/**
* Parses a URL query string into a JS map
* @param {string} queryString query string in the format param1=1234¶m2=abcde¶m3=xyz
* @return map containing key/values matching the URL parameters
*/
parseQueryString:function(queryString){
var parts,
pos,
components,
result = {},
key,
value;
if (!queryString){
return null;
}
pos = queryString.indexOf('?');
if (pos >= 0){
queryString = queryString.substr(pos + 1);
}
parts = queryString.replace(/\+/g, '%20').split('&');
for (var i = 0; i < parts.length; i++){
// split on first equal sign
var part = parts[i];
pos = part.indexOf('=');
if (pos >= 0) {
components = [
part.substr(0, pos),
part.substr(pos + 1)
];
}
else {
// key only
components = [part];
}
if (!components.length){
continue;
}
key = decodeURIComponent(components[0]);
if (!key){
continue;
}
// if equal sign was there, return string
if (components.length > 1) {
result[key] = decodeURIComponent(components[1]);
}
// no equal sign => null value
else {
result[key] = null;
}
}
return result;
},
/**
* Builds a URL query from a JS map.
* @param params parameter map
* @return {string} String containing a URL query (without question) mark
*/
buildQueryString: function(params) {
if (!params) {
return '';
}
return $.map(params, function(value, key) {
var s = encodeURIComponent(key);
if (value !== null && typeof(value) !== 'undefined') {
s += '=' + encodeURIComponent(value);
}
return s;
}).join('&');
},
/**
* Opens a popup with the setting for an app.
* @param {string} appid The ID of the app e.g. 'calendar', 'contacts' or 'files'.
* @param {boolean|string} loadJS If true 'js/settings.js' is loaded. If it's a string
* it will attempt to load a script by that name in the 'js' directory.
* @param {boolean} [cache] If true the javascript file won't be forced refreshed. Defaults to true.
* @param {string} [scriptName] The name of the PHP file to load. Defaults to 'settings.php' in
* the root of the app directory hierarchy.
*/
appSettings:function(args) {
if(typeof args === 'undefined' || typeof args.appid === 'undefined') {
throw { name: 'MissingParameter', message: 'The parameter appid is missing' };
}
var props = {scriptName:'settings.php', cache:true};
$.extend(props, args);
var settings = $('#appsettings');
if(settings.length === 0) {
throw { name: 'MissingDOMElement', message: 'There has be be an element with id "appsettings" for the popup to show.' };
}
var popup = $('#appsettings_popup');
if(popup.length === 0) {
$('body').prepend('