EntityUserDefineItemView.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. define([ "require", "backbone", "hbs!tmpl/entity/EntityUserDefineItemView_tmpl" ], function(require, Backbone, EntityUserDefineItemView_tmpl) {
  2. "use strict";
  3. return Backbone.Marionette.ItemView.extend({
  4. _viewName: "EntityUserDefineItemView",
  5. template: EntityUserDefineItemView_tmpl,
  6. templateHelpers: function() {
  7. return {
  8. items: this.items,
  9. allValueRemovedUpdate: this.allValueRemovedUpdate
  10. };
  11. },
  12. regions: {},
  13. ui: {
  14. itemKey: "[data-type='key']",
  15. itemValue: "[data-type='value']",
  16. addItem: "[data-id='addItem']",
  17. deleteItem: "[data-id='deleteItem']",
  18. charSupportMsg: "[data-id='charSupportMsg']"
  19. },
  20. events: function() {
  21. var events = {};
  22. return events["input " + this.ui.itemKey] = "onItemKeyChange", events["input " + this.ui.itemValue] = "onItemValueChange",
  23. events["click " + this.ui.addItem] = "onAddItemClick", events["click " + this.ui.deleteItem] = "onDeleteItemClick",
  24. events;
  25. },
  26. initialize: function(options) {
  27. 0 === options.items.length ? this.items = [ {
  28. key: "",
  29. value: ""
  30. } ] : this.items = $.extend(!0, [], options.items), this.updateParentButtonState = options.updateButtonState;
  31. },
  32. onRender: function() {},
  33. onAddItemClick: function(e) {
  34. this.allValueRemovedUpdate = !1;
  35. var el = e.currentTarget;
  36. this.items.splice(parseInt(el.dataset.index) + 1, 0, {
  37. key: "",
  38. value: ""
  39. }), this.render();
  40. },
  41. onDeleteItemClick: function(e) {
  42. var el = e.currentTarget;
  43. if (this.items.splice(el.dataset.index, 1), this.allValueRemovedUpdate = !1, 0 === this.items.length) {
  44. var updated = this.updateParentButtonState();
  45. updated === !1 && (this.allValueRemovedUpdate = !0, this.render());
  46. } else this.render();
  47. },
  48. onItemKeyChange: function(e) {
  49. var el = e.currentTarget;
  50. this.handleCharSupport(el), el.value.trim().includes(":") || (this.items[el.dataset.index].key = _.escape(el.value.trim()));
  51. },
  52. onItemValueChange: function(e) {
  53. var el = e.currentTarget;
  54. this.handleCharSupport(el), el.value.trim().includes(":") || (this.items[el.dataset.index].value = el.value.trim());
  55. },
  56. handleCharSupport: function(el) {
  57. el.value.trim().includes(":") ? (el.setAttribute("class", "form-control errorClass"),
  58. this.ui.charSupportMsg.html("These special character '(:)' are not supported.")) : (el.setAttribute("class", "form-control"),
  59. this.ui.charSupportMsg.html(""));
  60. }
  61. });
  62. });