-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonFlyer.js
100 lines (89 loc) · 2.7 KB
/
jsonFlyer.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
/*!
* JSON-Flyer JavaScript Library v0.1
* https://github.com/heroygt/JSON-Flyer
*
* Copyright 2011, Guangtao Yang
* Dual licensed under the MIT or GPL Version 2 licenses.
* You are free to use a this project in commercial projects
* as long as the copyright header is left intact.
*
* Date: 2011-03-02
*/
function JsonFlyer(){
};
JsonFlyer.prototype = {
render : function(template) {
var html = this.convertJSONToHTML(template);
return html
},
assign : function(data, selector) {
for(var key in data) {
var value = data[key];
var part = /^\./.test(key) ? key : "#" + key;
var newSelector = selector == undefined ? part : selector + " " + part;
if (typeof(value) == 'object') {
this.assign(value, newSelector);
} else {
this.setValue(newSelector, value);
}
}
},
setValue : function(selector, value) {
var elements = $(selector);
switch(elements.get(0).tagName) {
case 'INPUT' : elements.val(value); elements.click();break;
case 'TEXTAREA' : elements.val(value);break;
case 'SELECT': {
if(new RegExp("^options =>").test(value)) {
value = value.replace("options =>", "");
var optionsHTML = this.initSelectOptions($.parseJSON(value));
elements.html(optionsHTML);
break;
} else {
elements.val(value);
break;
}
}
default: elements.text(value);
}
},
initSelectOptions : function(options) {
var optionsHTML = "";
$.each(options, function(text, value) {
optionsHTML += '<option value="' + value + '">' + text + "</option>";
});
return optionsHTML;
},
convertJSONToHTML : function(json) {
if (typeof(json)=='string') return json;
var html = '';
for (var key in json) {
html += this.convertJSONToHTMLElement(key, json[key]);
};
return html;
},
convertJSONToHTMLElement : function(key, body) {
if (key == 'attrs') return '';
var params = this.convertKeyToCSSSelector(key);
var attrs = body.attrs == null ? '' : ' ' + body.attrs;
var element =
'<' + params['start'] + attrs + '>' +
this.convertJSONToHTML(body) +
'</' + params['tag'] + '>';
return element;
},
convertKeyToCSSSelector : function(key) {
var tagFilter = new RegExp("^\\w+");
var idFilter = new RegExp("#\\w+");
var classFilter = new RegExp("\\.\\w+");
var element = {};
element['tag'] = tagFilter.exec(key) || 'div';
element['id'] = (idFilter.exec(key) || '').toString().replace('#', '') ;
element['class'] = (classFilter.exec(key) || '').toString().replace('.', '') || '';
element['start'] = '';
element['start'] += element['tag'];
element['start'] += element['id'] == '' ? '' : ' id="' + element['id'] + '"';
element['start'] += element['class'] == '' ? '' : ' class="' + element['class'] + '"';
return element;
}
}