PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/draft/library/server/wsf_js_widget/examples/custom-template/assets/widget.js

https://bitbucket.org/jocelyn/ewf
JavaScript | 1373 lines | 1154 code | 218 blank | 1 comment | 189 complexity | 1e6e53f68ddc741cacf466e3cb00cea3 MD5 | raw file
  1. // Generated by CoffeeScript 1.6.1
  2. var Mini, WSF_AUTOCOMPLETE_CONTROL, WSF_BUTTON_CONTROL, WSF_CHECKBOX_CONTROL, WSF_CHECKBOX_LIST_CONTROL, WSF_CODEVIEW_CONTROL, WSF_CONTROL, WSF_DATE_PICKER_CONTROL, WSF_DROPDOWN_CONTROL, WSF_DYNAMIC_MULTI_CONTROL, WSF_FILE_CONTROL, WSF_FORM_ELEMENT_CONTROL, WSF_GRID_CONTROL, WSF_HTML_CONTROL, WSF_INPUT_CONTROL, WSF_MAX_VALIDATOR, WSF_MIN_VALIDATOR, WSF_NAVLIST_ITEM_CONTROL, WSF_PAGE_CONTROL, WSF_PAGINATION_CONTROL, WSF_PASSWORD_CONTROL, WSF_PROGRESS_CONTROL, WSF_REGEXP_VALIDATOR, WSF_REPEATER_CONTROL, WSF_SLIDER_CONTROL, WSF_TEXTAREA_CONTROL, WSF_VALIDATOR, build_control, cache, controls, find_control, lazy_load, loaded, once, parseSuggestions, redirect, show_alert, start_modal, start_modal_big, template, tmpl,
  3. __hasProp = {}.hasOwnProperty,
  4. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  5. cache = {};
  6. jQuery.cachedAsset = function(url, options) {
  7. var head, onload, script, success, successful, timeoutHandle;
  8. if (/\.css$/.test(url)) {
  9. $("<link/>", {
  10. rel: "stylesheet",
  11. type: "text/css",
  12. href: url
  13. }).appendTo("head");
  14. return {
  15. done: function(fn) {
  16. return fn();
  17. }
  18. };
  19. } else {
  20. success = [];
  21. head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
  22. script = document.createElement('script');
  23. script.async = 'async';
  24. script.src = url;
  25. successful = false;
  26. onload = function(_, aborted) {
  27. var s, _i, _len;
  28. if (aborted == null) {
  29. aborted = false;
  30. }
  31. if (!(aborted || !script.readyState || script.readyState === 'complete')) {
  32. return;
  33. }
  34. clearTimeout(timeoutHandle);
  35. script.onload = script.onreadystatechange = script.onerror = null;
  36. if (head && script.parentNode) {
  37. head.removeChild(script);
  38. }
  39. script = void 0;
  40. if (success && !aborted) {
  41. successful = true;
  42. for (_i = 0, _len = success.length; _i < _len; _i++) {
  43. s = success[_i];
  44. s();
  45. }
  46. return success = [];
  47. }
  48. };
  49. script.onload = script.onreadystatechange = onload;
  50. script.onerror = function() {
  51. return onload(null, true);
  52. };
  53. timeoutHandle = setTimeout(script.onerror, 7500);
  54. head.insertBefore(script, head.firstChild);
  55. return {
  56. done: function(fn) {
  57. if (!successful) {
  58. success.push(fn);
  59. } else {
  60. fn();
  61. }
  62. }
  63. };
  64. }
  65. };
  66. jQuery.unparam = function(value) {
  67. var i, l, pair, params, pieces;
  68. params = {};
  69. pieces = value.split("&");
  70. pair = void 0;
  71. i = void 0;
  72. l = void 0;
  73. i = 0;
  74. l = pieces.length;
  75. while (i < l) {
  76. pair = pieces[i].split("=", 2);
  77. params[decodeURIComponent(pair[0])] = (pair.length === 2 ? decodeURIComponent(pair[1].replace(/\+/g, " ")) : true);
  78. i++;
  79. }
  80. return params;
  81. };
  82. template = tmpl = function(str, data) {
  83. var fn;
  84. fn = (!/\W/.test(str) ? cache[str] = cache[str] || tmpl(str) : new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};" + "with(obj){p.push('" + str.replace(/[\r\t\n]/g, " ").split("{{").join("\t").replace(/((^|}})[^\t]*)'/g, "$1\r").replace(/\t=(.*?)}}/g, "',$1,'").split("\t").join("');").split("}}").join("p.push('").split("\r").join("\\'") + "');}return p.join('');"));
  85. if (data) {
  86. return fn(data);
  87. } else {
  88. return fn;
  89. }
  90. };
  91. Mini = {
  92. compile: function(t) {
  93. return {
  94. render: template(t)
  95. };
  96. }
  97. };
  98. parseSuggestions = function(data) {
  99. var a, d;
  100. for (a in data) {
  101. if (a === 'suggestions') {
  102. return data[a];
  103. } else {
  104. d = parseSuggestions(data[a]);
  105. if (d != null) {
  106. return d;
  107. }
  108. }
  109. }
  110. return null;
  111. };
  112. loaded = {};
  113. once = function(f) {
  114. var executed;
  115. executed = false;
  116. return function() {
  117. if (!executed) {
  118. executed = true;
  119. return f.apply(this, arguments);
  120. }
  121. };
  122. };
  123. lazy_load = function(requirements, fn, that) {
  124. if (requirements.length === 0) {
  125. return function() {
  126. var a;
  127. a = arguments;
  128. return fn.apply(that, a);
  129. };
  130. }
  131. if (that == null) {
  132. that = window;
  133. }
  134. return function() {
  135. var a, args, counter, done, r, self, _i, _len;
  136. a = arguments;
  137. if (typeof args === "undefined" || args === null) {
  138. args = [];
  139. }
  140. counter = requirements.length + 1;
  141. self = this;
  142. done = function() {
  143. counter = counter - 1;
  144. if (counter === 0) {
  145. fn.apply(that, a);
  146. }
  147. };
  148. for (_i = 0, _len = requirements.length; _i < _len; _i++) {
  149. r = requirements[_i];
  150. if (loaded[r] == null) {
  151. loaded[r] = $.cachedAsset(r);
  152. }
  153. loaded[r].done(done);
  154. }
  155. return done();
  156. };
  157. };
  158. find_control = function(root, name) {
  159. var children, matching;
  160. children = root.children();
  161. if (children.length) {
  162. matching = children.filter('[data-name=' + name + ']');
  163. if (matching.length) {
  164. return matching.first();
  165. } else {
  166. return find_control(children.filter(':not([data-name])'), name);
  167. }
  168. } else {
  169. return null;
  170. }
  171. };
  172. build_control = function(control_name, state, control) {
  173. var $el, type, typeclass;
  174. $el = find_control(control.$el, control_name);
  175. if ($el === null) {
  176. return null;
  177. }
  178. if ($el.data('control') != null) {
  179. return $el.data('control');
  180. }
  181. type = $el.data('type');
  182. typeclass = null;
  183. try {
  184. typeclass = eval(type);
  185. } catch (e) {
  186. typeclass = WSF_CONTROL;
  187. }
  188. if ((type != null) && (typeclass != null)) {
  189. return new typeclass(control, $el, control_name, state);
  190. }
  191. return null;
  192. };
  193. WSF_VALIDATOR = (function() {
  194. function WSF_VALIDATOR(parent_control, settings) {
  195. this.parent_control = parent_control;
  196. this.settings = settings;
  197. this.error = this.settings.error;
  198. return;
  199. }
  200. WSF_VALIDATOR.prototype.validate = function() {
  201. return true;
  202. };
  203. return WSF_VALIDATOR;
  204. })();
  205. WSF_REGEXP_VALIDATOR = (function(_super) {
  206. __extends(WSF_REGEXP_VALIDATOR, _super);
  207. function WSF_REGEXP_VALIDATOR() {
  208. WSF_REGEXP_VALIDATOR.__super__.constructor.apply(this, arguments);
  209. this.pattern = new RegExp(this.settings.expression, 'g');
  210. }
  211. WSF_REGEXP_VALIDATOR.prototype.validate = function() {
  212. var res, val;
  213. val = this.parent_control.value();
  214. res = val.match(this.pattern);
  215. return res !== null;
  216. };
  217. return WSF_REGEXP_VALIDATOR;
  218. })(WSF_VALIDATOR);
  219. WSF_MIN_VALIDATOR = (function(_super) {
  220. __extends(WSF_MIN_VALIDATOR, _super);
  221. function WSF_MIN_VALIDATOR() {
  222. return WSF_MIN_VALIDATOR.__super__.constructor.apply(this, arguments);
  223. }
  224. WSF_MIN_VALIDATOR.prototype.validate = function() {
  225. var val;
  226. val = this.parent_control.value();
  227. return val.length >= this.settings.min;
  228. };
  229. return WSF_MIN_VALIDATOR;
  230. })(WSF_VALIDATOR);
  231. WSF_MAX_VALIDATOR = (function(_super) {
  232. __extends(WSF_MAX_VALIDATOR, _super);
  233. function WSF_MAX_VALIDATOR() {
  234. return WSF_MAX_VALIDATOR.__super__.constructor.apply(this, arguments);
  235. }
  236. WSF_MAX_VALIDATOR.prototype.validate = function() {
  237. var val;
  238. val = this.parent_control.value();
  239. return val.length <= this.settings.max;
  240. };
  241. return WSF_MAX_VALIDATOR;
  242. })(WSF_VALIDATOR);
  243. WSF_CONTROL = (function() {
  244. WSF_CONTROL.prototype.requirements = [];
  245. function WSF_CONTROL(parent_control, $el, control_name, fullstate) {
  246. this.parent_control = parent_control;
  247. this.$el = $el;
  248. this.control_name = control_name;
  249. this.fullstate = fullstate;
  250. this.state = this.fullstate.state;
  251. this.load_subcontrols();
  252. this.isolation = "" + this.$el.data('isolation') === "1";
  253. this.$el.data('control', this);
  254. this.initialize = lazy_load(this.requirements, this.attach_events, this);
  255. this.initialize = once(this.initialize);
  256. return;
  257. }
  258. WSF_CONTROL.prototype.load_subcontrols = function() {
  259. var control_name, state;
  260. if (this.fullstate.controls != null) {
  261. this.controls = (function() {
  262. var _ref, _results;
  263. _ref = this.fullstate.controls;
  264. _results = [];
  265. for (control_name in _ref) {
  266. state = _ref[control_name];
  267. _results.push(build_control(control_name, state, this));
  268. }
  269. return _results;
  270. }).call(this);
  271. } else {
  272. this.controls = [];
  273. }
  274. };
  275. WSF_CONTROL.prototype.attach_events = function() {
  276. var control, _i, _len, _ref;
  277. console.log("Attached " + this.control_name);
  278. _ref = this.controls;
  279. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  280. control = _ref[_i];
  281. if (control != null) {
  282. control.initialize();
  283. }
  284. }
  285. };
  286. WSF_CONTROL.prototype.update = function(state) {};
  287. WSF_CONTROL.prototype.process_actions = function(actions) {
  288. var action, fn, _i, _len;
  289. for (_i = 0, _len = actions.length; _i < _len; _i++) {
  290. action = actions[_i];
  291. try {
  292. fn = null;
  293. if (this[action.type] != null) {
  294. fn = this[action.type];
  295. fn.call(this, action);
  296. } else {
  297. fn = eval(action.type);
  298. fn(action);
  299. }
  300. } catch (e) {
  301. console.log("Failed preforming action " + action.type);
  302. }
  303. }
  304. };
  305. WSF_CONTROL.prototype.process_update = function(new_states) {
  306. var control, _i, _len, _ref;
  307. try {
  308. if ((new_states != null ? new_states.actions : void 0) != null) {
  309. this.process_actions(new_states.actions);
  310. }
  311. if ((new_states != null ? new_states[this.control_name] : void 0) != null) {
  312. this.update(new_states[this.control_name]);
  313. _ref = this.controls;
  314. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  315. control = _ref[_i];
  316. if (control != null) {
  317. control.process_update(new_states[this.control_name]['controls']);
  318. }
  319. }
  320. }
  321. } catch (e) {
  322. console.error(e);
  323. return;
  324. }
  325. };
  326. WSF_CONTROL.prototype.get_context_state = function() {
  327. if ((this.parent_control != null) && !this.isolation) {
  328. return this.parent_control.get_context_state();
  329. }
  330. return this.wrap(this.control_name, this.fullstate);
  331. };
  332. WSF_CONTROL.prototype.get_full_control_name = function() {
  333. var val;
  334. if (this.parent_control != null) {
  335. val = this.parent_control.get_full_control_name();
  336. if (val !== "") {
  337. val = val + "-";
  338. }
  339. return val + this.control_name;
  340. }
  341. return this.control_name;
  342. };
  343. WSF_CONTROL.prototype.wrap = function(cname, state) {
  344. var ctrs;
  345. ctrs = {};
  346. ctrs[cname] = state;
  347. state = {
  348. "controls": ctrs
  349. };
  350. if (this.parent_control != null) {
  351. return this.parent_control.wrap(this.parent_control.control_name, state);
  352. }
  353. return state;
  354. };
  355. WSF_CONTROL.prototype.callback_url = function(params) {
  356. if (this.parent_control != null) {
  357. return this.parent_control.callback_url(params);
  358. }
  359. $.extend(params, this.url_params);
  360. return this.url + '?' + $.param(params);
  361. };
  362. WSF_CONTROL.prototype.trigger_callback = function(control_name, event, event_parameter) {
  363. return this.run_trigger_callback(this.get_full_control_name(), event, event_parameter);
  364. };
  365. WSF_CONTROL.prototype.get_page = function() {
  366. if (this.parent_control != null) {
  367. return this.parent_control.get_page();
  368. }
  369. return this;
  370. };
  371. WSF_CONTROL.prototype.run_trigger_callback = function(control_name, event, event_parameter) {
  372. var self;
  373. if ((this.parent_control != null) && !this.isolation) {
  374. return this.parent_control.run_trigger_callback(control_name, event, event_parameter);
  375. }
  376. self = this;
  377. return $.ajax({
  378. type: 'POST',
  379. url: this.callback_url({
  380. control_name: control_name,
  381. event: event,
  382. event_parameter: event_parameter
  383. }),
  384. data: JSON.stringify(this.get_context_state()),
  385. processData: false,
  386. contentType: 'application/json',
  387. cache: false
  388. }).done(function(new_states) {
  389. return self.get_page().process_update(new_states);
  390. });
  391. };
  392. WSF_CONTROL.prototype.on = function(name, callback, context) {
  393. if (this._events == null) {
  394. this._events = {};
  395. }
  396. if (this._events[name] == null) {
  397. this._events[name] = [];
  398. }
  399. this._events[name].push({
  400. callback: callback,
  401. context: context
  402. });
  403. return this;
  404. };
  405. WSF_CONTROL.prototype.trigger = function(name) {
  406. var ev, _i, _len, _ref, _ref1;
  407. if (((_ref = this._events) != null ? _ref[name] : void 0) == null) {
  408. return this;
  409. }
  410. _ref1 = this._events[name];
  411. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  412. ev = _ref1[_i];
  413. ev.callback.call(ev.context);
  414. }
  415. return this;
  416. };
  417. WSF_CONTROL.prototype.remove = function() {
  418. var control, _i, _len, _ref;
  419. _ref = this.controls;
  420. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  421. control = _ref[_i];
  422. if (control != null) {
  423. control.remove();
  424. }
  425. }
  426. console.log("Removed " + this.control_name);
  427. return this.$el.remove();
  428. };
  429. return WSF_CONTROL;
  430. })();
  431. WSF_PAGE_CONTROL = (function(_super) {
  432. __extends(WSF_PAGE_CONTROL, _super);
  433. function WSF_PAGE_CONTROL(fullstate) {
  434. this.fullstate = fullstate;
  435. this.state = this.fullstate.state;
  436. this.parent_control = null;
  437. this.$el = $('[data-name=' + this.state.id + ']');
  438. this.control_name = this.state.id;
  439. this.url = this.state['url'];
  440. this.url_params = jQuery.unparam(this.state['url_params']);
  441. this.$el.data('control', this);
  442. this.initialize = lazy_load(this.requirements, this.attach_events, this);
  443. this.initialize = once(this.initialize);
  444. this.load_subcontrols();
  445. }
  446. WSF_PAGE_CONTROL.prototype.process_update = function(new_states) {
  447. var control, _i, _len, _ref;
  448. _ref = this.controls;
  449. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  450. control = _ref[_i];
  451. if (control != null) {
  452. control.process_update(new_states);
  453. }
  454. }
  455. };
  456. WSF_PAGE_CONTROL.prototype.get_full_control_name = function() {
  457. return "";
  458. };
  459. WSF_PAGE_CONTROL.prototype.wrap = function(cname, state) {
  460. return state;
  461. };
  462. WSF_PAGE_CONTROL.prototype.remove = function() {
  463. var control, _i, _len, _ref;
  464. _ref = this.controls;
  465. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  466. control = _ref[_i];
  467. if (control != null) {
  468. control.remove();
  469. }
  470. }
  471. console.log("Removed " + this.control_name);
  472. return this.$el.remove();
  473. };
  474. return WSF_PAGE_CONTROL;
  475. })(WSF_CONTROL);
  476. WSF_SLIDER_CONTROL = (function(_super) {
  477. __extends(WSF_SLIDER_CONTROL, _super);
  478. function WSF_SLIDER_CONTROL() {
  479. return WSF_SLIDER_CONTROL.__super__.constructor.apply(this, arguments);
  480. }
  481. WSF_SLIDER_CONTROL.prototype.requirements = ['/assets/bootstrap.min.js'];
  482. WSF_SLIDER_CONTROL.prototype.attach_events = function() {
  483. var id;
  484. WSF_SLIDER_CONTROL.__super__.attach_events.apply(this, arguments);
  485. id = "slider" + Math.round(Math.random() * 10000);
  486. this.$el.attr("id", id);
  487. this.$el.find("ol li").attr("data-target", "#" + id);
  488. return this.$el.find(".carousel-control").attr("href", "#" + id);
  489. };
  490. return WSF_SLIDER_CONTROL;
  491. })(WSF_CONTROL);
  492. WSF_DROPDOWN_CONTROL = (function(_super) {
  493. __extends(WSF_DROPDOWN_CONTROL, _super);
  494. function WSF_DROPDOWN_CONTROL() {
  495. return WSF_DROPDOWN_CONTROL.__super__.constructor.apply(this, arguments);
  496. }
  497. WSF_DROPDOWN_CONTROL.prototype.requirements = ['/assets/bootstrap.min.js'];
  498. return WSF_DROPDOWN_CONTROL;
  499. })(WSF_CONTROL);
  500. controls = {};
  501. WSF_BUTTON_CONTROL = (function(_super) {
  502. __extends(WSF_BUTTON_CONTROL, _super);
  503. function WSF_BUTTON_CONTROL() {
  504. return WSF_BUTTON_CONTROL.__super__.constructor.apply(this, arguments);
  505. }
  506. WSF_BUTTON_CONTROL.prototype.attach_events = function() {
  507. var self;
  508. WSF_BUTTON_CONTROL.__super__.attach_events.apply(this, arguments);
  509. self = this;
  510. return this.$el.click(function(e) {
  511. e.preventDefault();
  512. return self.click();
  513. });
  514. };
  515. WSF_BUTTON_CONTROL.prototype.click = function() {
  516. if (this.state['callback_click']) {
  517. return this.trigger_callback(this.control_name, 'click');
  518. }
  519. };
  520. WSF_BUTTON_CONTROL.prototype.update = function(state) {
  521. if (state.disabled !== void 0) {
  522. this.state['disabled'] = state.disabled;
  523. this.$el.prop('disabled', state.disabled);
  524. }
  525. if (state.text != null) {
  526. this.state['text'] = state.text;
  527. return this.$el.text(state.text);
  528. }
  529. };
  530. return WSF_BUTTON_CONTROL;
  531. })(WSF_CONTROL);
  532. WSF_INPUT_CONTROL = (function(_super) {
  533. __extends(WSF_INPUT_CONTROL, _super);
  534. function WSF_INPUT_CONTROL() {
  535. return WSF_INPUT_CONTROL.__super__.constructor.apply(this, arguments);
  536. }
  537. WSF_INPUT_CONTROL.prototype.attach_events = function() {
  538. var self;
  539. WSF_INPUT_CONTROL.__super__.attach_events.apply(this, arguments);
  540. self = this;
  541. return this.$el.change(function() {
  542. return self.change();
  543. });
  544. };
  545. WSF_INPUT_CONTROL.prototype.change = function() {
  546. this.state['text'] = this.$el.val();
  547. if (this.state['callback_change']) {
  548. this.trigger_callback(this.control_name, 'change');
  549. }
  550. return this.trigger('change');
  551. };
  552. WSF_INPUT_CONTROL.prototype.value = function() {
  553. return this.$el.val();
  554. };
  555. WSF_INPUT_CONTROL.prototype.update = function(state) {
  556. if (state.disabled !== void 0) {
  557. this.state['disabled'] = state.disabled;
  558. this.$el.prop('disabled', state.disabled);
  559. }
  560. if (state.text != null) {
  561. this.state['text'] = state.text;
  562. return this.$el.val(state.text);
  563. }
  564. };
  565. return WSF_INPUT_CONTROL;
  566. })(WSF_CONTROL);
  567. WSF_FILE_CONTROL = (function(_super) {
  568. __extends(WSF_FILE_CONTROL, _super);
  569. function WSF_FILE_CONTROL() {
  570. WSF_FILE_CONTROL.__super__.constructor.apply(this, arguments);
  571. this.uploading = false;
  572. }
  573. WSF_FILE_CONTROL.prototype.start_upload = function() {
  574. var action, file, formData;
  575. if (this.$el[0].files.length === 0) {
  576. return;
  577. }
  578. if (this.uploading) {
  579. return;
  580. }
  581. this.uploading = true;
  582. this.$el.hide();
  583. this.progressbar = $("<div class=\"progress progress-striped active upload\"><div rstyle=\"width: 10%;\" class=\"progress-bar\"></div></div>");
  584. this.$el.parent().prepend(this.progressbar);
  585. formData = new FormData();
  586. action = this.callback_url({
  587. control_name: this.get_full_control_name(),
  588. event: "uploadfile",
  589. event_parameter: ""
  590. });
  591. file = this.$el[0].files[0];
  592. formData.append('file', file);
  593. formData.append('state', JSON.stringify(this.get_context_state()));
  594. return this.sendXHRequest(formData, action);
  595. };
  596. WSF_FILE_CONTROL.prototype.sendXHRequest = function(formData, uri) {
  597. var onprogressHandler, onstatechange, self, xhr;
  598. xhr = new XMLHttpRequest();
  599. self = this;
  600. onprogressHandler = function(evt) {
  601. var percent;
  602. percent = evt.loaded / evt.total * 100;
  603. return self.progressbar.find('.progress-bar').css({
  604. 'width': percent + "%"
  605. });
  606. };
  607. onstatechange = function(evt) {
  608. if (xhr.readyState === 4 && xhr.status === 200) {
  609. return self.get_page().process_update(JSON.parse(xhr.responseText));
  610. }
  611. };
  612. xhr.upload.addEventListener('progress', onprogressHandler, false);
  613. xhr.addEventListener('readystatechange', onstatechange, false);
  614. xhr.open('POST', uri, true);
  615. return xhr.send(formData);
  616. };
  617. WSF_FILE_CONTROL.prototype.attach_events = function() {
  618. var self;
  619. WSF_FILE_CONTROL.__super__.attach_events.apply(this, arguments);
  620. self = this;
  621. this.$el.change(function() {
  622. return self.change();
  623. });
  624. return this.refresh();
  625. };
  626. WSF_FILE_CONTROL.prototype.change = function() {
  627. var file, reader, self;
  628. this.state['file_name'] = null;
  629. this.state['file_type'] = null;
  630. this.state['file_size'] = null;
  631. this.state['file_id'] = null;
  632. if (this.$el[0].files.length > 0) {
  633. file = this.$el[0].files[0];
  634. this.state['file_name'] = file.name;
  635. this.state['file_type'] = file.type;
  636. this.state['file_size'] = file.size;
  637. }
  638. if (this.state['image_preview']) {
  639. reader = new FileReader();
  640. reader.readAsDataURL(this.$el[0].files[0]);
  641. self = this;
  642. reader.onload = function(e) {
  643. var preview;
  644. self.$el.parent().find("img").remove();
  645. preview = $("<img >").addClass("media thumbnail");
  646. preview[0].src = e.target.result;
  647. return self.$el.parent().append(preview);
  648. };
  649. }
  650. if (this.state['callback_change']) {
  651. this.trigger_callback(this.control_name, 'change');
  652. }
  653. return this.trigger('change');
  654. };
  655. WSF_FILE_CONTROL.prototype.value = function() {
  656. return this.$el.val();
  657. };
  658. WSF_FILE_CONTROL.prototype.update = function(state) {
  659. if (state.image_preview !== void 0) {
  660. this.state['image_preview'] = state.image_preview;
  661. this.refresh();
  662. }
  663. if (state.disabled !== void 0) {
  664. this.state['disabled'] = state.disabled;
  665. this.$el.prop('disabled', state.disabled);
  666. this.refresh();
  667. }
  668. if (state.file_name !== void 0) {
  669. this.state['file_name'] = state.file_name;
  670. }
  671. if (state.file_type !== void 0) {
  672. this.state['file_type'] = state.file_type;
  673. }
  674. if (state.file_size !== void 0) {
  675. this.state['file_size'] = state.file_size;
  676. }
  677. if (state.file_id !== void 0) {
  678. if (this.state['file_id'] !== state.file_id) {
  679. this.state['file_id'] = state.file_id;
  680. if (this.state['callback_uploaddone']) {
  681. this.trigger_callback(this.control_name, 'uploaddone');
  682. }
  683. this.uploading = false;
  684. }
  685. return this.refresh();
  686. }
  687. };
  688. WSF_FILE_CONTROL.prototype.refresh = function() {
  689. var fname, preview, removebtn, self, _ref;
  690. if (this.uploading) {
  691. return;
  692. }
  693. if ((_ref = this.progressbar) != null) {
  694. _ref.remove();
  695. }
  696. this.$el.parent().find("p, img").remove();
  697. if (this.state['file_id'] != null) {
  698. this.$el.hide();
  699. fname = $("<p></p>").addClass("form-control-static").text(this.state['file_name']);
  700. this.$el.parent().append(fname);
  701. if (this.state['image_preview']) {
  702. preview = $("<img >").attr('src', this.state['file_id']).addClass("media thumbnail");
  703. this.$el.parent().append(preview);
  704. }
  705. if (!this.state['disabled']) {
  706. fname.append(" ");
  707. removebtn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger");
  708. self = this;
  709. removebtn.click(function() {
  710. var _ref1;
  711. if ((_ref1 = self.progressbar) != null) {
  712. _ref1.remove();
  713. }
  714. self.$el.parent().find("p, img").remove();
  715. self.$el.show();
  716. self.$el.val('');
  717. return self.change();
  718. });
  719. return fname.append(removebtn);
  720. }
  721. } else {
  722. this.$el.show();
  723. this.$el.val('');
  724. return this.change();
  725. }
  726. };
  727. return WSF_FILE_CONTROL;
  728. })(WSF_CONTROL);
  729. WSF_DYNAMIC_MULTI_CONTROL = (function(_super) {
  730. __extends(WSF_DYNAMIC_MULTI_CONTROL, _super);
  731. function WSF_DYNAMIC_MULTI_CONTROL() {
  732. return WSF_DYNAMIC_MULTI_CONTROL.__super__.constructor.apply(this, arguments);
  733. }
  734. WSF_DYNAMIC_MULTI_CONTROL.prototype.update = function(state) {
  735. var control, el, k, v, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3;
  736. console.log(state);
  737. if ((state.items != null) && (state.render != null) && (state.newstate != null)) {
  738. this.state['items'] = state.items;
  739. _ref = this.controls;
  740. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  741. control = _ref[_i];
  742. if (!((control != null ? control.control_name : void 0) in state.newstate)) {
  743. if (control != null) {
  744. control.remove();
  745. }
  746. }
  747. }
  748. _ref1 = $(state.render).children();
  749. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  750. el = _ref1[_j];
  751. if (this.$el.children('[data-name=' + $(el).data('name') + ']').length === 0) {
  752. this.$el.append(el);
  753. }
  754. }
  755. _ref2 = state.newstate;
  756. for (k in _ref2) {
  757. v = _ref2[k];
  758. if (!(k in this.fullstate.controls)) {
  759. this.fullstate.controls[k] = v;
  760. }
  761. }
  762. this.load_subcontrols();
  763. _ref3 = this.controls;
  764. for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
  765. control = _ref3[_k];
  766. if (control != null) {
  767. control.initialize();
  768. }
  769. }
  770. }
  771. };
  772. return WSF_DYNAMIC_MULTI_CONTROL;
  773. })(WSF_CONTROL);
  774. WSF_PASSWORD_CONTROL = (function(_super) {
  775. __extends(WSF_PASSWORD_CONTROL, _super);
  776. function WSF_PASSWORD_CONTROL() {
  777. return WSF_PASSWORD_CONTROL.__super__.constructor.apply(this, arguments);
  778. }
  779. return WSF_PASSWORD_CONTROL;
  780. })(WSF_INPUT_CONTROL);
  781. WSF_NAVLIST_ITEM_CONTROL = (function(_super) {
  782. __extends(WSF_NAVLIST_ITEM_CONTROL, _super);
  783. function WSF_NAVLIST_ITEM_CONTROL() {
  784. return WSF_NAVLIST_ITEM_CONTROL.__super__.constructor.apply(this, arguments);
  785. }
  786. WSF_NAVLIST_ITEM_CONTROL.prototype.update = function(state) {
  787. WSF_NAVLIST_ITEM_CONTROL.__super__.update.apply(this, arguments);
  788. if (state.active != null) {
  789. this.state['active'] = state.active;
  790. if (state.active) {
  791. return this.$el.addClass("active");
  792. } else {
  793. return this.$el.removeClass("active");
  794. }
  795. }
  796. };
  797. return WSF_NAVLIST_ITEM_CONTROL;
  798. })(WSF_BUTTON_CONTROL);
  799. WSF_TEXTAREA_CONTROL = (function(_super) {
  800. __extends(WSF_TEXTAREA_CONTROL, _super);
  801. function WSF_TEXTAREA_CONTROL() {
  802. return WSF_TEXTAREA_CONTROL.__super__.constructor.apply(this, arguments);
  803. }
  804. return WSF_TEXTAREA_CONTROL;
  805. })(WSF_INPUT_CONTROL);
  806. WSF_CODEVIEW_CONTROL = (function(_super) {
  807. __extends(WSF_CODEVIEW_CONTROL, _super);
  808. function WSF_CODEVIEW_CONTROL() {
  809. WSF_CODEVIEW_CONTROL.__super__.constructor.apply(this, arguments);
  810. this.initialize = lazy_load(['/assets/codemirror/codemirror.js', '/assets/codemirror/codemirror.css', '/assets/codemirror/estudio.css'], lazy_load(['/assets/codemirror/eiffel.js'], this.attach_events, this), this);
  811. }
  812. WSF_CODEVIEW_CONTROL.prototype.attach_events = function() {
  813. WSF_CODEVIEW_CONTROL.__super__.attach_events.apply(this, arguments);
  814. this.editor = CodeMirror.fromTextArea(this.$el[0], {
  815. mode: "eiffel",
  816. tabMode: "indent",
  817. indentUnit: 4,
  818. lineNumbers: true,
  819. theme: 'estudio'
  820. });
  821. return this.editor.setSize("100%", 700);
  822. };
  823. WSF_CODEVIEW_CONTROL.prototype.remove = function() {
  824. this.editor.toTextArea();
  825. return WSF_CODEVIEW_CONTROL.__super__.remove.apply(this, arguments);
  826. };
  827. return WSF_CODEVIEW_CONTROL;
  828. })(WSF_INPUT_CONTROL);
  829. WSF_AUTOCOMPLETE_CONTROL = (function(_super) {
  830. __extends(WSF_AUTOCOMPLETE_CONTROL, _super);
  831. function WSF_AUTOCOMPLETE_CONTROL() {
  832. return WSF_AUTOCOMPLETE_CONTROL.__super__.constructor.apply(this, arguments);
  833. }
  834. WSF_AUTOCOMPLETE_CONTROL.prototype.requirements = ['assets/typeahead.min.js'];
  835. WSF_AUTOCOMPLETE_CONTROL.prototype.attach_events = function() {
  836. var self;
  837. WSF_AUTOCOMPLETE_CONTROL.__super__.attach_events.apply(this, arguments);
  838. self = this;
  839. console.log(this.$el);
  840. this.$el.typeahead({
  841. name: this.control_name + Math.random(),
  842. template: this.state['template'],
  843. engine: Mini,
  844. remote: {
  845. url: "",
  846. replace: function(url, uriEncodedQuery) {
  847. self.state['text'] = self.$el.val();
  848. return '?' + $.param({
  849. control_name: self.control_name,
  850. event: 'autocomplete',
  851. states: JSON.stringify(self.get_context_state())
  852. });
  853. },
  854. filter: function(parsedResponse) {
  855. return parseSuggestions(parsedResponse);
  856. },
  857. fn: function() {
  858. return self.trigger_callback(self.control_name, 'autocomplete');
  859. }
  860. }
  861. });
  862. this.$el.on('typeahead:closed', function() {
  863. return self.change();
  864. });
  865. return this.$el.on('typeahead:blured', function() {
  866. return self.change();
  867. });
  868. };
  869. return WSF_AUTOCOMPLETE_CONTROL;
  870. })(WSF_INPUT_CONTROL);
  871. WSF_DATE_PICKER_CONTROL = (function(_super) {
  872. __extends(WSF_DATE_PICKER_CONTROL, _super);
  873. function WSF_DATE_PICKER_CONTROL() {
  874. return WSF_DATE_PICKER_CONTROL.__super__.constructor.apply(this, arguments);
  875. }
  876. WSF_DATE_PICKER_CONTROL.prototype.requirements = ['assets/datepicker.css', 'assets/bootstrap-datepicker.js'];
  877. WSF_DATE_PICKER_CONTROL.prototype.attach_events = function() {
  878. var self;
  879. WSF_DATE_PICKER_CONTROL.__super__.attach_events.apply(this, arguments);
  880. self = this;
  881. console.log(this.$el);
  882. return this.$el.datepicker({
  883. format: 'dd-mm-yyyy'
  884. });
  885. };
  886. return WSF_DATE_PICKER_CONTROL;
  887. })(WSF_INPUT_CONTROL);
  888. WSF_CHECKBOX_CONTROL = (function(_super) {
  889. __extends(WSF_CHECKBOX_CONTROL, _super);
  890. function WSF_CHECKBOX_CONTROL() {
  891. return WSF_CHECKBOX_CONTROL.__super__.constructor.apply(this, arguments);
  892. }
  893. WSF_CHECKBOX_CONTROL.prototype.attach_events = function() {
  894. var self;
  895. WSF_CHECKBOX_CONTROL.__super__.attach_events.apply(this, arguments);
  896. self = this;
  897. this.checked_value = this.state['checked_value'];
  898. return this.$el.change(function() {
  899. return self.change();
  900. });
  901. };
  902. WSF_CHECKBOX_CONTROL.prototype.change = function() {
  903. this.state['checked'] = this.$el.is(':checked');
  904. if (this.state['callback_change']) {
  905. this.trigger_callback(this.control_name, 'change');
  906. }
  907. return this.trigger('change');
  908. };
  909. WSF_CHECKBOX_CONTROL.prototype.value = function() {
  910. return this.$el.is(':checked');
  911. };
  912. WSF_CHECKBOX_CONTROL.prototype.update = function(state) {
  913. if (state.text != null) {
  914. this.state['checked'] = state.checked;
  915. return this.$el.prop('checked', state.checked);
  916. }
  917. };
  918. return WSF_CHECKBOX_CONTROL;
  919. })(WSF_CONTROL);
  920. WSF_FORM_ELEMENT_CONTROL = (function(_super) {
  921. __extends(WSF_FORM_ELEMENT_CONTROL, _super);
  922. function WSF_FORM_ELEMENT_CONTROL() {
  923. return WSF_FORM_ELEMENT_CONTROL.__super__.constructor.apply(this, arguments);
  924. }
  925. WSF_FORM_ELEMENT_CONTROL.prototype.attach_events = function() {
  926. var self, validator, validatorclass, _i, _len, _ref;
  927. WSF_FORM_ELEMENT_CONTROL.__super__.attach_events.apply(this, arguments);
  928. self = this;
  929. this.value_control = this.controls[0];
  930. if (this.value_control != null) {
  931. this.value_control.on('change', this.change, this);
  932. }
  933. this.serverside_validator = false;
  934. this.validators = [];
  935. _ref = this.state['validators'];
  936. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  937. validator = _ref[_i];
  938. try {
  939. validatorclass = eval(validator.name);
  940. this.validators.push(new validatorclass(this, validator));
  941. } catch (e) {
  942. this.serverside_validator = true;
  943. }
  944. }
  945. };
  946. WSF_FORM_ELEMENT_CONTROL.prototype.change = function() {
  947. var validator, _i, _len, _ref;
  948. _ref = this.validators;
  949. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  950. validator = _ref[_i];
  951. if (!validator.validate()) {
  952. this.showerror(validator.error);
  953. return;
  954. }
  955. }
  956. this.showerror("");
  957. if (this.serverside_validator) {
  958. this.trigger_callback(this.control_name, 'validate');
  959. }
  960. };
  961. WSF_FORM_ELEMENT_CONTROL.prototype.showerror = function(message) {
  962. var errordiv;
  963. this.$el.removeClass("has-error");
  964. this.$el.find(".validation").remove();
  965. if (message.length > 0) {
  966. this.$el.addClass("has-error");
  967. errordiv = $("<div />").addClass('help-block').addClass('validation').text(message);
  968. return this.$el.children("div").append(errordiv);
  969. }
  970. };
  971. WSF_FORM_ELEMENT_CONTROL.prototype.update = function(state) {
  972. if (state.error != null) {
  973. return this.showerror(state.error);
  974. }
  975. };
  976. WSF_FORM_ELEMENT_CONTROL.prototype.value = function() {
  977. return this.value_control.value();
  978. };
  979. return WSF_FORM_ELEMENT_CONTROL;
  980. })(WSF_CONTROL);
  981. WSF_HTML_CONTROL = (function(_super) {
  982. __extends(WSF_HTML_CONTROL, _super);
  983. function WSF_HTML_CONTROL() {
  984. return WSF_HTML_CONTROL.__super__.constructor.apply(this, arguments);
  985. }
  986. WSF_HTML_CONTROL.prototype.value = function() {
  987. return this.$el.html();
  988. };
  989. WSF_HTML_CONTROL.prototype.update = function(state) {
  990. if (state.html != null) {
  991. this.state['html'] = state.html;
  992. return this.$el.html(state.html);
  993. }
  994. };
  995. return WSF_HTML_CONTROL;
  996. })(WSF_CONTROL);
  997. WSF_CHECKBOX_LIST_CONTROL = (function(_super) {
  998. __extends(WSF_CHECKBOX_LIST_CONTROL, _super);
  999. function WSF_CHECKBOX_LIST_CONTROL() {
  1000. return WSF_CHECKBOX_LIST_CONTROL.__super__.constructor.apply(this, arguments);
  1001. }
  1002. WSF_CHECKBOX_LIST_CONTROL.prototype.attach_events = function() {
  1003. var control, _i, _len, _ref;
  1004. WSF_CHECKBOX_LIST_CONTROL.__super__.attach_events.apply(this, arguments);
  1005. _ref = this.controls;
  1006. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1007. control = _ref[_i];
  1008. control.on('change', this.change, this);
  1009. }
  1010. };
  1011. WSF_CHECKBOX_LIST_CONTROL.prototype.change = function() {
  1012. return this.trigger("change");
  1013. };
  1014. WSF_CHECKBOX_LIST_CONTROL.prototype.value = function() {
  1015. var result, subc, _i, _len, _ref;
  1016. result = [];
  1017. _ref = this.controls;
  1018. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1019. subc = _ref[_i];
  1020. if (subc.value()) {
  1021. result.push(subc.checked_value);
  1022. }
  1023. }
  1024. return result;
  1025. };
  1026. return WSF_CHECKBOX_LIST_CONTROL;
  1027. })(WSF_CONTROL);
  1028. WSF_PROGRESS_CONTROL = (function(_super) {
  1029. __extends(WSF_PROGRESS_CONTROL, _super);
  1030. function WSF_PROGRESS_CONTROL() {
  1031. return WSF_PROGRESS_CONTROL.__super__.constructor.apply(this, arguments);
  1032. }
  1033. WSF_PROGRESS_CONTROL.prototype.attach_events = function() {
  1034. var runfetch, self;
  1035. WSF_PROGRESS_CONTROL.__super__.attach_events.apply(this, arguments);
  1036. self = this;
  1037. runfetch = function() {
  1038. return self.fetch();
  1039. };
  1040. return this.int = setInterval(runfetch, 5000);
  1041. };
  1042. WSF_PROGRESS_CONTROL.prototype.fetch = function() {
  1043. return this.trigger_callback(this.control_name, 'progress_fetch');
  1044. };
  1045. WSF_PROGRESS_CONTROL.prototype.update = function(state) {
  1046. if (state.progress != null) {
  1047. this.state['progress'] = state.progress;
  1048. return this.$el.children('.progress-bar').attr('aria-valuenow', state.progress).width(state.progress + '%');
  1049. }
  1050. };
  1051. WSF_PROGRESS_CONTROL.prototype.remove = function() {
  1052. clearInterval(this.int);
  1053. return WSF_PROGRESS_CONTROL.__super__.remove.apply(this, arguments);
  1054. };
  1055. return WSF_PROGRESS_CONTROL;
  1056. })(WSF_CONTROL);
  1057. WSF_PAGINATION_CONTROL = (function(_super) {
  1058. __extends(WSF_PAGINATION_CONTROL, _super);
  1059. function WSF_PAGINATION_CONTROL() {
  1060. return WSF_PAGINATION_CONTROL.__super__.constructor.apply(this, arguments);
  1061. }
  1062. WSF_PAGINATION_CONTROL.prototype.attach_events = function() {
  1063. var self;
  1064. self = this;
  1065. return this.$el.on('click', 'a', function(e) {
  1066. e.preventDefault();
  1067. return self.click(e);
  1068. });
  1069. };
  1070. WSF_PAGINATION_CONTROL.prototype.click = function(e) {
  1071. var nr;
  1072. nr = $(e.target).data('nr');
  1073. if (nr === "next") {
  1074. return this.trigger_callback(this.control_name, "next");
  1075. } else if (nr === "prev") {
  1076. return this.trigger_callback(this.control_name, "prev");
  1077. } else {
  1078. return this.trigger_callback(this.control_name, "goto", nr);
  1079. }
  1080. };
  1081. WSF_PAGINATION_CONTROL.prototype.update = function(state) {
  1082. if (state._html != null) {
  1083. return this.$el.html($(state._html).html());
  1084. }
  1085. };
  1086. return WSF_PAGINATION_CONTROL;
  1087. })(WSF_CONTROL);
  1088. WSF_GRID_CONTROL = (function(_super) {
  1089. __extends(WSF_GRID_CONTROL, _super);
  1090. function WSF_GRID_CONTROL() {
  1091. return WSF_GRID_CONTROL.__super__.constructor.apply(this, arguments);
  1092. }
  1093. WSF_GRID_CONTROL.prototype.attach_events = function() {
  1094. var self;
  1095. WSF_GRID_CONTROL.__super__.attach_events.apply(this, arguments);
  1096. return self = this;
  1097. };
  1098. WSF_GRID_CONTROL.prototype.update = function(state) {
  1099. if (state.datasource != null) {
  1100. this.state['datasource'] = state.datasource;
  1101. }
  1102. if (state._body != null) {
  1103. return this.$el.find('tbody').html(state._body);
  1104. }
  1105. };
  1106. return WSF_GRID_CONTROL;
  1107. })(WSF_CONTROL);
  1108. WSF_REPEATER_CONTROL = (function(_super) {
  1109. __extends(WSF_REPEATER_CONTROL, _super);
  1110. function WSF_REPEATER_CONTROL() {
  1111. return WSF_REPEATER_CONTROL.__super__.constructor.apply(this, arguments);
  1112. }
  1113. WSF_REPEATER_CONTROL.prototype.attach_events = function() {
  1114. var self;
  1115. WSF_REPEATER_CONTROL.__super__.attach_events.apply(this, arguments);
  1116. return self = this;
  1117. };
  1118. WSF_REPEATER_CONTROL.prototype.update = function(state) {
  1119. if (state.datasource != null) {
  1120. this.state['datasource'] = state.datasource;
  1121. }
  1122. if (state._body != null) {
  1123. this.$el.find('.repeater_content').html(state._body);
  1124. return console.log(state._body);
  1125. }
  1126. };
  1127. return WSF_REPEATER_CONTROL;
  1128. })(WSF_CONTROL);
  1129. redirect = function(action) {
  1130. return document.location.href = action.url;
  1131. };
  1132. show_alert = function(action) {
  1133. return alert(action.message);
  1134. };
  1135. start_modal = lazy_load(['/assets/bootstrap.min.js'], function(action) {
  1136. var cssclass, modal;
  1137. cssclass = "";
  1138. if (action.type === "start_modal_big") {
  1139. cssclass = " big";
  1140. }
  1141. modal = $("<div class=\"modal fade\">\n<div class=\"modal-dialog" + cssclass + "\">\n <div class=\"modal-content\">\n <div class=\"modal-header\">\n <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>\n <h4 class=\"modal-title\">" + action.title + "</h4>\n </div>\n <div class=\"modal-body\">\n \n </div>\n </div>\n</div>\n</div>");
  1142. modal.appendTo('body');
  1143. modal.modal('show');
  1144. modal.on('hidden.bs.modal', function() {
  1145. $(modal.find('[data-name]').get().reverse()).each(function(i, value) {
  1146. $(value).data('control').remove();
  1147. });
  1148. });
  1149. return $.get(action.url, {
  1150. ajax: 1
  1151. }).done(function(data) {
  1152. return modal.find('.modal-body').append(data);
  1153. });
  1154. });
  1155. start_modal_big = start_modal;