corrade-http-templates – Blame information for rev 15
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
15 | eva | 1 | /** |
2 | * JavaScript format string function |
||
3 | * |
||
4 | */ |
||
5 | String.prototype.format = function() |
||
6 | { |
||
7 | var args = arguments; |
||
8 | |||
9 | return this.replace(/{(\d+)}/g, function(match, number) |
||
10 | { |
||
11 | return typeof args[number] != 'undefined' ? args[number] : |
||
12 | '{' + number + '}'; |
||
13 | }); |
||
14 | }; |
||
15 | |||
16 | |||
17 | /** |
||
18 | * Convert a Javascript Oject array or String array to an HTML table |
||
19 | * JSON parsing has to be made before function call |
||
20 | * It allows use of other JSON parsing methods like jQuery.parseJSON |
||
21 | * http(s)://, ftp://, file:// and javascript:; links are automatically computed |
||
22 | * |
||
23 | * JSON data samples that should be parsed and then can be converted to an HTML table |
||
24 | * var objectArray = '[{"Total":"34","Version":"1.0.4","Office":"New York"},{"Total":"67","Version":"1.1.0","Office":"Paris"}]'; |
||
25 | * var stringArray = '["New York","Berlin","Paris","Marrakech","Moscow"]'; |
||
26 | * var nestedTable = '[{ key1: "val1", key2: "val2", key3: { tableId: "tblIdNested1", tableClassName: "clsNested", linkText: "Download", data: [{ subkey1: "subval1", subkey2: "subval2", subkey3: "subval3" }] } }]'; |
||
27 | * |
||
28 | * Code sample to create a HTML table Javascript String |
||
29 | * var jsonHtmlTable = ConvertJsonToTable(eval(dataString), 'jsonTable', null, 'Download'); |
||
30 | * |
||
31 | * Code sample explaned |
||
32 | * - eval is used to parse a JSON dataString |
||
33 | * - table HTML id attribute will be 'jsonTable' |
||
34 | * - table HTML class attribute will not be added |
||
35 | * - 'Download' text will be displayed instead of the link itself |
||
36 | * |
||
37 | * @author Afshin Mehrabani <afshin dot meh at gmail dot com> |
||
38 | * |
||
39 | * @class ConvertJsonToTable |
||
40 | * |
||
41 | * @method ConvertJsonToTable |
||
42 | * |
||
43 | * @param parsedJson object Parsed JSON data |
||
44 | * @param tableId string Optional table id |
||
45 | * @param tableClassName string Optional table css class name |
||
46 | * @param linkText string Optional text replacement for link pattern |
||
47 | * |
||
48 | * @return string Converted JSON to HTML table |
||
49 | */ |
||
50 | function ConvertJsonToTable(parsedJson, tableId, tableClassName, linkText) |
||
51 | { |
||
52 | //Patterns for links and NULL value |
||
53 | var italic = '<i>{0}</i>'; |
||
54 | var link = linkText ? '<a href="{0}">' + linkText + '</a>' : |
||
55 | '<a href="{0}">{0}</a>'; |
||
56 | |||
57 | //Pattern for table |
||
58 | var idMarkup = tableId ? ' id="' + tableId + '"' : |
||
59 | ''; |
||
60 | |||
61 | var classMarkup = tableClassName ? ' class="' + tableClassName + '"' : |
||
62 | ''; |
||
63 | |||
64 | var tbl = '<table border="1" cellpadding="1" cellspacing="1"' + idMarkup + classMarkup + '>{0}{1}</table>'; |
||
65 | |||
66 | //Patterns for table content |
||
67 | var th = '<thead>{0}</thead>'; |
||
68 | var tb = '<tbody>{0}</tbody>'; |
||
69 | var tr = '<tr>{0}</tr>'; |
||
70 | var thRow = '<th>{0}</th>'; |
||
71 | var tdRow = '<td>{0}</td>'; |
||
72 | var thCon = ''; |
||
73 | var tbCon = ''; |
||
74 | var trCon = ''; |
||
75 | |||
76 | if (parsedJson) |
||
77 | { |
||
78 | var isStringArray = typeof(parsedJson[0]) == 'string'; |
||
79 | var headers; |
||
80 | |||
81 | // Create table headers from JSON data |
||
82 | // If JSON data is a simple string array we create a single table header |
||
83 | if(isStringArray) |
||
84 | thCon += thRow.format('value'); |
||
85 | else |
||
86 | { |
||
87 | // If JSON data is an object array, headers are automatically computed |
||
88 | if(typeof(parsedJson[0]) == 'object') |
||
89 | { |
||
90 | headers = array_keys(parsedJson[0]); |
||
91 | |||
92 | for (i = 0; i < headers.length; i++) |
||
93 | thCon += thRow.format(headers[i]); |
||
94 | } |
||
95 | } |
||
96 | th = th.format(tr.format(thCon)); |
||
97 | |||
98 | // Create table rows from Json data |
||
99 | if(isStringArray) |
||
100 | { |
||
101 | for (i = 0; i < parsedJson.length; i++) |
||
102 | { |
||
103 | tbCon += tdRow.format(parsedJson[i]); |
||
104 | trCon += tr.format(tbCon); |
||
105 | tbCon = ''; |
||
106 | } |
||
107 | } |
||
108 | else |
||
109 | { |
||
110 | if(headers) |
||
111 | { |
||
112 | var urlRegExp = new RegExp(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig); |
||
113 | var javascriptRegExp = new RegExp(/(^javascript:[\s\S]*;$)/ig); |
||
114 | |||
115 | for (i = 0; i < parsedJson.length; i++) |
||
116 | { |
||
117 | for (j = 0; j < headers.length; j++) |
||
118 | { |
||
119 | var value = parsedJson[i][headers[j]]; |
||
120 | var isUrl = urlRegExp.test(value) || javascriptRegExp.test(value); |
||
121 | |||
122 | if(isUrl) // If value is URL we auto-create a link |
||
123 | tbCon += tdRow.format(link.format(value)); |
||
124 | else |
||
125 | { |
||
126 | if(value){ |
||
127 | if(typeof(value) == 'object'){ |
||
128 | //for supporting nested tables |
||
129 | tbCon += tdRow.format(ConvertJsonToTable(eval(value.data), value.tableId, value.tableClassName, value.linkText)); |
||
130 | } else { |
||
131 | tbCon += tdRow.format(value); |
||
132 | } |
||
133 | |||
134 | } else { // If value == null we format it like PhpMyAdmin NULL values |
||
135 | tbCon += tdRow.format(italic.format(value).toUpperCase()); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | trCon += tr.format(tbCon); |
||
140 | tbCon = ''; |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | tb = tb.format(trCon); |
||
145 | tbl = tbl.format(th, tb); |
||
146 | |||
147 | return tbl; |
||
148 | } |
||
149 | return null; |
||
150 | } |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Return just the keys from the input array, optionally only for the specified search_value |
||
155 | * version: 1109.2015 |
||
156 | * discuss at: http://phpjs.org/functions/array_keys |
||
157 | * + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
||
158 | * + input by: Brett Zamir (http://brett-zamir.me) |
||
159 | * + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
||
160 | * + improved by: jd |
||
161 | * + improved by: Brett Zamir (http://brett-zamir.me) |
||
162 | * + input by: P |
||
163 | * + bugfixed by: Brett Zamir (http://brett-zamir.me) |
||
164 | * * example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} ); |
||
165 | * * returns 1: {0: 'firstname', 1: 'surname'} |
||
166 | */ |
||
167 | function array_keys(input, search_value, argStrict) |
||
168 | { |
||
169 | var search = typeof search_value !== 'undefined', tmp_arr = [], strict = !!argStrict, include = true, key = ''; |
||
170 | |||
171 | if (input && typeof input === 'object' && input.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array |
||
172 | return input.keys(search_value, argStrict); |
||
173 | } |
||
174 | |||
175 | for (key in input) |
||
176 | { |
||
177 | if (input.hasOwnProperty(key)) |
||
178 | { |
||
179 | include = true; |
||
180 | if (search) |
||
181 | { |
||
182 | if (strict && input[key] !== search_value) |
||
183 | include = false; |
||
184 | else if (input[key] != search_value) |
||
185 | include = false; |
||
186 | } |
||
187 | if (include) |
||
188 | tmp_arr[tmp_arr.length] = key; |
||
189 | } |
||
190 | } |
||
191 | return tmp_arr; |
||
192 | } |