nextcloud/core/js/octemplate.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-04-07 01:24:43 +04:00
/**
* jQuery plugin for micro templates
*
2014-03-01 05:46:27 +04:00
* Strings are automatically escaped, but that can be disabled by setting
* escapeFunction to null.
2013-04-07 01:24:43 +04:00
*
* Usage examples:
*
2013-04-16 00:10:29 +04:00
* var htmlStr = '<p>Bake, uncovered, until the {greasystuff} is melted and the {pasta} is heated through, about {min} minutes.</p>'
* $(htmlStr).octemplate({greasystuff: 'cheese', pasta: 'macaroni', min: 10});
2013-04-07 01:24:43 +04:00
*
* var htmlStr = '<p>Welcome back {user}</p>';
* $(htmlStr).octemplate({user: 'John Q. Public'}, {escapeFunction: null});
*
2014-03-01 05:46:27 +04:00
* Be aware that the target string must be wrapped in an HTML element for the
* plugin to work. The following won't work:
2013-04-16 00:10:29 +04:00
*
* var textStr = 'Welcome back {user}';
* $(textStr).octemplate({user: 'John Q. Public'});
*
2014-03-01 05:46:27 +04:00
* For anything larger than one-liners, you can use a simple $.get() ajax
* request to get the template, or you can embed them it the page using the
* text/template type:
2013-04-07 01:24:43 +04:00
*
* <script id="contactListItemTemplate" type="text/template">
* <tr class="contact" data-id="{id}">
* <td class="name">
* <input type="checkbox" name="id" value="{id}" /><span class="nametext">{name}</span>
* </td>
* <td class="email">
* <a href="mailto:{email}">{email}</a>
* </td>
* <td class="phone">{phone}</td>
* </tr>
* </script>
*
* var $tmpl = $('#contactListItemTemplate');
* var contacts = // fetched in some ajax call
*
* $.each(contacts, function(idx, contact) {
* $contactList.append(
* $tmpl.octemplate({
* id: contact.getId(),
* name: contact.getDisplayName(),
* email: contact.getPreferredEmail(),
* phone: contact.getPreferredPhone(),
* });
* );
* });
*/
(function( $ ) {
/**
* Object Template
* Inspired by micro templating done by e.g. underscore.js
*/
var Template = {
init: function(vars, options, elem) {
// Mix in the passed in options with the default options
this.vars = vars;
this.options = $.extend({},this.options,options);
this.elem = elem;
var self = this;
if(typeof this.options.escapeFunction === 'function') {
2013-08-28 14:50:05 +04:00
var keys = Object.keys(this.vars);
for (var key = 0; key < keys.length; key++) {
if(typeof this.vars[keys[key]] === 'string') {
this.vars[keys[key]] = self.options.escapeFunction(this.vars[keys[key]]);
2013-06-01 18:47:34 +04:00
}
2013-06-01 18:38:35 +04:00
}
2013-04-07 01:24:43 +04:00
}
var _html = this._build(this.vars);
return $(_html);
},
// From stackoverflow.com/questions/1408289/best-way-to-do-variable-interpolation-in-javascript
_build: function(o){
var data = this.elem.attr('type') === 'text/template' ? this.elem.html() : outerHTML(this.elem.get(0));
2013-04-07 01:24:43 +04:00
try {
return data.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
} catch(e) {
2013-10-23 12:53:52 +04:00
console.error(e, 'data:', data);
2013-04-07 01:24:43 +04:00
}
},
options: {
2013-08-15 01:06:43 +04:00
escapeFunction: escapeHTML
2013-04-07 01:24:43 +04:00
}
};
$.fn.octemplate = function(vars, options) {
var vars = vars ? vars : {};
if(this.length) {
var _template = Object.create(Template);
return _template.init(vars, options, this);
}
};
})( jQuery );