forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.templateData.js
More file actions
170 lines (164 loc) · 6.31 KB
/
Copy pathjquery.templateData.js
File metadata and controls
170 lines (164 loc) · 6.31 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Copyright (C) 2011 Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define([
'jquery' /* $ */,
'str/htmlEscape',
'jquery.instructure_misc_helpers' /* replaceTags */
], function($, htmlEscape) {
// Fills the selected object(s) with data values as specified. Plaintext values should be specified in the
// data: data used to fill template.
// id: set the id attribute of the template object
// textValues: a list of strings, which values should be plaintext
// htmlValues: a list of strings, which values should be html
// hrefValues: List of string. Searches for all anchor tags in the template
// and globally replaces "{{ value }}" with data[value]. Useful for adding
// new elements asynchronously, when you don't know what their URL will be
// until they're created.
$.fn.fillTemplateData = function(options) {
if(this.length && options) {
if (options.iterator) {
// todo: replace .andSelf with .addBack when JQuery is upgraded.
this.find("*").andSelf().each(function(){
var $el = $(this);
$.each(["name", "id", "class"], function(i, attr){
if ( $el.attr(attr) ) {
$el.attr(attr, $el.attr(attr).replace(/-iterator-/, options.iterator));
}
});
});
}
if(options.id) {
this.attr('id', options.id);
}
var contentChange = false;
if(options.data) {
for(var item in options.data) {
if(options.except && $.inArray(item, options.except) != -1) {
continue;
}
if (options.data[item] && options.dataValues && $.inArray(item, options.dataValues) != -1) {
this.data(item, options.data[item].toString());
}
var $found_all = this.find("." + item);
var avoid = options.avoid || "";
$found_all.each(function() {
var $found = $(this);
if($found.length > 0 && $found.closest(avoid).length === 0) {
if(typeof(options.data[item]) == "undefined" || options.data[item] === null) {
options.data[item] = "";
}
if(options.htmlValues && $.inArray(item, options.htmlValues) != -1) {
$found.html($.raw(options.data[item].toString()));
if($found.hasClass('user_content')) {
contentChange = true;
$found.removeClass('enhanced');
$found.data('unenhanced_content_html', options.data[item].toString());
}
} else if ($found[0].tagName.toUpperCase() == "INPUT") {
$found.val(options.data[item]);
} else {
try {
var str = options.data[item].toString();
$found.html(htmlEscape(str));
} catch(e) { }
}
}
});
}
}
if(options.hrefValues && options.data) {
this.find("a,span[rel]").each(function() {
var $obj = $(this),
oldHref, oldRel, oldName;
for(var i in options.hrefValues) {
if(!options.hrefValues.hasOwnProperty(i)) {
continue;
}
var name = options.hrefValues[i];
if(oldHref = $obj.attr('href')) {
var newHref = $.replaceTags(oldHref, name, encodeURIComponent(options.data[name]));
var orig = $obj.text() === $obj.html() ? $obj.text() : null;
if(oldHref !== newHref) {
$obj.attr('href', newHref);
if(orig) {
$obj.text(orig);
}
}
}
if(oldRel = $obj.attr('rel')) {
$obj.attr('rel', $.replaceTags(oldRel, name, options.data[name]));
}
if(oldName = $obj.attr('name')) {
$obj.attr('name', $.replaceTags(oldName, name, options.data[name]));
}
}
});
}
if(contentChange) {
$(document).triggerHandler('user_content_change');
}
}
return this;
};
$.fn.fillTemplateData.defaults = {htmlValues: null, hrefValues: null};
// Reverse version of fillTemplateData. Lets you pull out the string versions of values held in divs, spans, etc.
// Based on the usage of class names within an object to specify an object's sub-parts.
$.fn.getTemplateData = function(options) {
if(!this.length || !options) {
return {};
}
var result = {}, item, val;
if(options.textValues) {
var _this = this;
options.textValues.forEach(function(item) {
var $item = _this.find("." + item.replace(/\[/g, '\\[').replace(/\]/g, '\\]') + ":first");
val = $.trim($item.text());
if($item.html() === " ") { val = ""; }
if(val.length === 1 && val.charCodeAt(0) === 160) {
val = "";
}
result[item] = val;
});
}
if(options.dataValues) {
for(item in options.dataValues) {
var val = this.data(options.dataValues[item]);
if(val) {
result[options.dataValues[item]] = val;
}
}
}
if(options.htmlValues) {
for(item in options.htmlValues) {
var $elem = this.find("." + options.htmlValues[item].replace(/\[/g, '\\[').replace(/\]/g, '\\]') + ":first");
val = null;
if($elem.hasClass('user_content') && $elem.data('unenhanced_content_html')) {
val = $elem.data('unenhanced_content_html');
} else {
val = $.trim($elem.html());
}
result[options.htmlValues[item]] = val;
}
}
return result;
};
$.fn.getTemplateValue = function(value, options) {
var opts = $.extend({}, options, {textValues: [value]});
return this.getTemplateData(opts)[value];
};
});