[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lti/amd/src/ -> cartridge_registration_form.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   * Encapsules the behavior for creating a tool type from a cartridge URL
  18   * in Moodle. Manages the UI while operations are occuring.
  19   *
  20   * See template: mod_lti/cartridge_registration_form
  21   *
  22   * @module     mod_lti/cartridge_registration_form
  23   * @class      cartridge_registration_form
  24   * @package    mod_lti
  25   * @copyright  2015 Ryan Wyllie <ryan@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', 'mod_lti/tool_type', 'mod_lti/events', 'mod_lti/keys', 'core/str'],
  30          function($, ajax, notification, toolType, ltiEvents, KEYS, str) {
  31  
  32      var SELECTORS = {
  33          CARTRIDGE_URL: '#cartridge-url',
  34          CONSUMER_KEY: '#registration-key',
  35          SHARED_SECRET: '#registration-secret',
  36          REGISTRATION_FORM: '#cartridge-registration-form',
  37          REGISTRATION_SUBMIT_BUTTON: '#cartridge-registration-submit',
  38          REGISTRATION_CANCEL_BUTTON: '#cartridge-registration-cancel',
  39      };
  40  
  41      /**
  42       * Return the URL the user entered for the cartridge.
  43       *
  44       * @method getCartridgeURL
  45       * @private
  46       * @return {String}
  47       */
  48      var getCartridgeURL = function() {
  49          return $(SELECTORS.REGISTRATION_FORM).attr('data-cartridge-url');
  50      };
  51  
  52      /**
  53       * Return the submit button element.
  54       *
  55       * @method getSubmitButton
  56       * @private
  57       * @return {JQuery} jQuery object
  58       */
  59      var getSubmitButton = function() {
  60          return $(SELECTORS.REGISTRATION_SUBMIT_BUTTON);
  61      };
  62  
  63      /**
  64       * Return the cancel button element.
  65       *
  66       * @method getCancelButton
  67       * @private
  68       * @return {JQuery} jQuery object
  69       */
  70      var getCancelButton = function() {
  71          return $(SELECTORS.REGISTRATION_CANCEL_BUTTON);
  72      };
  73  
  74      /**
  75       * Return the value that the user entered for the consumer key.
  76       *
  77       * @method getConsumerKey
  78       * @private
  79       * @return {String} the value entered for consumer key.
  80       */
  81      var getConsumerKey = function() {
  82          return $(SELECTORS.CONSUMER_KEY).val();
  83      };
  84  
  85      /**
  86       * Return the value that the user entered for the shared secret.
  87       *
  88       * @method getSharedSecret
  89       * @private
  90       * @return {String} the value entered for shared secret
  91       */
  92      var getSharedSecret = function() {
  93          return $(SELECTORS.SHARED_SECRET).val();
  94      };
  95  
  96      /**
  97       * Trigger a visual loading indicator.
  98       *
  99       * @method startLoading
 100       * @private
 101       */
 102      var startLoading = function() {
 103          getSubmitButton().addClass('loading');
 104      };
 105  
 106      /**
 107       * Stop the visual loading indicator.
 108       *
 109       * @method stopLoading
 110       * @private
 111       */
 112      var stopLoading = function() {
 113          getSubmitButton().removeClass('loading');
 114      };
 115  
 116      /**
 117       * Check if the page is currently loading.
 118       *
 119       * @method isLoading
 120       * @private
 121       * @return {Boolean}
 122       */
 123      var isLoading = function() {
 124          return getSubmitButton().hasClass('loading');
 125      };
 126  
 127      /**
 128       * Create a tool type from the cartridge URL that the user input. This will
 129       * send an ajax request to the Moodle server to create the Type. The request will
 130       * include the consumer key and secret, if any.
 131       *
 132       * On success the page will be re-rendered to take the user back to the original
 133       * page with the list of tools and an alert notifying them of success.
 134       *
 135       * @method submitCartridgeURL
 136       * @private
 137       * @return {Promise} jQuery Deferred object
 138       */
 139      var submitCartridgeURL = function() {
 140          if (isLoading()) {
 141              return false;
 142          }
 143  
 144          var url = getCartridgeURL();
 145          // No URL? Do nothing.
 146          if (url === "") {
 147              return false;
 148          }
 149  
 150          startLoading();
 151          var consumerKey = getConsumerKey();
 152          var sharedSecret = getSharedSecret();
 153          var promise = toolType.create({cartridgeurl: url, key: consumerKey, secret: sharedSecret});
 154  
 155          promise.done(function() {
 156              str.get_string('successfullycreatedtooltype', 'mod_lti').done(function(s) {
 157                  $(document).trigger(ltiEvents.NEW_TOOL_TYPE);
 158                  $(document).trigger(ltiEvents.STOP_CARTRIDGE_REGISTRATION);
 159                  $(document).trigger(ltiEvents.REGISTRATION_FEEDBACK, {
 160                      message: s
 161                  });
 162              }).fail(notification.exception);
 163          }).fail(function() {
 164              str.get_string('failedtocreatetooltype', 'mod_lti').done(function(s) {
 165                  $(document).trigger(ltiEvents.NEW_TOOL_TYPE);
 166                  $(document).trigger(ltiEvents.STOP_CARTRIDGE_REGISTRATION);
 167                  $(document).trigger(ltiEvents.REGISTRATION_FEEDBACK, {
 168                      message: s,
 169                      error: true
 170                  });
 171              }).fail(notification.exception);
 172          }).always(function() {
 173            stopLoading();
 174          });
 175  
 176          return promise;
 177      };
 178  
 179      /**
 180       * Sets up the listeners for user interaction on the page.
 181       *
 182       * @method registerEventListeners
 183       * @private
 184       */
 185      var registerEventListeners = function() {
 186          var form = $(SELECTORS.REGISTRATION_FORM);
 187          form.submit(function(e) {
 188              e.preventDefault();
 189              submitCartridgeURL();
 190          });
 191  
 192          var cancelButton = getCancelButton();
 193          cancelButton.click(function(e) {
 194              e.preventDefault();
 195              $(document).trigger(ltiEvents.STOP_CARTRIDGE_REGISTRATION);
 196          });
 197          cancelButton.keypress(function(e) {
 198              if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
 199                  if (e.keyCode == KEYS.ENTER || e.keyCode == KEYS.SPACE) {
 200                      e.preventDefault();
 201                      cancelButton.click();
 202                  }
 203              }
 204          });
 205      };
 206  
 207      return /** @alias module:mod_lti/cartridge_registration_form */ {
 208  
 209          /**
 210           * Initialise this module.
 211           */
 212          init: function() {
 213              registerEventListeners();
 214          }
 215      };
 216  });


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