[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/templatelibrary/amd/src/ -> display.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   * This module adds ajax display functions to the template library page.
  18   *
  19   * @module     tool_templatelibrary/display
  20   * @package    tool_templatelibrary
  21   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates', 'core/config', 'core/str'],
  25         function($, ajax, log, notification, templates, config, str) {
  26  
  27      /**
  28       * Search through a template for a template docs comment.
  29       *
  30       * @param {String} templateSource The raw template
  31       * @param {String} templateName The name of the template used to search for docs tag
  32       * @return {String|boolean} the correct comment or false
  33       */
  34      var findDocsSection = function(templateSource, templateName) {
  35  
  36          // Find the comment section marked with @template component/template.
  37          var marker = "@template " + templateName,
  38              i = 0,
  39              sections = [];
  40  
  41          sections = templateSource.match(/{{!([\s\S]*?)}}/g);
  42  
  43          // If no sections match - show the entire file.
  44          if (sections !== null) {
  45              for (i = 0; i < sections.length; i++) {
  46                  var section = sections[i];
  47                  var start = section.indexOf(marker);
  48                  if (start !== -1) {
  49                      // Remove {{! and }} from start and end.
  50                      var offset = start + marker.length + 1;
  51                      section = section.substr(offset, section.length - 2 - offset);
  52                      return section;
  53                  }
  54              }
  55          }
  56          // No matching comment.
  57          return false;
  58      };
  59  
  60      /**
  61       * Handle a template loaded response.
  62       *
  63       * @param {String} templateName The template name
  64       * @param {String} source The template source
  65       * @param {String} originalSource The original template source (not theme overridden)
  66       */
  67      var templateLoaded = function(templateName, source, originalSource) {
  68          str.get_string('templateselected', 'tool_templatelibrary', templateName).done(function(s) {
  69              $('[data-region="displaytemplateheader"]').text(s);
  70          }).fail(notification.exception);
  71  
  72          // Find the comment section marked with @template component/template.
  73          var docs = findDocsSection(source, templateName);
  74  
  75          if (docs === false) {
  76              // Docs was not in theme template, try original.
  77              docs = findDocsSection(originalSource, templateName);
  78          }
  79  
  80          // If we found a docs section, limit the template library to showing this section.
  81          if (docs) {
  82              source = docs;
  83          }
  84  
  85          $('[data-region="displaytemplatesource"]').text(source);
  86  
  87          // Now search the text for a json example.
  88  
  89          var example = source.match(/Example context \(json\):([\s\S]*)/);
  90          var context = false;
  91          if (example) {
  92              var rawJSON = example[1].trim();
  93              try {
  94                  context = $.parseJSON(rawJSON);
  95              } catch (e) {
  96                  log.debug('Could not parse json example context for template.');
  97                  log.debug(e);
  98              }
  99          }
 100          if (context) {
 101              templates.render(templateName, context).done(function(html, js) {
 102                  templates.replaceNodeContents($('[data-region="displaytemplateexample"]'), html, js);
 103              }).fail(notification.exception);
 104          } else {
 105              str.get_string('templatehasnoexample', 'tool_templatelibrary').done(function(s) {
 106                  $('[data-region="displaytemplateexample"]').text(s);
 107              }).fail(notification.exception);
 108          }
 109      };
 110  
 111      /**
 112       * Load the a template source from Moodle.
 113       *
 114       * @param {String} templateName
 115       */
 116      var loadTemplate = function(templateName) {
 117          var parts = templateName.split('/');
 118          var component = parts.shift();
 119          var name = parts.shift();
 120  
 121          var promises = ajax.call([{
 122              methodname: 'core_output_load_template',
 123              args: {
 124                      component: component,
 125                      template: name,
 126                      themename: config.theme
 127              }
 128          }, {
 129              methodname: 'tool_templatelibrary_load_canonical_template',
 130              args: {
 131                      component: component,
 132                      template: name
 133              }
 134          }], true, false);
 135  
 136          // When returns a new promise that is resolved when all the passed in promises are resolved.
 137          // The arguments to the done become the values of each resolved promise.
 138          $.when.apply($, promises)
 139              .done(function(source, originalSource) {
 140                templateLoaded(templateName, source, originalSource);
 141              })
 142              .fail(notification.exception);
 143      };
 144  
 145      // Add the event listeners.
 146      $('[data-region="list-templates"]').on('click', '[data-templatename]', function(e) {
 147          var templatename = $(this).data('templatename');
 148          e.preventDefault();
 149          loadTemplate(templatename);
 150      });
 151  
 152      // This module does not expose anything.
 153      return {};
 154  });


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