CreateEntityLayoutView.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 = "Edit entity",
  32. okLabel = "Update") : (entityTitle = "Create entity", okLabel = "Create"), this.modal = new Modal({
  33. title: entityTitle,
  34. content: this,
  35. cancelText: "Cancel",
  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>--Select entity-type--</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="">--Select a Relationship Type--</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(),
  208. this.$(".entity-list").removeClass("col-sm-12").addClass("col-sm-8")) : (this.$(".toggleRequiredSwitch").hide(),
  209. this.$(".entity-list").removeClass("col-sm-8").addClass("col-sm-12")), this.renderAttribute({
  210. attributeDefs: attributeDefs
  211. }), this.required && (this.ui.entityInputData.find("fieldset div.true").hide(),
  212. this.ui.entityInputData.find("div.true").hide()), "placeholder" in HTMLInputElement.prototype || this.ui.entityInputData.find("input,select,textarea").placeholder(),
  213. that.initilizeElements();
  214. },
  215. initilizeElements: function() {
  216. var that = this, $createTime = this.modal.$el.find('input[name="createTime"]'), dateObj = {
  217. singleDatePicker: !0,
  218. showDropdowns: !0,
  219. startDate: new Date(),
  220. locale: {
  221. format: Globals.dateFormat
  222. }
  223. };
  224. this.$('input[data-type="date"]').each(function() {
  225. $(this).data("daterangepicker") || (that.guid && this.value.length && (dateObj.startDate = new Date(Number(this.value))),
  226. "modifiedTime" === $(this).attr("name") && (dateObj.minDate = $createTime.val()),
  227. $(this).daterangepicker(dateObj));
  228. }), modifiedDateObj = _.extend({}, dateObj), $createTime.on("apply.daterangepicker", function(ev, picker) {
  229. that.modal.$el.find('input[name="modifiedTime"]').daterangepicker(_.extend(modifiedDateObj, {
  230. minDate: $createTime.val()
  231. }));
  232. }), 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),
  233. this.guid || this.bindNonRequiredField()), this.$('select[data-type="boolean"]').each(function(value, key) {
  234. var dataKey = $(key).data("key");
  235. if (that.entityData) {
  236. var setValue = that.entityData.get("entity").attributes[dataKey];
  237. this.value = setValue;
  238. }
  239. }), this.addJsonSearchData();
  240. },
  241. initializeValidation: function() {
  242. var regex = /^[0-9]*((?=[^.]|$))?$/, removeText = function(e, value) {
  243. if (!regex.test(value)) {
  244. var txtfld = e.currentTarget, newtxt = txtfld.value.slice(0, txtfld.value.length - 1);
  245. txtfld.value = newtxt;
  246. }
  247. };
  248. this.$('input[data-type="int"],input[data-type="long"]').on("keydown", function(e) {
  249. if (!regex.test(e.currentTarget.value)) return !1;
  250. }), this.$('input[data-type="int"],input[data-type="long"]').on("paste", function(e) {
  251. return !1;
  252. }), this.$('input[data-type="long"],input[data-type="int"]').on("keyup click", function(e) {
  253. removeText(e, e.currentTarget.value);
  254. }), this.$('input[data-type="date"]').on("hide.daterangepicker keydown", function(event) {
  255. if (event.type) if ("hide" == event.type) this.blur(); else if ("keydown" == event.type) return !1;
  256. });
  257. },
  258. getContainer: function(object) {
  259. var value = object.value, entityLabel = this.capitalize(_.escape(value.name));
  260. 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="Data Type : ' + value.typeName + '">(' + _.escape(value.typeName) + ')</span></label></span><span class="col-sm-9">' + this.getElement(object) + "</input></span></div>";
  261. },
  262. getFieldElementContainer: function(object) {
  263. var htmlField = object.htmlField, relationshipType = (!!object.attributeType && object.attributeType,
  264. !!object.relationshipType && object.relationshipType), alloptional = object.alloptional, typeOfDefination = relationshipType ? "Relationships" : "Attributes";
  265. return '<div class="attribute-dash-box ' + (alloptional ? "alloptional" : "") + ' "><span class="attribute-type-label">' + typeOfDefination + "</span>" + htmlField + "</div>";
  266. },
  267. getSelect: function(object) {
  268. var value = object.value, name = _.escape(value.name), entityValue = _.escape(object.entityValue), isAttribute = object.isAttribute, isRelation = object.isRelation;
  269. 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="">--Select true or false--</option><option value="true">true</option><option value="false">false</option></select>';
  270. var splitTypeName = value.typeName.split("<");
  271. return splitTypeName = splitTypeName.length > 1 ? splitTypeName[1].split(">")[0] : value.typeName,
  272. '<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>";
  273. },
  274. getTextArea: function(object) {
  275. var value = object.value, name = _.escape(value.name), setValue = _.escape(object.entityValue), isAttribute = object.isAttribute, isRelation = object.isRelation, structType = object.structType;
  276. try {
  277. if (structType && entityValue && entityValue.length) {
  278. var parseValue = JSON.parse(entityValue);
  279. _.isObject(parseValue) && !_.isArray(parseValue) && parseValue.attributes && (setValue = JSON.stringify(parseValue.attributes));
  280. }
  281. } catch (err) {}
  282. 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>";
  283. },
  284. getInput: function(object) {
  285. var value = object.value, name = _.escape(value.name), entityValue = _.escape(object.entityValue), isAttribute = object.isAttribute, isRelation = object.isRelation;
  286. 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">';
  287. },
  288. getElement: function(object) {
  289. var value = object.value, isAttribute = object.isAttribute, isRelation = object.isRelation, typeName = value.typeName, entityValue = "";
  290. if (this.guid) {
  291. var dataValue = this.entityData.get("entity").attributes[value.name];
  292. _.isObject(dataValue) ? entityValue = JSON.stringify(dataValue) : (dataValue && (entityValue = dataValue),
  293. "date" === value.typeName && (entityValue = dataValue ? moment(dataValue) : Utils.formatDate({
  294. zone: !1,
  295. dateFormat: Globals.dateFormat
  296. })));
  297. }
  298. if (typeName && this.entityDefCollection.fullCollection.find({
  299. name: typeName
  300. }) || "boolean" === typeName || typeName.indexOf("array") > -1) return this.getSelect({
  301. value: value,
  302. entityValue: entityValue,
  303. isAttribute: isAttribute,
  304. isRelation: isRelation
  305. });
  306. if (typeName.indexOf("map") > -1) return this.getTextArea({
  307. value: value,
  308. entityValue: entityValue,
  309. isAttribute: isAttribute,
  310. isRelation: isRelation,
  311. structType: !1
  312. });
  313. var typeNameCategory = this.typeHeaders.fullCollection.findWhere({
  314. name: typeName
  315. });
  316. return typeNameCategory && "STRUCT" === typeNameCategory.get("category") ? this.getTextArea({
  317. value: value,
  318. entityValue: entityValue,
  319. isAttribute: isAttribute,
  320. isRelation: isRelation,
  321. structType: !0
  322. }) : this.getInput({
  323. value: value,
  324. entityValue: entityValue,
  325. isAttribute: isAttribute,
  326. isRelation: isRelation
  327. });
  328. },
  329. okButton: function() {
  330. var that = this;
  331. this.showLoader({
  332. editVisiblityOfEntitySelectionBox: !0
  333. }), this.parentEntity = this.ui.entityList.val();
  334. var entityAttribute = {}, referredEntities = {}, relationshipAttribute = {}, extractValue = function(value, typeName) {
  335. if (!value) return value;
  336. if (_.isArray(value)) {
  337. var parseData = [];
  338. _.map(value, function(val) {
  339. parseData.push({
  340. guid: val,
  341. typeName: typeName
  342. });
  343. });
  344. } else var parseData = {
  345. guid: value,
  346. typeName: typeName
  347. };
  348. return parseData;
  349. };
  350. try {
  351. this.ui.entityInputData.find("input,select,textarea").each(function() {
  352. var value = $(this).val(), el = this;
  353. if ($(this).val() && $(this).val().trim && (value = $(this).val().trim()), "TEXTAREA" === this.nodeName) try {
  354. value && value.length && (JSON.parse(value), $(this).removeClass("errorClass"));
  355. } catch (err) {
  356. throw new Error(err.message);
  357. }
  358. if ($(this).hasClass("true") && ("" == value || void 0 == value)) throw $(this).data("select2") ? $(this).data("select2").$container.find(".select2-selection").addClass("errorClass") : $(this).addClass("errorClass"),
  359. that.hideLoader(), that.modal.$el.find("button.ok").hideButtonLoader(), new Error("Please fill the required fields");
  360. 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({
  361. name: dataTypeEnitity
  362. }), val = null;
  363. if (dataTypeEnitity && datakeyEntity) {
  364. if (that.entityDefCollection.fullCollection.find({
  365. name: dataTypeEnitity
  366. })) 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 {
  367. value && value.length && (parseData = JSON.parse(value), val = parseData);
  368. } catch (err) {
  369. throw $(this).addClass("errorClass"), new Error(datakeyEntity + " : " + err.message);
  370. } else val = dataTypeEnitity.indexOf("array") > -1 && dataTypeEnitity.indexOf("string") === -1 ? extractValue(value, typeName) : _.isString(value) ? value.length ? value : null : value;
  371. attribute ? entityAttribute[datakeyEntity] = val : relation && (relationshipAttribute[datakeyEntity] = val);
  372. } else {
  373. var dataRelEntity = $(this).data("forKey");
  374. dataRelEntity && relationshipAttribute[dataRelEntity] && (_.isArray(relationshipAttribute[dataRelEntity]) ? _.each(relationshipAttribute[dataRelEntity], function(obj) {
  375. obj && (obj.relationshipType = $(el).val());
  376. }) : relationshipAttribute[dataRelEntity].relationshipType = $(this).val());
  377. }
  378. });
  379. var entityJson = {
  380. entity: {
  381. typeName: this.guid ? this.entityData.get("entity").typeName : this.parentEntity,
  382. attributes: entityAttribute,
  383. relationshipAttributes: relationshipAttribute,
  384. guid: this.guid ? this.guid : -1
  385. },
  386. referredEntities: referredEntities
  387. };
  388. this.entityModel.createOreditEntity({
  389. data: JSON.stringify(entityJson),
  390. type: "POST",
  391. success: function(model, response) {
  392. that.modal.$el.find("button.ok").hideButtonLoader(), that.modal.close();
  393. var msgType = model.mutatedEntities && model.mutatedEntities.UPDATE ? "editSuccessMessage" : "addSuccessMessage";
  394. if (Utils.notifySuccess({
  395. content: "Entity" + Messages.getAbbreviationMsg(!1, msgType)
  396. }), that.guid && that.callback) that.callback(); else if (that.searchVent && that.searchVent.trigger("Entity:Count:Update"),
  397. model.mutatedEntities) {
  398. var mutatedEntities = model.mutatedEntities.CREATE || model.mutatedEntities.UPDATE;
  399. mutatedEntities && _.isArray(mutatedEntities) && mutatedEntities[0] && mutatedEntities[0].guid && Utils.setUrl({
  400. url: "#!/detailPage/" + mutatedEntities[0].guid,
  401. mergeBrowserUrl: !1,
  402. trigger: !0
  403. });
  404. }
  405. },
  406. complete: function() {
  407. that.hideLoader({
  408. editVisiblityOfEntitySelectionBox: !0
  409. }), that.modal.$el.find("button.ok").hideButtonLoader();
  410. }
  411. });
  412. } catch (e) {
  413. Utils.notifyError({
  414. content: e.message
  415. }), that.hideLoader({
  416. editVisiblityOfEntitySelectionBox: !0
  417. });
  418. }
  419. },
  420. showLoader: function(options) {
  421. var editVisiblityOfEntitySelectionBox = options && options.editVisiblityOfEntitySelectionBox;
  422. this.$(".entityLoader").addClass("show"), this.$(".entityInputData").hide(), (this.guid || editVisiblityOfEntitySelectionBox) && this.ui.entitySelectionBox.hide();
  423. },
  424. hideLoader: function(options) {
  425. var editVisiblityOfEntitySelectionBox = options && options.editVisiblityOfEntitySelectionBox;
  426. this.$(".entityLoader").removeClass("show"), this.$(".entityInputData").show(),
  427. (this.guid || editVisiblityOfEntitySelectionBox) && this.ui.entitySelectionBox.show(),
  428. this.ui.entityList.select2("open"), this.ui.entityList.select2("close");
  429. },
  430. addJsonSearchData: function() {
  431. var that = this;
  432. this.$('select[data-id="entitySelectData"]').each(function(value, key) {
  433. var $this = $(this), keyData = $(this).data("key"), typeData = $(this).data("type"), queryData = $(this).data("querydata"), placeholderName = ($(this).data("skip"),
  434. "Select a " + typeData + " from the dropdown list");
  435. if ($this.attr("multiple", $this.data("type").indexOf("array") !== -1), that.guid) {
  436. var dataValue = that.entityData.get("entity").attributes[keyData], relationshipType = (that.entityData.get("entity").attributes,
  437. that.entityData.get("entity").relationshipAttributes ? that.entityData.get("entity").relationshipAttributes[keyData] : null), referredEntities = that.entityData.get("referredEntities"), selectedValue = [], select2Options = [];
  438. if (dataValue && (_.isObject(dataValue) && !_.isArray(dataValue) && (dataValue = [ dataValue ]),
  439. _.each(dataValue, function(obj) {
  440. if (_.isObject(obj) && obj.guid && referredEntities[obj.guid]) {
  441. var refEntiyFound = referredEntities[obj.guid];
  442. refEntiyFound.id = refEntiyFound.guid, Enums.entityStateReadOnly[refEntiyFound.status] || (select2Options.push(refEntiyFound),
  443. selectedValue.push(refEntiyFound.guid));
  444. }
  445. }), _.isUndefined(relationshipType) || relationshipType && relationshipType.relationshipAttributes && relationshipType.relationshipAttributes.typeName && that.$("select[data-for-key=" + keyData + "]").val(relationshipType.relationshipAttributes.typeName).trigger("change")),
  446. 0 === selectedValue.length && dataValue && dataValue.length && "string" === $this.data("querydata")) {
  447. var str = "";
  448. _.each(dataValue, function(obj) {
  449. _.isString(obj) && (selectedValue.push(obj), str += "<option>" + _.escape(obj) + "</option>");
  450. }), $this.html(str);
  451. }
  452. } else $this.val([]);
  453. var select2Option = {
  454. placeholder: placeholderName,
  455. allowClear: !0,
  456. tags: "string" == $this.data("querydata")
  457. }, getTypeAheadData = function(data, params) {
  458. var dataList = data.entities, foundOptions = [];
  459. return _.each(dataList, function(obj) {
  460. obj && (obj.guid && (obj.id = obj.guid), foundOptions.push(obj));
  461. }), foundOptions;
  462. };
  463. "string" !== $this.data("querydata") && _.extend(select2Option, {
  464. ajax: {
  465. url: UrlLinks.searchApiUrl("attribute"),
  466. dataType: "json",
  467. delay: 250,
  468. data: function(params) {
  469. return {
  470. attrValuePrefix: params.term,
  471. typeName: queryData,
  472. limit: 10,
  473. offset: 0
  474. };
  475. },
  476. processResults: function(data, params) {
  477. return {
  478. results: getTypeAheadData(data, params)
  479. };
  480. },
  481. cache: !0
  482. },
  483. templateResult: function(option) {
  484. var name = Utils.getName(option, "qualifiedName");
  485. return "-" === name ? option.text : name;
  486. },
  487. templateSelection: function(option) {
  488. var name = Utils.getName(option, "qualifiedName");
  489. return "-" === name ? option.text : name;
  490. },
  491. escapeMarkup: function(markup) {
  492. return markup;
  493. },
  494. data: select2Options,
  495. minimumInputLength: 1
  496. }), $this.select2(select2Option), selectedValue && $this.val(selectedValue).trigger("change");
  497. }), this.guid && (this.bindRequiredField(), this.bindNonRequiredField()), this.hideLoader();
  498. }
  499. });
  500. return CreateEntityLayoutView;
  501. });