PageSourceSearch

https://tartupauluse.ee/javascripts/jquery.mustache.js

js tartupauluse.ee collected 2026-10-02 20:50:48 UTC 6,643 bytes, 214 lines download raw bytes

1/*! jQuery Mustache - v0.2.8 - 2013-06-23
2* https://github.com/jonnyreeves/jquery-Mustache
3* Copyright (c) 2013 Jonny Reeves; Licensed MIT */
4
5/*global jQuery, window */
6(function ($, window) {
7	'use strict';
8
9	var templateMap = {},
10		instance = null,
11		options = {
12			// Should an error be thrown if an attempt is made to render a non-existent template.  If false, the
13			// operation will fail silently.
14			warnOnMissingTemplates: false,
15
16			// Should an error be thrown if an attempt is made to overwrite a template which has already been added.
17			// If true the original template will be overwritten with the new value.
18			allowOverwrite: true,
19
20			// The 'type' attribute which you use to denoate a Mustache Template in the DOM; eg:
21			// `<script type="text/html" id="my-template"></script>`
22			domTemplateType: 'text/html',
23
24			// Specifies the `dataType` attribute used when external templates are loaded.
25			externalTemplateDataType: 'text'
26		};
27
28	function getMustache() {
29		// Lazily retrieve Mustache from the window global if it hasn't been defined by
30		// the User.
31		if (instance === null) {
32			instance = window.Mustache;
33			if (instance === void 0) {
34				$.error("Failed to locate Mustache instance, are you sure it has been loaded?");
35			}
36		}
37		return instance;
38	}
39
40	/**
41	 * @return {boolean} if the supplied templateName has been added.
42	 */
43	function has(templateName) {
44		return templateMap[templateName] !== void 0;
45	}
46
47	/**
48	 * Registers a template so that it can be used by $.Mustache.
49	 *
50	 * @param templateName		A name which uniquely identifies this template.
51	 * @param templateHtml		The HTML which makes us the template; this will be rendered by Mustache when render()
52	 *							is invoked.
53	 * @throws					If options.allowOverwrite is false and the templateName has already been registered.
54	 */
55	function add(templateName, templateHtml) {
56		if (!options.allowOverwrite && has(templateName)) {
57			$.error('TemplateName: ' + templateName + ' is already mapped.');
58			return;
59		}
60		templateMap[templateName] = $.trim(templateHtml);
61	}
62
63	/**
64	 * Adds one or more tempaltes from the DOM using either the supplied templateElementIds or by retrieving all script
65	 * tags of the 'domTemplateType'.  Templates added in this fashion will be registered with their elementId value.
66	 *
67	 * @param [...templateElementIds]	List of element id's present on the DOM which contain templates to be added; 
68	 *									if none are supplied all script tags that are of the same type as the 
69	 *									`options.domTemplateType` configuration value will be added.
70	 */
71	function addFromDom() {
72		var templateElementIds;
73
74		// If no args are supplied, all script blocks will be read from the document.
75		if (arguments.length === 0) {
76			templateElementIds = $('script[type="' + options.domTemplateType + '"]').map(function () {
77				return this.id;
78			});
79		}
80		else {
81			templateElementIds = $.makeArray(arguments);
82		}
83
84		$.each(templateElementIds, function() {
85			var templateElement = document.getElementById(this);
86
87			if (templateElement === null) {
88				$.error('No such elementId: #' + this);
89			}
90			else {
91				add(this, $(templateElement).html());
92			}
93		});
94	}
95
96	/**
97	 * Removes a template, the contents of the removed Template will be returned.
98	 *
99	 * @param templateName		The name of the previously registered Mustache template that you wish to remove.
100	 * @returns					String which represents the raw content of the template.
101	 */
102	function remove(templateName) {
103		var result = templateMap[templateName];
104		delete templateMap[templateName];
105		return result;
106	}
107
108	/**
109	 * Removes all templates and tells Mustache to flush its cache.
110	 */
111	function clear() {
112		templateMap = {};
113		getMustache().clearCache();
114	}
115
116	/**
117	 * Renders a previously added Mustache template using the supplied templateData object.  Note if the supplied
118	 * templateName doesn't exist an empty String will be returned.
119	 */
120	function render(templateName, templateData) {
121		if (!has(templateName)) {
122			if (options.warnOnMissingTemplates) {
123				$.error('No template registered for: ' + templateName);
124			}
125			return '';
126		}
127		return getMustache().to_html(templateMap[templateName], templateData, templateMap);
128	}
129
130	/**
131	 * Loads the external Mustache templates located at the supplied URL and registers them for later use.  This method
132	 * returns a jQuery Promise and also support an `onComplete` callback.
133	 *
134	 * @param url			URL of the external Mustache template file to load.
135	 * @param onComplete	Optional callback function which will be invoked when the templates from the supplied URL
136	 *						have been loaded and are ready for use.
137	 * @returns				jQuery deferred promise which will complete when the templates have been loaded and are
138	 *						ready for use.
139	 */
140	function load(url, onComplete) {
141		return $.ajax({
142				url: url,
143				dataType: options.externalTemplateDataType
144			}).done(function (templates) {
145				$(templates).filter('script').each(function (i, el) {
146					add(el.id, $(el).html());
147				});
148
149				if ($.isFunction(onComplete)) {
150					onComplete();
151				}
152			});
153	}
154
155	/**
156	 * Returns an Array of templateNames which have been registered and can be retrieved via
157	 * $.Mustache.render() or $(element).mustache().
158	 */
159	function templates() {
160		return $.map(templateMap, function (value, key) {
161			return key;
162		});
163	}
164
165	// Expose the public methods on jQuery.Mustache
166	$.Mustache = {
167		options: options,
168		load: load,
169		has: has,
170		add: add,
171		addFromDom: addFromDom,
172		remove: remove,
173		clear: clear,
174		render: render,
175		templates: templates,
176		instance: instance
177	};
178
179	/**
180	 * Renders one or more viewModels into the current jQuery element.
181	 *
182	 * @param templateName	The name of the Mustache template you wish to render, Note that the
183	 *						template must have been previously loaded and / or added.
184	 * @param templateData	One or more JavaScript objects which will be used to render the Mustache
185	 *						template.
186	 * @param options.method	jQuery method to use when rendering, defaults to 'append'.
187	 */
188	$.fn.mustache = function (templateName, templateData, options) {
189		var settings = $.extend({
190			method:	'append'
191		}, options);
192
193		var renderTemplate = function (obj, viewModel) {
194			$(obj)[settings.method](render(templateName, viewModel));
195		};
196
197		return this.each(function () {
198			var element = this;
199
200			// Render a collection of viewModels.
201			if ($.isArray(templateData)) {
202				$.each(templateData, function () {
203					renderTemplate(element, this);
204				});
205			}
206
207			// Render a single viewModel.
208			else {
209				renderTemplate(element, templateData);
210			}
211		});
212	};
213
214}(window.jQuery||window.Zepto, window));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.