Skip to content

Commit

Permalink
Add ability to use JS prompt / confirm instead of modal
Browse files Browse the repository at this point in the history
  • Loading branch information
SoarinFerret committed May 31, 2020
1 parent 5b648a2 commit 4f41d85
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 113 deletions.
15 changes: 15 additions & 0 deletions action.php
Expand Up @@ -35,12 +35,25 @@ class action_plugin_pagebuttons extends DokuWiki_Action_Plugin {
* @param Doku_Event_Handler $controller The plugin controller
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addjsinfo');
$controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addNewPageButton' );
$controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addNewFolderButton' );
$controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addDeleteButton' );
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'actionPage' );
}


/**
* Adds details to JSINFO
*
*/
function addjsinfo($event, $params){
global $JSINFO;
$JSINFO['plugin_pagebuttons'] = array(
'usePrompt' => $this->getConf('usePrompt')
);
}

/**
* Hook for MENU_ITEMS_ASSEMBLY event.
*
Expand Down Expand Up @@ -75,6 +88,7 @@ public function addNewPageButton(Doku_Event $event) {
if (
$event->data['view'] !== 'page'
|| $this->getConf('hideNewPage')
|| !page_exists($ID)
|| !(substr_compare($ID, ":start", -strlen(":start")) === 0)
) {
return;
Expand All @@ -96,6 +110,7 @@ public function addNewFolderButton(Doku_Event $event) {
if (
$event->data['view'] !== 'page'
|| $this->getConf('hideNewFolder')
|| !page_exists($ID)
|| !(substr_compare($ID, ":start", -strlen(":start")) === 0)
) {
return;
Expand Down
3 changes: 2 additions & 1 deletion conf/default.php
Expand Up @@ -6,4 +6,5 @@

$conf['hideDelete'] = 0;
$conf['hideNewPage'] = 0;
$conf['hideNewFolder'] = 0;
$conf['hideNewFolder'] = 0;
$conf['usePrompt'] = 0;
1 change: 1 addition & 0 deletions conf/metadata.php
Expand Up @@ -7,3 +7,4 @@
$meta['hideDelete'] = array('onoff');
$meta['hideNewPage'] = array('onoff');
$meta['hideNewFolder'] = array('onoff');
$meta['usePrompt'] = array('onoff');
1 change: 1 addition & 0 deletions lang/en/settings.php
Expand Up @@ -7,3 +7,4 @@
$lang['hideDelete'] = 'Disable the delete button';
$lang['hideNewPage'] = 'Disable the New Page button';
$lang['hideNewFolder'] = 'Disable the New Folder button';
$lang['usePrompt'] = 'Use JS Prompt method instead of modal - some themes do not properly implement modals';
253 changes: 141 additions & 112 deletions script.js
Expand Up @@ -14,140 +14,169 @@
* @author Damien Regad
*/
jQuery(function() {
console.log(JSINFO['id']);
console.log(JSINFO['namespace']);
var usePrompt = 0;
if(JSINFO && JSINFO['plugin_pagebuttons'] && JSINFO['plugin_pagebuttons']['usePrompt']){
var usePrompt = JSINFO['plugin_pagebuttons']['usePrompt'];
}

jQuery('.deletepagebutton a').click(function(d) {
d.preventDefault();

var submit_url = this.href;
console.log(submit_url);
var $dialog = jQuery(
'<div><span>'
+ LANG.plugins.pagebuttons.delete_confirm
+ '</span></div>'
);
$dialog.dialog({
title: LANG.plugins.pagebuttons.delete_title,
resizable: true,
width: "auto",
height: "auto",
modal: true,
buttons: [
{
text: LANG.plugins.pagebuttons.btn_ok,
click: function () {
$dialog.dialog("close");
console.log(submit_url);
window.location.href = submit_url
}
},
{
text: LANG.plugins.pagebuttons.btn_cancel,
click: function () {
$dialog.dialog("close");
if(usePrompt){
var page = window.confirm(LANG.plugins.pagebuttons.delete_confirm);
if(page == null || page == ''){}
else{
window.location.href = submit_url;
}
}else{
console.log(submit_url);
var $dialog = jQuery(
'<div><span>'
+ LANG.plugins.pagebuttons.delete_confirm
+ '</span></div>'
);
$dialog.dialog({
title: LANG.plugins.pagebuttons.delete_title,
resizable: true,
width: "auto",
height: "auto",
modal: true,
buttons: [
{
text: LANG.plugins.pagebuttons.btn_ok,
click: function () {
$dialog.dialog("close");
console.log(submit_url);
window.location.href = submit_url
}
},
{
text: LANG.plugins.pagebuttons.btn_cancel,
click: function () {
$dialog.dialog("close");
}
}
],
close: function () {
// remove the dialog's HTML
jQuery(this).remove();
// Due to the preventDefault() call, the "Delete page" span
// remains active when the dialog is closed, so we need to
// manually remove focus from it.
document.activeElement.blur();
}
],
close: function () {
// remove the dialog's HTML
jQuery(this).remove();
// Due to the preventDefault() call, the "Delete page" span
// remains active when the dialog is closed, so we need to
// manually remove focus from it.
document.activeElement.blur();
}
});
});
}
});

jQuery('.newfolderbutton a').click(function(f) {
f.preventDefault();

var pre_url = window.location.href.substring(0, window.location.href.indexOf(JSINFO['id'])) + JSINFO['id'].replace(":start","");

console.log(pre_url);
var $dialog = jQuery(
'<div><span>'
+ LANG.plugins.pagebuttons.newfolder_prompt
+ '<br /><input type="text" style="z-index:10000" name="new_folder_name"><br />'
+ '</span></div>'
);
$dialog.dialog({
title: LANG.plugins.pagebuttons.newfolder_title,
resizable: true,
width: "auto",
height: "auto",
modal: true,
buttons: [
{
text: LANG.plugins.pagebuttons.btn_ok,
click: function () {
var folder = document.getElementsByName("new_folder_name")[0].value;
$dialog.dialog("close");
var submit_url = pre_url + ":" + folder + ":start&do=edit";
console.log(submit_url);
window.location.href = submit_url
}
},
{
text: LANG.plugins.pagebuttons.btn_cancel,
click: function () {
$dialog.dialog("close");
if(usePrompt){
var page = window.prompt(LANG.plugins.pagebuttons.newfolder_prompt);
if(page == null || page == ''){}
else{
var submit_url = pre_url + ":" + page + ":start&do=edit";
window.location.href = submit_url;
}
}else{
var $dialog = jQuery(
'<div><span>'
+ LANG.plugins.pagebuttons.newfolder_prompt
+ '<br /><input type="text" style="z-index:10000" name="new_folder_name"><br />'
+ '</span></div>'
);
$dialog.dialog({
title: LANG.plugins.pagebuttons.newfolder_title,
resizable: true,
width: "auto",
height: "auto",
modal: true,
buttons: [
{
text: LANG.plugins.pagebuttons.btn_ok,
click: function () {
var folder = document.getElementsByName("new_folder_name")[0].value;
$dialog.dialog("close");
var submit_url = pre_url + ":" + folder + ":start&do=edit";
console.log(submit_url);
window.location.href = submit_url
}
},
{
text: LANG.plugins.pagebuttons.btn_cancel,
click: function () {
$dialog.dialog("close");
}
}
],
close: function () {
// remove the dialog's HTML
jQuery(this).remove();
// Due to the preventDefault() call, the "Delete page" span
// remains active when the dialog is closed, so we need to
// manually remove focus from it.
document.activeElement.blur();
}
],
close: function () {
// remove the dialog's HTML
jQuery(this).remove();
// Due to the preventDefault() call, the "Delete page" span
// remains active when the dialog is closed, so we need to
// manually remove focus from it.
document.activeElement.blur();
}
});
});
}
});

jQuery('.newpagebutton a').click(function(p) {
p.preventDefault();

var pre_url = window.location.href.substring(0, window.location.href.indexOf(JSINFO['id'])) + JSINFO['id'].replace(":start","");
var $dialog = jQuery(
'<div><span>'
+ LANG.plugins.pagebuttons.newpage_prompt
+ '<br /><input type="text" style="z-index:10000" name="new_page_name"><br />'
+ '</span></div>'
);
$dialog.dialog({
title: LANG.plugins.pagebuttons.newpage_title,
resizable: true,
width: "auto",
height: "auto",
modal: true,
buttons: [
{
text: LANG.plugins.pagebuttons.btn_ok,
click: function () {
var newpage = document.getElementsByName("new_page_name")[0].value;
$dialog.dialog("close");
var submit_url = pre_url + ":" + newpage + "&do=edit";
console.log(submit_url);
window.location.href = submit_url
}
},
{
text: LANG.plugins.pagebuttons.btn_cancel,
click: function () {
$dialog.dialog("close");

if(usePrompt){
var page = window.prompt(LANG.plugins.pagebuttons.newpage_prompt);
if(page == null || page == ''){}
else{
var submit_url = pre_url + ":" + page + "&do=edit";
window.location.href = submit_url;
}
}else{
var $dialog = jQuery(
'<div><span>'
+ LANG.plugins.pagebuttons.newpage_prompt
+ '<br /><input type="text" style="z-index:10000" name="new_page_name"><br />'
+ '</span></div>'
);
$dialog.dialog({
title: LANG.plugins.pagebuttons.newpage_title,
resizable: true,
width: "auto",
height: "auto",
modal: true,
buttons: [
{
text: LANG.plugins.pagebuttons.btn_ok,
click: function () {
var newpage = document.getElementsByName("new_page_name")[0].value;
$dialog.dialog("close");
var submit_url = pre_url + ":" + newpage + "&do=edit";
console.log(submit_url);
window.location.href = submit_url
}
},
{
text: LANG.plugins.pagebuttons.btn_cancel,
click: function () {
$dialog.dialog("close");
}
}
],
close: function () {
// remove the dialog's HTML
jQuery(this).remove();
// Due to the preventDefault() call, the "Delete page" span
// remains active when the dialog is closed, so we need to
// manually remove focus from it.
document.activeElement.blur();
}
],
close: function () {
// remove the dialog's HTML
jQuery(this).remove();
// Due to the preventDefault() call, the "Delete page" span
// remains active when the dialog is closed, so we need to
// manually remove focus from it.
document.activeElement.blur();
}
});
});
}
});
});

0 comments on commit 4f41d85

Please sign in to comment.