gluon-web.js 11 KB

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