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