BusinessMetadataAttributeItemView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. define([ "require", "backbone", "hbs!tmpl/business_metadata/BusinessMetadataAttributeItemView_tmpl" ], function(require, Backbone, BusinessMetadataAttributeItemViewTmpl) {
  2. "use strict";
  3. return Backbone.Marionette.ItemView.extend({
  4. template: BusinessMetadataAttributeItemViewTmpl,
  5. templateHelpers: function() {
  6. return {
  7. modalID: this.viewId
  8. };
  9. },
  10. regions: {},
  11. ui: {
  12. attributeInput: "[data-id='attributeInput']",
  13. close: "[data-id='close']",
  14. dataTypeSelector: "[data-id='dataTypeSelector']",
  15. searchWeightSelector: "[data-id='searchWeightSelector']",
  16. entityTypeSelector: "[data-id='entityTypeSelector']",
  17. enumTypeSelectorContainer: "[data-id='enumTypeSelectorContainer']",
  18. enumTypeSelector: "[data-id='enumTypeSelector']",
  19. enumValueSelectorContainer: "[data-id='enumValueSelectorContainer']",
  20. enumValueSelector: "[data-id='enumValueSelector']",
  21. multiValueSelect: "[data-id='multiValueSelect']",
  22. multiValueSelectStatus: "[data-id='multiValueSelectStatus']",
  23. stringLengthContainer: "[data-id='stringLengthContainer']",
  24. stringLengthValue: "[data-id='stringLength']",
  25. createNewEnum: "[data-id='createNewEnum']"
  26. },
  27. events: function() {
  28. var events = {};
  29. return events["keyup " + this.ui.attributeInput] = function(e) {
  30. this.model.set({
  31. name: e.target.value.trim()
  32. });
  33. }, events["change " + this.ui.searchWeightSelector] = function(e) {
  34. this.model.set({
  35. searchWeight: e.target.value.trim()
  36. });
  37. }, events["change " + this.ui.dataTypeSelector] = function(e) {
  38. var obj = {
  39. options: this.model.get("options") || {}
  40. };
  41. delete obj.enumValues, delete obj.options.maxStrLength, "enumeration" === e.target.value.trim().toLowerCase() ? (this.ui.enumTypeSelectorContainer.show(),
  42. this.ui.enumTypeSelector.show(), this.emumTypeSelectDisplay(), this.ui.stringLengthContainer.hide(),
  43. this.ui.stringLengthValue.hide()) : (obj.typeName = e.target.value.trim(), "string" === e.target.value.trim().toLowerCase() ? (this.ui.stringLengthContainer.show(),
  44. this.ui.stringLengthValue.show(), this.ui.enumTypeSelectorContainer.hide(), this.ui.enumTypeSelector.hide(),
  45. this.ui.enumValueSelectorContainer.hide(), obj.options.maxStrLength = this.ui.stringLengthValue.val()) : (this.ui.enumTypeSelectorContainer.hide(),
  46. this.ui.enumTypeSelector.hide(), this.ui.enumValueSelectorContainer.hide(), this.ui.stringLengthContainer.hide(),
  47. this.ui.stringLengthValue.hide())), this.model.set(obj), "enumeration" != e.target.value.trim() && this.ui.multiValueSelectStatus.trigger("change");
  48. }, events["change " + this.ui.enumTypeSelector] = function(e) {
  49. this.model.set({
  50. enumValues: e.target.value.trim()
  51. });
  52. }, events["change " + this.ui.stringLengthContainer] = function(e) {
  53. var options = this.model.get("options") || {};
  54. "string" == this.ui.dataTypeSelector.val() && (options.maxStrLength = e.target.value.trim()),
  55. this.model.set({
  56. options: options
  57. });
  58. }, events["change " + this.ui.enumTypeSelector] = function(e) {
  59. var emumValue = this.ui.enumTypeSelector.select2("data")[0] ? this.ui.enumTypeSelector.select2("data")[0].text : this.ui.enumTypeSelector.val();
  60. this.model.set({
  61. typeName: emumValue
  62. }), this.model.get("multiValueSelect") && this.model.set({
  63. typeName: "array<" + emumValue + ">"
  64. }), "" == emumValue || null == emumValue ? this.ui.enumValueSelectorContainer.hide() : (this.ui.enumValueSelectorContainer.show(),
  65. this.showEnumValues(_.escape(emumValue)));
  66. }, events["change " + this.ui.enumValueSelector] = function(e) {
  67. this.model.set({
  68. enumValues: this.ui.enumValueSelector.val()
  69. });
  70. }, events["change " + this.ui.multiValueSelectStatus] = function(e) {
  71. this.model.set({
  72. multiValueSelect: e.target.checked
  73. });
  74. var typename = this.model.get("typeName");
  75. typename = e.target.checked ? "array<" + typename + ">" : typename.replace("array<", "").replace(">", ""),
  76. this.model.set({
  77. typeName: typename
  78. });
  79. }, events["change " + this.ui.entityTypeSelector] = function(e) {
  80. var options = this.model.get("options") || {};
  81. options.applicableEntityTypes = JSON.stringify(this.ui.entityTypeSelector.val()),
  82. this.model.set({
  83. options: options
  84. });
  85. }, events["click " + this.ui.close] = "onCloseButton", events["click " + this.ui.createNewEnum] = "onCreateUpdateEnum",
  86. events;
  87. },
  88. initialize: function(options) {
  89. _.extend(this, _.pick(options, "typeHeaders", "businessMetadataDefCollection", "enumDefCollection", "isAttrEdit", "viewId", "collection")),
  90. this.viewId = options.model ? options.model.cid : this.viewId;
  91. },
  92. onRender: function() {
  93. var that = this, entitytypes = "", searchWeightValue = "5", stringLengthValue = "50", applicableEntityType = "";
  94. if (this.typeHeaders.fullCollection.each(function(model) {
  95. "ENTITY" == model.toJSON().category && (that.ui.entityTypeSelector.append("<option>" + model.get("name") + "</option>"),
  96. entitytypes += '<option value="' + model.get("name") + '" data-name="' + model.get("name") + '">' + model.get("name") + "</option>");
  97. }), this.ui.entityTypeSelector.select2({
  98. placeholder: "选择实体类型",
  99. allowClear: !0,
  100. multiple: !0,
  101. selectionAdapter: $.fn.select2.amd.require("TagHideDeleteButtonAdapter")
  102. }), this.ui.entityTypeSelector.html(entitytypes), this.ui.entityTypeSelector.on("select2:open", function(e) {
  103. $(".select2-dropdown--below").addClass("remove-from-list");
  104. }), this.model.get("searchWeight") && (searchWeightValue = this.model.get("searchWeight") === -1 ? 0 : this.model.get("searchWeight")),
  105. this.model.get("options") && (stringLengthValue = this.model.get("options").maxStrLength || "50",
  106. applicableEntityType = this.model.get("options").applicableEntityTypes ? JSON.parse(this.model.get("options").applicableEntityTypes) : null),
  107. this.ui.stringLengthValue.val(stringLengthValue).trigger("change"), this.ui.searchWeightSelector.val(searchWeightValue).trigger("change"),
  108. this.ui.enumValueSelector.attr("disabled", "false"), this.emumTypeSelectDisplay(),
  109. this.ui.enumTypeSelectorContainer.hide(), this.ui.enumTypeSelector.hide(), this.ui.enumValueSelectorContainer.hide(),
  110. this.isAttrEdit) {
  111. var typeName = this.model.get("typeName");
  112. this.ui.close.hide(), this.ui.createNewEnum.hide(), this.ui.attributeInput.val(this.model.get("name")),
  113. this.ui.attributeInput.attr("disabled", "false"), this.ui.dataTypeSelector.attr("disabled", "false"),
  114. this.ui.dataTypeSelector.attr("disabled", "false"), this.ui.multiValueSelect.hide(),
  115. this.ui.dataTypeSelector.val(typeName), "string" == typeName ? (this.ui.stringLengthContainer.show(),
  116. this.ui.stringLengthValue.show()) : (this.ui.stringLengthContainer.hide(), this.ui.stringLengthValue.hide()),
  117. applicableEntityType && _.each(applicableEntityType, function(valName) {
  118. that.ui.entityTypeSelector.find("option").each(function(o) {
  119. var $el = $(this);
  120. $el.data("name") === valName && $el.attr("data-allowremove", "false");
  121. });
  122. }), this.ui.entityTypeSelector.val(applicableEntityType).trigger("change"), "string" != typeName && "boolean" != typeName && "byte" != typeName && "short" != typeName && "int" != typeName && "float" != typeName && "double" != typeName && "long" != typeName && "date" != typeName && (this.ui.enumTypeSelector.attr("disabled", "false"),
  123. this.ui.dataTypeSelector.val("enumeration").trigger("change"), this.ui.enumTypeSelector.val(typeName).trigger("change")),
  124. this.model.get("multiValued") && (this.ui.multiValueSelect.show(), $(this.ui.multiValueSelectStatus).prop("checked", !0).trigger("change"),
  125. this.ui.multiValueSelectStatus.attr("disabled", "false"));
  126. }
  127. },
  128. showEnumValues: function(enumName) {
  129. var enumValues = "", selectedValues = [], selectedEnum = this.enumDefCollection.fullCollection.findWhere({
  130. name: enumName
  131. }), selectedEnumValues = selectedEnum ? selectedEnum.get("elementDefs") : null;
  132. _.each(selectedEnumValues, function(enumVal, index) {
  133. selectedValues.push(enumVal.value), enumValues += "<option>" + _.escape(enumVal.value) + "</option>";
  134. }), this.ui.enumValueSelector.empty(), this.ui.enumValueSelector.append(enumValues),
  135. this.ui.enumValueSelector.val(selectedValues), this.ui.enumValueSelector.select2({
  136. placeholder: "Select Enum value",
  137. allowClear: !1,
  138. tags: !1,
  139. multiple: !0
  140. }), this.model.set({
  141. enumValues: this.ui.enumValueSelector.val()
  142. });
  143. },
  144. emumTypeSelectDisplay: function() {
  145. var enumTypes = "";
  146. this.enumDefCollection.fullCollection.each(function(model, index) {
  147. enumTypes += "<option>" + _.escape(model.get("name")) + "</option>";
  148. }), this.ui.enumTypeSelector.empty(), this.ui.enumTypeSelector.append(enumTypes),
  149. this.ui.enumTypeSelector.val(""), this.ui.enumTypeSelector.select2({
  150. placeholder: "Select Enum name",
  151. tags: !1,
  152. allowClear: !0,
  153. multiple: !1
  154. });
  155. },
  156. onCreateUpdateEnum: function(e) {
  157. var that = this;
  158. require([ "views/business_metadata/EnumCreateUpdateItemView", "modules/Modal" ], function(EnumCreateUpdateItemView, Modal) {
  159. var view = new EnumCreateUpdateItemView({
  160. onUpdateEnum: function() {
  161. that.ui.enumValueSelectorContainer.hide(), that.emumTypeSelectDisplay(), that.ui.enumValueSelector.empty();
  162. },
  163. closeModal: function() {
  164. modal.trigger("cancel"), that.enumDefCollection.fetch({
  165. success: function() {
  166. that.ui.enumTypeSelector.val(that.model.get("typeName")).trigger("change");
  167. }
  168. });
  169. },
  170. enumDefCollection: that.enumDefCollection,
  171. businessMetadataDefCollection: that.businessMetadataDefCollection
  172. }), modal = new Modal({
  173. title: "Create/ Update Enum",
  174. content: view,
  175. cancelText: "Cancel",
  176. okCloses: !1,
  177. okText: "Update",
  178. allowCancel: !0,
  179. showFooter: !1
  180. }).open();
  181. modal.on("closeModal", function() {
  182. modal.trigger("cancel");
  183. });
  184. });
  185. },
  186. onCloseButton: function() {
  187. var tagName = this.$el.find('[data-id="tagName"]').val();
  188. this.collection.models.length > 0 && this.model.destroy(), 0 == this.collection.models.length && "" != tagName && this.$el.parent().next().find("button.ok").removeAttr("disabled");
  189. }
  190. });
  191. });