annotate src/implab/text/template-compile.js @ 12:23be39fd3851

fixed typo in template-compile the built-in function 'print'
author cin
date Thu, 24 Aug 2017 04:30:11 +0300
parents f0035923ff3e
children 7c22fc01fcec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 [ "dojo/request", "./format" ],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 function(request, format) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 var compile = function(str) {
12
23be39fd3851 fixed typo in template-compile the built-in function 'print'
cin
parents: 8
diff changeset
5 var code = "var p=[],print=function(){p.push(format.apply(null,arguments));};" +
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 // Introduce the data as local variables using with(){}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 "with(obj){p.push('" +
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 // Convert the template into pure JavaScript
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 str.replace(/[\r\t\n]/g, " ").split("<%").join("\t").replace(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 /(^|%>)[^\t]*/g,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 function(x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 return x.replace(/('|\\)/g, "\\$1");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 }).replace(/\t=(.*?)%>/g, "',$1,'").split("\t").join("');")
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 .split("%>").join("p.push('") + "');}return p.join('');";
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 /* jshint -W054 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 var compiled = new Function("obj, format, nls", code);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 * Функция форматирования по шаблону
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 * @type{Function}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 * @param{Object} obj объект с параметрами для подстановки
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 return function(obj) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 return compiled(obj || {}, format);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 } catch (e) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 if (console && console.error) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 console.error(code);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 var cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 compile.load = function(id, require, callback) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 var url = require.toUrl(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 if (url in cache) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 callback(cache[url]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 request(url).then(compile).then(function(tc) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 callback(cache[url] = tc);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 }, function(err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 require.signal("error", [ {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 error : err,
8
f0035923ff3e добавлена библиотека для работы с openlayers 3+
cin
parents: 0
diff changeset
47 src : 'implab/text/template-compile'
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 } ]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 return compile;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 });