[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lti/amd/src/ -> tool_configure_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   * Standard Ajax wrapper for Moodle. It calls the central Ajax script,
  18   * which can call any existing webservice using the current session.
  19   * In addition, it can batch multiple requests and return multiple responses.
  20   *
  21   * @module     mod_lti/tool_configure_controller
  22   * @class      tool_configure_controller
  23   * @package    mod_lti
  24   * @copyright  2015 Ryan Wyllie <ryan@moodle.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @since      3.1
  27   */
  28  define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/events', 'mod_lti/keys', 'mod_lti/tool_type',
  29          'mod_lti/tool_proxy', 'core/str'],
  30          function($, ajax, notification, templates, ltiEvents, KEYS, toolType, toolProxy, str) {
  31  
  32      var SELECTORS = {
  33          EXTERNAL_REGISTRATION_CONTAINER: '#external-registration-container',
  34          EXTERNAL_REGISTRATION_PAGE_CONTAINER: '#external-registration-page-container',
  35          CARTRIDGE_REGISTRATION_CONTAINER: '#cartridge-registration-container',
  36          CARTRIDGE_REGISTRATION_FORM: '#cartridge-registration-form',
  37          ADD_TOOL_FORM: '#add-tool-form',
  38          TOOL_LIST_CONTAINER: '#tool-list-container',
  39          TOOL_CREATE_BUTTON: '#tool-create-button',
  40          REGISTRATION_CHOICE_CONTAINER: '#registration-choice-container',
  41          TOOL_URL: '#tool-url'
  42      };
  43  
  44      /**
  45       * Get the tool create button element.
  46       *
  47       * @method getToolCreateButton
  48       * @private
  49       * @return {Object} jQuery object
  50       */
  51      var getToolCreateButton = function() {
  52          return $(SELECTORS.TOOL_CREATE_BUTTON);
  53      };
  54  
  55      /**
  56       * Get the tool list container element.
  57       *
  58       * @method getToolListContainer
  59       * @private
  60       * @return {Object} jQuery object
  61       */
  62      var getToolListContainer = function() {
  63          return $(SELECTORS.TOOL_LIST_CONTAINER);
  64      };
  65  
  66      /**
  67       * Get the external registration container element.
  68       *
  69       * @method getExternalRegistrationContainer
  70       * @private
  71       * @return {Object} jQuery object
  72       */
  73      var getExternalRegistrationContainer = function() {
  74          return $(SELECTORS.EXTERNAL_REGISTRATION_CONTAINER);
  75      };
  76  
  77      /**
  78       * Get the cartridge registration container element.
  79       *
  80       * @method getCartridgeRegistrationContainer
  81       * @private
  82       * @return {Object} jQuery object
  83       */
  84      var getCartridgeRegistrationContainer = function() {
  85          return $(SELECTORS.CARTRIDGE_REGISTRATION_CONTAINER);
  86      };
  87  
  88      /**
  89       * Get the registration choice container element.
  90       *
  91       * @method getRegistrationChoiceContainer
  92       * @private
  93       * @return {Object} jQuery object
  94       */
  95      var getRegistrationChoiceContainer = function() {
  96          return $(SELECTORS.REGISTRATION_CHOICE_CONTAINER);
  97      };
  98  
  99      /**
 100       * Get the tool type URL.
 101       *
 102       * @method getToolURL
 103       * @private
 104       * @return {String} the tool type url
 105       */
 106      var getToolURL = function() {
 107          return $(SELECTORS.TOOL_URL).val();
 108      };
 109  
 110      /**
 111       * Hide the external registration container.
 112       *
 113       * @method hideExternalRegistration
 114       * @private
 115       */
 116      var hideExternalRegistration = function() {
 117          getExternalRegistrationContainer().addClass('hidden');
 118      };
 119  
 120      /**
 121       * Hide the cartridge registration container.
 122       *
 123       * @method hideCartridgeRegistration
 124       * @private
 125       */
 126      var hideCartridgeRegistration = function() {
 127          getCartridgeRegistrationContainer().addClass('hidden');
 128      };
 129  
 130      /**
 131       * Hide the registration choice container.
 132       *
 133       * @method hideRegistrationChoices
 134       * @private
 135       */
 136      var hideRegistrationChoices = function() {
 137          getRegistrationChoiceContainer().addClass('hidden');
 138      };
 139  
 140      /**
 141       * Display the external registration panel and hides the other
 142       * panels.
 143       *
 144       * @method showExternalRegistration
 145       * @private
 146       */
 147      var showExternalRegistration = function() {
 148          hideCartridgeRegistration();
 149          hideRegistrationChoices();
 150          getExternalRegistrationContainer().removeClass('hidden');
 151          screenReaderAnnounce(getExternalRegistrationContainer());
 152      };
 153  
 154      /**
 155       * Display the cartridge registration panel and hides the other
 156       * panels.
 157       *
 158       * @method showCartridgeRegistration
 159       * @param {String} url
 160       * @private
 161       */
 162      var showCartridgeRegistration = function(url) {
 163          hideExternalRegistration();
 164          hideRegistrationChoices();
 165          getCartridgeRegistrationContainer().removeClass('hidden');
 166          getCartridgeRegistrationContainer().find(SELECTORS.CARTRIDGE_REGISTRATION_FORM).attr('data-cartridge-url', url);
 167          screenReaderAnnounce(getCartridgeRegistrationContainer());
 168      };
 169  
 170      /**
 171       * JAWS does not notice visibility changes with aria-live.
 172       * Remove and add the content back to force it to read it out.
 173       * This function can be removed once JAWS supports visibility.
 174       *
 175       * @method screenReaderAnnounce
 176       * @param {Object} element
 177       * @private
 178       */
 179      var screenReaderAnnounce = function(element) {
 180          var childClones = element.children().clone(true, true);
 181          element.empty();
 182          element.append(childClones);
 183      };
 184  
 185      /**
 186       * Display the registration choices panel and hides the other
 187       * panels.
 188       *
 189       * @method showRegistrationChoices
 190       * @private
 191       */
 192      var showRegistrationChoices = function() {
 193          hideExternalRegistration();
 194          hideCartridgeRegistration();
 195          getRegistrationChoiceContainer().removeClass('hidden');
 196          screenReaderAnnounce(getRegistrationChoiceContainer());
 197      };
 198  
 199      /**
 200       * Hides the list of tool types.
 201       *
 202       * @method hideToolList
 203       * @private
 204       */
 205      var hideToolList = function() {
 206          getToolListContainer().addClass('hidden');
 207      };
 208  
 209      /**
 210       * Display the list of tool types.
 211       *
 212       * @method hideToolList
 213       * @private
 214       */
 215      var showToolList = function() {
 216          getToolListContainer().removeClass('hidden');
 217      };
 218  
 219      /**
 220       * Display the registration feedback alert and hide the other panels.
 221       *
 222       * @method showRegistrationFeedback
 223       * @param {Object} data
 224       * @private
 225       */
 226      var showRegistrationFeedback = function(data) {
 227          var type = data.error ? 'error' : 'success';
 228          notification.addNotification({
 229              message: data.message,
 230              type: type
 231          });
 232      };
 233  
 234      /**
 235       * Show the loading animation
 236       *
 237       * @method startLoading
 238       * @private
 239       * @param {Object} element jQuery object
 240       */
 241      var startLoading = function(element) {
 242          element.addClass("loading");
 243      };
 244  
 245      /**
 246       * Hide the loading animation
 247       *
 248       * @method stopLoading
 249       * @private
 250       * @param {Object} element jQuery object
 251       */
 252      var stopLoading = function(element) {
 253          element.removeClass("loading");
 254      };
 255  
 256      /**
 257       * Refresh the list of tool types and render the new ones.
 258       *
 259       * @method reloadToolList
 260       * @private
 261       */
 262      var reloadToolList = function() {
 263          var promise = $.Deferred();
 264          var container = getToolListContainer();
 265          startLoading(container);
 266  
 267          $.when(
 268                  toolType.query(),
 269                  toolProxy.query({'orphanedonly': true})
 270              )
 271              .done(function(types, proxies) {
 272                      templates.render('mod_lti/tool_list', {tools: types, proxies: proxies})
 273                          .done(function(html, js) {
 274                                  container.empty();
 275                                  container.append(html);
 276                                  templates.runTemplateJS(js);
 277                                  promise.resolve();
 278                              }).fail(promise.reject);
 279                  })
 280              .fail(promise.reject);
 281  
 282          promise.fail(notification.exception)
 283              .always(function() {
 284                      stopLoading(container);
 285                  });
 286      };
 287  
 288      /**
 289       * Trigger appropriate registration process process for the user input
 290       * URL. It can either be a cartridge or a registration url.
 291       *
 292       * @method addTool
 293       * @private
 294       * @return {Promise} jQuery Deferred object
 295       */
 296      var addTool = function() {
 297          var url = $.trim(getToolURL());
 298  
 299          if (url === "") {
 300              return $.Deferred().resolve();
 301          }
 302  
 303          var toolButton = getToolCreateButton();
 304          startLoading(toolButton);
 305  
 306          var promise = toolType.isCartridge(url);
 307  
 308          promise.always(function() {
 309            stopLoading(toolButton);
 310          });
 311  
 312          promise.done(function(result) {
 313              if (result.iscartridge) {
 314                  $(SELECTORS.TOOL_URL).val('');
 315                  $(document).trigger(ltiEvents.START_CARTRIDGE_REGISTRATION, url);
 316              } else {
 317                  $(document).trigger(ltiEvents.START_EXTERNAL_REGISTRATION, {url: url});
 318              }
 319          });
 320  
 321          promise.fail(function() {
 322              str.get_string('errorbadurl', 'mod_lti')
 323                  .done(function(s) {
 324                          $(document).trigger(ltiEvents.REGISTRATION_FEEDBACK, {
 325                                  message: s,
 326                                  error: true
 327                              });
 328                      })
 329                  .fail(notification.exception);
 330          });
 331  
 332          return promise;
 333      };
 334  
 335      /**
 336       * Sets up the listeners for user interaction on the page.
 337       *
 338       * @method registerEventListeners
 339       * @private
 340       */
 341      var registerEventListeners = function() {
 342  
 343          // These are events fired by the registration processes. Either
 344          // the cartridge registration or the external registration url.
 345          $(document).on(ltiEvents.NEW_TOOL_TYPE, function() {
 346              reloadToolList();
 347          });
 348  
 349          $(document).on(ltiEvents.START_EXTERNAL_REGISTRATION, function() {
 350              showExternalRegistration();
 351              $(SELECTORS.TOOL_URL).val('');
 352              hideToolList();
 353          });
 354  
 355          $(document).on(ltiEvents.STOP_EXTERNAL_REGISTRATION, function() {
 356              showToolList();
 357              showRegistrationChoices();
 358          });
 359  
 360          $(document).on(ltiEvents.START_CARTRIDGE_REGISTRATION, function(event, url) {
 361              showCartridgeRegistration(url);
 362          });
 363  
 364          $(document).on(ltiEvents.STOP_CARTRIDGE_REGISTRATION, function() {
 365              getCartridgeRegistrationContainer().find(SELECTORS.CARTRIDGE_REGISTRATION_FORM).removeAttr('data-cartridge-url');
 366              showRegistrationChoices();
 367          });
 368  
 369          $(document).on(ltiEvents.REGISTRATION_FEEDBACK, function(event, data) {
 370              showRegistrationFeedback(data);
 371          });
 372  
 373          var form = $(SELECTORS.ADD_TOOL_FORM);
 374          form.submit(function(e) {
 375              e.preventDefault();
 376              addTool();
 377          });
 378  
 379      };
 380  
 381      return /** @alias module:mod_lti/cartridge_registration_form */ {
 382  
 383          /**
 384           * Initialise this module.
 385           */
 386          init: function() {
 387              registerEventListeners();
 388              reloadToolList();
 389          }
 390      };
 391  });


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