gluon-web.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. Copyright 2008 Steven Barth <steven@midlink.org>
  3. Copyright 2008-2012 Jo-Philipp Wich <jow@openwrt.org>
  4. Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. */
  10. /*
  11. Build using:
  12. uglifyjs javascript/gluon-web.js -o files/lib/gluon/web/www/static/resources/gluon-web.js -c -m --support-ie8
  13. */
  14. (function() {
  15. var dep_entries = {};
  16. function Int(x) {
  17. return (/^-?\d+$/.test(x) ? +x : NaN);
  18. }
  19. function Dec(x) {
  20. return (/^-?\d*\.?\d+?$/.test(x) ? +x : NaN);
  21. }
  22. var validators = {
  23. 'integer': function() {
  24. return !isNaN(Int(this));
  25. },
  26. 'uinteger': function() {
  27. return (Int(this) >= 0);
  28. },
  29. 'float': function() {
  30. return !isNaN(Dec(this));
  31. },
  32. 'ufloat': function() {
  33. return (Dec(this) >= 0);
  34. },
  35. 'ipaddr': function() {
  36. return validators.ip4addr.apply(this) ||
  37. validators.ip6addr.apply(this);
  38. },
  39. 'ip4addr': function() {
  40. var match;
  41. if ((match = this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))) {
  42. return (match[1] >= 0) && (match[1] <= 255) &&
  43. (match[2] >= 0) && (match[2] <= 255) &&
  44. (match[3] >= 0) && (match[3] <= 255) &&
  45. (match[4] >= 0) && (match[4] <= 255);
  46. }
  47. return false;
  48. },
  49. 'ip6addr': function() {
  50. if (this.indexOf('::') < 0)
  51. return (this.match(/^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i) != null);
  52. if (
  53. (this.indexOf(':::') >= 0) || this.match(/::.+::/) ||
  54. this.match(/^:[^:]/) || this.match(/[^:]:$/)
  55. )
  56. return false;
  57. if (this.match(/^(?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}$/i))
  58. return true;
  59. if (this.match(/^(?:[a-f0-9]{1,4}:){7}:$/i))
  60. return true;
  61. if (this.match(/^:(?::[a-f0-9]{1,4}){7}$/i))
  62. return true;
  63. return false;
  64. },
  65. 'wpakey': function() {
  66. var v = this;
  67. if (v.length == 64)
  68. return (v.match(/^[a-f0-9]{64}$/i) != null);
  69. else
  70. return (v.length >= 8) && (v.length <= 63);
  71. },
  72. 'range': function(min, max) {
  73. var val = Dec(this);
  74. return (val >= +min && val <= +max);
  75. },
  76. 'min': function(min) {
  77. return (Dec(this) >= +min);
  78. },
  79. 'max': function(max) {
  80. return (Dec(this) <= +max);
  81. },
  82. 'irange': function(min, max) {
  83. var val = Int(this);
  84. return (val >= +min && val <= +max);
  85. },
  86. 'imin': function(min) {
  87. return (Int(this) >= +min);
  88. },
  89. 'imax': function(max) {
  90. return (Int(this) <= +max);
  91. },
  92. 'minlength': function(min) {
  93. return ((''+this).length >= +min);
  94. },
  95. 'maxlength': function(max) {
  96. return ((''+this).length <= +max);
  97. },
  98. };
  99. function compile(type) {
  100. var v, match;
  101. if ((match = type.match(/^([^\(]+)\(([^,]+),([^\)]+)\)$/)) && (v = validators[match[1]]) !== undefined) {
  102. return function() {
  103. return v.apply(this, [match[2], match[3]]);
  104. }
  105. } else if ((match = type.match(/^([^\(]+)\(([^,\)]+)\)$/)) && (v = validators[match[1]]) !== undefined) {
  106. return function() {
  107. return v.apply(this, [match[2]]);
  108. }
  109. } else {
  110. return validators[type];
  111. }
  112. }
  113. function checkvalue(target, ref) {
  114. var t = document.getElementById(target);
  115. var value;
  116. if (t) {
  117. if (t.type == "checkbox") {
  118. value = t.checked;
  119. } else if (t.value) {
  120. value = t.value;
  121. } else {
  122. value = "";
  123. }
  124. return (value == ref);
  125. } else {
  126. t = document.getElementById(target + '.' + ref);
  127. return (t.type == "radio" && t.checked);
  128. }
  129. return false;
  130. }
  131. function check(deps) {
  132. for (var i=0; i < deps.length; i++) {
  133. var stat = true;
  134. for (var j in deps[i]) {
  135. stat = (stat && checkvalue(j, deps[i][j]));
  136. }
  137. if (stat)
  138. return true;
  139. }
  140. return false;
  141. }
  142. function update() {
  143. var state = false;
  144. for (var id in dep_entries) {
  145. var entry = dep_entries[id];
  146. var node = document.getElementById(id);
  147. var parent = document.getElementById(entry.parent);
  148. if (node && node.parentNode && !check(entry.deps)) {
  149. node.parentNode.removeChild(node);
  150. state = true;
  151. } else if (parent && (!node || !node.parentNode) && check(entry.deps)) {
  152. var next = undefined;
  153. for (next = parent.firstChild; next; next = next.nextSibling) {
  154. if (next.getAttribute && parseInt(next.getAttribute('data-index'), 10) > entry.index) {
  155. break;
  156. }
  157. }
  158. if (!next) {
  159. parent.appendChild(entry.node);
  160. } else {
  161. parent.insertBefore(entry.node, next);
  162. }
  163. state = true;
  164. }
  165. // hide optionals widget if no choices remaining
  166. if (parent && parent.parentNode && parent.getAttribute('data-optionals'))
  167. parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
  168. }
  169. if (state) {
  170. update();
  171. }
  172. }
  173. function bind(obj, type, callback, mode) {
  174. if (!obj.addEventListener) {
  175. obj.attachEvent('on' + type,
  176. function() {
  177. var e = window.event;
  178. if (!e.target && e.srcElement)
  179. e.target = e.srcElement;
  180. return !!callback(e);
  181. }
  182. );
  183. } else {
  184. obj.addEventListener(type, callback, !!mode);
  185. }
  186. return obj;
  187. }
  188. function init_dynlist(parent, attr) {
  189. var prefix = attr.prefix;
  190. function dynlist_redraw(focus, add, del) {
  191. var values = [];
  192. while (parent.firstChild) {
  193. var n = parent.firstChild;
  194. var i = +n.index;
  195. if (i != del) {
  196. if (n.nodeName.toLowerCase() == 'input')
  197. values.push(n.value || '');
  198. else if (n.nodeName.toLowerCase() == 'select')
  199. values[values.length-1] = n.options[n.selectedIndex].value;
  200. }
  201. parent.removeChild(n);
  202. }
  203. if (add >= 0) {
  204. focus = add + 1;
  205. values.splice(add, 0, '');
  206. } else if (!attr.optional && values.length == 0) {
  207. values.push('');
  208. }
  209. for (var i = 1; i <= values.length; i++) {
  210. var t = document.createElement('input');
  211. t.id = prefix + '.' + i;
  212. t.name = prefix;
  213. t.value = values[i-1];
  214. t.type = 'text';
  215. t.index = i;
  216. t.className = 'gluon-input-text';
  217. if (attr.size)
  218. t.size = attr.size;
  219. if (attr.placeholder)
  220. t.placeholder = attr.placeholder;
  221. parent.appendChild(t);
  222. if (attr.type)
  223. validate_field(t, false, attr.type);
  224. bind(t, 'keydown', dynlist_keydown);
  225. bind(t, 'keypress', dynlist_keypress);
  226. if (i == focus) {
  227. t.focus();
  228. } else if (-i == focus) {
  229. t.focus();
  230. /* force cursor to end */
  231. var v = t.value;
  232. t.value = ' '
  233. t.value = v;
  234. }
  235. if (attr.optional || values.length > 1) {
  236. var b = document.createElement('span');
  237. b.className = 'gluon-remove';
  238. parent.appendChild(b);
  239. bind(b, 'click', dynlist_btnclick(false));
  240. parent.appendChild(document.createElement('br'));
  241. }
  242. }
  243. var b = document.createElement('span');
  244. b.className = 'gluon-add';
  245. parent.appendChild(b);
  246. bind(b, 'click', dynlist_btnclick(true));
  247. }
  248. function dynlist_keypress(ev) {
  249. ev = ev ? ev : window.event;
  250. var se = ev.target ? ev.target : ev.srcElement;
  251. if (se.nodeType == 3)
  252. se = se.parentNode;
  253. switch (ev.keyCode) {
  254. /* backspace, delete */
  255. case 8:
  256. case 46:
  257. if (se.value.length == 0) {
  258. if (ev.preventDefault)
  259. ev.preventDefault();
  260. return false;
  261. }
  262. return true;
  263. /* enter, arrow up, arrow down */
  264. case 13:
  265. case 38:
  266. case 40:
  267. if (ev.preventDefault)
  268. ev.preventDefault();
  269. return false;
  270. }
  271. return true;
  272. }
  273. function dynlist_keydown(ev) {
  274. ev = ev ? ev : window.event;
  275. var se = ev.target ? ev.target : ev.srcElement;
  276. var index = 0;
  277. var prev, next;
  278. if (se) {
  279. if (se.nodeType == 3)
  280. se = se.parentNode;
  281. index = se.index;
  282. prev = se.previousSibling;
  283. while (prev && prev.name != prefix)
  284. prev = prev.previousSibling;
  285. next = se.nextSibling;
  286. while (next && next.name != prefix)
  287. next = next.nextSibling;
  288. }
  289. switch (ev.keyCode) {
  290. /* backspace, delete */
  291. case 8:
  292. case 46:
  293. var del = (se.nodeName.toLowerCase() == 'select')
  294. ? true : (se.value.length == 0);
  295. if (del) {
  296. if (ev.preventDefault)
  297. ev.preventDefault();
  298. var focus = se.index;
  299. if (ev.keyCode == 8)
  300. focus = -focus+1;
  301. dynlist_redraw(focus, -1, index);
  302. return false;
  303. }
  304. break;
  305. /* enter */
  306. case 13:
  307. dynlist_redraw(-1, index, -1);
  308. break;
  309. /* arrow up */
  310. case 38:
  311. if (prev)
  312. prev.focus();
  313. break;
  314. /* arrow down */
  315. case 40:
  316. if (next)
  317. next.focus();
  318. break;
  319. }
  320. return true;
  321. }
  322. function dynlist_btnclick(add) {
  323. return function(ev) {
  324. ev = ev ? ev : window.event;
  325. var se = ev.target ? ev.target : ev.srcElement;
  326. var input = se.previousSibling;
  327. while (input && input.name != prefix) {
  328. input = input.previousSibling;
  329. }
  330. if (add) {
  331. dynlist_keydown({
  332. target: input,
  333. keyCode: 13
  334. });
  335. } else {
  336. input.value = '';
  337. dynlist_keydown({
  338. target: input,
  339. keyCode: 8
  340. });
  341. }
  342. return false;
  343. }
  344. }
  345. dynlist_redraw(NaN, -1, -1);
  346. }
  347. function validate_field(field, optional, type) {
  348. var check = compile(type);
  349. if (!check)
  350. return;
  351. var validator = function() {
  352. if (!field.form)
  353. return;
  354. field.className = field.className.replace(/ gluon-input-invalid/g, '');
  355. var value = (field.options && field.options.selectedIndex > -1)
  356. ? field.options[field.options.selectedIndex].value : field.value;
  357. if (!(((value.length == 0) && optional) || check.apply(value)))
  358. field.className += ' gluon-input-invalid';
  359. };
  360. bind(field, "blur", validator);
  361. bind(field, "keyup", validator);
  362. if (field.nodeName.toLowerCase() == 'select') {
  363. bind(field, "change", validator);
  364. bind(field, "click", validator);
  365. }
  366. validator();
  367. }
  368. function add(obj, dep, index) {
  369. var entry = dep_entries[obj.id];
  370. if (!entry) {
  371. entry = {
  372. "node": obj,
  373. "parent": obj.parentNode.id,
  374. "deps": [],
  375. "index": index
  376. };
  377. dep_entries[obj.id] = entry;
  378. }
  379. entry.deps.push(dep)
  380. }
  381. (function() {
  382. var nodes;
  383. nodes = document.querySelectorAll('[data-depends]');
  384. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  385. var index = parseInt(node.getAttribute('data-index'), 10);
  386. var depends = JSON.parse(node.getAttribute('data-depends'));
  387. if (!isNaN(index) && depends.length > 0) {
  388. for (var alt = 0; alt < depends.length; alt++) {
  389. add(node, depends[alt], index);
  390. }
  391. }
  392. }
  393. nodes = document.querySelectorAll('[data-update]');
  394. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  395. var events = node.getAttribute('data-update').split(' ');
  396. for (var j = 0, event; (event = events[j]) !== undefined; j++) {
  397. bind(node, event, update);
  398. }
  399. }
  400. nodes = document.querySelectorAll('[data-type]');
  401. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  402. validate_field(node, node.getAttribute('data-optional') === 'true',
  403. node.getAttribute('data-type'));
  404. }
  405. nodes = document.querySelectorAll('[data-dynlist]');
  406. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  407. var attr = JSON.parse(node.getAttribute('data-dynlist'));
  408. init_dynlist(node, attr);
  409. }
  410. update();
  411. })();
  412. })();