[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/amd/src/ -> tag.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   * AJAX helper for the tag management page.
  18   *
  19   * @module     core/tag
  20   * @package    core_tag
  21   * @copyright  2015 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @since      3.0
  24   */
  25  define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'],
  26          function($, ajax, templates, notification, str) {
  27      return /** @alias module:core/tag */ {
  28  
  29          /**
  30           * Initialises tag index page.
  31           *
  32           * @method initTagindexPage
  33           */
  34          initTagindexPage: function() {
  35              // Click handler for changing tag type.
  36              $('body').delegate('.tagarea[data-ta] a[data-quickload=1]', 'click', function(e) {
  37                  e.preventDefault();
  38                  var target = $(this),
  39                      query = target[0].search.replace(/^\?/, ''),
  40                      tagarea = target.closest('.tagarea[data-ta]'),
  41                      args = query.split('&').reduce(function(s, c) {
  42                        var t = c.split('=');
  43                        s[t[0]] = decodeURIComponent(t[1]);
  44                        return s;
  45                      }, {});
  46  
  47                  var promises = ajax.call([{
  48                      methodname: 'core_tag_get_tagindex',
  49                      args: {tagindex: args}
  50                  }], true);
  51  
  52                  $.when.apply($, promises)
  53                      .done(function(data) {
  54                          templates.render('core_tag/index', data).done(function(html) {
  55                              tagarea.replaceWith(html);
  56                          });
  57                      });
  58              });
  59          },
  60  
  61          /**
  62           * Initialises tag management page.
  63           *
  64           * @method initManagePage
  65           */
  66          initManagePage: function() {
  67  
  68              // Set cell 'time modified' to 'now' when any of the element is updated in this row.
  69              $('body').on('updated', '[data-inplaceeditable]', function(e) {
  70                  str.get_string('now').done(function(s) {
  71                      $(e.target).closest('tr').find('td.col-timemodified').html(s);
  72                  });
  73                  if (e.ajaxreturn.itemtype === 'tagflag') {
  74                      var row = $(e.target).closest('tr');
  75                      if (e.ajaxreturn.value === '0') {
  76                          row.removeClass('flagged-tag');
  77                      } else {
  78                          row.addClass('flagged-tag');
  79                      }
  80                  }
  81              });
  82  
  83              // Confirmation for single tag delete link.
  84              $('.tag-management-table').delegate('a.tagdelete', 'click', function(e) {
  85                  e.preventDefault();
  86                  var href = $(this).attr('href');
  87                  str.get_strings([
  88                          {key: 'delete'},
  89                          {key: 'confirmdeletetag', component: 'tag'},
  90                          {key: 'yes'},
  91                          {key: 'no'},
  92                      ]).done(function(s) {
  93                          notification.confirm(s[0], s[1], s[2], s[3], function() {
  94                              window.location.href = href;
  95                          });
  96                      }
  97                  );
  98              });
  99  
 100              // Confirmation for bulk tag delete button.
 101              $("#tag-management-delete").click(function(e) {
 102                  var form = $(this).closest('form').get(0),
 103                      cnt = $(form).find("input[type=checkbox]:checked").length;
 104                  if (!cnt) {
 105                      return;
 106                  }
 107                  var tempElement = $("<input type='hidden'/>").attr('name', this.name);
 108                  e.preventDefault();
 109                  str.get_strings([
 110                          {key: 'delete'},
 111                          {key: 'confirmdeletetags', component: 'tag'},
 112                          {key: 'yes'},
 113                          {key: 'no'},
 114                      ]).done(function(s) {
 115                          notification.confirm(s[0], s[1], s[2], s[3], function() {
 116                              tempElement.appendTo(form);
 117                              form.submit();
 118                          });
 119                      }
 120                  );
 121              });
 122  
 123              // Confirmation for bulk tag combine button.
 124              $("#tag-management-combine").click(function(e) {
 125                  e.preventDefault();
 126                  var form = $(this).closest('form').get(0),
 127                      tags = $(form).find("input[type=checkbox]:checked");
 128                  if (tags.length <= 1) {
 129                      str.get_strings([
 130                          {key: 'combineselected', component: 'tag'},
 131                          {key: 'selectmultipletags', component: 'tag'},
 132                          {key: 'ok'},
 133                      ]).done(function(s) {
 134                              notification.alert(s[0], s[1], s[2]);
 135                          }
 136                      );
 137                      return;
 138                  }
 139                  var tempElement = $("<input type='hidden'/>").attr('name', this.name);
 140                  str.get_strings([
 141                      {key: 'combineselected', component: 'tag'},
 142                      {key: 'selectmaintag', component: 'tag'},
 143                      {key: 'continue'},
 144                      {key: 'cancel'},
 145                  ]).done(function(s) {
 146                      var el = $('<div><form id="combinetags_form" class="form-inline">' +
 147                          '<p class="description"></p><p class="options"></p>' +
 148                          '<p class="mdl-align"><input type="submit" id="combinetags_submit"/>' +
 149                          '<input type="button" id="combinetags_cancel"/></p>' +
 150                          '</form></div>');
 151                      el.find('.description').html(s[1]);
 152                      el.find('#combinetags_submit').attr('value', s[2]);
 153                      el.find('#combinetags_cancel').attr('value', s[3]);
 154                      var fldset = el.find('.options');
 155                      tags.each(function() {
 156                          var tagid = $(this).val(),
 157                              tagname = $('.inplaceeditable[data-itemtype=tagname][data-itemid=' + tagid + ']').attr('data-value');
 158                          fldset.append($('<input type="radio" name="maintag" id="combinetags_maintag_' + tagid + '" value="' +
 159                              tagid + '"/><label for="combinetags_maintag_' + tagid + '">' + tagname + '</label><br>'));
 160                      });
 161                      var panel = new M.core.dialogue({
 162                          draggable: true,
 163                          modal: true,
 164                          closeButton: true,
 165                          headerContent: s[0],
 166                          bodyContent: el.html()
 167                      });
 168                      panel.show();
 169                      $('#combinetags_form input[type=radio]').first().focus().prop('checked', true);
 170                      $('#combinetags_form #combinetags_cancel').on('click', function() {
 171                          panel.destroy();
 172                      });
 173                      $('#combinetags_form').on('submit', function() {
 174                          tempElement.appendTo(form);
 175                          var maintag = $('input[name=maintag]:checked', '#combinetags_form').val();
 176                          $("<input type='hidden'/>").attr('name', 'maintag').attr('value', maintag).appendTo(form);
 177                          form.submit();
 178                          return false;
 179                      });
 180                  });
 181              });
 182  
 183              // When user changes tag name to some name that already exists suggest to combine the tags.
 184              $('body').on('updatefailed', '[data-inplaceeditable][data-itemtype=tagname]', function(e) {
 185                  var exception = e.exception; // The exception object returned by the callback.
 186                  var newvalue = e.newvalue; // The value that user tried to udpated the element to.
 187                  var tagid = $(e.target).attr('data-itemid');
 188                  if (exception.errorcode === 'namesalreadybeeingused') {
 189                      e.preventDefault(); // This will prevent default error dialogue.
 190                      str.get_strings([
 191                          {key: 'nameuseddocombine', component: 'tag'},
 192                          {key: 'yes'},
 193                          {key: 'cancel'},
 194                      ]).done(function(s) {
 195                          notification.confirm(e.message, s[0], s[1], s[2], function() {
 196                              window.location.href = window.location.href + "&newname=" + encodeURIComponent(newvalue) +
 197                                  "&tagid=" + encodeURIComponent(tagid) +
 198                                  '&action=renamecombine&sesskey=' + M.cfg.sesskey;
 199                          });
 200                      });
 201                  }
 202              });
 203  
 204              // Form for adding standard tags.
 205              $('body').on('click', 'a[data-action=addstandardtag]', function(e) {
 206                  e.preventDefault();
 207                  str.get_strings([
 208                      {key: 'addotags', component: 'tag'},
 209                      {key: 'inputstandardtags', component: 'tag'},
 210                      {key: 'continue'},
 211                      {key: 'cancel'},
 212                  ]).done(function(s) {
 213                      var el = $('<div><form id="addtags_form" class="form-inline" method="POST">' +
 214                          '<input type="hidden" name="action" value="addstandardtag"/>' +
 215                          '<input type="hidden" name="sesskey" value="' + M.cfg.sesskey + '"/>' +
 216                          '<p><label for="id_tagslist">' + s[1] + '</label>' +
 217                          '<input type="text" id="id_tagslist" name="tagslist"/></p>' +
 218                          '<p class="mdl-align"><input type="submit" id="addtags_submit"/>' +
 219                          '<input type="button" id="addtags_cancel"/></p>' +
 220                          '</form></div>');
 221                      el.find('#addtags_form').attr('action', window.location.href);
 222                      el.find('#addtags_submit').attr('value', s[2]);
 223                      el.find('#addtags_cancel').attr('value', s[3]);
 224                      var panel = new M.core.dialogue({
 225                          draggable: true,
 226                          modal: true,
 227                          closeButton: true,
 228                          headerContent: s[0],
 229                          bodyContent: el.html()
 230                      });
 231                      panel.show();
 232                      $('#addtags_form input[type=text]').focus();
 233                      $('#addtags_form #addtags_cancel').on('click', function() {
 234                          panel.destroy();
 235                      });
 236                  });
 237              });
 238          },
 239  
 240          /**
 241           * Initialises tag collection management page.
 242           *
 243           * @method initManageCollectionsPage
 244           */
 245          initManageCollectionsPage: function() {
 246              $('body').on('updated', '[data-inplaceeditable]', function(e) {
 247                  var ajaxreturn = e.ajaxreturn,
 248                      areaid, collid, isenabled;
 249                  if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
 250                      areaid = $(this).attr('data-itemid');
 251                      $(".tag-collections-table ul[data-collectionid] li[data-areaid=" + areaid + "]").addClass('hidden');
 252                      isenabled = ajaxreturn.value;
 253                      if (isenabled === '1') {
 254                          $(this).closest('tr').removeClass('dimmed_text');
 255                          collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
 256                          $(".tag-collections-table ul[data-collectionid=" + collid + "] li[data-areaid=" + areaid + "]")
 257                                  .removeClass('hidden');
 258                      } else {
 259                          $(this).closest('tr').addClass('dimmed_text');
 260                      }
 261                  }
 262                  if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
 263                      areaid = $(this).attr('data-itemid');
 264                      $(".tag-collections-table ul[data-collectionid] li[data-areaid=" + areaid + "]").addClass('hidden');
 265                      collid = $(this).attr('data-value');
 266                      isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
 267                      if (isenabled === "1") {
 268                          $(".tag-collections-table ul[data-collectionid=" + collid + "] li[data-areaid=" + areaid + "]")
 269                                  .removeClass('hidden');
 270                      }
 271                  }
 272              });
 273  
 274              $('body').on('click', '.addtagcoll > a', function(e) {
 275                  e.preventDefault();
 276                  var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
 277                  str.get_strings([
 278                          {key: 'addtagcoll', component: 'tag'},
 279                          {key: 'name'},
 280                          {key: 'searchable', component: 'tag'},
 281                          {key: 'create'},
 282                          {key: 'cancel'},
 283                      ]).done(function(s) {
 284                          var el = $('<div><form id="addtagcoll_form" class="form-inline">' +
 285                              '<p><label for="addtagcoll_name"></label>: ' +
 286                              '<input id="addtagcoll_name" type="text"/></p>' +
 287                              '<p><label for="addtagcoll_searchable"></label>: ' +
 288                              '<input id="addtagcoll_searchable" type="checkbox" value="1" checked/></p>' +
 289                              '<p class="mdl-align"><input type="submit" id="addtagcoll_submit"/>' +
 290                              '<input type="button" id="addtagcoll_cancel"/></p>' +
 291                              '</form></div>');
 292                          el.find('label[for="addtagcoll_name"]').html(s[1]);
 293                          el.find('label[for="addtagcoll_searchable"]').html(s[2]);
 294                          el.find('#addtagcoll_submit').attr('value', s[3]);
 295                          el.find('#addtagcoll_cancel').attr('value', s[4]);
 296                          var panel = new M.core.dialogue({
 297                              draggable: true,
 298                              modal: true,
 299                              closeButton: true,
 300                              headerContent: s[0],
 301                              bodyContent: el.html()
 302                          });
 303                          panel.show();
 304                          $('#addtagcoll_form #addtagcoll_name').focus();
 305                          $('#addtagcoll_form #addtagcoll_cancel').on('click', function() {
 306                              panel.destroy();
 307                          });
 308                          $('#addtagcoll_form').on('submit', function() {
 309                              var name = $('#addtagcoll_form #addtagcoll_name').val();
 310                              var searchable = $('#addtagcoll_form #addtagcoll_searchable').prop('checked') ? 1 : 0;
 311                              if (String(name).length > 0) {
 312                                  window.location.href = href + "&name=" + encodeURIComponent(name) + "&searchable=" + searchable;
 313                              }
 314                              return false;
 315                          });
 316                      }
 317                  );
 318  
 319              });
 320  
 321              $('body').on('click', '.tag-collections-table .action_delete', function(e) {
 322                  e.preventDefault();
 323                  var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
 324                  str.get_strings([
 325                          {key: 'delete'},
 326                          {key: 'suredeletecoll', component: 'tag', param: $(this).attr('data-collname')},
 327                          {key: 'yes'},
 328                          {key: 'no'},
 329                      ]).done(function(s) {
 330                          notification.confirm(s[0], s[1], s[2], s[3], function() {
 331                              window.location.href = href;
 332                          });
 333                      }
 334                  );
 335              });
 336          }
 337      };
 338  });


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