-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.js
108 lines (89 loc) · 3.25 KB
/
draw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Generated by CoffeeScript 1.12.7
(function() {
this.KXDrawRender = (function() {
var EL_ATTR_TEMPLATE_ID, EL_ATTR_UNIQUE_ID, OBJ_VAR_TEMPLATE_ID, OBJ_VAR_UNIQUE_ID;
EL_ATTR_TEMPLATE_ID = "data-kx-draw-name";
EL_ATTR_UNIQUE_ID = "data-kx-draw-id";
OBJ_VAR_TEMPLATE_ID = "_kx_draw_template_name";
OBJ_VAR_UNIQUE_ID = "_kx_draw_unique_id";
KXDrawRender.templates = null;
KXDrawRender.data = null;
KXDrawRender.partials = null;
function KXDrawRender(options) {
var name, raw, ref;
this.templates = options.templates, this.data = options.data, this.partials = options.partials;
ref = this.partials;
for (name in ref) {
raw = ref[name];
Handlebars.registerPartial(name, raw);
}
return;
}
KXDrawRender.prototype.getRawTemplates = function() {
return $.extend({}, this.templates);
};
KXDrawRender.prototype.getRawData = function() {
return $.extend({}, this.data);
};
KXDrawRender.prototype.getRawPartials = function() {
return $.extend({}, this.partials);
};
KXDrawRender.prototype.getDataByUniqId = function(templateName, id) {
var bit, ref, ref1;
bit = (ref = this.getRawData()) != null ? (ref1 = ref[templateName]) != null ? ref1[id] : void 0 : void 0;
if (bit == null) {
console.error("Can't get data from template '" + templateName + "' with id " + id + ". Not found!");
return false;
}
return bit;
};
KXDrawRender.prototype.getNodeByUniqId = function(templateName, id) {
var $rootNode;
$rootNode = $("[" + EL_ATTR_TEMPLATE_ID + "='" + templateName + "'][" + EL_ATTR_UNIQUE_ID + "='" + id + "']");
if (!(($rootNode != null) && $rootNode.length)) {
console.error("Node " + templateName + "/" + id + " not found.");
return false;
}
return $rootNode;
};
KXDrawRender.prototype.render = function(templateName, id) {
var $rootNode, context, doc, ref, renderer, template;
template = (ref = this.getRawTemplates()) != null ? ref[templateName] : void 0;
if (template == null) {
console.error("Can't render " + templateName + ". Template with this name - not found.");
return;
}
context = this.getDataByUniqId(templateName, id);
if (!context) {
return;
}
$rootNode = this.getNodeByUniqId(templateName, id);
if (!$rootNode) {
return;
}
renderer = Handlebars.compile(template);
doc = $(renderer(context));
$rootNode.html(doc.html());
return doc;
};
KXDrawRender.prototype.update = function(templateName, id, newData, assign) {
var context, newContext;
if (assign == null) {
assign = true;
}
context = this.getDataByUniqId(templateName, id);
newContext = {};
if (assign) {
newContext = $.extend({}, context, newData);
} else {
newContext = newData;
newContext[OBJ_VAR_TEMPLATE_ID] = context[OBJ_VAR_TEMPLATE_ID];
newContext[OBJ_VAR_UNIQUE_ID] = context[OBJ_VAR_UNIQUE_ID];
}
this.data[templateName][id] = newContext;
return this.render(templateName, id);
};
return KXDrawRender;
})();
}).call(this);
//# sourceMappingURL=draw.js.map