MDL-50783 Ajax: Configure how to call a webservice through db/service.php
[moodle.git] / admin / tool / templatelibrary / amd / src / display.js
CommitLineData
274d79c9
DW
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 */
24define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates', 'core/config', 'core/str'],
25 function($, ajax, log, notification, templates, config, str) {
26
27 /**
917bd02b 28 * Search through a template for a template docs comment.
274d79c9 29 *
917bd02b
DW
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
274d79c9 33 */
917bd02b 34 var findDocsSection = function(templateSource, templateName) {
274d79c9 35
1fcad843 36 // Find the comment section marked with @template component/template.
917bd02b
DW
37 var marker = "@template " + templateName,
38 i = 0,
39 sections = [];
274d79c9 40
917bd02b 41 sections = templateSource.match(/{{!([\s\S]*?)}}/g);
274d79c9 42
274d79c9
DW
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];
1fcad843
DW
47 var start = section.indexOf(marker);
48 if (start !== -1) {
274d79c9 49 // Remove {{! and }} from start and end.
1fcad843
DW
50 var offset = start + marker.length + 1;
51 section = section.substr(offset, section.length - 2 - offset);
917bd02b 52 return section;
274d79c9
DW
53 }
54 }
55 }
917bd02b
DW
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 }
274d79c9
DW
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 $('[data-region="displaytemplateexample"]').empty();
103 $('[data-region="displaytemplateexample"]').append(html);
104 templates.runTemplateJS(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.
917bd02b 115 *
274d79c9
DW
116 * @param {String} templateName
117 */
118 var loadTemplate = function(templateName) {
119 var parts = templateName.split('/');
120 var component = parts.shift();
121 var name = parts.shift();
122
917bd02b 123 var promises = ajax.call([{
274d79c9
DW
124 methodname: 'core_output_load_template',
125 args:{
126 component: component,
127 template: name,
128 themename: config.theme
917bd02b
DW
129 }
130 }, {
131 methodname: 'tool_templatelibrary_load_canonical_template',
132 args:{
133 component: component,
134 template: name
135 }
ba224fb4 136 }], true, false);
917bd02b
DW
137
138 // When returns a new promise that is resolved when all the passed in promises are resolved.
139 // The arguments to the done become the values of each resolved promise.
140 $.when.apply($, promises)
141 .done( function(source, originalSource) { templateLoaded(templateName, source, originalSource); })
142 .fail(notification.exception);
274d79c9
DW
143 };
144
145 // Add the event listeners.
146 $('[data-region="list-templates"]').on('click', '[data-templatename]', function() {
147 var templatename = $(this).data('templatename');
148 loadTemplate(templatename);
149 });
150
151 // This module does not expose anything.
152 return {};
153});