functions.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. var notify_silent = false;
  2. var loading_progress = 0;
  3. var sanity_check_done = false;
  4. /* add method to remove element from array */
  5. Array.prototype.remove = function(s) {
  6. for (var i=0; i < this.length; i++) {
  7. if (s == this[i]) this.splice(i, 1);
  8. }
  9. };
  10. /* create console.log if it doesn't exist */
  11. if (!window.console) console = {};
  12. console.log = console.log || function(msg) { };
  13. console.warn = console.warn || function(msg) { };
  14. console.error = console.error || function(msg) { };
  15. function exception_error(location, e, ext_info) {
  16. var msg = format_exception_error(location, e);
  17. if (!ext_info) ext_info = false;
  18. try {
  19. if (ext_info) {
  20. if (ext_info.responseText) {
  21. ext_info = ext_info.responseText;
  22. }
  23. }
  24. var content = "<div class=\"fatalError\">" +
  25. "<pre>" + msg + "</pre>";
  26. content += "<form name=\"exceptionForm\" id=\"exceptionForm\" target=\"_blank\" "+
  27. "action=\"http://tt-rss.org/report.php\" method=\"POST\">";
  28. content += "<textarea style=\"display : none\" name=\"message\">" + msg + "</textarea>";
  29. content += "<textarea style=\"display : none\" name=\"params\">N/A</textarea>";
  30. if (ext_info) {
  31. content += "<div><b>Additional information:</b></div>" +
  32. "<textarea name=\"xinfo\" readonly=\"1\">" + ext_info + "</textarea>";
  33. }
  34. content += "<div><b>Stack trace:</b></div>" +
  35. "<textarea name=\"stack\" readonly=\"1\">" + e.stack + "</textarea>";
  36. content += "</form>";
  37. content += "</div>";
  38. content += "<div class='dlgButtons'>";
  39. content += "<button dojoType=\"dijit.form.Button\""+
  40. "onclick=\"dijit.byId('exceptionDlg').report()\">" +
  41. __('Report to tt-rss.org') + "</button> ";
  42. content += "<button dojoType=\"dijit.form.Button\" "+
  43. "onclick=\"dijit.byId('exceptionDlg').hide()\">" +
  44. __('Close') + "</button>";
  45. content += "</div>";
  46. if (dijit.byId("exceptionDlg"))
  47. dijit.byId("exceptionDlg").destroyRecursive();
  48. var dialog = new dijit.Dialog({
  49. id: "exceptionDlg",
  50. title: "Unhandled exception",
  51. style: "width: 600px",
  52. report: function() {
  53. if (confirm(__("Are you sure to report this exception to tt-rss.org? The report will include your browser information. Your IP would be saved in the database."))) {
  54. document.forms['exceptionForm'].params.value = $H({
  55. browserName: navigator.appName,
  56. browserVersion: navigator.appVersion,
  57. browserPlatform: navigator.platform,
  58. browserCookies: navigator.cookieEnabled,
  59. }).toQueryString();
  60. document.forms['exceptionForm'].submit();
  61. }
  62. },
  63. content: content});
  64. dialog.show();
  65. } catch (e) {
  66. alert(msg);
  67. }
  68. }
  69. function format_exception_error(location, e) {
  70. var msg;
  71. if (e.fileName) {
  72. var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
  73. msg = "Exception: " + e.name + ", " + e.message +
  74. "\nFunction: " + location + "()" +
  75. "\nLocation: " + base_fname + ":" + e.lineNumber;
  76. } else if (e.description) {
  77. msg = "Exception: " + e.description + "\nFunction: " + location + "()";
  78. } else {
  79. msg = "Exception: " + e + "\nFunction: " + location + "()";
  80. }
  81. console.error("EXCEPTION: " + msg);
  82. return msg;
  83. }
  84. function param_escape(arg) {
  85. if (typeof encodeURIComponent != 'undefined')
  86. return encodeURIComponent(arg);
  87. else
  88. return escape(arg);
  89. }
  90. function param_unescape(arg) {
  91. if (typeof decodeURIComponent != 'undefined')
  92. return decodeURIComponent(arg);
  93. else
  94. return unescape(arg);
  95. }
  96. var notify_hide_timerid = false;
  97. function hide_notify() {
  98. var n = $("notify");
  99. if (n) {
  100. n.style.display = "none";
  101. }
  102. }
  103. function notify_silent_next() {
  104. notify_silent = true;
  105. }
  106. function notify_real(msg, no_hide, n_type) {
  107. if (notify_silent) {
  108. notify_silent = false;
  109. return;
  110. }
  111. var n = $("notify");
  112. var nb = $("notify_body");
  113. if (!n || !nb) return;
  114. if (notify_hide_timerid) {
  115. window.clearTimeout(notify_hide_timerid);
  116. }
  117. if (msg == "") {
  118. if (n.style.display == "block") {
  119. notify_hide_timerid = window.setTimeout("hide_notify()", 0);
  120. }
  121. return;
  122. } else {
  123. n.style.display = "block";
  124. }
  125. /* types:
  126. 1 - generic
  127. 2 - progress
  128. 3 - error
  129. 4 - info
  130. */
  131. if (typeof __ != 'undefined') {
  132. msg = __(msg);
  133. }
  134. if (n_type == 1) {
  135. n.className = "notify";
  136. } else if (n_type == 2) {
  137. n.className = "notifyProgress";
  138. msg = "<img src='"+getInitParam("sign_progress")+"'> " + msg;
  139. } else if (n_type == 3) {
  140. n.className = "notifyError";
  141. msg = "<img src='"+getInitParam("sign_excl")+"'> " + msg;
  142. } else if (n_type == 4) {
  143. n.className = "notifyInfo";
  144. msg = "<img src='"+getInitParam("sign_info")+"'> " + msg;
  145. }
  146. // msg = "<img src='images/live_com_loading.gif'> " + msg;
  147. nb.innerHTML = msg;
  148. if (!no_hide) {
  149. notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
  150. }
  151. }
  152. function notify(msg, no_hide) {
  153. notify_real(msg, no_hide, 1);
  154. }
  155. function notify_progress(msg, no_hide) {
  156. notify_real(msg, no_hide, 2);
  157. }
  158. function notify_error(msg, no_hide) {
  159. notify_real(msg, no_hide, 3);
  160. }
  161. function notify_info(msg, no_hide) {
  162. notify_real(msg, no_hide, 4);
  163. }
  164. function setCookie(name, value, lifetime, path, domain, secure) {
  165. var d = false;
  166. if (lifetime) {
  167. d = new Date();
  168. d.setTime(d.getTime() + (lifetime * 1000));
  169. }
  170. console.log("setCookie: " + name + " => " + value + ": " + d);
  171. int_setCookie(name, value, d, path, domain, secure);
  172. }
  173. function int_setCookie(name, value, expires, path, domain, secure) {
  174. document.cookie= name + "=" + escape(value) +
  175. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  176. ((path) ? "; path=" + path : "") +
  177. ((domain) ? "; domain=" + domain : "") +
  178. ((secure) ? "; secure" : "");
  179. }
  180. function delCookie(name, path, domain) {
  181. if (getCookie(name)) {
  182. document.cookie = name + "=" +
  183. ((path) ? ";path=" + path : "") +
  184. ((domain) ? ";domain=" + domain : "" ) +
  185. ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  186. }
  187. }
  188. function getCookie(name) {
  189. var dc = document.cookie;
  190. var prefix = name + "=";
  191. var begin = dc.indexOf("; " + prefix);
  192. if (begin == -1) {
  193. begin = dc.indexOf(prefix);
  194. if (begin != 0) return null;
  195. }
  196. else {
  197. begin += 2;
  198. }
  199. var end = document.cookie.indexOf(";", begin);
  200. if (end == -1) {
  201. end = dc.length;
  202. }
  203. return unescape(dc.substring(begin + prefix.length, end));
  204. }
  205. function gotoPreferences() {
  206. document.location.href = "prefs.php";
  207. }
  208. function gotoMain() {
  209. document.location.href = "tt-rss.php";
  210. }
  211. function gotoExportOpml(filename, settings) {
  212. tmp = settings ? 1 : 0;
  213. document.location.href = "opml.php?op=Export&filename=" + filename + "&settings=" + tmp;
  214. }
  215. /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
  216. * * @author Sundar Dorai-Raj
  217. * * Email: sdoraira@vt.edu
  218. * * This program is free software; you can redistribute it and/or
  219. * * modify it under the terms of the GNU General Public License
  220. * * as published by the Free Software Foundation; either version 2
  221. * * of the License, or (at your option) any later version,
  222. * * provided that any use properly credits the author.
  223. * * This program is distributed in the hope that it will be useful,
  224. * * but WITHOUT ANY WARRANTY; without even the implied warranty of
  225. * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  226. * * GNU General Public License for more details at http://www.gnu.org * * */
  227. var numbers=".0123456789";
  228. function isNumeric(x) {
  229. // is x a String or a character?
  230. if(x.length>1) {
  231. // remove negative sign
  232. x=Math.abs(x)+"";
  233. for(var j=0;j<x.length;j++) {
  234. // call isNumeric recursively for each character
  235. number=isNumeric(x.substring(j,j+1));
  236. if(!number) return number;
  237. }
  238. return number;
  239. }
  240. else {
  241. // if x is number return true
  242. if(numbers.indexOf(x)>=0) return true;
  243. return false;
  244. }
  245. }
  246. function toggleSelectRowById(sender, id) {
  247. var row = $(id);
  248. return toggleSelectRow(sender, row);
  249. }
  250. function toggleSelectListRow(sender) {
  251. var row = sender.parentNode;
  252. return toggleSelectRow(sender, row);
  253. }
  254. /* this is for dijit Checkbox */
  255. function toggleSelectListRow2(sender) {
  256. var row = sender.domNode.parentNode;
  257. return toggleSelectRow(sender, row);
  258. }
  259. function tSR(sender, row) {
  260. return toggleSelectRow(sender, row);
  261. }
  262. /* this is for dijit Checkbox */
  263. function toggleSelectRow2(sender, row) {
  264. if (!row) row = sender.domNode.parentNode.parentNode;
  265. if (sender.checked && !row.hasClassName('Selected'))
  266. row.addClassName('Selected');
  267. else
  268. row.removeClassName('Selected');
  269. }
  270. function toggleSelectRow(sender, row) {
  271. if (!row) row = sender.parentNode.parentNode;
  272. if (sender.checked && !row.hasClassName('Selected'))
  273. row.addClassName('Selected');
  274. else
  275. row.removeClassName('Selected');
  276. }
  277. function checkboxToggleElement(elem, id) {
  278. if (elem.checked) {
  279. Effect.Appear(id, {duration : 0.5});
  280. } else {
  281. Effect.Fade(id, {duration : 0.5});
  282. }
  283. }
  284. function dropboxSelect(e, v) {
  285. for (var i = 0; i < e.length; i++) {
  286. if (e[i].value == v) {
  287. e.selectedIndex = i;
  288. break;
  289. }
  290. }
  291. }
  292. function getURLParam(param){
  293. return String(window.location.href).parseQuery()[param];
  294. }
  295. function leading_zero(p) {
  296. var s = String(p);
  297. if (s.length == 1) s = "0" + s;
  298. return s;
  299. }
  300. function make_timestamp() {
  301. var d = new Date();
  302. return leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
  303. ":" + leading_zero(d.getSeconds());
  304. }
  305. function closeInfoBox(cleanup) {
  306. try {
  307. dialog = dijit.byId("infoBox");
  308. if (dialog) dialog.hide();
  309. } catch (e) {
  310. //exception_error("closeInfoBox", e);
  311. }
  312. return false;
  313. }
  314. function displayDlg(id, param, callback) {
  315. notify_progress("Loading, please wait...", true);
  316. var query = "?op=dlg&id=" +
  317. param_escape(id) + "&param=" + param_escape(param);
  318. new Ajax.Request("backend.php", {
  319. parameters: query,
  320. onComplete: function (transport) {
  321. infobox_callback2(transport);
  322. if (callback) callback(transport);
  323. } });
  324. return false;
  325. }
  326. function infobox_callback2(transport) {
  327. try {
  328. var dialog = false;
  329. if (dijit.byId("infoBox")) {
  330. dialog = dijit.byId("infoBox");
  331. }
  332. //console.log("infobox_callback2");
  333. notify('');
  334. var title = transport.responseXML.getElementsByTagName("title")[0];
  335. if (title)
  336. title = title.firstChild.nodeValue;
  337. var content = transport.responseXML.getElementsByTagName("content")[0];
  338. content = content.firstChild.nodeValue;
  339. if (!dialog) {
  340. dialog = new dijit.Dialog({
  341. title: title,
  342. id: 'infoBox',
  343. style: "width: 600px",
  344. onCancel: function() {
  345. return true;
  346. },
  347. onExecute: function() {
  348. return true;
  349. },
  350. onClose: function() {
  351. return true;
  352. },
  353. content: content});
  354. } else {
  355. dialog.attr('title', title);
  356. dialog.attr('content', content);
  357. }
  358. dialog.show();
  359. notify("");
  360. } catch (e) {
  361. exception_error("infobox_callback2", e);
  362. }
  363. }
  364. function filterCR(e, f)
  365. {
  366. var key;
  367. if(window.event)
  368. key = window.event.keyCode; //IE
  369. else
  370. key = e.which; //firefox
  371. if (key == 13) {
  372. if (typeof f != 'undefined') {
  373. f();
  374. return false;
  375. } else {
  376. return false;
  377. }
  378. } else {
  379. return true;
  380. }
  381. }
  382. function getInitParam(key) {
  383. return init_params[key];
  384. }
  385. function setInitParam(key, value) {
  386. init_params[key] = value;
  387. }
  388. function fatalError(code, msg, ext_info) {
  389. try {
  390. if (code == 6) {
  391. window.location.href = "tt-rss.php";
  392. } else if (code == 5) {
  393. window.location.href = "db-updater.php";
  394. } else {
  395. if (msg == "") msg = "Unknown error";
  396. if (ext_info) {
  397. if (ext_info.responseText) {
  398. ext_info = ext_info.responseText;
  399. }
  400. }
  401. if (ERRORS && ERRORS[code] && !msg) {
  402. msg = ERRORS[code];
  403. }
  404. var content = "<div><b>Error code:</b> " + code + "</div>" +
  405. "<p>" + msg + "</p>";
  406. if (ext_info) {
  407. content = content + "<div><b>Additional information:</b></div>" +
  408. "<textarea style='width: 100%' readonly=\"1\">" +
  409. ext_info + "</textarea>";
  410. }
  411. var dialog = new dijit.Dialog({
  412. title: "Fatal error",
  413. style: "width: 600px",
  414. content: content});
  415. dialog.show();
  416. }
  417. return false;
  418. } catch (e) {
  419. exception_error("fatalError", e);
  420. }
  421. }
  422. function filterDlgCheckType(sender) {
  423. try {
  424. var ftype = sender.value;
  425. // if selected filter type is 5 (Date) enable the modifier dropbox
  426. if (ftype == 5) {
  427. Element.show("filterDlg_dateModBox");
  428. Element.show("filterDlg_dateChkBox");
  429. } else {
  430. Element.hide("filterDlg_dateModBox");
  431. Element.hide("filterDlg_dateChkBox");
  432. }
  433. } catch (e) {
  434. exception_error("filterDlgCheckType", e);
  435. }
  436. }
  437. function filterDlgCheckAction(sender) {
  438. try {
  439. var action = sender.value;
  440. var action_param = $("filterDlg_paramBox");
  441. if (!action_param) {
  442. console.log("filterDlgCheckAction: can't find action param box!");
  443. return;
  444. }
  445. // if selected action supports parameters, enable params field
  446. if (action == 4 || action == 6 || action == 7) {
  447. new Effect.Appear(action_param, {duration : 0.5});
  448. if (action != 7) {
  449. Element.show(dijit.byId("filterDlg_actionParam").domNode);
  450. Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
  451. } else {
  452. Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
  453. Element.hide(dijit.byId("filterDlg_actionParam").domNode);
  454. }
  455. } else {
  456. Element.hide(action_param);
  457. }
  458. } catch (e) {
  459. exception_error("filterDlgCheckAction", e);
  460. }
  461. }
  462. function filterDlgCheckDate() {
  463. try {
  464. var dialog = dijit.byId("filterEditDlg");
  465. var reg_exp = dialog.attr('value').reg_exp;
  466. var query = "?op=rpc&subop=checkDate&date=" + reg_exp;
  467. new Ajax.Request("backend.php", {
  468. parameters: query,
  469. onComplete: function(transport) {
  470. var reply = JSON.parse(transport.responseText);
  471. if (reply['result'] == true) {
  472. alert(__("Date syntax appears to be correct:") + " " + reply['date']);
  473. return;
  474. } else {
  475. alert(__("Date syntax is incorrect."));
  476. }
  477. } });
  478. } catch (e) {
  479. exception_error("filterDlgCheckDate", e);
  480. }
  481. }
  482. function explainError(code) {
  483. return displayDlg("explainError", code);
  484. }
  485. function displayHelpInfobox(topic_id) {
  486. var url = "backend.php?op=help&tid=" + param_escape(topic_id);
  487. window.open(url, "ttrss_help",
  488. "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
  489. }
  490. function loading_set_progress(p) {
  491. try {
  492. loading_progress += p;
  493. if (dijit.byId("loading_bar"))
  494. dijit.byId("loading_bar").update({progress: loading_progress});
  495. if (loading_progress >= 90)
  496. remove_splash();
  497. } catch (e) {
  498. exception_error("loading_set_progress", e);
  499. }
  500. }
  501. function remove_splash() {
  502. if (Element.visible("overlay")) {
  503. console.log("about to remove splash, OMG!");
  504. Element.hide("overlay");
  505. console.log("removed splash!");
  506. }
  507. }
  508. function transport_error_check(transport) {
  509. try {
  510. if (transport.responseXML) {
  511. var error = transport.responseXML.getElementsByTagName("error")[0];
  512. if (error) {
  513. var code = error.getAttribute("error-code");
  514. var msg = error.getAttribute("error-msg");
  515. if (code != 0) {
  516. fatalError(code, msg);
  517. return false;
  518. }
  519. }
  520. }
  521. } catch (e) {
  522. exception_error("check_for_error_xml", e);
  523. }
  524. return true;
  525. }
  526. function strip_tags(s) {
  527. return s.replace(/<\/?[^>]+(>|$)/g, "");
  528. }
  529. function truncate_string(s, length) {
  530. if (!length) length = 30;
  531. var tmp = s.substring(0, length);
  532. if (s.length > length) tmp += "&hellip;";
  533. return tmp;
  534. }
  535. function hotkey_prefix_timeout() {
  536. try {
  537. var date = new Date();
  538. var ts = Math.round(date.getTime() / 1000);
  539. if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
  540. console.log("hotkey_prefix seems to be stuck, aborting");
  541. hotkey_prefix_pressed = false;
  542. hotkey_prefix = false;
  543. Element.hide('cmdline');
  544. }
  545. setTimeout("hotkey_prefix_timeout()", 1000);
  546. } catch (e) {
  547. exception_error("hotkey_prefix_timeout", e);
  548. }
  549. }
  550. function hideAuxDlg() {
  551. try {
  552. Element.hide('auxDlg');
  553. } catch (e) {
  554. exception_error("hideAuxDlg", e);
  555. }
  556. }
  557. function uploadIconHandler(rc) {
  558. try {
  559. switch (rc) {
  560. case 0:
  561. notify_info("Upload complete.");
  562. if (inPreferences()) {
  563. updateFeedList();
  564. } else {
  565. setTimeout('updateFeedList(false, false)', 50);
  566. }
  567. break;
  568. case 1:
  569. notify_error("Upload failed: icon is too big.");
  570. break;
  571. case 2:
  572. notify_error("Upload failed.");
  573. break;
  574. }
  575. } catch (e) {
  576. exception_error("uploadIconHandler", e);
  577. }
  578. }
  579. function removeFeedIcon(id) {
  580. try {
  581. if (confirm(__("Remove stored feed icon?"))) {
  582. var query = "backend.php?op=pref-feeds&subop=removeicon&feed_id=" + param_escape(id);
  583. console.log(query);
  584. notify_progress("Removing feed icon...", true);
  585. new Ajax.Request("backend.php", {
  586. parameters: query,
  587. onComplete: function(transport) {
  588. notify_info("Feed icon removed.");
  589. if (inPreferences()) {
  590. updateFeedList();
  591. } else {
  592. setTimeout('updateFeedList(false, false)', 50);
  593. }
  594. } });
  595. }
  596. return false;
  597. } catch (e) {
  598. exception_error("uploadFeedIcon", e);
  599. }
  600. }
  601. function uploadFeedIcon() {
  602. try {
  603. var file = $("icon_file");
  604. if (file.value.length == 0) {
  605. alert(__("Please select an image file to upload."));
  606. } else {
  607. if (confirm(__("Upload new icon for this feed?"))) {
  608. notify_progress("Uploading, please wait...", true);
  609. return true;
  610. }
  611. }
  612. return false;
  613. } catch (e) {
  614. exception_error("uploadFeedIcon", e);
  615. }
  616. }
  617. function addLabel(select, callback) {
  618. try {
  619. var caption = prompt(__("Please enter label caption:"), "");
  620. if (caption != undefined) {
  621. if (caption == "") {
  622. alert(__("Can't create label: missing caption."));
  623. return false;
  624. }
  625. var query = "?op=pref-labels&subop=add&caption=" +
  626. param_escape(caption);
  627. if (select)
  628. query += "&output=select";
  629. notify_progress("Loading, please wait...", true);
  630. if (inPreferences() && !select) active_tab = "labelConfig";
  631. new Ajax.Request("backend.php", {
  632. parameters: query,
  633. onComplete: function(transport) {
  634. if (callback) {
  635. callback(transport);
  636. } else if (inPreferences()) {
  637. updateLabelList();
  638. } else {
  639. updateFeedList();
  640. }
  641. } });
  642. }
  643. } catch (e) {
  644. exception_error("addLabel", e);
  645. }
  646. }
  647. function quickAddFeed() {
  648. try {
  649. var query = "backend.php?op=dlg&id=quickAddFeed";
  650. if (dijit.byId("feedAddDlg"))
  651. dijit.byId("feedAddDlg").destroyRecursive();
  652. var dialog = new dijit.Dialog({
  653. id: "feedAddDlg",
  654. title: __("Subscribe to Feed"),
  655. style: "width: 600px",
  656. execute: function() {
  657. if (this.validate()) {
  658. console.log(dojo.objectToQuery(this.attr('value')));
  659. var feed_url = this.attr('value').feed;
  660. notify_progress(__("Subscribing to feed..."), true);
  661. new Ajax.Request("backend.php", {
  662. parameters: dojo.objectToQuery(this.attr('value')),
  663. onComplete: function(transport) {
  664. try {
  665. var reply = JSON.parse(transport.responseText);
  666. var rc = parseInt(reply['result']);
  667. notify('');
  668. console.log("GOT RC: " + rc);
  669. switch (rc) {
  670. case 1:
  671. dialog.hide();
  672. notify_info(__("Subscribed to %s").replace("%s", feed_url));
  673. updateFeedList();
  674. break;
  675. case 2:
  676. alert(__("Specified URL seems to be invalid."));
  677. break;
  678. case 3:
  679. alert(__("Specified URL doesn't seem to contain any feeds."));
  680. break;
  681. case 4:
  682. notify_progress("Searching for feed urls...", true);
  683. new Ajax.Request("backend.php", {
  684. parameters: 'op=rpc&subop=extractfeedurls&url=' + param_escape(feed_url),
  685. onComplete: function(transport, dialog, feed_url) {
  686. notify('');
  687. var reply = JSON.parse(transport.responseText);
  688. var feeds = reply['urls'];
  689. console.log(transport.responseText);
  690. var select = dijit.byId("feedDlg_feedContainerSelect");
  691. while (select.getOptions().length > 0)
  692. select.removeOption(0);
  693. var count = 0;
  694. for (var feedUrl in feeds) {
  695. select.addOption({value: feedUrl, label: feeds[feedUrl]});
  696. count++;
  697. }
  698. // if (count > 5) count = 5;
  699. // select.size = count;
  700. Effect.Appear('feedDlg_feedsContainer', {duration : 0.5});
  701. }
  702. });
  703. break;
  704. case 5:
  705. alert(__("Couldn't download the specified URL."));
  706. break;
  707. case 0:
  708. alert(__("You are already subscribed to this feed."));
  709. break;
  710. }
  711. } catch (e) {
  712. exception_error("subscribeToFeed", e, transport);
  713. }
  714. } });
  715. }
  716. },
  717. href: query});
  718. dialog.show();
  719. } catch (e) {
  720. exception_error("quickAddFeed", e);
  721. }
  722. }
  723. function quickAddFilter() {
  724. try {
  725. var query = "backend.php?op=dlg&id=quickAddFilter";
  726. if (dijit.byId("filterEditDlg"))
  727. dijit.byId("filterEditDlg").destroyRecursive();
  728. dialog = new dijit.Dialog({
  729. id: "filterEditDlg",
  730. title: __("Create Filter"),
  731. style: "width: 600px",
  732. test: function() {
  733. if (this.validate()) {
  734. if (dijit.byId("filterTestDlg"))
  735. dijit.byId("filterTestDlg").destroyRecursive();
  736. tdialog = new dijit.Dialog({
  737. id: "filterTestDlg",
  738. title: __("Filter Test Results"),
  739. style: "width: 600px",
  740. href: "backend.php?savemode=test&" +
  741. dojo.objectToQuery(dialog.attr('value')),
  742. });
  743. tdialog.show();
  744. }
  745. },
  746. execute: function() {
  747. if (this.validate()) {
  748. var query = "?op=rpc&subop=verifyRegexp&reg_exp=" +
  749. param_escape(dialog.attr('value').reg_exp);
  750. notify_progress("Verifying regular expression...");
  751. new Ajax.Request("backend.php", {
  752. parameters: query,
  753. onComplete: function(transport) {
  754. var reply = JSON.parse(transport.responseText);
  755. if (reply) {
  756. notify('');
  757. if (!reply['status']) {
  758. alert("Match regular expression seems to be invalid.");
  759. return;
  760. } else {
  761. notify_progress("Saving data...", true);
  762. console.log(dojo.objectToQuery(dialog.attr('value')));
  763. new Ajax.Request("backend.php", {
  764. parameters: dojo.objectToQuery(dialog.attr('value')),
  765. onComplete: function(transport) {
  766. dialog.hide();
  767. notify_info(transport.responseText);
  768. if (inPreferences()) {
  769. updateFilterList();
  770. }
  771. }});
  772. }
  773. }
  774. }});
  775. }
  776. },
  777. href: query});
  778. dialog.show();
  779. } catch (e) {
  780. exception_error("quickAddFilter", e);
  781. }
  782. }
  783. function resetPubSub(feed_id, title) {
  784. var msg = __("Reset subscription? Tiny Tiny RSS will try to subscribe to the notification hub again on next feed update.").replace("%s", title);
  785. if (title == undefined || confirm(msg)) {
  786. notify_progress("Loading, please wait...");
  787. var query = "?op=pref-feeds&quiet=1&subop=resetPubSub&ids=" + feed_id;
  788. new Ajax.Request("backend.php", {
  789. parameters: query,
  790. onComplete: function(transport) {
  791. dijit.byId("pubsubReset_Btn").attr('disabled', true);
  792. notify_info("Subscription reset.");
  793. } });
  794. }
  795. return false;
  796. }
  797. function unsubscribeFeed(feed_id, title) {
  798. var msg = __("Unsubscribe from %s?").replace("%s", title);
  799. if (title == undefined || confirm(msg)) {
  800. notify_progress("Removing feed...");
  801. var query = "?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id;
  802. new Ajax.Request("backend.php", {
  803. parameters: query,
  804. onComplete: function(transport) {
  805. if (dijit.byId("feedEditDlg")) dijit.byId("feedEditDlg").hide();
  806. if (inPreferences()) {
  807. updateFeedList();
  808. } else {
  809. if (feed_id == getActiveFeedId())
  810. setTimeout("viewfeed(-5)", 100);
  811. }
  812. } });
  813. }
  814. return false;
  815. }
  816. function backend_sanity_check_callback(transport) {
  817. try {
  818. if (sanity_check_done) {
  819. fatalError(11, "Sanity check request received twice. This can indicate "+
  820. "presence of Firebug or some other disrupting extension. "+
  821. "Please disable it and try again.");
  822. return;
  823. }
  824. var reply = JSON.parse(transport.responseText);
  825. if (!reply) {
  826. fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
  827. return;
  828. }
  829. var error_code = reply['error']['code'];
  830. if (error_code && error_code != 0) {
  831. return fatalError(error_code, reply['error']['message']);
  832. }
  833. console.log("sanity check ok");
  834. var params = reply['init-params'];
  835. if (params) {
  836. console.log('reading init-params...');
  837. if (params) {
  838. for (k in params) {
  839. var v = params[k];
  840. console.log("IP: " + k + " => " + v);
  841. }
  842. }
  843. init_params = params;
  844. }
  845. sanity_check_done = true;
  846. init_second_stage();
  847. } catch (e) {
  848. exception_error("backend_sanity_check_callback", e, transport);
  849. }
  850. }
  851. /*function has_local_storage() {
  852. try {
  853. return 'sessionStorage' in window && window['sessionStorage'] != null;
  854. } catch (e) {
  855. return false;
  856. }
  857. } */
  858. function catSelectOnChange(elem) {
  859. try {
  860. /* var value = elem[elem.selectedIndex].value;
  861. var def = elem.getAttribute('default');
  862. if (value == "ADD_CAT") {
  863. if (def)
  864. dropboxSelect(elem, def);
  865. else
  866. elem.selectedIndex = 0;
  867. quickAddCat(elem);
  868. } */
  869. } catch (e) {
  870. exception_error("catSelectOnChange", e);
  871. }
  872. }
  873. function quickAddCat(elem) {
  874. try {
  875. var cat = prompt(__("Please enter category title:"));
  876. if (cat) {
  877. var query = "?op=rpc&subop=quickAddCat&cat=" + param_escape(cat);
  878. notify_progress("Loading, please wait...", true);
  879. new Ajax.Request("backend.php", {
  880. parameters: query,
  881. onComplete: function (transport) {
  882. var response = transport.responseXML;
  883. var select = response.getElementsByTagName("select")[0];
  884. var options = select.getElementsByTagName("option");
  885. dropbox_replace_options(elem, options);
  886. notify('');
  887. } });
  888. }
  889. } catch (e) {
  890. exception_error("quickAddCat", e);
  891. }
  892. }
  893. function genUrlChangeKey(feed, is_cat) {
  894. try {
  895. var ok = confirm(__("Generate new syndication address for this feed?"));
  896. if (ok) {
  897. notify_progress("Trying to change address...", true);
  898. var query = "?op=rpc&subop=regenFeedKey&id=" + param_escape(feed) +
  899. "&is_cat=" + param_escape(is_cat);
  900. new Ajax.Request("backend.php", {
  901. parameters: query,
  902. onComplete: function(transport) {
  903. var reply = JSON.parse(transport.responseText);
  904. var new_link = reply.link;
  905. var e = $('gen_feed_url');
  906. if (new_link) {
  907. e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
  908. "&amp;key=" + new_link);
  909. e.href = e.href.replace(/\&key=.*$/,
  910. "&key=" + new_link);
  911. new Effect.Highlight(e);
  912. notify('');
  913. } else {
  914. notify_error("Could not change feed URL.");
  915. }
  916. } });
  917. }
  918. } catch (e) {
  919. exception_error("genUrlChangeKey", e);
  920. }
  921. return false;
  922. }
  923. function labelSelectOnChange(elem) {
  924. try {
  925. /* var value = elem[elem.selectedIndex].value;
  926. var def = elem.getAttribute('default');
  927. if (value == "ADD_LABEL") {
  928. if (def)
  929. dropboxSelect(elem, def);
  930. else
  931. elem.selectedIndex = 0;
  932. addLabel(elem, function(transport) {
  933. try {
  934. var response = transport.responseXML;
  935. var select = response.getElementsByTagName("select")[0];
  936. var options = select.getElementsByTagName("option");
  937. dropbox_replace_options(elem, options);
  938. notify('');
  939. } catch (e) {
  940. exception_error("addLabel", e);
  941. }
  942. });
  943. } */
  944. } catch (e) {
  945. exception_error("labelSelectOnChange", e);
  946. }
  947. }
  948. function dropbox_replace_options(elem, options) {
  949. try {
  950. while (elem.hasChildNodes())
  951. elem.removeChild(elem.firstChild);
  952. var sel_idx = -1;
  953. for (var i = 0; i < options.length; i++) {
  954. var text = options[i].firstChild.nodeValue;
  955. var value = options[i].getAttribute("value");
  956. if (value == undefined) value = text;
  957. var issel = options[i].getAttribute("selected") == "1";
  958. var option = new Option(text, value, issel);
  959. if (options[i].getAttribute("disabled"))
  960. option.setAttribute("disabled", true);
  961. elem.insert(option);
  962. if (issel) sel_idx = i;
  963. }
  964. // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
  965. if (sel_idx >= 0) elem.selectedIndex = sel_idx;
  966. } catch (e) {
  967. exception_error("dropbox_replace_options", e);
  968. }
  969. }
  970. // mode = all, none, invert
  971. function selectTableRows(id, mode) {
  972. try {
  973. var rows = $(id).rows;
  974. for (var i = 0; i < rows.length; i++) {
  975. var row = rows[i];
  976. var cb = false;
  977. if (row.id && row.className) {
  978. var bare_id = row.id.replace(/^[A-Z]*?-/, "");
  979. var inputs = rows[i].getElementsByTagName("input");
  980. for (var j = 0; j < inputs.length; j++) {
  981. var input = inputs[j];
  982. if (input.getAttribute("type") == "checkbox" &&
  983. input.id.match(bare_id)) {
  984. cb = input;
  985. break;
  986. }
  987. }
  988. if (cb) {
  989. var issel = row.hasClassName("Selected");
  990. if (mode == "all" && !issel) {
  991. row.addClassName("Selected");
  992. cb.checked = true;
  993. } else if (mode == "none" && issel) {
  994. row.removeClassName("Selected");
  995. cb.checked = false;
  996. } else if (mode == "invert") {
  997. if (issel) {
  998. row.removeClassName("Selected");
  999. cb.checked = false;
  1000. } else {
  1001. row.addClassName("Selected");
  1002. cb.checked = true;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. }
  1008. } catch (e) {
  1009. exception_error("selectTableRows", e);
  1010. }
  1011. }
  1012. function getSelectedTableRowIds(id) {
  1013. var rows = [];
  1014. try {
  1015. var elem_rows = $(id).rows;
  1016. for (var i = 0; i < elem_rows.length; i++) {
  1017. if (elem_rows[i].hasClassName("Selected")) {
  1018. var bare_id = elem_rows[i].id.replace(/^[A-Z]*?-/, "");
  1019. rows.push(bare_id);
  1020. }
  1021. }
  1022. } catch (e) {
  1023. exception_error("getSelectedTableRowIds", e);
  1024. }
  1025. return rows;
  1026. }
  1027. function editFeed(feed, event) {
  1028. try {
  1029. if (feed <= 0)
  1030. return alert(__("You can't edit this kind of feed."));
  1031. var query = "backend.php?op=pref-feeds&subop=editfeed&id=" +
  1032. param_escape(feed);
  1033. console.log(query);
  1034. if (dijit.byId("feedEditDlg"))
  1035. dijit.byId("feedEditDlg").destroyRecursive();
  1036. dialog = new dijit.Dialog({
  1037. id: "feedEditDlg",
  1038. title: __("Edit Feed"),
  1039. style: "width: 600px",
  1040. execute: function() {
  1041. if (this.validate()) {
  1042. // console.log(dojo.objectToQuery(this.attr('value')));
  1043. notify_progress("Saving data...", true);
  1044. new Ajax.Request("backend.php", {
  1045. parameters: dojo.objectToQuery(dialog.attr('value')),
  1046. onComplete: function(transport) {
  1047. dialog.hide();
  1048. notify('');
  1049. updateFeedList();
  1050. }});
  1051. }
  1052. },
  1053. href: query});
  1054. dialog.show();
  1055. } catch (e) {
  1056. exception_error("editFeed", e);
  1057. }
  1058. }
  1059. function feedBrowser() {
  1060. try {
  1061. var query = "backend.php?op=dlg&id=feedBrowser";
  1062. if (dijit.byId("feedAddDlg"))
  1063. dijit.byId("feedAddDlg").hide();
  1064. if (dijit.byId("feedBrowserDlg"))
  1065. dijit.byId("feedBrowserDlg").destroyRecursive();
  1066. var dialog = new dijit.Dialog({
  1067. id: "feedBrowserDlg",
  1068. title: __("More Feeds"),
  1069. style: "width: 600px",
  1070. getSelectedFeedIds: function() {
  1071. var list = $$("#browseFeedList li[id*=FBROW]");
  1072. var selected = new Array();
  1073. list.each(function(child) {
  1074. var id = child.id.replace("FBROW-", "");
  1075. if (child.hasClassName('Selected')) {
  1076. selected.push(id);
  1077. }
  1078. });
  1079. return selected;
  1080. },
  1081. getSelectedFeeds: function() {
  1082. var list = $$("#browseFeedList li.Selected");
  1083. var selected = new Array();
  1084. list.each(function(child) {
  1085. var title = child.getElementsBySelector("span.fb_feedTitle")[0].innerHTML;
  1086. var url = child.getElementsBySelector("a.fb_feedUrl")[0].href;
  1087. selected.push([title,url]);
  1088. });
  1089. return selected;
  1090. },
  1091. subscribe: function() {
  1092. var mode = this.attr('value').mode;
  1093. var selected = [];
  1094. if (mode == "1")
  1095. selected = this.getSelectedFeeds();
  1096. else
  1097. selected = this.getSelectedFeedIds();
  1098. if (selected.length > 0) {
  1099. dijit.byId("feedBrowserDlg").hide();
  1100. notify_progress("Loading, please wait...", true);
  1101. // we use dojo.toJson instead of JSON.stringify because
  1102. // it somehow escapes everything TWICE, at least in Chrome 9
  1103. var query = "?op=rpc&subop=massSubscribe&payload="+
  1104. param_escape(dojo.toJson(selected)) + "&mode=" + param_escape(mode);
  1105. console.log(query);
  1106. new Ajax.Request("backend.php", {
  1107. parameters: query,
  1108. onComplete: function(transport) {
  1109. notify('');
  1110. updateFeedList();
  1111. } });
  1112. } else {
  1113. alert(__("No feeds are selected."));
  1114. }
  1115. },
  1116. update: function() {
  1117. var query = dojo.objectToQuery(dialog.attr('value'));
  1118. Element.show('feed_browser_spinner');
  1119. new Ajax.Request("backend.php", {
  1120. parameters: query,
  1121. onComplete: function(transport) {
  1122. notify('');
  1123. Element.hide('feed_browser_spinner');
  1124. var c = $("browseFeedList");
  1125. var reply = JSON.parse(transport.responseText);
  1126. var r = reply['content'];
  1127. var mode = reply['mode'];
  1128. if (c && r) {
  1129. c.innerHTML = r;
  1130. }
  1131. dojo.parser.parse("browseFeedList");
  1132. if (mode == 2) {
  1133. Element.show(dijit.byId('feed_archive_remove').domNode);
  1134. } else {
  1135. Element.hide(dijit.byId('feed_archive_remove').domNode);
  1136. }
  1137. } });
  1138. },
  1139. removeFromArchive: function() {
  1140. var selected = this.getSelectedFeeds();
  1141. if (selected.length > 0) {
  1142. var pr = __("Remove selected feeds from the archive? Feeds with stored articles will not be removed.");
  1143. if (confirm(pr)) {
  1144. Element.show('feed_browser_spinner');
  1145. var query = "?op=rpc&subop=remarchived&ids=" +
  1146. param_escape(selected.toString());;
  1147. new Ajax.Request("backend.php", {
  1148. parameters: query,
  1149. onComplete: function(transport) {
  1150. dialog.update();
  1151. } });
  1152. }
  1153. }
  1154. },
  1155. execute: function() {
  1156. if (this.validate()) {
  1157. this.subscribe();
  1158. }
  1159. },
  1160. href: query});
  1161. dialog.show();
  1162. } catch (e) {
  1163. exception_error("editFeed", e);
  1164. }
  1165. }
  1166. function showFeedsWithErrors() {
  1167. try {
  1168. var query = "backend.php?op=dlg&id=feedsWithErrors";
  1169. if (dijit.byId("errorFeedsDlg"))
  1170. dijit.byId("errorFeedsDlg").destroyRecursive();
  1171. dialog = new dijit.Dialog({
  1172. id: "errorFeedsDlg",
  1173. title: __("Feeds with update errors"),
  1174. style: "width: 600px",
  1175. getSelectedFeeds: function() {
  1176. return getSelectedTableRowIds("prefErrorFeedList");
  1177. },
  1178. removeSelected: function() {
  1179. var sel_rows = this.getSelectedFeeds();
  1180. console.log(sel_rows);
  1181. if (sel_rows.length > 0) {
  1182. var ok = confirm(__("Remove selected feeds?"));
  1183. if (ok) {
  1184. notify_progress("Removing selected feeds...", true);
  1185. var query = "?op=pref-feeds&subop=remove&ids="+
  1186. param_escape(sel_rows.toString());
  1187. new Ajax.Request("backend.php", {
  1188. parameters: query,
  1189. onComplete: function(transport) {
  1190. notify('');
  1191. dialog.hide();
  1192. updateFeedList();
  1193. } });
  1194. }
  1195. } else {
  1196. alert(__("No feeds are selected."));
  1197. }
  1198. },
  1199. execute: function() {
  1200. if (this.validate()) {
  1201. }
  1202. },
  1203. href: query});
  1204. dialog.show();
  1205. } catch (e) {
  1206. exception_error("showFeedsWithErrors", e);
  1207. }
  1208. }
  1209. /* new support functions for SelectByTag */
  1210. function get_all_tags(selObj){
  1211. try {
  1212. if( !selObj ) return "";
  1213. var result = "";
  1214. var len = selObj.options.length;
  1215. for (var i=0; i < len; i++){
  1216. if (selObj.options[i].selected) {
  1217. result += selObj[i].value + "%2C"; // is really a comma
  1218. }
  1219. }
  1220. if (result.length > 0){
  1221. result = result.substr(0, result.length-3); // remove trailing %2C
  1222. }
  1223. return(result);
  1224. } catch (e) {
  1225. exception_error("get_all_tags", e);
  1226. }
  1227. }
  1228. function get_radio_checked(radioObj) {
  1229. try {
  1230. if (!radioObj) return "";
  1231. var len = radioObj.length;
  1232. if (len == undefined){
  1233. if(radioObj.checked){
  1234. return(radioObj.value);
  1235. } else {
  1236. return("");
  1237. }
  1238. }
  1239. for( var i=0; i < len; i++ ){
  1240. if( radioObj[i].checked ){
  1241. return( radioObj[i].value);
  1242. }
  1243. }
  1244. } catch (e) {
  1245. exception_error("get_radio_checked", e);
  1246. }
  1247. return("");
  1248. }