From a716a27c97f0f9a72f9ccef76aee0b9813755b03 Mon Sep 17 00:00:00 2001 From: emanuele Date: Sat, 11 May 2013 14:53:54 +0200 Subject: [PATCH] And the javascript to handle the notification button Signed-off-by: emanuele --- sources/controllers/Display.controller.php | 3 +- sources/controllers/Notify.controller.php | 14 +++++---- themes/default/Xml.template.php | 5 +-- themes/default/scripts/script.js | 36 ++++++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/sources/controllers/Display.controller.php b/sources/controllers/Display.controller.php index 0a4aa577fc..6e6a1479b1 100644 --- a/sources/controllers/Display.controller.php +++ b/sources/controllers/Display.controller.php @@ -895,10 +895,11 @@ function action_index() } // Build the normal button array. + addJavascriptVar('notification_topic_notice', $context['is_marked_notify'] ? $txt['notification_disable_topic'] : $txt['notification_enable_topic'], true); $context['normal_buttons'] = array( 'reply' => array('test' => 'can_reply', 'text' => 'reply', 'image' => 'reply.png', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';last_msg=' . $context['topic_last_message'], 'active' => true), 'add_poll' => array('test' => 'can_add_poll', 'text' => 'add_poll', 'image' => 'add_poll.png', 'lang' => true, 'url' => $scripturl . '?action=editpoll;add;topic=' . $context['current_topic'] . '.' . $context['start']), - 'notify' => array('test' => 'can_mark_notify', 'text' => $context['is_marked_notify'] ? 'unnotify' : 'notify', 'image' => ($context['is_marked_notify'] ? 'un' : '') . 'notify.png', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . ($context['is_marked_notify'] ? $txt['notification_disable_topic'] : $txt['notification_enable_topic']) . '\');"', 'url' => $scripturl . '?action=notify;sa=' . ($context['is_marked_notify'] ? 'off' : 'on') . ';topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']), + 'notify' => array( 'test' => 'can_mark_notify', 'text' => $context['is_marked_notify'] ? 'unnotify' : 'notify', 'image' => ($context['is_marked_notify'] ? 'un' : '') . 'notify.png', 'lang' => true, 'custom' => 'onclick="return notifyButton(this);"', 'url' => $scripturl . '?action=notify;sa=' . ($context['is_marked_notify'] ? 'off' : 'on') . ';topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']), 'mark_unread' => array('test' => 'can_mark_unread', 'text' => 'mark_unread', 'image' => 'markunread.png', 'lang' => true, 'url' => $scripturl . '?action=markasread;sa=topic;t=' . $context['mark_unread_time'] . ';topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']), 'disregard' => array('test' => 'can_disregard', 'text' => ($context['topic_disregarded'] ? 'un' : '') . 'disregard', 'image' => ($context['topic_disregarded'] ? 'un' : '') . 'disregard.png', 'lang' => true, 'url' => $scripturl . '?action=disregardtopic;topic=' . $context['current_topic'] . '.' . $context['start'] . ';sa=' . ($context['topic_disregarded'] ? 'off' : 'on') . ';' . $context['session_var'] . '=' . $context['session_id']), 'send' => array('test' => 'can_send_topic', 'text' => 'send_topic', 'image' => 'sendtopic.png', 'lang' => true, 'url' => $scripturl . '?action=emailuser;sa=sendtopic;topic=' . $context['current_topic'] . '.0'), diff --git a/sources/controllers/Notify.controller.php b/sources/controllers/Notify.controller.php index eabf2b3dae..83ab233ba3 100644 --- a/sources/controllers/Notify.controller.php +++ b/sources/controllers/Notify.controller.php @@ -93,15 +93,20 @@ function action_notify_api() // Our topic functions are here require_once(SUBSDIR . '/Topic.subs.php'); - // Attempt to turn notifications on. + // Attempt to turn notifications on/off. setTopicNotification($user_info['id'], $topic, $_GET['sa'] == 'on'); $text = $_GET['sa'] == 'on' ? $txt['unnotify'] : $txt['notify']; $url = $scripturl . '?action=notify;sa=' . ($_GET['sa'] == 'on' ? 'off' : 'on') . ';topic=' . $topic . '.' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';api' . (isset($_REQUEST['json']) ? ';json' : ''); + $return = array( + 'text' => $_GET['sa'] == 'on' ? $txt['unnotify'] : $txt['notify'], + 'url' => $scripturl . '?action=notify;sa=' . ($_GET['sa'] == 'on' ? 'off' : 'on') . ';topic=' . $topic . '.' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';api' . (isset($_REQUEST['json']) ? ';json' : ''), + 'confirm' => $_GET['sa'] == 'on' ? $txt['notification_disable_topic'] : $txt['notification_enable_topic'] + ); if (isset($_REQUEST['json'])) { - die(json_encode(array('text' => $text, 'url' => $url))); + die(json_encode($return)); } loadTemplate('Xml'); @@ -109,10 +114,7 @@ function action_notify_api() $context['sub_template'] = 'generic_xml_buttons'; $context['xml_data'] = array( - array( - 'text' => $text, - 'url' => $url, - ), + $return, ); } diff --git a/themes/default/Xml.template.php b/themes/default/Xml.template.php index ffc9c7b3c5..b82cc86159 100644 --- a/themes/default/Xml.template.php +++ b/themes/default/Xml.template.php @@ -339,8 +339,9 @@ function template_generic_xml_buttons() foreach ($context['xml_data'] as $button) echo ' '; echo ' diff --git a/themes/default/scripts/script.js b/themes/default/scripts/script.js index dca7f1c2b1..ba01568531 100644 --- a/themes/default/scripts/script.js +++ b/themes/default/scripts/script.js @@ -1685,3 +1685,39 @@ function doAutoSubmit() setTimeout("doAutoSubmit();", 1000); } + +function toggleButtonAJAX(btn, text_confirm) +{ + ajax_indicator(true); + + var oXMLDoc = getXMLDocument(btn.href + ';api'); + + if (oXMLDoc.responseXML) + { + var text = oXMLDoc.responseXML.getElementsByTagName('elk')[0].getElementsByTagName('text')[0].firstChild.nodeValue.removeEntities(); + var url = oXMLDoc.responseXML.getElementsByTagName('elk')[0].getElementsByTagName('url')[0].firstChild.nodeValue.removeEntities(); + var confirm_elem = oXMLDoc.responseXML.getElementsByTagName('elk')[0].getElementsByTagName('confirm'); + if (confirm_elem.length == 1) + var confirm_text = confirm_elem[0].firstChild.nodeValue.removeEntities(); + + $('.' + btn.className).each(function() { + // @todo: the span should be moved somewhere in themes.js? + $(this).html('' + text + ''); + $(this).attr('href', url); + if (typeof(confirm_text) != 'undefined') + eval(text_confirm + '= \'' + confirm_text.replace(/[\\']/g, '\\$&') + '\''); + }); + } + + ajax_indicator(false); + +} + +function notifyButton(btn) +{ + if (typeof(notification_topic_notice) != 'undefined' && !confirm(notification_topic_notice)) + return false; + + toggleButtonAJAX(btn, 'notification_topic_notice'); + return false; +} \ No newline at end of file