]>
Commit | Line | Data |
---|---|---|
1 | /** | |
2 | * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | |
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license | |
4 | */ | |
5 | ||
6 | /** | |
7 | * @fileOverview Defines the {@link CKEDITOR.template} class, which represents | |
8 | * an UI template for an editor instance. | |
9 | */ | |
10 | ||
11 | ( function() { | |
12 | var rePlaceholder = /{([^}]+)}/g; | |
13 | ||
14 | /** | |
15 | * Lightweight template used to build the output string from variables. | |
16 | * | |
17 | * // HTML template for presenting a label UI. | |
18 | * var tpl = new CKEDITOR.template( '<div class="{cls}">{label}</div>' ); | |
19 | * alert( tpl.output( { cls: 'cke-label', label: 'foo'} ) ); // '<div class="cke-label">foo</div>' | |
20 | * | |
21 | * @class | |
22 | * @constructor Creates a template class instance. | |
23 | * @param {String} source The template source. | |
24 | */ | |
25 | CKEDITOR.template = function( source ) { | |
26 | /** | |
27 | * The current template source. | |
28 | * | |
29 | * @readonly | |
30 | * @member CKEDITOR.template | |
31 | * @property {String} | |
32 | */ | |
33 | this.source = String( source ); | |
34 | }; | |
35 | ||
36 | /** | |
37 | * Processes the template, filling its variables with the provided data. | |
38 | * | |
39 | * @method | |
40 | * @member CKEDITOR.template | |
41 | * @param {Object} data An object containing properties whose values will be | |
42 | * used to fill the template variables. The property names must match the | |
43 | * template variables names. Variables without matching properties will be | |
44 | * kept untouched. | |
45 | * @param {Array} [buffer] An array that the output data will be pushed into. | |
46 | * The number of entries appended to the array is unknown. | |
47 | * @returns {String/Number} If `buffer` has not been provided, the processed | |
48 | * template output data; otherwise the new length of `buffer`. | |
49 | */ | |
50 | CKEDITOR.template.prototype.output = function( data, buffer ) { | |
51 | var output = this.source.replace( rePlaceholder, function( fullMatch, dataKey ) { | |
52 | return data[ dataKey ] !== undefined ? data[ dataKey ] : fullMatch; | |
53 | } ); | |
54 | ||
55 | return buffer ? buffer.push( output ) : output; | |
56 | }; | |
57 | } )(); |