You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

js.js 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /**
  2. * Disable console output unless DEBUG mode is enabled.
  3. * Add
  4. * define('DEBUG', true);
  5. * To the end of config/config.php to enable debug mode.
  6. * The undefined checks fix the broken ie8 console
  7. */
  8. var oc_debug;
  9. var oc_webroot;
  10. var oc_current_user = document.getElementsByTagName('head')[0].getAttribute('data-user');
  11. var oc_requesttoken = document.getElementsByTagName('head')[0].getAttribute('data-requesttoken');
  12. window.oc_config = window.oc_config || {};
  13. if (typeof oc_webroot === "undefined") {
  14. oc_webroot = location.pathname;
  15. var pos = oc_webroot.indexOf('/index.php/');
  16. if (pos !== -1) {
  17. oc_webroot = oc_webroot.substr(0, pos);
  18. }
  19. else {
  20. oc_webroot = oc_webroot.substr(0, oc_webroot.lastIndexOf('/'));
  21. }
  22. }
  23. if (
  24. oc_debug !== true || typeof console === "undefined" ||
  25. typeof console.log === "undefined"
  26. ) {
  27. if (!window.console) {
  28. window.console = {};
  29. }
  30. var noOp = function() { };
  31. var methods = ['log', 'debug', 'warn', 'info', 'error', 'assert', 'time', 'timeEnd'];
  32. for (var i = 0; i < methods.length; i++) {
  33. console[methods[i]] = noOp;
  34. }
  35. }
  36. function initL10N(app) {
  37. if (!( t.cache[app] )) {
  38. $.ajax(OC.filePath('core', 'ajax', 'translations.php'), {
  39. // TODO a proper solution for this without sync ajax calls
  40. async: false,
  41. data: {'app': app},
  42. type: 'POST',
  43. success: function (jsondata) {
  44. t.cache[app] = jsondata.data;
  45. t.plural_form = jsondata.plural_form;
  46. }
  47. });
  48. // Bad answer ...
  49. if (!( t.cache[app] )) {
  50. t.cache[app] = [];
  51. }
  52. }
  53. if (typeof t.plural_function[app] === 'undefined') {
  54. t.plural_function[app] = function (n) {
  55. var p = (n !== 1) ? 1 : 0;
  56. return { 'nplural' : 2, 'plural' : p };
  57. };
  58. /**
  59. * code below has been taken from jsgettext - which is LGPL licensed
  60. * https://developer.berlios.de/projects/jsgettext/
  61. * http://cvs.berlios.de/cgi-bin/viewcvs.cgi/jsgettext/jsgettext/lib/Gettext.js
  62. */
  63. var pf_re = new RegExp('^(\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;a-zA-Z0-9_\\(\\)])+)', 'm');
  64. if (pf_re.test(t.plural_form)) {
  65. //ex english: "Plural-Forms: nplurals=2; plural=(n != 1);\n"
  66. //pf = "nplurals=2; plural=(n != 1);";
  67. //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)
  68. //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)";
  69. var pf = t.plural_form;
  70. if (! /;\s*$/.test(pf)) {
  71. pf = pf.concat(';');
  72. }
  73. /* We used to use eval, but it seems IE has issues with it.
  74. * We now use "new Function", though it carries a slightly
  75. * bigger performance hit.
  76. var code = 'function (n) { var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) }; };';
  77. Gettext._locale_data[domain].head.plural_func = eval("("+code+")");
  78. */
  79. var code = 'var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) };';
  80. t.plural_function[app] = new Function("n", code);
  81. } else {
  82. console.log("Syntax error in language file. Plural-Forms header is invalid ["+t.plural_forms+"]");
  83. }
  84. }
  85. }
  86. /**
  87. * translate a string
  88. * @param {string} app the id of the app for which to translate the string
  89. * @param {string} text the string to translate
  90. * @param [vars] FIXME
  91. * @param {number} [count] number to replace %n with
  92. * @return {string}
  93. */
  94. function t(app, text, vars, count){
  95. initL10N(app);
  96. var _build = function (text, vars, count) {
  97. return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
  98. function (a, b) {
  99. var r = vars[b];
  100. return typeof r === 'string' || typeof r === 'number' ? r : a;
  101. }
  102. );
  103. };
  104. var translation = text;
  105. if( typeof( t.cache[app][text] ) !== 'undefined' ){
  106. translation = t.cache[app][text];
  107. }
  108. if(typeof vars === 'object' || count !== undefined ) {
  109. return _build(translation, vars, count);
  110. } else {
  111. return translation;
  112. }
  113. }
  114. t.cache = {};
  115. // different apps might or might not redefine the nplurals function correctly
  116. // this is to make sure that a "broken" app doesn't mess up with the
  117. // other app's plural function
  118. t.plural_function = {};
  119. /**
  120. * translate a string
  121. * @param {string} app the id of the app for which to translate the string
  122. * @param {string} text_singular the string to translate for exactly one object
  123. * @param {string} text_plural the string to translate for n objects
  124. * @param {number} count number to determine whether to use singular or plural
  125. * @param [vars] FIXME
  126. * @return {string} Translated string
  127. */
  128. function n(app, text_singular, text_plural, count, vars) {
  129. initL10N(app);
  130. var identifier = '_' + text_singular + '_::_' + text_plural + '_';
  131. if( typeof( t.cache[app][identifier] ) !== 'undefined' ){
  132. var translation = t.cache[app][identifier];
  133. if ($.isArray(translation)) {
  134. var plural = t.plural_function[app](count);
  135. return t(app, translation[plural.plural], vars, count);
  136. }
  137. }
  138. if(count === 1) {
  139. return t(app, text_singular, vars, count);
  140. }
  141. else{
  142. return t(app, text_plural, vars, count);
  143. }
  144. }
  145. /**
  146. * Sanitizes a HTML string by replacing all potential dangerous characters with HTML entities
  147. * @param {string} s String to sanitize
  148. * @return {string} Sanitized string
  149. */
  150. function escapeHTML(s) {
  151. return s.toString().split('&').join('&amp;').split('<').join('&lt;').split('>').join('&gt;').split('"').join('&quot;').split('\'').join('&#039;');
  152. }
  153. /**
  154. * Get the path to download a file
  155. * @param {string} file The filename
  156. * @param {string} dir The directory the file is in - e.g. $('#dir').val()
  157. * @return {string} Path to download the file
  158. * @deprecated use Files.getDownloadURL() instead
  159. */
  160. function fileDownloadPath(dir, file) {
  161. return OC.filePath('files', 'ajax', 'download.php')+'?files='+encodeURIComponent(file)+'&dir='+encodeURIComponent(dir);
  162. }
  163. var OC={
  164. PERMISSION_CREATE:4,
  165. PERMISSION_READ:1,
  166. PERMISSION_UPDATE:2,
  167. PERMISSION_DELETE:8,
  168. PERMISSION_SHARE:16,
  169. PERMISSION_ALL:31,
  170. /* jshint camelcase: false */
  171. webroot:oc_webroot,
  172. appswebroots:(typeof oc_appswebroots !== 'undefined') ? oc_appswebroots:false,
  173. currentUser:(typeof oc_current_user!=='undefined')?oc_current_user:false,
  174. config: window.oc_config,
  175. appConfig: window.oc_appconfig || {},
  176. theme: window.oc_defaults || {},
  177. coreApps:['', 'admin','log','search','settings','core','3rdparty'],
  178. menuSpeed: 100,
  179. /**
  180. * Get an absolute url to a file in an app
  181. * @param {string} app the id of the app the file belongs to
  182. * @param {string} file the file path relative to the app folder
  183. * @return {string} Absolute URL to a file
  184. */
  185. linkTo:function(app,file){
  186. return OC.filePath(app,'',file);
  187. },
  188. /**
  189. * Creates a relative url for remote use
  190. * @param {string} service id
  191. * @return {string} the url
  192. */
  193. linkToRemoteBase:function(service) {
  194. return OC.webroot + '/remote.php/' + service;
  195. },
  196. /**
  197. * @brief Creates an absolute url for remote use
  198. * @param {string} service id
  199. * @return {string} the url
  200. */
  201. linkToRemote:function(service) {
  202. return window.location.protocol + '//' + window.location.host + OC.linkToRemoteBase(service);
  203. },
  204. /**
  205. * Gets the base path for the given OCS API service.
  206. * @param {string} service name
  207. * @return {string} OCS API base path
  208. */
  209. linkToOCS: function(service) {
  210. return window.location.protocol + '//' + window.location.host + OC.webroot + '/ocs/v1.php/' + service + '/';
  211. },
  212. /**
  213. * Generates the absolute url for the given relative url, which can contain parameters.
  214. * @param {string} url
  215. * @param params
  216. * @return {string} Absolute URL for the given relative URL
  217. */
  218. generateUrl: function(url, params) {
  219. var _build = function (text, vars) {
  220. var vars = vars || [];
  221. return text.replace(/{([^{}]*)}/g,
  222. function (a, b) {
  223. var r = vars[b];
  224. return typeof r === 'string' || typeof r === 'number' ? r : a;
  225. }
  226. );
  227. };
  228. if (url.charAt(0) !== '/') {
  229. url = '/' + url;
  230. }
  231. // TODO save somewhere whether the webserver is able to skip the index.php to have shorter links (e.g. for sharing)
  232. return OC.webroot + '/index.php' + _build(url, params);
  233. },
  234. /**
  235. * Get the absolute url for a file in an app
  236. * @param {string} app the id of the app
  237. * @param {string} type the type of the file to link to (e.g. css,img,ajax.template)
  238. * @param {string} file the filename
  239. * @return {string} Absolute URL for a file in an app
  240. */
  241. filePath:function(app,type,file){
  242. var isCore=OC.coreApps.indexOf(app)!==-1,
  243. link=OC.webroot;
  244. if((file.substring(file.length-3) === 'php' || file.substring(file.length-3) === 'css') && !isCore){
  245. link+='/index.php/apps/' + app;
  246. if (file != 'index.php') {
  247. link+='/';
  248. if(type){
  249. link+=encodeURI(type + '/');
  250. }
  251. link+= file;
  252. }
  253. }else if(file.substring(file.length-3) !== 'php' && !isCore){
  254. link=OC.appswebroots[app];
  255. if(type){
  256. link+= '/'+type+'/';
  257. }
  258. if(link.substring(link.length-1) !== '/'){
  259. link+='/';
  260. }
  261. link+=file;
  262. }else{
  263. if ((app == 'settings' || app == 'core' || app == 'search') && type == 'ajax') {
  264. link+='/index.php/';
  265. }
  266. else {
  267. link+='/';
  268. }
  269. if(!isCore){
  270. link+='apps/';
  271. }
  272. if (app !== '') {
  273. app+='/';
  274. link+=app;
  275. }
  276. if(type){
  277. link+=type+'/';
  278. }
  279. link+=file;
  280. }
  281. return link;
  282. },
  283. /**
  284. * Redirect to the target URL, can also be used for downloads.
  285. * @param {string} targetURL URL to redirect to
  286. */
  287. redirect: function(targetURL) {
  288. window.location = targetURL;
  289. },
  290. /**
  291. * get the absolute path to an image file
  292. * if no extension is given for the image, it will automatically decide
  293. * between .png and .svg based on what the browser supports
  294. * @param {string} app the app id to which the image belongs
  295. * @param {string} file the name of the image file
  296. * @return {string}
  297. */
  298. imagePath:function(app,file){
  299. if(file.indexOf('.')==-1){//if no extension is given, use png or svg depending on browser support
  300. file+=(OC.Util.hasSVGSupport())?'.svg':'.png';
  301. }
  302. return OC.filePath(app,'img',file);
  303. },
  304. /**
  305. * Load a script for the server and load it. If the script is already loaded,
  306. * the event handler will be called directly
  307. * @param {string} app the app id to which the script belongs
  308. * @param {string} script the filename of the script
  309. * @param ready event handler to be called when the script is loaded
  310. */
  311. addScript:function(app,script,ready){
  312. var deferred, path=OC.filePath(app,'js',script+'.js');
  313. if(!OC.addScript.loaded[path]){
  314. if(ready){
  315. deferred=$.getScript(path,ready);
  316. }else{
  317. deferred=$.getScript(path);
  318. }
  319. OC.addScript.loaded[path]=deferred;
  320. }else{
  321. if(ready){
  322. ready();
  323. }
  324. }
  325. return OC.addScript.loaded[path];
  326. },
  327. /**
  328. * Loads a CSS file
  329. * @param {string} app the app id to which the css style belongs
  330. * @param {string} style the filename of the css file
  331. */
  332. addStyle:function(app,style){
  333. var path=OC.filePath(app,'css',style+'.css');
  334. if(OC.addStyle.loaded.indexOf(path)===-1){
  335. OC.addStyle.loaded.push(path);
  336. if (document.createStyleSheet) {
  337. document.createStyleSheet(path);
  338. } else {
  339. style=$('<link rel="stylesheet" type="text/css" href="'+path+'"/>');
  340. $('head').append(style);
  341. }
  342. }
  343. },
  344. /**
  345. * @todo Write the documentation
  346. */
  347. basename: function(path) {
  348. return path.replace(/\\/g,'/').replace( /.*\//, '' );
  349. },
  350. /**
  351. * @todo Write the documentation
  352. */
  353. dirname: function(path) {
  354. return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
  355. },
  356. /**
  357. * Do a search query and display the results
  358. * @param {string} query the search query
  359. */
  360. search: _.debounce(function(query){
  361. if(query){
  362. OC.addStyle('search','results');
  363. $.getJSON(OC.filePath('search','ajax','search.php')+'?query='+encodeURIComponent(query), function(results){
  364. OC.search.lastResults=results;
  365. OC.search.showResults(results);
  366. });
  367. }
  368. }, 500),
  369. dialogs:OCdialogs,
  370. /**
  371. * Parses a URL query string into a JS map
  372. * @param {string} queryString query string in the format param1=1234&param2=abcde&param3=xyz
  373. * @return map containing key/values matching the URL parameters
  374. */
  375. parseQueryString:function(queryString){
  376. var parts,
  377. pos,
  378. components,
  379. result = {},
  380. key,
  381. value;
  382. if (!queryString){
  383. return null;
  384. }
  385. pos = queryString.indexOf('?');
  386. if (pos >= 0){
  387. queryString = queryString.substr(pos + 1);
  388. }
  389. parts = queryString.replace(/\+/g, '%20').split('&');
  390. for (var i = 0; i < parts.length; i++){
  391. // split on first equal sign
  392. var part = parts[i];
  393. pos = part.indexOf('=');
  394. if (pos >= 0) {
  395. components = [
  396. part.substr(0, pos),
  397. part.substr(pos + 1)
  398. ];
  399. }
  400. else {
  401. // key only
  402. components = [part];
  403. }
  404. if (!components.length){
  405. continue;
  406. }
  407. key = decodeURIComponent(components[0]);
  408. if (!key){
  409. continue;
  410. }
  411. // if equal sign was there, return string
  412. if (components.length > 1) {
  413. result[key] = decodeURIComponent(components[1]);
  414. }
  415. // no equal sign => null value
  416. else {
  417. result[key] = null;
  418. }
  419. }
  420. return result;
  421. },
  422. /**
  423. * Builds a URL query from a JS map.
  424. * @param params parameter map
  425. * @return {string} String containing a URL query (without question) mark
  426. */
  427. buildQueryString: function(params) {
  428. if (!params) {
  429. return '';
  430. }
  431. return $.map(params, function(value, key) {
  432. var s = encodeURIComponent(key);
  433. if (value !== null && typeof(value) !== 'undefined') {
  434. s += '=' + encodeURIComponent(value);
  435. }
  436. return s;
  437. }).join('&');
  438. },
  439. /**
  440. * Opens a popup with the setting for an app.
  441. * @param {string} appid The ID of the app e.g. 'calendar', 'contacts' or 'files'.
  442. * @param {boolean|string} loadJS If true 'js/settings.js' is loaded. If it's a string
  443. * it will attempt to load a script by that name in the 'js' directory.
  444. * @param {boolean} [cache] If true the javascript file won't be forced refreshed. Defaults to true.
  445. * @param {string} [scriptName] The name of the PHP file to load. Defaults to 'settings.php' in
  446. * the root of the app directory hierarchy.
  447. */
  448. appSettings:function(args) {
  449. if(typeof args === 'undefined' || typeof args.appid === 'undefined') {
  450. throw { name: 'MissingParameter', message: 'The parameter appid is missing' };
  451. }
  452. var props = {scriptName:'settings.php', cache:true};
  453. $.extend(props, args);
  454. var settings = $('#appsettings');
  455. if(settings.length === 0) {
  456. throw { name: 'MissingDOMElement', message: 'There has be be an element with id "appsettings" for the popup to show.' };
  457. }
  458. var popup = $('#appsettings_popup');
  459. if(popup.length === 0) {
  460. $('body').prepend('<div class="popup hidden" id="appsettings_popup"></div>');
  461. popup = $('#appsettings_popup');
  462. popup.addClass(settings.hasClass('topright') ? 'topright' : 'bottomleft');
  463. }
  464. if(popup.is(':visible')) {
  465. popup.hide().remove();
  466. } else {
  467. var arrowclass = settings.hasClass('topright') ? 'up' : 'left';
  468. var jqxhr = $.get(OC.filePath(props.appid, '', props.scriptName), function(data) {
  469. popup.html(data).ready(function() {
  470. popup.prepend('<span class="arrow '+arrowclass+'"></span><h2>'+t('core', 'Settings')+'</h2><a class="close svg"></a>').show();
  471. popup.find('.close').bind('click', function() {
  472. popup.remove();
  473. });
  474. if(typeof props.loadJS !== 'undefined') {
  475. var scriptname;
  476. if(props.loadJS === true) {
  477. scriptname = 'settings.js';
  478. } else if(typeof props.loadJS === 'string') {
  479. scriptname = props.loadJS;
  480. } else {
  481. throw { name: 'InvalidParameter', message: 'The "loadJS" parameter must be either boolean or a string.' };
  482. }
  483. if(props.cache) {
  484. $.ajaxSetup({cache: true});
  485. }
  486. $.getScript(OC.filePath(props.appid, 'js', scriptname))
  487. .fail(function(jqxhr, settings, e) {
  488. throw e;
  489. });
  490. }
  491. if(!OC.Util.hasSVGSupport()) {
  492. OC.Util.replaceSVG();
  493. }
  494. }).show();
  495. }, 'html');
  496. }
  497. },
  498. /**
  499. * For menu toggling
  500. * @todo Write documentation
  501. */
  502. registerMenu: function($toggle, $menuEl) {
  503. $menuEl.addClass('menu');
  504. $toggle.on('click.menu', function(event) {
  505. if ($menuEl.is(OC._currentMenu)) {
  506. $menuEl.slideUp(OC.menuSpeed);
  507. OC._currentMenu = null;
  508. OC._currentMenuToggle = null;
  509. return false;
  510. }
  511. // another menu was open?
  512. else if (OC._currentMenu) {
  513. // close it
  514. OC._currentMenu.hide();
  515. }
  516. $menuEl.slideToggle(OC.menuSpeed);
  517. OC._currentMenu = $menuEl;
  518. OC._currentMenuToggle = $toggle;
  519. return false;
  520. });
  521. },
  522. /**
  523. * @todo Write documentation
  524. */
  525. unregisterMenu: function($toggle, $menuEl) {
  526. // close menu if opened
  527. if ($menuEl.is(OC._currentMenu)) {
  528. $menuEl.slideUp(OC.menuSpeed);
  529. OC._currentMenu = null;
  530. OC._currentMenuToggle = null;
  531. }
  532. $toggle.off('click.menu').removeClass('menutoggle');
  533. $menuEl.removeClass('menu');
  534. },
  535. /**
  536. * Wrapper for matchMedia
  537. *
  538. * This is makes it possible for unit tests to
  539. * stub matchMedia (which doesn't work in PhantomJS)
  540. * @todo Write documentation
  541. */
  542. _matchMedia: function(media) {
  543. if (window.matchMedia) {
  544. return window.matchMedia(media);
  545. }
  546. return false;
  547. }
  548. };
  549. OC.search.customResults={};
  550. OC.search.currentResult=-1;
  551. OC.search.lastQuery='';
  552. OC.search.lastResults={};
  553. //translations for result type ids, can be extended by apps
  554. OC.search.resultTypes={
  555. file: t('core','File'),
  556. folder: t('core','Folder'),
  557. image: t('core','Image'),
  558. audio: t('core','Audio')
  559. };
  560. OC.addStyle.loaded=[];
  561. OC.addScript.loaded=[];
  562. /**
  563. * @todo Write documentation
  564. */
  565. OC.msg={
  566. /**
  567. * @param selector
  568. * @todo Write documentation
  569. */
  570. startSaving:function(selector){
  571. OC.msg.startAction(selector, t('core', 'Saving...'));
  572. },
  573. /**
  574. * @param selector
  575. * @param data
  576. * @todo Write documentation
  577. */
  578. finishedSaving:function(selector, data){
  579. OC.msg.finishedAction(selector, data);
  580. },
  581. /**
  582. * @param selector
  583. * @param {string} message Message to display
  584. * @todo WRite documentation
  585. */
  586. startAction:function(selector, message){
  587. $(selector)
  588. .html( message )
  589. .removeClass('success')
  590. .removeClass('error')
  591. .stop(true, true)
  592. .show();
  593. },
  594. /**
  595. * @param selector
  596. * @param data
  597. * @todo Write documentation
  598. */
  599. finishedAction:function(selector, data){
  600. if( data.status === "success" ){
  601. $(selector).html( data.data.message )
  602. .addClass('success')
  603. .stop(true, true)
  604. .delay(3000)
  605. .fadeOut(900);
  606. }else{
  607. $(selector).html( data.data.message ).addClass('error');
  608. }
  609. }
  610. };
  611. /**
  612. * @todo Write documentation
  613. */
  614. OC.Notification={
  615. queuedNotifications: [],
  616. getDefaultNotificationFunction: null,
  617. /**
  618. * @param callback
  619. * @todo Write documentation
  620. */
  621. setDefault: function(callback) {
  622. OC.Notification.getDefaultNotificationFunction = callback;
  623. },
  624. /**
  625. * Hides a notification
  626. * @param callback
  627. * @todo Write documentation
  628. */
  629. hide: function(callback) {
  630. $('#notification').fadeOut('400', function(){
  631. if (OC.Notification.isHidden()) {
  632. if (OC.Notification.getDefaultNotificationFunction) {
  633. OC.Notification.getDefaultNotificationFunction.call();
  634. }
  635. }
  636. if (callback) {
  637. callback.call();
  638. }
  639. $('#notification').empty();
  640. if(OC.Notification.queuedNotifications.length > 0){
  641. OC.Notification.showHtml(OC.Notification.queuedNotifications[0]);
  642. OC.Notification.queuedNotifications.shift();
  643. }
  644. });
  645. },
  646. /**
  647. * Shows a notification as HTML without being sanitized before.
  648. * If you pass unsanitized user input this may lead to a XSS vulnerability.
  649. * Consider using show() instead of showHTML()
  650. * @param {string} html Message to display
  651. */
  652. showHtml: function(html) {
  653. var notification = $('#notification');
  654. if((notification.filter('span.undo').length == 1) || OC.Notification.isHidden()){
  655. notification.html(html);
  656. notification.fadeIn().css('display','inline-block');
  657. }else{
  658. OC.Notification.queuedNotifications.push(html);
  659. }
  660. },
  661. /**
  662. * Shows a sanitized notification
  663. * @param {string} text Message to display
  664. */
  665. show: function(text) {
  666. var notification = $('#notification');
  667. if((notification.filter('span.undo').length == 1) || OC.Notification.isHidden()){
  668. notification.text(text);
  669. notification.fadeIn().css('display','inline-block');
  670. }else{
  671. OC.Notification.queuedNotifications.push($('<div/>').text(text).html());
  672. }
  673. },
  674. /**
  675. * Returns whether a notification is hidden.
  676. * @return {boolean}
  677. */
  678. isHidden: function() {
  679. return ($("#notification").text() === '');
  680. }
  681. };
  682. /**
  683. * @todo Write documentation
  684. */
  685. OC.Breadcrumb={
  686. container:null,
  687. /**
  688. * @todo Write documentation
  689. * @param dir
  690. * @param leafName
  691. * @param leafLink
  692. */
  693. show:function(dir, leafName, leafLink){
  694. if(!this.container){//default
  695. this.container=$('#controls');
  696. }
  697. this._show(this.container, dir, leafName, leafLink);
  698. },
  699. _show:function(container, dir, leafname, leaflink){
  700. var self = this;
  701. this._clear(container);
  702. // show home + path in subdirectories
  703. if (dir) {
  704. //add home
  705. var link = OC.linkTo('files','index.php');
  706. var crumb=$('<div/>');
  707. crumb.addClass('crumb');
  708. var crumbLink=$('<a/>');
  709. crumbLink.attr('href',link);
  710. var crumbImg=$('<img/>');
  711. crumbImg.attr('src',OC.imagePath('core','places/home'));
  712. crumbLink.append(crumbImg);
  713. crumb.append(crumbLink);
  714. container.prepend(crumb);
  715. //add path parts
  716. var segments = dir.split('/');
  717. var pathurl = '';
  718. jQuery.each(segments, function(i,name) {
  719. if (name !== '') {
  720. pathurl = pathurl+'/'+name;
  721. var link = OC.linkTo('files','index.php')+'?dir='+encodeURIComponent(pathurl);
  722. self._push(container, name, link);
  723. }
  724. });
  725. }
  726. //add leafname
  727. if (leafname && leaflink) {
  728. this._push(container, leafname, leaflink);
  729. }
  730. },
  731. /**
  732. * @todo Write documentation
  733. * @param {string} name
  734. * @param {string} link
  735. */
  736. push:function(name, link){
  737. if(!this.container){//default
  738. this.container=$('#controls');
  739. }
  740. return this._push(OC.Breadcrumb.container, name, link);
  741. },
  742. _push:function(container, name, link){
  743. var crumb=$('<div/>');
  744. crumb.addClass('crumb').addClass('last');
  745. var crumbLink=$('<a/>');
  746. crumbLink.attr('href',link);
  747. crumbLink.text(name);
  748. crumb.append(crumbLink);
  749. var existing=container.find('div.crumb');
  750. if(existing.length){
  751. existing.removeClass('last');
  752. existing.last().after(crumb);
  753. }else{
  754. container.prepend(crumb);
  755. }
  756. return crumb;
  757. },
  758. /**
  759. * @todo Write documentation
  760. */
  761. pop:function(){
  762. if(!this.container){//default
  763. this.container=$('#controls');
  764. }
  765. this.container.find('div.crumb').last().remove();
  766. this.container.find('div.crumb').last().addClass('last');
  767. },
  768. /**
  769. * @todo Write documentation
  770. */
  771. clear:function(){
  772. if(!this.container){//default
  773. this.container=$('#controls');
  774. }
  775. this._clear(this.container);
  776. },
  777. _clear:function(container) {
  778. container.find('div.crumb').remove();
  779. }
  780. };
  781. if(typeof localStorage !=='undefined' && localStorage !== null){
  782. /**
  783. * User and instance aware localstorage
  784. */
  785. OC.localStorage={
  786. namespace:'oc_'+OC.currentUser+'_'+OC.webroot+'_',
  787. /**
  788. * Whether the storage contains items
  789. * @param {string} name
  790. * @return {boolean}
  791. */
  792. hasItem:function(name){
  793. return OC.localStorage.getItem(name)!==null;
  794. },
  795. /**
  796. * Add an item to the storage
  797. * @param {string} name
  798. * @param {string} item
  799. */
  800. setItem:function(name,item){
  801. return localStorage.setItem(OC.localStorage.namespace+name,JSON.stringify(item));
  802. },
  803. /**
  804. * Removes an item from the storage
  805. * @param {string} name
  806. * @param {string} item
  807. */
  808. removeItem:function(name,item){
  809. return localStorage.removeItem(OC.localStorage.namespace+name);
  810. },
  811. /**
  812. * Get an item from the storage
  813. * @param {string} name
  814. * @return {null|string}
  815. */
  816. getItem:function(name){
  817. var item = localStorage.getItem(OC.localStorage.namespace+name);
  818. if(item === null) {
  819. return null;
  820. } else if (typeof JSON === 'undefined') {
  821. //fallback to jquery for IE6/7/8
  822. return $.parseJSON(item);
  823. } else {
  824. return JSON.parse(item);
  825. }
  826. }
  827. };
  828. }else{
  829. //dummy localstorage
  830. OC.localStorage={
  831. hasItem:function(){
  832. return false;
  833. },
  834. setItem:function(){
  835. return false;
  836. },
  837. getItem:function(){
  838. return null;
  839. }
  840. };
  841. }
  842. /**
  843. * check if the browser support svg images
  844. * @return {boolean}
  845. */
  846. function SVGSupport() {
  847. return SVGSupport.checkMimeType.correct && !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', "svg").createSVGRect;
  848. }
  849. SVGSupport.checkMimeType=function(){
  850. $.ajax({
  851. url: OC.imagePath('core','breadcrumb.svg'),
  852. success:function(data,text,xhr){
  853. var headerParts=xhr.getAllResponseHeaders().split("\n");
  854. var headers={};
  855. $.each(headerParts,function(i,text){
  856. if(text){
  857. var parts=text.split(':',2);
  858. if(parts.length===2){
  859. var value=parts[1].trim();
  860. if(value[0]==='"'){
  861. value=value.substr(1,value.length-2);
  862. }
  863. headers[parts[0].toLowerCase()]=value;
  864. }
  865. }
  866. });
  867. if(headers["content-type"]!=='image/svg+xml'){
  868. OC.Util.replaceSVG();
  869. SVGSupport.checkMimeType.correct=false;
  870. }
  871. }
  872. });
  873. };
  874. SVGSupport.checkMimeType.correct=true;
  875. /**
  876. * Replace all svg images with png for browser compatibility
  877. * @param $el
  878. * @deprecated use OC.Util.replaceSVG instead
  879. */
  880. function replaceSVG($el){
  881. return OC.Util.replaceSVG($el);
  882. }
  883. /**
  884. * prototypical inheritance functions
  885. * @todo Write documentation
  886. * usage:
  887. * MySubObject=object(MyObject)
  888. */
  889. function object(o) {
  890. function F() {}
  891. F.prototype = o;
  892. return new F();
  893. }
  894. /**
  895. * Initializes core
  896. */
  897. function initCore() {
  898. /**
  899. * Calls the server periodically to ensure that session doesn't
  900. * time out
  901. */
  902. function initSessionHeartBeat(){
  903. // max interval in seconds set to 24 hours
  904. var maxInterval = 24 * 3600;
  905. // interval in seconds
  906. var interval = 900;
  907. if (oc_config.session_lifetime) {
  908. interval = Math.floor(oc_config.session_lifetime / 2);
  909. }
  910. // minimum one minute
  911. if (interval < 60) {
  912. interval = 60;
  913. }
  914. if (interval > maxInterval) {
  915. interval = maxInterval;
  916. }
  917. var url = OC.generateUrl('/heartbeat');
  918. setInterval(function(){
  919. $.post(url);
  920. }, interval * 1000);
  921. }
  922. // session heartbeat (defaults to enabled)
  923. if (typeof(oc_config.session_keepalive) === 'undefined' ||
  924. !!oc_config.session_keepalive) {
  925. initSessionHeartBeat();
  926. }
  927. if(!OC.Util.hasSVGSupport()){ //replace all svg images with png images for browser that dont support svg
  928. OC.Util.replaceSVG();
  929. }else{
  930. SVGSupport.checkMimeType();
  931. }
  932. $('form.searchbox').submit(function(event){
  933. event.preventDefault();
  934. });
  935. $('#searchbox').keyup(function(event){
  936. if(event.keyCode===13){//enter
  937. if(OC.search.currentResult>-1){
  938. var result=$('#searchresults tr.result a')[OC.search.currentResult];
  939. window.location = $(result).attr('href');
  940. }
  941. }else if(event.keyCode===38){//up
  942. if(OC.search.currentResult>0){
  943. OC.search.currentResult--;
  944. OC.search.renderCurrent();
  945. }
  946. }else if(event.keyCode===40){//down
  947. if(OC.search.lastResults.length>OC.search.currentResult+1){
  948. OC.search.currentResult++;
  949. OC.search.renderCurrent();
  950. }
  951. }else if(event.keyCode===27){//esc
  952. OC.search.hide();
  953. if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
  954. FileList.unfilter();
  955. }
  956. }else{
  957. var query=$('#searchbox').val();
  958. if(OC.search.lastQuery!==query){
  959. OC.search.lastQuery=query;
  960. OC.search.currentResult=-1;
  961. if (FileList && typeof FileList.filter === 'function') { //TODO add hook system
  962. FileList.filter(query);
  963. }
  964. if(query.length>2){
  965. OC.search(query);
  966. }else{
  967. if(OC.search.hide){
  968. OC.search.hide();
  969. }
  970. }
  971. }
  972. }
  973. });
  974. var setShowPassword = function(input, label) {
  975. input.showPassword().keyup();
  976. };
  977. setShowPassword($('#adminpass'), $('label[for=show]'));
  978. setShowPassword($('#pass2'), $('label[for=personal-show]'));
  979. setShowPassword($('#dbpass'), $('label[for=dbpassword]'));
  980. var checkShowCredentials = function() {
  981. var empty = false;
  982. $('input#user, input#password').each(function() {
  983. if ($(this).val() === '') {
  984. empty = true;
  985. }
  986. });
  987. if(empty) {
  988. $('#submit').fadeOut();
  989. $('#remember_login').hide();
  990. $('#remember_login+label').fadeOut();
  991. } else {
  992. $('#submit').fadeIn();
  993. $('#remember_login').show();
  994. $('#remember_login+label').fadeIn();
  995. }
  996. };
  997. // hide log in button etc. when form fields not filled
  998. // commented out due to some browsers having issues with it
  999. // checkShowCredentials();
  1000. // $('input#user, input#password').keyup(checkShowCredentials);
  1001. // user menu
  1002. $('#settings #expand').keydown(function(event) {
  1003. if (event.which === 13 || event.which === 32) {
  1004. $('#expand').click();
  1005. }
  1006. });
  1007. $('#settings #expand').click(function(event) {
  1008. $('#settings #expanddiv').slideToggle(OC.menuSpeed);
  1009. event.stopPropagation();
  1010. });
  1011. $('#settings #expanddiv').click(function(event){
  1012. event.stopPropagation();
  1013. });
  1014. //hide the user menu when clicking outside it
  1015. $(document).click(function(){
  1016. $('#settings #expanddiv').slideUp(OC.menuSpeed);
  1017. });
  1018. // all the tipsy stuff needs to be here (in reverse order) to work
  1019. $('.displayName .action').tipsy({gravity:'se', fade:true, live:true});
  1020. $('.password .action').tipsy({gravity:'se', fade:true, live:true});
  1021. $('#upload').tipsy({gravity:'w', fade:true});
  1022. $('.selectedActions a').tipsy({gravity:'s', fade:true, live:true});
  1023. $('a.action.delete').tipsy({gravity:'e', fade:true, live:true});
  1024. $('a.action').tipsy({gravity:'s', fade:true, live:true});
  1025. $('td .modified').tipsy({gravity:'s', fade:true, live:true});
  1026. $('td.lastLogin').tipsy({gravity:'s', fade:true, html:true});
  1027. $('input').tipsy({gravity:'w', fade:true});
  1028. // toggle for menus
  1029. $(document).on('mouseup.closemenus', function(event) {
  1030. var $el = $(event.target);
  1031. if ($el.closest('.menu').length || $el.closest('.menutoggle').length) {
  1032. // don't close when clicking on the menu directly or a menu toggle
  1033. return false;
  1034. }
  1035. if (OC._currentMenu) {
  1036. OC._currentMenu.slideUp(OC.menuSpeed);
  1037. }
  1038. OC._currentMenu = null;
  1039. OC._currentMenuToggle = null;
  1040. });
  1041. /**
  1042. * Set up the main menu toggle to react to media query changes.
  1043. * If the screen is small enough, the main menu becomes a toggle.
  1044. * If the screen is bigger, the main menu is not a toggle any more.
  1045. */
  1046. function setupMainMenu() {
  1047. // toggle the navigation
  1048. var $toggle = $('#header .menutoggle');
  1049. var $navigation = $('#navigation');
  1050. // init the menu
  1051. OC.registerMenu($toggle, $navigation);
  1052. $toggle.data('oldhref', $toggle.attr('href'));
  1053. $toggle.attr('href', '#');
  1054. $navigation.hide();
  1055. // show loading feedback
  1056. $navigation.delegate('a', 'click', function(event) {
  1057. var $app = $(event.target);
  1058. if(!$app.is('a')) {
  1059. $app = $app.closest('a');
  1060. }
  1061. if(!event.ctrlKey) {
  1062. $app.addClass('app-loading');
  1063. }
  1064. });
  1065. }
  1066. setupMainMenu();
  1067. // just add snapper for logged in users
  1068. if($('#app-navigation').length && !$('html').hasClass('lte9')) {
  1069. // App sidebar on mobile
  1070. var snapper = new Snap({
  1071. element: document.getElementById('app-content'),
  1072. disable: 'right',
  1073. maxPosition: 250
  1074. });
  1075. $('#app-content').prepend('<div id="app-navigation-toggle" class="icon-menu" style="display:none;"></div>');
  1076. $('#app-navigation-toggle').click(function(){
  1077. if(snapper.state().state == 'left'){
  1078. snapper.close();
  1079. } else {
  1080. snapper.open('left');
  1081. }
  1082. });
  1083. // close sidebar when switching navigation entry
  1084. var $appNavigation = $('#app-navigation');
  1085. $appNavigation.delegate('a', 'click', function(event) {
  1086. var $target = $(event.target);
  1087. // don't hide navigation when changing settings or adding things
  1088. if($target.is('.app-navigation-noclose') ||
  1089. $target.closest('.app-navigation-noclose').length) {
  1090. return;
  1091. }
  1092. if($target.is('.add-new') ||
  1093. $target.closest('.add-new').length) {
  1094. return;
  1095. }
  1096. if($target.is('#app-settings') ||
  1097. $target.closest('#app-settings').length) {
  1098. return;
  1099. }
  1100. snapper.close();
  1101. });
  1102. var toggleSnapperOnSize = function() {
  1103. if($(window).width() > 768) {
  1104. snapper.close();
  1105. snapper.disable();
  1106. } else {
  1107. snapper.enable();
  1108. }
  1109. };
  1110. $(window).resize(_.debounce(toggleSnapperOnSize, 250));
  1111. // initial call
  1112. toggleSnapperOnSize();
  1113. }
  1114. }
  1115. $(document).ready(initCore);
  1116. /**
  1117. * Filter Jquery selector by attribute value
  1118. */
  1119. $.fn.filterAttr = function(attr_name, attr_value) {
  1120. return this.filter(function() { return $(this).attr(attr_name) === attr_value; });
  1121. };
  1122. /**
  1123. * Returns a human readable file size
  1124. * @param {number} size Size in bytes
  1125. * @param {boolean} skipSmallSizes return '< 1 kB' for small files
  1126. * @return {string}
  1127. */
  1128. function humanFileSize(size, skipSmallSizes) {
  1129. var humanList = ['B', 'kB', 'MB', 'GB', 'TB'];
  1130. // Calculate Log with base 1024: size = 1024 ** order
  1131. var order = size?Math.floor(Math.log(size) / Math.log(1024)):0;
  1132. // Stay in range of the byte sizes that are defined
  1133. order = Math.min(humanList.length - 1, order);
  1134. var readableFormat = humanList[order];
  1135. var relativeSize = (size / Math.pow(1024, order)).toFixed(1);
  1136. if(skipSmallSizes === true && order === 0) {
  1137. if(relativeSize !== "0.0"){
  1138. return '< 1 kB';
  1139. } else {
  1140. return '0 kB';
  1141. }
  1142. }
  1143. if(order < 2){
  1144. relativeSize = parseFloat(relativeSize).toFixed(0);
  1145. }
  1146. else if(relativeSize.substr(relativeSize.length-2,2)==='.0'){
  1147. relativeSize=relativeSize.substr(0,relativeSize.length-2);
  1148. }
  1149. return relativeSize + ' ' + readableFormat;
  1150. }
  1151. /**
  1152. * Format an UNIX timestamp to a human understandable format
  1153. * @param {number} timestamp UNIX timestamp
  1154. * @return {string} Human readable format
  1155. */
  1156. function formatDate(timestamp){
  1157. return OC.Util.formatDate(timestamp);
  1158. }
  1159. //
  1160. /**
  1161. * Get the value of a URL parameter
  1162. * @link http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery
  1163. * @param {string} name URL parameter
  1164. * @return {string}
  1165. */
  1166. function getURLParameter(name) {
  1167. return decodeURI(
  1168. (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
  1169. );
  1170. }
  1171. /**
  1172. * Takes an absolute timestamp and return a string with a human-friendly relative date
  1173. * @param {number} timestamp A Unix timestamp
  1174. */
  1175. function relative_modified_date(timestamp) {
  1176. /*
  1177. Were multiplying by 1000 to bring the timestamp back to its original value
  1178. per https://github.com/owncloud/core/pull/10647#discussion_r16790315
  1179. */
  1180. return OC.Util.relativeModifiedDate(timestamp * 1000);
  1181. }
  1182. /**
  1183. * Utility functions
  1184. */
  1185. OC.Util = {
  1186. // TODO: remove original functions from global namespace
  1187. humanFileSize: humanFileSize,
  1188. /**
  1189. * @param timestamp
  1190. * @param format
  1191. * @returns {string} timestamp formatted as requested
  1192. */
  1193. formatDate: function (timestamp, format) {
  1194. format = format || "MMMM D, YYYY h:mm";
  1195. return moment(timestamp).format(format);
  1196. },
  1197. /**
  1198. * @param timestamp
  1199. * @returns {string} human readable difference from now
  1200. */
  1201. relativeModifiedDate: function (timestamp) {
  1202. return moment(timestamp).fromNow();
  1203. },
  1204. /**
  1205. * Returns whether the browser supports SVG
  1206. * @return {boolean} true if the browser supports SVG, false otherwise
  1207. */
  1208. // TODO: replace with original function
  1209. hasSVGSupport: SVGSupport,
  1210. /**
  1211. * If SVG is not supported, replaces the given icon's extension
  1212. * from ".svg" to ".png".
  1213. * If SVG is supported, return the image path as is.
  1214. * @param {string} file image path with svg extension
  1215. * @return {string} fixed image path with png extension if SVG is not supported
  1216. */
  1217. replaceSVGIcon: function(file) {
  1218. if (file && !OC.Util.hasSVGSupport()) {
  1219. var i = file.lastIndexOf('.svg');
  1220. if (i >= 0) {
  1221. file = file.substr(0, i) + '.png' + file.substr(i+4);
  1222. }
  1223. }
  1224. return file;
  1225. },
  1226. /**
  1227. * Replace SVG images in all elements that have the "svg" class set
  1228. * with PNG images.
  1229. *
  1230. * @param $el root element from which to search, defaults to $('body')
  1231. */
  1232. replaceSVG: function($el) {
  1233. if (!$el) {
  1234. $el = $('body');
  1235. }
  1236. $el.find('img.svg').each(function(index,element){
  1237. element=$(element);
  1238. var src=element.attr('src');
  1239. element.attr('src',src.substr(0, src.length-3) + 'png');
  1240. });
  1241. $el.find('.svg').each(function(index,element){
  1242. element = $(element);
  1243. var background = element.css('background-image');
  1244. if (background){
  1245. var i = background.lastIndexOf('.svg');
  1246. if (i >= 0){
  1247. background = background.substr(0,i) + '.png' + background.substr(i + 4);
  1248. element.css('background-image', background);
  1249. }
  1250. }
  1251. element.find('*').each(function(index, element) {
  1252. element = $(element);
  1253. var background = element.css('background-image');
  1254. if (background) {
  1255. var i = background.lastIndexOf('.svg');
  1256. if(i >= 0){
  1257. background = background.substr(0,i) + '.png' + background.substr(i + 4);
  1258. element.css('background-image', background);
  1259. }
  1260. }
  1261. });
  1262. });
  1263. },
  1264. /**
  1265. * Remove the time component from a given date
  1266. *
  1267. * @param {Date} date date
  1268. * @return {Date} date with stripped time
  1269. */
  1270. stripTime: function(date) {
  1271. // FIXME: likely to break when crossing DST
  1272. // would be better to use a library like momentJS
  1273. return new Date(date.getFullYear(), date.getMonth(), date.getDate());
  1274. }
  1275. };
  1276. /**
  1277. * Utility class for the history API,
  1278. * includes fallback to using the URL hash when
  1279. * the browser doesn't support the history API.
  1280. */
  1281. OC.Util.History = {
  1282. _handlers: [],
  1283. /**
  1284. * Push the current URL parameters to the history stack
  1285. * and change the visible URL.
  1286. * Note: this includes a workaround for IE8/IE9 that uses
  1287. * the hash part instead of the search part.
  1288. *
  1289. * @param params to append to the URL, can be either a string
  1290. * or a map
  1291. */
  1292. pushState: function(params) {
  1293. var strParams;
  1294. if (typeof(params) === 'string') {
  1295. strParams = params;
  1296. }
  1297. else {
  1298. strParams = OC.buildQueryString(params);
  1299. }
  1300. if (window.history.pushState) {
  1301. var url = location.pathname + '?' + strParams;
  1302. window.history.pushState(params, '', url);
  1303. }
  1304. // use URL hash for IE8
  1305. else {
  1306. window.location.hash = '?' + strParams;
  1307. // inhibit next onhashchange that just added itself
  1308. // to the event queue
  1309. this._cancelPop = true;
  1310. }
  1311. },
  1312. /**
  1313. * Add a popstate handler
  1314. *
  1315. * @param handler function
  1316. */
  1317. addOnPopStateHandler: function(handler) {
  1318. this._handlers.push(handler);
  1319. },
  1320. /**
  1321. * Parse a query string from the hash part of the URL.
  1322. * (workaround for IE8 / IE9)
  1323. */
  1324. _parseHashQuery: function() {
  1325. var hash = window.location.hash,
  1326. pos = hash.indexOf('?');
  1327. if (pos >= 0) {
  1328. return hash.substr(pos + 1);
  1329. }
  1330. if (hash.length) {
  1331. // remove hash sign
  1332. return hash.substr(1);
  1333. }
  1334. return '';
  1335. },
  1336. _decodeQuery: function(query) {
  1337. return query.replace(/\+/g, ' ');
  1338. },
  1339. /**
  1340. * Parse the query/search part of the URL.
  1341. * Also try and parse it from the URL hash (for IE8)
  1342. *
  1343. * @return map of parameters
  1344. */
  1345. parseUrlQuery: function() {
  1346. var query = this._parseHashQuery(),
  1347. params;
  1348. // try and parse from URL hash first
  1349. if (query) {
  1350. params = OC.parseQueryString(this._decodeQuery(query));
  1351. }
  1352. // else read from query attributes
  1353. if (!params) {
  1354. params = OC.parseQueryString(this._decodeQuery(location.search));
  1355. }
  1356. return params || {};
  1357. },
  1358. _onPopState: function(e) {
  1359. if (this._cancelPop) {
  1360. this._cancelPop = false;
  1361. return;
  1362. }
  1363. var params;
  1364. if (!this._handlers.length) {
  1365. return;
  1366. }
  1367. params = (e && e.state) || this.parseUrlQuery() || {};
  1368. for (var i = 0; i < this._handlers.length; i++) {
  1369. this._handlers[i](params);
  1370. }
  1371. }
  1372. };
  1373. // fallback to hashchange when no history support
  1374. if (window.history.pushState) {
  1375. window.onpopstate = _.bind(OC.Util.History._onPopState, OC.Util.History);
  1376. }
  1377. else {
  1378. $(window).on('hashchange', _.bind(OC.Util.History._onPopState, OC.Util.History));
  1379. }
  1380. /**
  1381. * Get a variable by name
  1382. * @param {string} name
  1383. * @return {*}
  1384. */
  1385. OC.get=function(name) {
  1386. var namespaces = name.split(".");
  1387. var tail = namespaces.pop();
  1388. var context=window;
  1389. for(var i = 0; i < namespaces.length; i++) {
  1390. context = context[namespaces[i]];
  1391. if(!context){
  1392. return false;
  1393. }
  1394. }
  1395. return context[tail];
  1396. };
  1397. /**
  1398. * Set a variable by name
  1399. * @param {string} name
  1400. * @param {*} value
  1401. */
  1402. OC.set=function(name, value) {
  1403. var namespaces = name.split(".");
  1404. var tail = namespaces.pop();
  1405. var context=window;
  1406. for(var i = 0; i < namespaces.length; i++) {
  1407. if(!context[namespaces[i]]){
  1408. context[namespaces[i]]={};
  1409. }
  1410. context = context[namespaces[i]];
  1411. }
  1412. context[tail]=value;
  1413. };
  1414. // fix device width on windows phone
  1415. (function() {
  1416. if ("-ms-user-select" in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/)) {
  1417. var msViewportStyle = document.createElement("style");
  1418. msViewportStyle.appendChild(
  1419. document.createTextNode("@-ms-viewport{width:auto!important}")
  1420. );
  1421. document.getElementsByTagName("head")[0].appendChild(msViewportStyle);
  1422. }
  1423. })();
  1424. /**
  1425. * Namespace for apps
  1426. */
  1427. window.OCA = {};
  1428. /**
  1429. * select a range in an input field
  1430. * @link http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
  1431. * @param {type} start
  1432. * @param {type} end
  1433. */
  1434. jQuery.fn.selectRange = function(start, end) {
  1435. return this.each(function() {
  1436. if (this.setSelectionRange) {
  1437. this.focus();
  1438. this.setSelectionRange(start, end);
  1439. } else if (this.createTextRange) {
  1440. var range = this.createTextRange();
  1441. range.collapse(true);
  1442. range.moveEnd('character', end);
  1443. range.moveStart('character', start);
  1444. range.select();
  1445. }
  1446. });
  1447. };
  1448. /**
  1449. * check if an element exists.
  1450. * allows you to write if ($('#myid').exists()) to increase readability
  1451. * @link http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery
  1452. */
  1453. jQuery.fn.exists = function(){
  1454. return this.length > 0;
  1455. };