define(['common'], function (Common) {
var s = function (gs, section) {
this.section = section;
this.dialog = null;
this.gsmodule = gs;
this.datasaved = false;
};
s.prototype.description = "Add or edit section";
s.prototype.version = "1.0.0.0";
s.prototype.onsave = null;
var namecontrol = undefined;
var displaytextcontrol = undefined;
var notescontrol = undefined;
s.prototype.createContent = function () {
var _this = this;
editable = true;
var content = $('
');
function createHeader() {
var header = $('');
var func = $('');
if (sectiontype == 0) {
var btn = $('' + GetTextByKey("P_IPT_SAVE","Save") + '').click(function () {
saveData(0);
});
func.append(btn);
btn = $('' + GetTextByKey("P_IPT_SAVE1", "Save and Exit") + '').click(function () {
saveData(1);
});
func.append(btn);
btn = $('' + GetTextByKey("P_IPT_SAVE2", "Exit Without Saving") + '').click(function () {
showRightPopup(false);
if (_this.datasaved)
_this.gsmodule.refresh();
});
func.append(btn);
}
else {
header.append($('').text(_this.section ? 'Edit Section' : 'Add Section'));
if (!templatereadonly && (!_this.section || (_this.section && !_this.section.IsLink))) {
//if (!_this.section || (_this.section && !_this.section.IsLink)) {
var btn = $('' + GetTextByKey("P_IPT_OK", "OK") + '').click(function () {
var item = getData();
if (!item)
return;
if (_this.onsave)
_this.onsave(item);
$('#right_popup1').empty().hide();
});
func.append(btn);
}
btn = $('' + GetTextByKey("P_IPT_CANCEL", "Cancel") + '').click(function () {
$('#right_popup1').empty().hide();
});
func.append(btn);
}
header.append(func)
return header;
}
function createSectionContent() {
var div_main = $('');
var div_content = $('');
div_main.append(div_content);
var tb = $('');
div_content.append(tb);
var tr = $('
');
tb.append(tr);
tr.append('' + GetTextByKey("P_IPT_NAME_COLON", "Name:") + '* | ');
namecontrol = $('');
var addiText = $('');
addiText.click(function () {
Common.createMultiTextDialog(namecontrol, _this.section);
});
tr.append($(' | ').append(namecontrol).append(addiText));
tr = $('
');
tb.append(tr);
tr.append('' + GetTextByKey("P_IPT_DISPLAYTEXT_COLON", "Display Text:") + '* | ');
displaytextcontrol = $('');
addiText = $('');
addiText.click(function () {
Common.createMultiTextDialog(displaytextcontrol, _this.section);
});
tr.append($(' | ').append(displaytextcontrol).append(addiText));
tr = $('
');
tb.append(tr);
tr.append('' + GetTextByKey("P_IPT_NOTES_COLON", "Notes:") + ' | ');
notescontrol = $('');
tr.append($(' | ').append(notescontrol));
return div_main;
}
function updateContent() {
var section = _this.section;
if (section) {
namecontrol.val(section.Name).data("texts", section.LocalNames);
displaytextcontrol.val(section.DisplayText).data("texts", section.LocalDisplayTexts);
notescontrol.val(section.Notes);
}
}
function setDisabled() {
namecontrol.prop('disabled', true);
displaytextcontrol.prop('disabled', true);
notescontrol.prop('disabled', true);
}
function getData() {
var item = {
'Name': namecontrol.val(),
'DisplayText': displaytextcontrol.val(),
'Notes': notescontrol.val()
};
var texts = namecontrol.data("texts");
if (texts) {
item.LocalNames = texts;
}
texts = displaytextcontrol.data("texts");
if (texts) {
item.LocalDisplayTexts = texts;
}
var alerttitle;
if (_this.section) {
item.Id = _this.section.Id;
alerttitle = GetTextByKey("P_IPT_EDITSECTION","Edit Section");
} else {
alerttitle = GetTextByKey("P_IPT_ADDSECTION","Add Section");
}
if (!item.Name || item.Name.length == 0) {
showAlert(GetTextByKey("P_IPT_NAMENOTBEEMPTY",'Name cannot be empty.'), alerttitle);
return false;
}
if (!item.DisplayText || item.DisplayText.length == 0) {
showAlert(GetTextByKey("P_IPT_DISPLAYTEXTNOTBEEMPTY",'Display Text cannot be empty.'), alerttitle);
return false;
}
return item;
}
function saveData(exit) {
var item = getData();
if (!item) return;
var param = JSON.stringify(item);
param = htmlencode(param);
var p = JSON.stringify([teamintelligence, param]);
var alerttitle = GetTextByKey("P_IPT_SAVESECTION", 'Save Section');
inspectionrequest("SaveGlobalSection", p, function (data) {
if (typeof (data) === "string") {
showAlert(data, alerttitle);
} else {
if (!_this.section) {
_this.section = { 'Id': data[0] };
}
if (exit == 0) {
showAlert(GetTextByKey("P_IPT_SAVSUCCESSFULLY",'Saved successfully.'), alerttitle);
_this.datasaved = true;
}
if (exit == 1) {
_this.gsmodule.refresh();
showRightPopup(false);
}
}
}, function (err) {
showAlert(GetTextByKey("P_IPT_FAILEDTOSAVESECTION",'Failed to save Section.'), alerttitle);
});
}
content.append(createHeader());
content.append(createSectionContent());
updateContent();
if (templatereadonly)
setDisabled();
return content;
}
return s;
});