gluon-web.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. }
  125. return (value == ref)
  126. }
  127. function check(deps) {
  128. for (var i=0; i < deps.length; i++) {
  129. var stat = true;
  130. for (var j in deps[i]) {
  131. stat = (stat && checkvalue(j, deps[i][j]));
  132. }
  133. if (stat)
  134. return true;
  135. }
  136. return false;
  137. }
  138. function update() {
  139. var state = false;
  140. for (var id in dep_entries) {
  141. var entry = dep_entries[id];
  142. var node = document.getElementById(id);
  143. var parent = document.getElementById(entry.parent);
  144. if (node && node.parentNode && !check(entry.deps)) {
  145. node.parentNode.removeChild(node);
  146. state = true;
  147. } else if (parent && (!node || !node.parentNode) && check(entry.deps)) {
  148. var next = undefined;
  149. for (next = parent.firstChild; next; next = next.nextSibling) {
  150. if (next.getAttribute && parseInt(next.getAttribute('data-index'), 10) > entry.index) {
  151. break;
  152. }
  153. }
  154. if (!next) {
  155. parent.appendChild(entry.node);
  156. } else {
  157. parent.insertBefore(entry.node, next);
  158. }
  159. state = true;
  160. }
  161. // hide optionals widget if no choices remaining
  162. if (parent && parent.parentNode && parent.getAttribute('data-optionals'))
  163. parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
  164. }
  165. if (state) {
  166. update();
  167. }
  168. }
  169. function bind(obj, type, callback, mode) {
  170. if (!obj.addEventListener) {
  171. obj.attachEvent('on' + type,
  172. function() {
  173. var e = window.event;
  174. if (!e.target && e.srcElement)
  175. e.target = e.srcElement;
  176. return !!callback(e);
  177. }
  178. );
  179. } else {
  180. obj.addEventListener(type, callback, !!mode);
  181. }
  182. return obj;
  183. }
  184. function init_dynlist(parent, attr) {
  185. var prefix = attr.prefix;
  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 (!attr.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 (attr.size)
  214. t.size = attr.size;
  215. if (attr.placeholder)
  216. t.placeholder = attr.placeholder;
  217. parent.appendChild(t);
  218. if (attr.type)
  219. validate_field(t, false, attr.type);
  220. bind(t, 'keydown', dynlist_keydown);
  221. bind(t, 'keypress', dynlist_keypress);
  222. if (i == focus) {
  223. t.focus();
  224. } else if (-i == focus) {
  225. t.focus();
  226. /* force cursor to end */
  227. var v = t.value;
  228. t.value = ' '
  229. t.value = v;
  230. }
  231. if (attr.optional || values.length > 1) {
  232. var b = document.createElement('span');
  233. b.className = 'gluon-remove';
  234. parent.appendChild(b);
  235. bind(b, 'click', dynlist_btnclick(false));
  236. parent.appendChild(document.createElement('br'));
  237. }
  238. }
  239. var b = document.createElement('span');
  240. b.className = 'gluon-add';
  241. parent.appendChild(b);
  242. bind(b, 'click', dynlist_btnclick(true));
  243. }
  244. function dynlist_keypress(ev) {
  245. ev = ev ? ev : window.event;
  246. var se = ev.target ? ev.target : ev.srcElement;
  247. if (se.nodeType == 3)
  248. se = se.parentNode;
  249. switch (ev.keyCode) {
  250. /* backspace, delete */
  251. case 8:
  252. case 46:
  253. if (se.value.length == 0) {
  254. if (ev.preventDefault)
  255. ev.preventDefault();
  256. return false;
  257. }
  258. return true;
  259. /* enter, arrow up, arrow down */
  260. case 13:
  261. case 38:
  262. case 40:
  263. if (ev.preventDefault)
  264. ev.preventDefault();
  265. return false;
  266. }
  267. return true;
  268. }
  269. function dynlist_keydown(ev) {
  270. ev = ev ? ev : window.event;
  271. var se = ev.target ? ev.target : ev.srcElement;
  272. var index = 0;
  273. var prev, next;
  274. if (se) {
  275. if (se.nodeType == 3)
  276. se = se.parentNode;
  277. index = se.index;
  278. prev = se.previousSibling;
  279. while (prev && prev.name != prefix)
  280. prev = prev.previousSibling;
  281. next = se.nextSibling;
  282. while (next && next.name != prefix)
  283. next = next.nextSibling;
  284. }
  285. switch (ev.keyCode) {
  286. /* backspace, delete */
  287. case 8:
  288. case 46:
  289. var del = (se.nodeName.toLowerCase() == 'select')
  290. ? true : (se.value.length == 0);
  291. if (del) {
  292. if (ev.preventDefault)
  293. ev.preventDefault();
  294. var focus = se.index;
  295. if (ev.keyCode == 8)
  296. focus = -focus+1;
  297. dynlist_redraw(focus, -1, index);
  298. return false;
  299. }
  300. break;
  301. /* enter */
  302. case 13:
  303. dynlist_redraw(-1, index, -1);
  304. break;
  305. /* arrow up */
  306. case 38:
  307. if (prev)
  308. prev.focus();
  309. break;
  310. /* arrow down */
  311. case 40:
  312. if (next)
  313. next.focus();
  314. break;
  315. }
  316. return true;
  317. }
  318. function dynlist_btnclick(add) {
  319. return function(ev) {
  320. ev = ev ? ev : window.event;
  321. var se = ev.target ? ev.target : ev.srcElement;
  322. var input = se.previousSibling;
  323. while (input && input.name != prefix) {
  324. input = input.previousSibling;
  325. }
  326. if (add) {
  327. dynlist_keydown({
  328. target: input,
  329. keyCode: 13
  330. });
  331. } else {
  332. input.value = '';
  333. dynlist_keydown({
  334. target: input,
  335. keyCode: 8
  336. });
  337. }
  338. return false;
  339. }
  340. }
  341. dynlist_redraw(NaN, -1, -1);
  342. }
  343. function validate_field(field, optional, type) {
  344. var check = compile(type);
  345. if (!check)
  346. return;
  347. var validator = function() {
  348. if (!field.form)
  349. return;
  350. field.className = field.className.replace(/ gluon-input-invalid/g, '');
  351. var value = (field.options && field.options.selectedIndex > -1)
  352. ? field.options[field.options.selectedIndex].value : field.value;
  353. if (!(((value.length == 0) && optional) || check.apply(value)))
  354. field.className += ' gluon-input-invalid';
  355. };
  356. bind(field, "blur", validator);
  357. bind(field, "keyup", validator);
  358. if (field.nodeName == 'SELECT') {
  359. bind(field, "change", validator);
  360. bind(field, "click", validator);
  361. }
  362. validator();
  363. }
  364. function add(obj, dep, index) {
  365. var entry = dep_entries[obj.id];
  366. if (!entry) {
  367. entry = {
  368. "node": obj,
  369. "parent": obj.parentNode.id,
  370. "deps": [],
  371. "index": index
  372. };
  373. dep_entries[obj.id] = entry;
  374. }
  375. entry.deps.push(dep)
  376. }
  377. (function() {
  378. var nodes;
  379. nodes = document.querySelectorAll('[data-depends]');
  380. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  381. var index = parseInt(node.getAttribute('data-index'), 10);
  382. var depends = JSON.parse(node.getAttribute('data-depends'));
  383. if (!isNaN(index) && depends.length > 0) {
  384. for (var alt = 0; alt < depends.length; alt++) {
  385. add(node, depends[alt], index);
  386. }
  387. }
  388. }
  389. nodes = document.querySelectorAll('[data-update]');
  390. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  391. var events = node.getAttribute('data-update').split(' ');
  392. for (var j = 0, event; (event = events[j]) !== undefined; j++) {
  393. bind(node, event, update);
  394. }
  395. }
  396. nodes = document.querySelectorAll('[data-type]');
  397. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  398. validate_field(node, node.getAttribute('data-optional') === 'true',
  399. node.getAttribute('data-type'));
  400. }
  401. nodes = document.querySelectorAll('[data-dynlist]');
  402. for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
  403. var attr = JSON.parse(node.getAttribute('data-dynlist'));
  404. init_dynlist(node, attr);
  405. }
  406. update();
  407. })();
  408. })();