CreateEntityLayoutView.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. define([ "require", "backbone", "hbs!tmpl/entity/CreateEntityLayoutView_tmpl", "utils/Utils", "collection/VTagList", "collection/VEntityList", "models/VEntity", "modules/Modal", "utils/Messages", "moment", "utils/UrlLinks", "collection/VSearchList", "utils/Enums", "utils/Globals", "daterangepicker" ], function(require, Backbone, CreateEntityLayoutViewTmpl, Utils, VTagList, VEntityList, VEntity, Modal, Messages, moment, UrlLinks, VSearchList, Enums, Globals) {
  2. var CreateEntityLayoutView = Backbone.Marionette.LayoutView.extend({
  3. _viewName: "CreateEntityLayoutView",
  4. template: CreateEntityLayoutViewTmpl,
  5. templateHelpers: function() {
  6. return {
  7. guid: this.guid
  8. };
  9. },
  10. regions: {},
  11. ui: {
  12. entityName: "[data-id='entityName']",
  13. entityList: "[data-id='entityList']",
  14. entityInputData: "[data-id='entityInputData']",
  15. toggleRequired: 'input[name="toggleRequired"]',
  16. assetName: "[data-id='assetName']",
  17. entityInput: "[data-id='entityInput']",
  18. entitySelectionBox: "[data-id='entitySelectionBox']"
  19. },
  20. events: function() {
  21. var events = {};
  22. return events["change " + this.ui.entityList] = "onEntityChange", events["change " + this.ui.toggleRequired] = function(e) {
  23. this.requiredAllToggle(e.currentTarget.checked);
  24. }, events;
  25. },
  26. initialize: function(options) {
  27. _.extend(this, _.pick(options, "guid", "callback", "showLoader", "entityDefCollection", "typeHeaders", "searchVent"));
  28. var entityTitle, okLabel, that = this;
  29. this.selectStoreCollection = new Backbone.Collection(), this.collection = new VEntityList(),
  30. this.entityModel = new VEntity(), this.guid && (this.collection.modelAttrName = "createEntity"),
  31. this.asyncReferEntityCounter = 0, this.required = !0, this.guid ? (entityTitle = "编辑实体",
  32. okLabel = "更新") : (entityTitle = "创建实体", okLabel = "创建"), this.modal = new Modal({
  33. title: entityTitle,
  34. content: this,
  35. cancelText: "取消",
  36. okText: okLabel,
  37. allowCancel: !0,
  38. okCloses: !1,
  39. width: "50%"
  40. }).open(), this.modal.$el.find("button.ok").attr("disabled", !0), this.modal.on("ok", function(e) {
  41. that.modal.$el.find("button.ok").showButtonLoader(), that.okButton();
  42. }), this.modal.on("closeModal", function() {
  43. that.modal.trigger("cancel");
  44. });
  45. },
  46. bindEvents: function() {
  47. this.listenTo(this.collection, "reset", function() {
  48. this.entityCollectionList();
  49. }, this), this.listenTo(this.collection, "error", function() {
  50. this.hideLoader();
  51. }, this);
  52. },
  53. onRender: function() {
  54. this.bindEvents(), this.guid || this.bindRequiredField(), this.showLoader(), this.fetchCollections(),
  55. this.$(".toggleRequiredSwitch").hide();
  56. },
  57. bindRequiredField: function() {
  58. var that = this;
  59. this.ui.entityInputData.on("keyup change", "textarea", function(e) {
  60. var value = this.value;
  61. if (!value.length && $(this).hasClass("false")) $(this).removeClass("errorClass"),
  62. that.modal.$el.find("button.ok").prop("disabled", !1); else try {
  63. value && value.length && (JSON.parse(value), $(this).removeClass("errorClass"),
  64. that.modal.$el.find("button.ok").prop("disabled", !1));
  65. } catch (err) {
  66. $(this).addClass("errorClass"), that.modal.$el.find("button.ok").prop("disabled", !0);
  67. }
  68. }), this.ui.entityInputData.on("keyup change", "input.true,select.true", function(e) {
  69. "" !== this.value.trim() ? ($(this).data("select2") ? $(this).data("select2").$container.find(".select2-selection").removeClass("errorClass") : $(this).removeClass("errorClass"),
  70. 0 === that.ui.entityInputData.find(".errorClass").length && that.modal.$el.find("button.ok").prop("disabled", !1)) : (that.modal.$el.find("button.ok").prop("disabled", !0),
  71. $(this).data("select2") ? $(this).data("select2").$container.find(".select2-selection").addClass("errorClass") : $(this).addClass("errorClass"));
  72. });
  73. },
  74. bindNonRequiredField: function() {
  75. var that = this;
  76. this.ui.entityInputData.off("keyup change", "input.false,select.false").on("keyup change", "input.false,select.false", function(e) {
  77. that.modal.$el.find("button.ok").prop("disabled") && 0 === that.ui.entityInputData.find(".errorClass").length && that.modal.$el.find("button.ok").prop("disabled", !1);
  78. });
  79. },
  80. decrementCounter: function(counter) {
  81. this[counter] > 0 && --this[counter];
  82. },
  83. fetchCollections: function() {
  84. this.guid ? (this.collection.url = UrlLinks.entitiesApiUrl({
  85. guid: this.guid
  86. }), this.collection.fetch({
  87. reset: !0
  88. })) : this.entityCollectionList();
  89. },
  90. entityCollectionList: function() {
  91. this.ui.entityList.empty();
  92. var that = this, name = "";
  93. if (this.guid) {
  94. this.collection.each(function(val) {
  95. name += Utils.getName(val.get("entity")), that.entityData = val;
  96. }), this.ui.assetName.html(name);
  97. var referredEntities = this.entityData.get("referredEntities"), attributes = this.entityData.get("entity").attributes;
  98. _.map(_.keys(attributes), function(key) {
  99. if (_.isObject(attributes[key])) {
  100. var attrObj = attributes[key];
  101. _.isObject(attrObj) && !_.isArray(attrObj) && (attrObj = [ attrObj ]), _.each(attrObj, function(obj) {
  102. obj.guid && !referredEntities[obj.guid] && (++that.asyncReferEntityCounter, that.collection.url = UrlLinks.entitiesApiUrl({
  103. guid: obj.guid
  104. }), that.collection.fetch({
  105. success: function(data, response) {
  106. referredEntities[obj.guid] = response.entity;
  107. },
  108. complete: function() {
  109. that.decrementCounter("asyncReferEntityCounter"), 0 === that.asyncReferEntityCounter && that.onEntityChange(null, that.entityData);
  110. },
  111. silent: !0
  112. }));
  113. });
  114. }
  115. }), 0 === this.asyncReferEntityCounter && this.onEntityChange(null, this.entityData);
  116. } else {
  117. var str = '<option disabled="disabled" selected>--请选择实体类型--</option>';
  118. this.entityDefCollection.fullCollection.each(function(val) {
  119. var name = Utils.getName(val.toJSON());
  120. Globals.entityTypeConfList && 0 !== name.indexOf("__") && (_.isEmptyArray(Globals.entityTypeConfList) ? str += "<option>" + name + "</option>" : _.contains(Globals.entityTypeConfList, val.get("name")) && (str += "<option>" + name + "</option>"));
  121. }), this.ui.entityList.html(str), this.ui.entityList.select2({}), this.hideLoader();
  122. }
  123. },
  124. capitalize: function(string) {
  125. return string.charAt(0).toUpperCase() + string.slice(1);
  126. },
  127. requiredAllToggle: function(checked) {
  128. checked ? (this.ui.entityInputData.addClass("all").removeClass("required"), this.ui.entityInputData.find("div.true").show(),
  129. this.ui.entityInputData.find("fieldset div.true").show(), this.ui.entityInputData.find("fieldset").show(),
  130. this.required = !1) : (this.ui.entityInputData.addClass("required").removeClass("all"),
  131. this.ui.entityInputData.find("fieldset").each(function() {
  132. $(this).find("div").hasClass("false") || $(this).hide();
  133. }), this.ui.entityInputData.find("div.true").hide(), this.ui.entityInputData.find("fieldset div.true").hide(),
  134. this.required = !0);
  135. },
  136. onEntityChange: function(e, value) {
  137. var that = this, typeName = value && value.get("entity") ? value.get("entity").typeName : null;
  138. this.guid || this.showLoader(), this.ui.entityInputData.empty(), typeName ? this.collection.url = UrlLinks.entitiesDefApiUrl(typeName) : e && (this.collection.url = UrlLinks.entitiesDefApiUrl(e.target.value),
  139. this.collection.modelAttrName = "attributeDefs"), this.collection.fetch({
  140. success: function(model, data) {
  141. that.supuertypeFlag = 0, that.subAttributeData(data);
  142. },
  143. complete: function() {
  144. that.modal.$el.find("button.ok").prop("disabled", !0);
  145. },
  146. silent: !0
  147. });
  148. },
  149. renderAttribute: function(object) {
  150. var that = this, visitedAttr = {}, attributeObj = object.attributeDefs, isAllAttributeOptinal = !0, isAllRelationshipAttributeOptinal = !0, attributeDefList = attributeObj.attributeDefs, relationshipAttributeDefsList = attributeObj.relationshipAttributeDefs, attributeHtml = "", relationShipAttributeHtml = "", fieldElemetHtml = "", commonInput = function(object) {
  151. var containerHtml = (object.value, "");
  152. return containerHtml += that.getContainer(object);
  153. };
  154. return (attributeDefList.length || relationshipAttributeDefsList.length) && (_.each(attributeDefList, function(value) {
  155. value.isOptional === !1 && (isAllAttributeOptinal = !1), attributeHtml += commonInput({
  156. value: value,
  157. duplicateValue: !1,
  158. isAttribute: !0
  159. });
  160. }), _.each(relationshipAttributeDefsList, function(value) {
  161. if (value.isOptional === !1 && (isAllRelationshipAttributeOptinal = !1), null === visitedAttr[value.name]) {
  162. var duplicateRelationship = _.where(relationshipAttributeDefsList, {
  163. name: value.name
  164. }), str = '<option value="">--请选择一个关联类型--</option>';
  165. _.each(duplicateRelationship, function(val, index, list) {
  166. str += "<option>" + _.escape(val.relationshipTypeName) + "</option>";
  167. });
  168. value.isOptional;
  169. visitedAttr[value.name] = '<div class="form-group"><select class="form-control row-margin-bottom entityInputBox ' + (value.isOptional === !0 ? "false" : "true") + '" data-for-key= "' + value.name + '"> ' + str + "</select></div>";
  170. } else relationShipAttributeHtml += commonInput({
  171. value: value,
  172. duplicateValue: !0,
  173. isRelation: !0
  174. }), visitedAttr[value.name] = null;
  175. }), attributeHtml.length && (fieldElemetHtml += that.getFieldElementContainer({
  176. htmlField: attributeHtml,
  177. attributeType: !0,
  178. alloptional: isAllAttributeOptinal
  179. })), relationShipAttributeHtml.length && (fieldElemetHtml += that.getFieldElementContainer({
  180. htmlField: relationShipAttributeHtml,
  181. relationshipType: !0,
  182. alloptional: isAllRelationshipAttributeOptinal
  183. })), fieldElemetHtml.length ? (that.ui.entityInputData.append(fieldElemetHtml),
  184. _.each(_.keys(visitedAttr), function(key) {
  185. if (null !== visitedAttr[key]) {
  186. var elFound = that.ui.entityInputData.find('[data-key="' + key + '"]');
  187. elFound.prop("disabled", !0), elFound.parent().prepend(visitedAttr[key]);
  188. }
  189. })) : (fieldElemetHtml = "<h4 class='text-center'>Defination not found</h4>", that.ui.entityInputData.append(fieldElemetHtml))),
  190. that.ui.entityInputData.find("select[data-for-key]").select2({}).on("change", function() {
  191. var forKey = $(this).data("forKey"), forKeyEl = null;
  192. forKey && forKey.length && (forKeyEl = that.ui.entityInputData.find('[data-key="' + forKey + '"]'),
  193. forKeyEl && ("" == this.value ? (forKeyEl.val(null).trigger("change"), forKeyEl.prop("disabled", !0)) : forKeyEl.prop("disabled", !1)));
  194. }), !1;
  195. },
  196. subAttributeData: function(data) {
  197. var that = this, attributeDefs = Utils.getNestedSuperTypeObj({
  198. seperateRelatioshipAttr: !0,
  199. attrMerge: !0,
  200. data: data,
  201. collection: this.entityDefCollection
  202. });
  203. attributeDefs && attributeDefs.relationshipAttributeDefs.length && (attributeDefs.attributeDefs = _.filter(attributeDefs.attributeDefs, function(obj) {
  204. if (void 0 === _.find(attributeDefs.relationshipAttributeDefs, {
  205. name: obj.name
  206. })) return !0;
  207. })), attributeDefs.attributeDefs.length || attributeDefs.relationshipAttributeDefs.length ? this.$(".toggleRequiredSwitch").show() : this.$(".toggleRequiredSwitch").hide(),
  208. this.renderAttribute({
  209. attributeDefs: attributeDefs
  210. }), this.required && (this.ui.entityInputData.find("fieldset div.true").hide(),
  211. this.ui.entityInputData.find("div.true").hide()), "placeholder" in HTMLInputElement.prototype || this.ui.entityInputData.find("input,select,textarea").placeholder(),
  212. that.initilizeElements();
  213. },
  214. initilizeElements: function() {
  215. var that = this, $createTime = this.modal.$el.find('input[name="createTime"]'), dateObj = {
  216. singleDatePicker: !0,
  217. showDropdowns: !0,
  218. startDate: new Date(),
  219. locale: {
  220. format: Globals.dateFormat
  221. }
  222. };
  223. this.$('input[data-type="date"]').each(function() {
  224. $(this).data("daterangepicker") || (that.guid && this.value.length && (dateObj.startDate = new Date(Number(this.value))),
  225. "modifiedTime" === $(this).attr("name") && (dateObj.minDate = $createTime.val()),
  226. $(this).daterangepicker(dateObj));
  227. }), modifiedDateObj = _.extend({}, dateObj), $createTime.on("apply.daterangepicker", function(ev, picker) {
  228. that.modal.$el.find('input[name="modifiedTime"]').daterangepicker(_.extend(modifiedDateObj, {
  229. minDate: $createTime.val()
  230. }));
  231. }), this.initializeValidation(), this.ui.entityInputData.find("fieldset").length > 0 && 0 === this.ui.entityInputData.find("select.true,input.true").length && (this.requiredAllToggle(0 === this.ui.entityInputData.find("select.true,input.true").length),
  232. this.guid || this.bindNonRequiredField()), this.$('select[data-type="boolean"]').each(function(value, key) {
  233. var dataKey = $(key).data("key");
  234. if (that.entityData) {
  235. var setValue = that.entityData.get("entity").attributes[dataKey];
  236. this.value = setValue;
  237. }
  238. }), this.addJsonSearchData();
  239. },
  240. initializeValidation: function() {
  241. var regex = /^[0-9]*((?=[^.]|$))?$/, removeText = function(e, value) {
  242. if (!regex.test(value)) {
  243. var txtfld = e.currentTarget, newtxt = txtfld.value.slice(0, txtfld.value.length - 1);
  244. txtfld.value = newtxt;
  245. }
  246. };
  247. this.$('input[data-type="int"],input[data-type="long"]').on("keydown", function(e) {
  248. if (!regex.test(e.currentTarget.value)) return !1;
  249. }), this.$('input[data-type="int"],input[data-type="long"]').on("paste", function(e) {
  250. return !1;
  251. }), this.$('input[data-type="long"],input[data-type="int"]').on("keyup click", function(e) {
  252. removeText(e, e.currentTarget.value);
  253. }), this.$('input[data-type="date"]').on("hide.daterangepicker keydown", function(event) {
  254. if (event.type) if ("hide" == event.type) this.blur(); else if ("keydown" == event.type) return !1;
  255. });
  256. },
  257. getContainer: function(object) {
  258. var value = object.value, entityLabel = this.capitalize(_.escape(value.name));
  259. return '<div class=" row ' + value.isOptional + '"><span class="col-sm-3"><label><span class="' + (value.isOptional ? "true" : "false required") + '">' + entityLabel + '</span><span class="center-block ellipsis-with-margin text-gray" title="日期类型: ' + value.typeName + '">(' + _.escape(value.typeName) + ')</span></label></span><span class="col-sm-9">' + this.getElement(object) + "</input></span></div>";
  260. },
  261. getFieldElementContainer: function(object) {
  262. var htmlField = object.htmlField, relationshipType = (!!object.attributeType && object.attributeType,
  263. !!object.relationshipType && object.relationshipType), alloptional = object.alloptional, typeOfDefination = relationshipType ? "Relationships" : "Attributes";
  264. return '<div class="attribute-dash-box ' + (alloptional ? "alloptional" : "") + ' "><span class="attribute-type-label">' + typeOfDefination + "</span>" + htmlField + "</div>";
  265. },
  266. getSelect: function(object) {
  267. var value = object.value, name = _.escape(value.name), entityValue = _.escape(object.entityValue), isAttribute = object.isAttribute, isRelation = object.isRelation;
  268. if ("boolean" === value.typeName) return '<select class="form-control row-margin-bottom ' + (value.isOptional === !0 ? "false" : "true") + '" data-type="' + value.typeName + '" data-attribute="' + isAttribute + '" data-relation="' + isRelation + '" data-key="' + name + '" data-id="entityInput"><option value="">--选择是或否--</option><option value="true">是</option><option value="false">否</option></select>';
  269. var splitTypeName = value.typeName.split("<");
  270. return splitTypeName = splitTypeName.length > 1 ? splitTypeName[1].split(">")[0] : value.typeName,
  271. '<select class="form-control row-margin-bottom entityInputBox ' + (value.isOptional === !0 ? "false" : "true") + '" data-type="' + value.typeName + '" data-attribute="' + isAttribute + '" data-relation="' + isRelation + '" data-key="' + name + '" data-id="entitySelectData" data-queryData="' + splitTypeName + '">' + (this.guid ? entityValue : "") + "</select>";
  272. },
  273. getTextArea: function(object) {
  274. var value = object.value, name = _.escape(value.name), setValue = _.escape(object.entityValue), isAttribute = object.isAttribute, isRelation = object.isRelation, structType = object.structType;
  275. try {
  276. if (structType && entityValue && entityValue.length) {
  277. var parseValue = JSON.parse(entityValue);
  278. _.isObject(parseValue) && !_.isArray(parseValue) && parseValue.attributes && (setValue = JSON.stringify(parseValue.attributes));
  279. }
  280. } catch (err) {}
  281. return '<textarea class="form-control entityInputBox ' + (value.isOptional === !0 ? "false" : "true") + '" data-type="' + value.typeName + '" data-key="' + name + '" data-attribute="' + isAttribute + '" data-relation="' + isRelation + '" placeholder="' + name + '" data-id="entityInput">' + setValue + "</textarea>";
  282. },
  283. getInput: function(object) {
  284. var value = object.value, name = _.escape(value.name), entityValue = _.escape(object.entityValue), isAttribute = object.isAttribute, isRelation = object.isRelation;
  285. return '<input class="form-control entityInputBox ' + (value.isOptional === !0 ? "false" : "true") + '" data-type="' + value.typeName + '" value="' + entityValue + '" data-key="' + name + '" data-attribute="' + isAttribute + '" data-relation="' + isRelation + '" placeholder="' + name + '" name="' + name + '" data-id="entityInput">';
  286. },
  287. getElement: function(object) {
  288. var value = object.value, isAttribute = object.isAttribute, isRelation = object.isRelation, typeName = value.typeName, entityValue = "";
  289. if (this.guid) {
  290. var dataValue = this.entityData.get("entity").attributes[value.name];
  291. _.isObject(dataValue) ? entityValue = JSON.stringify(dataValue) : (dataValue && (entityValue = dataValue),
  292. "date" === value.typeName && (entityValue = dataValue ? moment(dataValue) : Utils.formatDate({
  293. zone: !1,
  294. dateFormat: Globals.dateFormat
  295. })));
  296. }
  297. if (typeName && this.entityDefCollection.fullCollection.find({
  298. name: typeName
  299. }) || "boolean" === typeName || typeName.indexOf("array") > -1) return this.getSelect({
  300. value: value,
  301. entityValue: entityValue,
  302. isAttribute: isAttribute,
  303. isRelation: isRelation
  304. });
  305. if (typeName.indexOf("map") > -1) return this.getTextArea({
  306. value: value,
  307. entityValue: entityValue,
  308. isAttribute: isAttribute,
  309. isRelation: isRelation,
  310. structType: !1
  311. });
  312. var typeNameCategory = this.typeHeaders.fullCollection.findWhere({
  313. name: typeName
  314. });
  315. return typeNameCategory && "STRUCT" === typeNameCategory.get("category") ? this.getTextArea({
  316. value: value,
  317. entityValue: entityValue,
  318. isAttribute: isAttribute,
  319. isRelation: isRelation,
  320. structType: !0
  321. }) : this.getInput({
  322. value: value,
  323. entityValue: entityValue,
  324. isAttribute: isAttribute,
  325. isRelation: isRelation
  326. });
  327. },
  328. okButton: function() {
  329. var that = this;
  330. this.showLoader({
  331. editVisiblityOfEntitySelectionBox: !0
  332. }), this.parentEntity = this.ui.entityList.val();
  333. var entityAttribute = {}, referredEntities = {}, relationshipAttribute = {}, extractValue = function(value, typeName) {
  334. if (!value) return value;
  335. if (_.isArray(value)) {
  336. var parseData = [];
  337. _.map(value, function(val) {
  338. parseData.push({
  339. guid: val,
  340. typeName: typeName
  341. });
  342. });
  343. } else var parseData = {
  344. guid: value,
  345. typeName: typeName
  346. };
  347. return parseData;
  348. };
  349. try {
  350. this.ui.entityInputData.find("input,select,textarea").each(function() {
  351. var value = $(this).val(), el = this;
  352. if ($(this).val() && $(this).val().trim && (value = $(this).val().trim()), "TEXTAREA" === this.nodeName) try {
  353. value && value.length && (JSON.parse(value), $(this).removeClass("errorClass"));
  354. } catch (err) {
  355. throw new Error(err.message);
  356. }
  357. if ($(this).hasClass("true") && ("" == value || void 0 == value)) throw $(this).data("select2") ? $(this).data("select2").$container.find(".select2-selection").addClass("errorClass") : $(this).addClass("errorClass"),
  358. that.hideLoader(), that.modal.$el.find("button.ok").hideButtonLoader(), new Error("Please fill the required fields");
  359. var dataTypeEnitity = $(this).data("type"), datakeyEntity = $(this).data("key"), typeName = $(this).data("querydata"), attribute = "undefined" != $(this).data("attribute"), relation = "undefined" != $(this).data("relation"), typeNameCategory = that.typeHeaders.fullCollection.findWhere({
  360. name: dataTypeEnitity
  361. }), val = null;
  362. if (dataTypeEnitity && datakeyEntity) {
  363. if (that.entityDefCollection.fullCollection.find({
  364. name: dataTypeEnitity
  365. })) val = extractValue(value, typeName); else if ("date" === dataTypeEnitity || "time" === dataTypeEnitity) val = Date.parse(value); else if (dataTypeEnitity.indexOf("map") > -1 || typeNameCategory && "STRUCT" === typeNameCategory.get("category")) try {
  366. value && value.length && (parseData = JSON.parse(value), val = parseData);
  367. } catch (err) {
  368. throw $(this).addClass("errorClass"), new Error(datakeyEntity + " : " + err.message);
  369. } else val = dataTypeEnitity.indexOf("array") > -1 && dataTypeEnitity.indexOf("string") === -1 ? extractValue(value, typeName) : _.isString(value) ? value.length ? value : null : value;
  370. attribute ? entityAttribute[datakeyEntity] = val : relation && (relationshipAttribute[datakeyEntity] = val);
  371. } else {
  372. var dataRelEntity = $(this).data("forKey");
  373. dataRelEntity && relationshipAttribute[dataRelEntity] && (_.isArray(relationshipAttribute[dataRelEntity]) ? _.each(relationshipAttribute[dataRelEntity], function(obj) {
  374. obj && (obj.relationshipType = $(el).val());
  375. }) : relationshipAttribute[dataRelEntity].relationshipType = $(this).val());
  376. }
  377. });
  378. var entityJson = {
  379. entity: {
  380. typeName: this.guid ? this.entityData.get("entity").typeName : this.parentEntity,
  381. attributes: entityAttribute,
  382. relationshipAttributes: relationshipAttribute,
  383. guid: this.guid ? this.guid : -1
  384. },
  385. referredEntities: referredEntities
  386. };
  387. this.entityModel.createOreditEntity({
  388. data: JSON.stringify(entityJson),
  389. type: "POST",
  390. success: function(model, response) {
  391. that.modal.$el.find("button.ok").hideButtonLoader(), that.modal.close();
  392. var msgType = model.mutatedEntities && model.mutatedEntities.UPDATE ? "editSuccessMessage" : "addSuccessMessage";
  393. if (Utils.notifySuccess({
  394. content: "实体" + Messages.getAbbreviationMsg(!1, msgType)
  395. }), that.guid && that.callback) that.callback(); else if (model.mutatedEntities) {
  396. var mutatedEntities = model.mutatedEntities.CREATE || model.mutatedEntities.UPDATE;
  397. mutatedEntities && _.isArray(mutatedEntities) && mutatedEntities[0] && mutatedEntities[0].guid && Utils.setUrl({
  398. url: "#!/detailPage/" + mutatedEntities[0].guid,
  399. mergeBrowserUrl: !1,
  400. trigger: !0
  401. }), that.searchVent && that.searchVent.trigger("entityList:refresh");
  402. }
  403. },
  404. complete: function() {
  405. that.hideLoader({
  406. editVisiblityOfEntitySelectionBox: !0
  407. }), that.modal.$el.find("button.ok").hideButtonLoader();
  408. }
  409. });
  410. } catch (e) {
  411. Utils.notifyError({
  412. content: e.message
  413. }), that.hideLoader({
  414. editVisiblityOfEntitySelectionBox: !0
  415. });
  416. }
  417. },
  418. showLoader: function(options) {
  419. var editVisiblityOfEntitySelectionBox = options && options.editVisiblityOfEntitySelectionBox;
  420. this.$(".entityLoader").addClass("show"), this.$(".entityInputData").hide(), (this.guid || editVisiblityOfEntitySelectionBox) && this.ui.entitySelectionBox.hide();
  421. },
  422. hideLoader: function(options) {
  423. var editVisiblityOfEntitySelectionBox = options && options.editVisiblityOfEntitySelectionBox;
  424. this.$(".entityLoader").removeClass("show"), this.$(".entityInputData").show(),
  425. (this.guid || editVisiblityOfEntitySelectionBox) && this.ui.entitySelectionBox.show(),
  426. this.ui.entityList.select2("open"), this.ui.entityList.select2("close");
  427. },
  428. addJsonSearchData: function() {
  429. var that = this;
  430. this.$('select[data-id="entitySelectData"]').each(function(value, key) {
  431. var $this = $(this), keyData = $(this).data("key"), typeData = $(this).data("type"), queryData = $(this).data("querydata"), placeholderName = ($(this).data("skip"),
  432. "请从以下列表中选择一个" + typeData);
  433. if ($this.attr("multiple", $this.data("type").indexOf("array") !== -1), that.guid) {
  434. var dataValue = that.entityData.get("entity").attributes[keyData], relationshipType = (that.entityData.get("entity").attributes,
  435. that.entityData.get("entity").relationshipAttributes ? that.entityData.get("entity").relationshipAttributes[keyData] : null), referredEntities = that.entityData.get("referredEntities"), selectedValue = [], select2Options = [];
  436. if (dataValue && (_.isObject(dataValue) && !_.isArray(dataValue) && (dataValue = [ dataValue ]),
  437. _.each(dataValue, function(obj) {
  438. if (_.isObject(obj) && obj.guid && referredEntities[obj.guid]) {
  439. var refEntiyFound = referredEntities[obj.guid];
  440. refEntiyFound.id = refEntiyFound.guid, Enums.entityStateReadOnly[refEntiyFound.status] || (select2Options.push(refEntiyFound),
  441. selectedValue.push(refEntiyFound.guid));
  442. }
  443. }), _.isUndefined(relationshipType) || relationshipType && relationshipType.relationshipAttributes && relationshipType.relationshipAttributes.typeName && that.$("select[data-for-key=" + keyData + "]").val(relationshipType.relationshipAttributes.typeName).trigger("change")),
  444. 0 === selectedValue.length && dataValue && dataValue.length && "string" === $this.data("querydata")) {
  445. var str = "";
  446. _.each(dataValue, function(obj) {
  447. _.isString(obj) && (selectedValue.push(obj), str += "<option>" + _.escape(obj) + "</option>");
  448. }), $this.html(str);
  449. }
  450. } else $this.val([]);
  451. var select2Option = {
  452. placeholder: placeholderName,
  453. allowClear: !0,
  454. tags: "string" == $this.data("querydata")
  455. }, getTypeAheadData = function(data, params) {
  456. var dataList = data.entities, foundOptions = [];
  457. return _.each(dataList, function(obj) {
  458. obj && (obj.guid && (obj.id = obj.guid), foundOptions.push(obj));
  459. }), foundOptions;
  460. };
  461. "string" !== $this.data("querydata") && _.extend(select2Option, {
  462. ajax: {
  463. url: UrlLinks.searchApiUrl("attribute"),
  464. dataType: "json",
  465. delay: 250,
  466. data: function(params) {
  467. return {
  468. attrValuePrefix: params.term,
  469. typeName: queryData,
  470. limit: 10,
  471. offset: 0
  472. };
  473. },
  474. processResults: function(data, params) {
  475. return {
  476. results: getTypeAheadData(data, params)
  477. };
  478. },
  479. cache: !0
  480. },
  481. templateResult: function(option) {
  482. var name = Utils.getName(option, "qualifiedName");
  483. return "-" === name ? option.text : name;
  484. },
  485. templateSelection: function(option) {
  486. var name = Utils.getName(option, "qualifiedName");
  487. return "-" === name ? option.text : name;
  488. },
  489. escapeMarkup: function(markup) {
  490. return markup;
  491. },
  492. data: select2Options,
  493. minimumInputLength: 1
  494. }), $this.select2(select2Option), selectedValue && $this.val(selectedValue).trigger("change");
  495. }), this.guid && (this.bindRequiredField(), this.bindNonRequiredField()), this.hideLoader();
  496. }
  497. });
  498. return CreateEntityLayoutView;
  499. });