move some more stuff out of common.js

rework client-side cookie functions a bit
limit dojo cachebust based on server scripts modification time
remove param_escape()
This commit is contained in:
Andrew Dolgov 2018-12-02 20:07:57 +03:00
parent ad1b6f0a86
commit d9c5c93cef
20 changed files with 111 additions and 137 deletions

View File

@ -52,7 +52,7 @@ class Dlg extends Handler_Protected {
print "<button dojoType=\"dijit.form.Button\" onclick=\"return opmlRegenKey()\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return opmlRegenKey()\">".
__('Generate new URL')."</button> "; __('Generate new URL')."</button> ";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return closeInfoBox()\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.closeInfoBox()\">".
__('Close this window')."</button>"; __('Close this window')."</button>";
print "</div>"; print "</div>";
@ -85,7 +85,7 @@ class Dlg extends Handler_Protected {
print "<div align='center'>"; print "<div align='center'>";
print "<button onclick=\"return closeInfoBox()\">". print "<button onclick=\"return CommonDialogs.closeInfoBox()\">".
__('Close this window')."</button>"; __('Close this window')."</button>";
print "</div>"; print "</div>";
@ -150,7 +150,7 @@ class Dlg extends Handler_Protected {
print "<div align='center'>"; print "<div align='center'>";
print "<button dojoType=\"dijit.form.Button\" print "<button dojoType=\"dijit.form.Button\"
onclick=\"return closeInfoBox()\">". onclick=\"return CommonDialogs.closeInfoBox()\">".
__('Close this window')."</button>"; __('Close this window')."</button>";
print "</div>"; print "</div>";
@ -179,7 +179,7 @@ class Dlg extends Handler_Protected {
print "<button dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.genUrlChangeKey('$feed_id', '$is_cat')\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.genUrlChangeKey('$feed_id', '$is_cat')\">".
__('Generate new URL')."</button> "; __('Generate new URL')."</button> ";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return closeInfoBox()\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.closeInfoBox()\">".
__('Close this window')."</button>"; __('Close this window')."</button>";
print "</div>"; print "</div>";
@ -195,7 +195,7 @@ class Dlg extends Handler_Protected {
print "<button dojoType=\"dijit.form.Button\" onclick=\"document.location.href = 'prefs.php'\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"document.location.href = 'prefs.php'\">".
__('Open Preferences')."</button> "; __('Open Preferences')."</button> ";
print "<button dojoType=\"dijit.form.Button\" print "<button dojoType=\"dijit.form.Button\"
onclick=\"return closeInfoBox()\">". onclick=\"return CommonDialogs.closeInfoBox()\">".
__('Close this window')."</button>"; __('Close this window')."</button>";
print "</div>"; print "</div>";
} }

View File

@ -745,9 +745,9 @@ class Pref_Feeds extends Handler_Protected {
<input type=\"hidden\" name=\"op\" value=\"pref-feeds\"> <input type=\"hidden\" name=\"op\" value=\"pref-feeds\">
<input type=\"hidden\" name=\"feed_id\" value=\"$feed_id\"> <input type=\"hidden\" name=\"feed_id\" value=\"$feed_id\">
<input type=\"hidden\" name=\"method\" value=\"uploadicon\"> <input type=\"hidden\" name=\"method\" value=\"uploadicon\">
<button class=\"\" dojoType=\"dijit.form.Button\" onclick=\"return uploadFeedIcon();\" <button class=\"\" dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.uploadFeedIcon();\"
type=\"submit\">".__('Replace')."</button> type=\"submit\">".__('Replace')."</button>
<button class=\"btn-danger\" dojoType=\"dijit.form.Button\" onclick=\"return removeFeedIcon($feed_id);\" <button class=\"btn-danger\" dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.removeFeedIcon($feed_id);\"
type=\"submit\">".__('Remove')."</button> type=\"submit\">".__('Remove')."</button>
</form>"; </form>";

View File

@ -2566,3 +2566,15 @@
function arr_qmarks($arr) { function arr_qmarks($arr) {
return str_repeat('?,', count($arr) - 1) . '?'; return str_repeat('?,', count($arr) - 1) . '?';
} }
function get_scripts_timestamp() {
$files = glob("js/*.js");
$ts = 0;
foreach ($files as $file) {
$file_ts = filemtime($file);
if ($file_ts > $ts) $ts = $file_ts;
}
return $ts;
}

View File

@ -91,7 +91,7 @@
<script> <script>
dojoConfig = { dojoConfig = {
async: true, async: true,
cacheBust: new Date(), cacheBust: "<?php echo get_scripts_timestamp(); ?>",
packages: [ packages: [
{ name: "fox", location: "../../js" }, { name: "fox", location: "../../js" },
] ]

View File

@ -176,7 +176,7 @@ define(["dojo/_base/declare"], function (declare) {
return false; return false;
}, },
editTags: function(id) { editTags: function(id) {
const query = "backend.php?op=article&method=editArticleTags&param=" + param_escape(id); const query = "backend.php?op=article&method=editArticleTags&param=" + encodeURIComponent(id);
if (dijit.byId("editTagsDlg")) if (dijit.byId("editTagsDlg"))
dijit.byId("editTagsDlg").destroyRecursive(); dijit.byId("editTagsDlg").destroyRecursive();

View File

@ -2,6 +2,40 @@
/* global __, ngettext */ /* global __, ngettext */
define(["dojo/_base/declare"], function (declare) { define(["dojo/_base/declare"], function (declare) {
return declare("fox.CommonDialogs", null, { return declare("fox.CommonDialogs", null, {
closeInfoBox: function() {
const dialog = dijit.byId("infoBox");
if (dialog) dialog.hide();
},
removeFeedIcon: function(id) {
if (confirm(__("Remove stored feed icon?"))) {
notify_progress("Removing feed icon...", true);
const query = {op: "pref-feeds", method: "removeicon", feed_id: id};
xhrPost("backend.php", query, () => {
notify_info("Feed icon removed.");
if (App.isPrefs()) {
Feeds.reload();
} else {
setTimeout('Feeds.reload(false, false)', 50);
}
});
}
return false;
},
uploadFeedIcon: function() {
const file = $("icon_file");
if (file.value.length == 0) {
alert(__("Please select an image file to upload."));
} else if (confirm(__("Upload new icon for this feed?"))) {
notify_progress("Uploading, please wait...", true);
return true;
}
return false;
},
quickAddFeed: function() { quickAddFeed: function() {
const query = "backend.php?op=feeds&method=quickAddFeed"; const query = "backend.php?op=feeds&method=quickAddFeed";

View File

@ -126,7 +126,7 @@ define(["dojo/_base/declare"], function (declare) {
dijit.byId("filterNewRuleDlg").destroyRecursive(); dijit.byId("filterNewRuleDlg").destroyRecursive();
const query = "backend.php?op=pref-filters&method=newrule&rule=" + const query = "backend.php?op=pref-filters&method=newrule&rule=" +
param_escape(ruleStr); encodeURIComponent(ruleStr);
const rule_dlg = new dijit.Dialog({ const rule_dlg = new dijit.Dialog({
id: "filterNewRuleDlg", id: "filterNewRuleDlg",
@ -148,7 +148,7 @@ define(["dojo/_base/declare"], function (declare) {
dijit.byId("filterNewActionDlg").destroyRecursive(); dijit.byId("filterNewActionDlg").destroyRecursive();
const query = "backend.php?op=pref-filters&method=newaction&action=" + const query = "backend.php?op=pref-filters&method=newaction&action=" +
param_escape(actionStr); encodeURIComponent(actionStr);
const rule_dlg = new dijit.Dialog({ const rule_dlg = new dijit.Dialog({
id: "filterNewActionDlg", id: "filterNewActionDlg",

View File

@ -608,7 +608,7 @@ define(["dojo/_base/declare"], function (declare) {
}, },
search: function() { search: function() {
const query = "backend.php?op=feeds&method=search&param=" + const query = "backend.php?op=feeds&method=search&param=" +
param_escape(Feeds.getActive() + ":" + Feeds.activeIsCat()); encodeURIComponent(Feeds.getActive() + ":" + Feeds.activeIsCat());
if (dijit.byId("searchDlg")) if (dijit.byId("searchDlg"))
dijit.byId("searchDlg").destroyRecursive(); dijit.byId("searchDlg").destroyRecursive();

View File

@ -138,7 +138,7 @@ define(["dojo/_base/declare", "dojo/dom-construct", "lib/CheckBoxTree"], functio
}, },
editFilter: function(id) { editFilter: function(id) {
const query = "backend.php?op=pref-filters&method=edit&id=" + param_escape(id); const query = "backend.php?op=pref-filters&method=edit&id=" + encodeURIComponent(id);
if (dijit.byId("feedEditDlg")) if (dijit.byId("feedEditDlg"))
dijit.byId("feedEditDlg").destroyRecursive(); dijit.byId("feedEditDlg").destroyRecursive();

View File

@ -56,7 +56,7 @@ define(["dojo/_base/declare", "dojo/dom-construct", "lib/CheckBoxTree", "dijit/f
}, },
editLabel: function(id) { editLabel: function(id) {
const query = "backend.php?op=pref-labels&method=edit&id=" + const query = "backend.php?op=pref-labels&method=edit&id=" +
param_escape(id); encodeURIComponent(id);
if (dijit.byId("labelEditDlg")) if (dijit.byId("labelEditDlg"))
dijit.byId("labelEditDlg").destroyRecursive(); dijit.byId("labelEditDlg").destroyRecursive();

View File

@ -27,7 +27,7 @@ define(["dojo/_base/declare"], function (declare) {
}, },
edit: function(id) { edit: function(id) {
const query = "backend.php?op=pref-users&method=edit&id=" + const query = "backend.php?op=pref-users&method=edit&id=" +
param_escape(id); encodeURIComponent(id);
if (dijit.byId("userEditDlg")) if (dijit.byId("userEditDlg"))
dijit.byId("userEditDlg").destroyRecursive(); dijit.byId("userEditDlg").destroyRecursive();

View File

@ -95,7 +95,7 @@ define(["dojo/_base/declare"], function (declare) {
}); });
}, },
helpDialog: function(topic) { helpDialog: function(topic) {
const query = "backend.php?op=backend&method=help&topic=" + param_escape(topic); const query = "backend.php?op=backend&method=help&topic=" + encodeURIComponent(topic);
if (dijit.byId("helpDlg")) if (dijit.byId("helpDlg"))
dijit.byId("helpDlg").destroyRecursive(); dijit.byId("helpDlg").destroyRecursive();

View File

@ -54,6 +54,8 @@ Array.prototype.remove = function(s) {
} }
}; };
/* common helpers not worthy of separate Dojo modules */
const Lists = { const Lists = {
onRowChecked: function(elem) { onRowChecked: function(elem) {
const checked = elem.domNode ? elem.attr("checked") : elem.checked; const checked = elem.domNode ? elem.attr("checked") : elem.checked;
@ -113,6 +115,31 @@ const Tables = {
} }
}; };
const Cookie = {
set: function (name, value, lifetime) {
const d = new Date();
d.setTime(d.getTime() + lifetime * 1000);
const expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + encodeURIComponent(value) + "; " + expires;
},
get: function (name) {
name = name + "=";
const ca = document.cookie.split(';');
for (let i=0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return decodeURIComponent(c.substring(name.length, c.length));
}
return "";
},
delete: function(name) {
const expires = "expires=Thu, 01-Jan-1970 00:00:01 GMT";
document.cookie = name + "=" + "" + "; " + expires;
}
};
/* error reporting */
function report_error(message, filename, lineno, colno, error) { function report_error(message, filename, lineno, colno, error) {
exception_error(error, null, filename, lineno); exception_error(error, null, filename, lineno);
} }
@ -177,10 +204,6 @@ function exception_error(e, e_compat, filename, lineno, colno) {
} }
function param_escape(arg) {
return encodeURIComponent(arg);
}
function notify_real(msg, no_hide, n_type) { function notify_real(msg, no_hide, n_type) {
const n = $("notify"); const n = $("notify");
@ -263,58 +286,6 @@ function notify_info(msg, no_hide) {
notify_real(msg, no_hide, 4); notify_real(msg, no_hide, 4);
} }
function setCookie(name, value, lifetime, path, domain, secure) {
let d = false;
if (lifetime) {
d = new Date();
d.setTime(d.getTime() + (lifetime * 1000));
}
console.log("setCookie: " + name + " => " + value + ": " + d);
int_setCookie(name, value, d, path, domain, secure);
}
function int_setCookie(name, value, expires, path, domain, secure) {
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function delCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
}
function getCookie(name) {
const dc = document.cookie;
const prefix = name + "=";
let begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else {
begin += 2;
}
let end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
function displayIfChecked(checkbox, elemId) { function displayIfChecked(checkbox, elemId) {
if (checkbox.checked) { if (checkbox.checked) {
@ -324,15 +295,6 @@ function displayIfChecked(checkbox, elemId) {
} }
} }
// noinspection JSUnusedGlobalSymbols
function closeInfoBox() {
const dialog = dijit.byId("infoBox");
if (dialog) dialog.hide();
return false;
}
function getInitParam(key) { function getInitParam(key) {
return init_params[key]; return init_params[key];
} }
@ -407,40 +369,6 @@ function uploadIconHandler(rc) {
} }
} }
// noinspection JSUnusedGlobalSymbols
function removeFeedIcon(id) {
if (confirm(__("Remove stored feed icon?"))) {
notify_progress("Removing feed icon...", true);
const query = { op: "pref-feeds", method: "removeicon", feed_id: id };
xhrPost("backend.php", query, () => {
notify_info("Feed icon removed.");
if (App.isPrefs()) {
Feeds.reload();
} else {
setTimeout('Feeds.reload(false, false)', 50);
}
});
}
return false;
}
// noinspection JSUnusedGlobalSymbols
function uploadFeedIcon() {
const file = $("icon_file");
if (file.value.length == 0) {
alert(__("Please select an image file to upload."));
} else if (confirm(__("Upload new icon for this feed?"))) {
notify_progress("Uploading, please wait...", true);
return true;
}
return false;
}
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
function label_to_feed_id(label) { function label_to_feed_id(label) {

View File

@ -116,9 +116,9 @@ const App = {
Feeds.reload(); Feeds.reload();
Article.close(); Article.close();
if (parseInt(getCookie("ttrss_fh_width")) > 0) { if (parseInt(Cookie.get("ttrss_fh_width")) > 0) {
dijit.byId("feeds-holder").domNode.setStyle( dijit.byId("feeds-holder").domNode.setStyle(
{width: getCookie("ttrss_fh_width") + "px"}); {width: Cookie.get("ttrss_fh_width") + "px"});
} }
dijit.byId("main").resize(); dijit.byId("main").resize();
@ -126,19 +126,19 @@ const App = {
dojo.connect(dijit.byId('feeds-holder'), 'resize', dojo.connect(dijit.byId('feeds-holder'), 'resize',
function (args) { function (args) {
if (args && args.w >= 0) { if (args && args.w >= 0) {
setCookie("ttrss_fh_width", args.w, getInitParam("cookie_lifetime")); Cookie.set("ttrss_fh_width", args.w, getInitParam("cookie_lifetime"));
} }
}); });
dojo.connect(dijit.byId('content-insert'), 'resize', dojo.connect(dijit.byId('content-insert'), 'resize',
function (args) { function (args) {
if (args && args.w >= 0 && args.h >= 0) { if (args && args.w >= 0 && args.h >= 0) {
setCookie("ttrss_ci_width", args.w, getInitParam("cookie_lifetime")); Cookie.set("ttrss_ci_width", args.w, getInitParam("cookie_lifetime"));
setCookie("ttrss_ci_height", args.h, getInitParam("cookie_lifetime")); Cookie.set("ttrss_ci_height", args.h, getInitParam("cookie_lifetime"));
} }
}); });
delCookie("ttrss_test"); Cookie.delete("ttrss_test");
const toolbar = document.forms["main_toolbar_form"]; const toolbar = document.forms["main_toolbar_form"];
@ -172,9 +172,9 @@ const App = {
console.log("second stage ok"); console.log("second stage ok");
}, },
genericSanityCheck: function() { genericSanityCheck: function() {
setCookie("ttrss_test", "TEST"); Cookie.set("ttrss_test", "TEST");
if (getCookie("ttrss_test") != "TEST") { if (Cookie.get("ttrss_test") != "TEST") {
return fatalError(2); return fatalError(2);
} }
@ -224,9 +224,9 @@ const App = {
height: 'auto', height: 'auto',
borderTopWidth: '0px' }); borderTopWidth: '0px' });
if (parseInt(getCookie("ttrss_ci_width")) > 0) { if (parseInt(Cookie.get("ttrss_ci_width")) > 0) {
dijit.byId("content-insert").domNode.setStyle( dijit.byId("content-insert").domNode.setStyle(
{width: getCookie("ttrss_ci_width") + "px" }); {width: Cookie.get("ttrss_ci_width") + "px" });
} }
$("headlines-frame").setStyle({ borderBottomWidth: '0px' }); $("headlines-frame").setStyle({ borderBottomWidth: '0px' });
@ -240,9 +240,9 @@ const App = {
height: '50%', height: '50%',
borderTopWidth: '0px'}); borderTopWidth: '0px'});
if (parseInt(getCookie("ttrss_ci_height")) > 0) { if (parseInt(Cookie.get("ttrss_ci_height")) > 0) {
dijit.byId("content-insert").domNode.setStyle( dijit.byId("content-insert").domNode.setStyle(
{height: getCookie("ttrss_ci_height") + "px" }); {height: Cookie.get("ttrss_ci_height") + "px" });
} }
$("headlines-frame").setStyle({ borderBottomWidth: '1px' }); $("headlines-frame").setStyle({ borderBottomWidth: '1px' });
@ -466,8 +466,8 @@ const App = {
App._widescreen_mode = !App._widescreen_mode; App._widescreen_mode = !App._widescreen_mode;
// reset stored sizes because geometry changed // reset stored sizes because geometry changed
setCookie("ttrss_ci_width", 0); Cookie.set("ttrss_ci_width", 0);
setCookie("ttrss_ci_height", 0); Cookie.set("ttrss_ci_height", 0);
App.switchPanelMode(App._widescreen_mode); App.switchPanelMode(App._widescreen_mode);
} else { } else {
@ -557,8 +557,8 @@ const App = {
App._widescreen_mode = !App._widescreen_mode; App._widescreen_mode = !App._widescreen_mode;
// reset stored sizes because geometry changed // reset stored sizes because geometry changed
setCookie("ttrss_ci_width", 0); Cookie.set("ttrss_ci_width", 0);
setCookie("ttrss_ci_height", 0); Cookie.set("ttrss_ci_height", 0);
App.switchPanelMode(App._widescreen_mode); App.switchPanelMode(App._widescreen_mode);
} else { } else {

View File

@ -1,7 +1,7 @@
function showTrgmRelated(id) { function showTrgmRelated(id) {
try { try {
const query = "backend.php?op=pluginhandler&plugin=af_psql_trgm&method=showrelated&param=" + param_escape(id); const query = "backend.php?op=pluginhandler&plugin=af_psql_trgm&method=showrelated&param=" + encodeURIComponent(id);
if (dijit.byId("trgmRelatedDlg")) if (dijit.byId("trgmRelatedDlg"))
dijit.byId("trgmRelatedDlg").destroyRecursive(); dijit.byId("trgmRelatedDlg").destroyRecursive();

View File

@ -14,7 +14,7 @@ function emailArticle(id) {
if (dijit.byId("emailArticleDlg")) if (dijit.byId("emailArticleDlg"))
dijit.byId("emailArticleDlg").destroyRecursive(); dijit.byId("emailArticleDlg").destroyRecursive();
var query = "backend.php?op=pluginhandler&plugin=mail&method=emailArticle&param=" + param_escape(id); var query = "backend.php?op=pluginhandler&plugin=mail&method=emailArticle&param=" + encodeURIComponent(id);
dialog = new dijit.Dialog({ dialog = new dijit.Dialog({
id: "emailArticleDlg", id: "emailArticleDlg",

View File

@ -14,7 +14,7 @@ function mailtoArticle(id) {
if (dijit.byId("emailArticleDlg")) if (dijit.byId("emailArticleDlg"))
dijit.byId("emailArticleDlg").destroyRecursive(); dijit.byId("emailArticleDlg").destroyRecursive();
const query = "backend.php?op=pluginhandler&plugin=mailto&method=emailArticle&param=" + param_escape(id); const query = "backend.php?op=pluginhandler&plugin=mailto&method=emailArticle&param=" + encodeURIComponent(id);
dialog = new dijit.Dialog({ dialog = new dijit.Dialog({
id: "emailArticleDlg", id: "emailArticleDlg",

View File

@ -1,7 +1,7 @@
function editArticleNote(id) { function editArticleNote(id) {
try { try {
var query = "backend.php?op=pluginhandler&plugin=note&method=edit&param=" + param_escape(id); var query = "backend.php?op=pluginhandler&plugin=note&method=edit&param=" + encodeURIComponent(id);
if (dijit.byId("editNoteDlg")) if (dijit.byId("editNoteDlg"))
dijit.byId("editNoteDlg").destroyRecursive(); dijit.byId("editNoteDlg").destroyRecursive();

View File

@ -3,7 +3,7 @@ function shareArticle(id) {
if (dijit.byId("shareArticleDlg")) if (dijit.byId("shareArticleDlg"))
dijit.byId("shareArticleDlg").destroyRecursive(); dijit.byId("shareArticleDlg").destroyRecursive();
var query = "backend.php?op=pluginhandler&plugin=share&method=shareArticle&param=" + param_escape(id); var query = "backend.php?op=pluginhandler&plugin=share&method=shareArticle&param=" + encodeURIComponent(id);
dialog = new dijit.Dialog({ dialog = new dijit.Dialog({
id: "shareArticleDlg", id: "shareArticleDlg",

View File

@ -57,7 +57,7 @@
<script> <script>
dojoConfig = { dojoConfig = {
async: true, async: true,
cacheBust: new Date(), cacheBust: "<?php echo get_scripts_timestamp(); ?>",
packages: [ packages: [
{ name: "lib", location: "../" }, { name: "lib", location: "../" },
{ name: "fox", location: "../../js" }, { name: "fox", location: "../../js" },