[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lti/amd/src/ -> tool_proxy_card_controller.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   * Controls all of the behaviour and interaction with a tool type card. These are
  18   * listed on the LTI tool type management page.
  19   *
  20   * See template: mod_lti/tool_proxy_card
  21   *
  22   * @module     mod_lti/tool_proxy_card_controller
  23   * @class      tool_card_controller
  24   * @package    mod_lti
  25   * @copyright  2016 John Okely <john@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   * @since      3.1
  28   */
  29  define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/tool_proxy', 'mod_lti/events', 'mod_lti/keys',
  30          'core/str'],
  31          function($, ajax, notification, templates, toolProxy, ltiEvents, KEYS, str) {
  32  
  33      var SELECTORS = {
  34          DELETE_BUTTON: '.delete',
  35          CAPABILITIES_CONTAINER: '.capabilities-container',
  36          ACTIVATE_BUTTON: '.tool-card-footer a.activate',
  37      };
  38  
  39      // Timeout in seconds.
  40      var ANNOUNCEMENT_TIMEOUT = 2000;
  41  
  42      /**
  43       * Return the delete button element.
  44       *
  45       * @method getDeleteButton
  46       * @private
  47       * @param {JQuery} element jQuery object representing the tool card.
  48       * @return {JQuery} jQuery object
  49       */
  50      var getDeleteButton = function(element) {
  51          return element.find(SELECTORS.DELETE_BUTTON);
  52      };
  53  
  54      /**
  55       * Return the activate button for the type.
  56       *
  57       * @method getActivateButton
  58       * @private
  59       * @param {JQuery} element jQuery object representing the tool card.
  60       * @return {JQuery}  jQuery object
  61       */
  62      var getActivateButton = function(element) {
  63          return element.find(SELECTORS.ACTIVATE_BUTTON);
  64      };
  65  
  66      /**
  67       * Get the type id.
  68       *
  69       * @method getTypeId
  70       * @private
  71       * @param {JQuery} element jQuery object representing the tool card.
  72       * @return {String} Type ID
  73       */
  74      var getTypeId = function(element) {
  75          return element.attr('data-proxy-id');
  76      };
  77  
  78      /**
  79       * Stop any announcement currently visible on the card.
  80       *
  81       * @method clearAllAnnouncements
  82       * @private
  83       * @param {JQuery} element jQuery object representing the tool card.
  84       */
  85      var clearAllAnnouncements = function(element) {
  86          element.removeClass('announcement loading success fail capabilities');
  87      };
  88  
  89      /**
  90       * Show the loading announcement.
  91       *
  92       * @method startLoading
  93       * @private
  94       * @param {JQuery} element jQuery object representing the tool card.
  95       */
  96      var startLoading = function(element) {
  97          clearAllAnnouncements(element);
  98          element.addClass('announcement loading');
  99      };
 100  
 101      /**
 102       * Hide the loading announcement.
 103       *
 104       * @method stopLoading
 105       * @private
 106       * @param {JQuery} element jQuery object representing the tool card.
 107       */
 108      var stopLoading = function(element) {
 109          element.removeClass('announcement loading');
 110      };
 111  
 112      /**
 113       * Show the success announcement. The announcement is only
 114       * visible for 2 seconds.
 115       *
 116       * @method announceSuccess
 117       * @private
 118       * @param {JQuery} element jQuery object representing the tool card.
 119       * @return {Promise} jQuery Deferred object
 120       */
 121      var announceSuccess = function(element) {
 122          var promise = $.Deferred();
 123  
 124          clearAllAnnouncements(element);
 125          element.addClass('announcement success');
 126          setTimeout(function() {
 127              element.removeClass('announcement success');
 128              promise.resolve();
 129          }, ANNOUNCEMENT_TIMEOUT);
 130  
 131          return promise;
 132      };
 133  
 134      /**
 135       * Show the failure announcement. The announcement is only
 136       * visible for 2 seconds.
 137       *
 138       * @method announceFailure
 139       * @private
 140       * @param {JQuery} element jQuery object representing the tool card.
 141       * @return {Promise} jQuery Deferred object
 142       */
 143      var announceFailure = function(element) {
 144          var promise = $.Deferred();
 145  
 146          clearAllAnnouncements(element);
 147          element.addClass('announcement fail');
 148          setTimeout(function() {
 149                  element.removeClass('announcement fail');
 150                  promise.resolve();
 151              }, ANNOUNCEMENT_TIMEOUT);
 152  
 153          return promise;
 154      };
 155  
 156      /**
 157       * Delete the tool type from the Moodle server. Triggers a success
 158       * or failure announcement depending on the result.
 159       *
 160       * @method deleteType
 161       * @private
 162       * @param {JQuery} element jQuery object representing the tool card.
 163       * @return {Promise} jQuery Deferred object
 164       */
 165      var deleteType = function(element) {
 166          var promise = $.Deferred();
 167          var typeId = getTypeId(element);
 168          startLoading(element);
 169  
 170          if (typeId === "") {
 171              return $.Deferred().resolve();
 172          }
 173  
 174          str.get_strings([
 175                  {
 176                      key: 'delete',
 177                      component: 'mod_lti'
 178                  },
 179                  {
 180                      key: 'delete_confirmation',
 181                      component: 'mod_lti'
 182                  },
 183                  {
 184                      key: 'delete',
 185                      component: 'mod_lti'
 186                  },
 187                  {
 188                      key: 'cancel',
 189                      component: 'core'
 190                  },
 191              ])
 192              .done(function(strs) {
 193                      notification.confirm(strs[0], strs[1], strs[2], strs[3], function() {
 194                              toolProxy.delete(typeId)
 195                                  .done(function() {
 196                                          stopLoading(element);
 197                                          announceSuccess(element)
 198                                              .done(function() {
 199                                                      element.remove();
 200                                                      promise.resolve();
 201                                                  })
 202                                              .fail(notification.exception);
 203                                      })
 204                                  .fail(function(error) {
 205                                          announceFailure(element);
 206                                          promise.reject(error);
 207                                      });
 208                      }, function() {
 209                              stopLoading(element);
 210                              promise.resolve();
 211                          });
 212                  })
 213              .fail(function(error) {
 214                      stopLoading(element);
 215                      notification.exception(error);
 216                      promise.reject(error);
 217                  });
 218  
 219          return promise;
 220      };
 221  
 222      /**
 223       * The user wishes to activate this tool so show them the capabilities that
 224       * they need to agree to or if there are none then set the tool type's state
 225       * to active.
 226       *
 227       * @method activateToolType
 228       * @private
 229       * @param {JQuery} element jQuery object representing the tool card.
 230       */
 231      var activateToolType = function(element) {
 232          var data = {proxyid: getTypeId(element)};
 233          $(document).trigger(ltiEvents.START_EXTERNAL_REGISTRATION, data);
 234      };
 235  
 236      /**
 237       * Sets up the listeners for user interaction on this tool type card.
 238       *
 239       * @method registerEventListeners
 240       * @private
 241       * @param {JQuery} element jQuery object representing the tool card.
 242       */
 243      var registerEventListeners = function(element) {
 244          var deleteButton = getDeleteButton(element);
 245          deleteButton.click(function(e) {
 246                  e.preventDefault();
 247                  deleteType(element);
 248              });
 249          deleteButton.keypress(function(e) {
 250                  if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
 251                      if (e.keyCode == KEYS.ENTER || e.keyCode == KEYS.SPACE) {
 252                          e.preventDefault();
 253                          deleteButton.click();
 254                      }
 255                  }
 256              });
 257  
 258          var activateButton = getActivateButton(element);
 259          activateButton.click(function(e) {
 260                  e.preventDefault();
 261                  activateToolType(element);
 262              });
 263          activateButton.keypress(function(e) {
 264                  if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
 265                      if (e.keyCode == KEYS.ENTER || e.keyCode == KEYS.SPACE) {
 266                          e.preventDefault();
 267                          activateButton.click();
 268                      }
 269                  }
 270              });
 271      };
 272  
 273      return /** @alias module:mod_lti/tool_card_controller */ {
 274  
 275          /**
 276           * Initialise this module.
 277           *
 278           * @param {JQuery} element jQuery object representing the tool card.
 279           */
 280          init: function(element) {
 281              registerEventListeners(element);
 282          }
 283      };
 284  });


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