[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/forum/yui/src/subscriptiontoggle/js/ -> toggle.js (source)

   1  // This file is part of Moodle - http://moodle.org/
   2  //
   3  // Moodle is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU General Public License as published by
   5  // the Free Software Foundation, either version 3 of the License, or
   6  // (at your option) any later version.
   7  //
   8  // Moodle is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU General Public License
  14  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  15  
  16  /**
  17   * A utility to check whether the connection to the Moodle server is still
  18   * active.
  19   *
  20   * @module     moodle-core-subscriptiontoggle
  21   * @package    mod_forum
  22   * @copyright  2014 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @main       moodle-mod_forum-subscriptiontoggle
  25   */
  26  
  27  /**
  28   * @namespace M.mod_forum
  29   * @class subscriptiontoggle
  30   */
  31  
  32  function SubscriptionToggle() {
  33      SubscriptionToggle.superclass.constructor.apply(this, arguments);
  34  }
  35  
  36  var LOGNAME = 'moodle-mod_forum-subscriptiontoggle';
  37  
  38  Y.extend(SubscriptionToggle, Y.Base, {
  39      initializer: function() {
  40          Y.delegate('click', this._toggleSubscription, Y.config.doc.body, '.discussionsubscription .discussiontoggle', this);
  41      },
  42      _toggleSubscription: function(e) {
  43          var clickedLink = e.currentTarget;
  44  
  45          Y.io(this.get('uri'), {
  46              data: {
  47                  sesskey: M.cfg.sesskey,
  48                  forumid: clickedLink.getData('forumid'),
  49                  discussionid: clickedLink.getData('discussionid'),
  50                  includetext: clickedLink.getData('includetext')
  51              },
  52              context: this,
  53              'arguments': {
  54                  clickedLink: clickedLink
  55              },
  56              on: {
  57                  complete: this._handleCompletion
  58              }
  59          });
  60  
  61          // Prevent the standard browser behaviour now.
  62          e.preventDefault();
  63      },
  64  
  65      _handleCompletion: function(tid, response, args) {
  66          var responseObject;
  67          // Attempt to parse the response into an object.
  68          try {
  69              responseObject = Y.JSON.parse(response.response);
  70              if (responseObject.error) {
  71                  Y.use('moodle-core-notification-ajaxexception', function() {
  72                      return new M.core.ajaxException(responseObject);
  73                  });
  74                  return this;
  75              }
  76          } catch (error) {
  77              Y.use('moodle-core-notification-exception', function() {
  78                  return new M.core.exception(error);
  79              });
  80              return this;
  81          }
  82  
  83          if (!responseObject.icon) {
  84              Y.log('No icon received. Skipping the current value replacement', 'warn', LOGNAME);
  85              return;
  86          }
  87  
  88          var container = args.clickedLink.ancestor('.discussionsubscription');
  89          if (container) {
  90              // We should just receive the new value for the table.
  91              // Note: We do not need to escape the HTML here as it should be provided sanitised from the JS response already.
  92              container.set('innerHTML', responseObject.icon);
  93          }
  94      }
  95  }, {
  96      NAME: 'subscriptionToggle',
  97      ATTRS: {
  98          /**
  99           * The AJAX endpoint which controls the subscription toggle.
 100           *
 101           * @attribute uri
 102           * @type String
 103           * @default M.cfg.wwwroot + '/mod/forum/subscribe_ajax.php'
 104           */
 105          uri: {
 106              value: M.cfg.wwwroot + '/mod/forum/subscribe_ajax.php'
 107          }
 108      }
 109  });
 110  
 111  var NS = Y.namespace('M.mod_forum.subscriptiontoggle');
 112  NS.init = function(config) {
 113      return new SubscriptionToggle(config);
 114  };


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1