in dashboardv2/public/js/views/tag/AddTagModalView.js [112:288]
initialize: function(options) {
_.extend(this, _.pick(options, 'modalCollection', 'guid', 'callback', 'multiple', 'entityCount', 'showLoader', 'hideLoader', 'tagList', 'tagModel', 'enumDefCollection'));
this.commonCollection = new VTagList();
if (this.tagModel) {
this.collection = new Backbone.Collection(this.tagModel.validityPeriods);
} else {
this.collection = new Backbone.Collection();
}
this.tagCollection = options.collection;
var that = this,
modalObj = {
title: 'Add Classification',
content: this,
okText: 'Add',
cancelText: "Cancel",
mainClass: 'modal-lg',
allowCancel: true,
okCloses: false
};
if (this.tagModel) {
modalObj.title = 'Edit Classification';
modalObj.okText = 'Update';
}
this.modal = new Modal(modalObj)
this.modal.open();
this.modal.$el.find('button.ok').attr("disabled", true);
this.on('ok', function() {
if (this.ui.checkTimeZone.is(':checked')) {
if (this.validateValues()) {
if (this.hideLoader) {
this.hideLoader();
};
return;
};
}
that.modal.$el.find('button.ok').showButtonLoader();
var tagName = this.tagModel ? this.tagModel.typeName : this.ui.addTagOptions.val(),
tagAttributes = {},
tagAttributeNames = this.$(".attrName"),
obj = {
tagName: tagName,
tagAttributes: tagAttributes,
guid: [],
skipEntity: [],
deletedEntity: []
},
isValidateAttrValue = true,
validationKey = [];
tagAttributeNames.each(function(i, item) {
var selection = $(item).data("key"),
isRequired = $(item).hasClass('required'),
datatypeSelection = $(item).data("type"),
$valueElement = $(item);
$valueElement.removeClass('errorValidate');
if (datatypeSelection === "date") {
var convertedDate = Globals.needToValidateDate ? Utils.convertToValidDate($valueElement.val()) : $valueElement.val();
tagAttributes[selection] = Date.parse(convertedDate) || null;
} else {
if (isRequired) {
if ($valueElement.val().length) {
tagAttributes[selection] = $(item).val() || null;
// tagAttributes[selection] = that.getValue($valueElement, datatypeSelection);
} else {
isValidateAttrValue = false;
$valueElement.addClass('errorValidate');
validationKey.push($(item).data("key"));
}
} else {
tagAttributes[selection] = $(item).val() || null;
// tagAttributes[selection] = that.getValue($valueElement, datatypeSelection);
}
}
});
if (that.multiple) {
_.each(that.multiple, function(entity, i) {
var name = Utils.getName(entity.model);
if (Enums.entityStateReadOnly[entity.model.status]) {
obj.deletedEntity.push(name);
} else {
if (_.indexOf((entity.model.classificationNames || _.pluck(entity.model.classifications, 'typeName')), tagName) === -1) {
obj.guid.push(entity.model.guid)
} else {
obj.skipEntity.push(name);
}
}
});
if (obj.deletedEntity.length) {
Utils.notifyError({
html: true,
content: "<b>" + obj.deletedEntity.join(', ') +
"</b> " + (obj.deletedEntity.length === 1 ? "entity " : "entities ") +
Messages.assignDeletedEntity
});
that.modal.close();
}
if (obj.skipEntity.length) {
var text = "<b>" + obj.skipEntity.length + " of " + that.multiple.length +
"</b> entities selected have already been associated with <b>" + tagName +
"</b> tag, Do you want to associate the tag with other entities ?",
removeCancelButton = false;
if ((obj.skipEntity.length + obj.deletedEntity.length) === that.multiple.length) {
text = (obj.skipEntity.length > 1 ? "All selected" : "Selected") + " entities have already been associated with <b>" + tagName + "</b> tag";
removeCancelButton = true;
}
var notifyObj = {
text: text,
modal: true,
ok: function(argument) {
if (obj.guid.length) {
that.saveTagData(obj);
} else {
that.hideLoader();
}
},
cancel: function(argument) {
that.hideLoader();
obj = {
tagName: tagName,
tagAttributes: tagAttributes,
guid: [],
skipEntity: [],
deletedEntity: []
}
}
}
if (removeCancelButton) {
notifyObj['confirm'] = {
confirm: true,
buttons: [{
text: 'Ok',
addClass: 'btn-atlas btn-md',
click: function(notice) {
notice.remove();
obj = {
tagName: tagName,
tagAttributes: tagAttributes,
guid: [],
skipEntity: [],
deletedEntity: []
}
}
},
null
]
}
}
Utils.notifyConfirm(notifyObj)
} else {
if (obj.guid.length) {
that.saveTagData(obj);
} else {
that.hideLoader();
}
}
} else if (isValidateAttrValue === false) {
var validationMsg = "" + _.each(validationKey, function(key) {
return key + " ";
});
that.modal.$el.find('button.ok').hideButtonLoader();
Utils.notifyInfo({
content: "Value for " + validationMsg + " cannot be empty"
});
if (this.hideLoader) {
this.hideLoader();
};
return;
} else {
obj.guid.push(that.guid);
that.saveTagData(obj);
}
});
this.on('closeModal', function() {
this.modal.trigger('cancel');
});
this.bindEvents();
},