2024-03-26 15:56:31 +08:00

393 lines
18 KiB
JavaScript
Raw Blame History

define(['modules/sections/section', 'modules/sections/addsection', 'common', 'modules/templates/linksection'], function (Section, AddSection, Common, LinkSection) {
var p = function (page, pagesmodule) {
this.page = page || {};
this.pagesmodule = pagesmodule;
this.namecontrol = undefined;
this.displaytextcontrol = undefined;
this.notescontrol = undefined;
this.displayonreportcontrol = undefined;
this.onNameChanged = undefined;
this.datacontent = null;
this.sectionmodules = [];
};
p.prototype.description = "Page";
p.prototype.version = "1.0.0.0";
p.prototype.createContent = function () {
var _this = this;
var content = $('<div></div>');
function createPageContent() {
var div_main = $('<div class="content_main"></div>');
var div_content = $('<div class="edit-content"></div>');
div_main.append(div_content);
var tb = $('<table></table>');
div_content.append(tb);
var tr = $('<tr></tr>');
tb.append(tr);
tr.append('<td class="label" style="width:154px;">' + GetTextByKey("P_IPT_NAME_COLON", "Name:") + '<span class="redasterisk">*</span></td>');
_this.namecontrol = $('<input type="text" class="form-control" maxlength="100" autocomplete="off"/>');
var addiText = $('<span class="spanbtn iconmultitext" style="font-size:14px;" title="' + GetTextByKey("P_IPT_LANGUAGESELECTION", "Language Selection") + '"/>');
addiText.click(function () {
Common.createMultiTextDialog(_this.namecontrol);
});
tr.append($('<td style="white-space:nowrap;width:370px;"></td>').append(_this.namecontrol).append(addiText));
_this.namecontrol.change(function () {
if (_this.onNameChanged)
_this.onNameChanged($.trim(_this.namecontrol.val()))
})
tr.append($('<td></td>'));
tb.append(tr);
tr.append('<td class="label" style="width:100px;">' + GetTextByKey("P_IPT_NOTES_COLON", "Notes:") + '</td>');
_this.notescontrol = $('<textarea id="dialog_notes" class="form-control" maxlength="500" autocomplete="off" style="width: 540px; margin-top:6px;height:55px;"></textarea>');
tr.append($('<td rowspan="2"></td>').append(_this.notescontrol));
tr = $('<tr></tr>');
tb.append(tr);
tr.append('<td class="label" style="width:154px;">' + GetTextByKey("P_IPT_DISPLAYTEXT_COLON", "Display Text:") + '<span class="redasterisk">*</span></td>');
_this.displaytextcontrol = $('<input type="text" class="form-control" maxlength="200" autocomplete="off"/>');
addiText = $('<span class="spanbtn iconmultitext" style="font-size:14px;" title="' + GetTextByKey("P_IPT_LANGUAGESELECTION", "Language Selection") + '"/>');
addiText.click(function () {
Common.createMultiTextDialog(_this.displaytextcontrol);
});
tr.append($('<td style="white-space:nowrap;"></td>').append(_this.displaytextcontrol).append(addiText));
var td_displayonreport = $('<td></td>')
.attr('title', GetTextByKey("P_IPT_PAGEDISPLAYONREPORTTIPS", 'Checked indicates that there will be a \n Page Break and Page Title on the report.\n\n Unchecked indicates that the Page Break \n is only visible during inspection completion.'));
tr.append(td_displayonreport);
_this.displayonreportcontrol = $('<input id="tempchk1" type="checkbox"/>');
td_displayonreport.append(_this.displayonreportcontrol);
td_displayonreport.append("<label for='tempchk1' data-lgid='P_IPT_DISPLAYONREPORT'>Display on Report</label>");
var sectioncontent = createSectionContent();
div_main.append(sectioncontent);
return div_main;
}
function createSectionContent() {
var content = $('<div style="min-width:1780px;"></div>');
function createSectionHeader() {
var header = $('<div></div>');
//header.append($('<div class="page_title"></div>').text(gs.title));
var func = $('<div class="function_title"></div>');
var iconAdd = $('<span class="sbutton iconadd">' + GetTextByKey("P_IPT_ADDSECTION", "Add Section") + '</span>').click(function () {
//var s = new AddSection();
//s.onsave = function (section) {
// _this.page.Sections.push(section);
// updateContent(true);
//}
//$('#right_popup1').empty().append(s.createContent()).show();
var section = { DisplayOnReport: true };
_this.page.Sections.push(section);
addSectionModule(section);
});
func.append(iconAdd);
var iconRefresh = $('<span class="sbutton iconlink">' + GetTextByKey("P_ADDGLOBALSECTION", "Add Global Section") + '</span>').click(function () {
addGlobalSection();
});
func.append(iconRefresh);
header.append(func)
return header;
}
if (!templatereadonly && editable)
content.append(createSectionHeader());
var dataheader = $('<div class="question-holder no-hover" style="font-weight: bold;font-size:14px;"></div>');
dataheader.append('<div style="width: 60px; flex-shrink: 0"></div>');
dataheader.append('<div class="question-cell" style="width: 235px">' + GetTextByKey("P_IPT_NAME", "Name") + '</div>');
dataheader.append('<div class="question-cell" style="width: 310px">' + GetTextByKey("P_IPT_DISPLAYTEXT", "Display Text") + '</div>');
dataheader.append('<div class="question-cell" style="width: 370px">' + GetTextByKey("P_IPT_TYPE", "Type") + '</div>');
dataheader.append('<div class="question-cell" style="width: 80px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_DISPLAYONREPORT", "Display on Report") + '</div>');
dataheader.append('<div class="question-cell" style="width: 80px; text-align: center;">' + GetTextByKey("P_IPT_REQUIRED", "Required") + '</div>');
dataheader.append('<div class="question-cell" style="width: 65px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_INCLUDECOMMENT", "Include Comment") + '</div>');
dataheader.append('<div class="question-cell" style="width: 70px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_ISIMPORTANT", "Is Important") + '</div>');
dataheader.append('<div class="question-cell" style="width: 70px; text-align: center;white-space:normal;line-height:20px;"">' + GetTextByKey("P_IPT_CUSTOMERVISIBLE", "Customer Visible") + '</div>');
dataheader.append('<div class="question-cell" style="width: 80px; text-align: center;white-space:normal;line-height:20px;">' + GetTextByKey("P_IPT_SEVERITYLEVEL", "Severity Level") + '</div>');
dataheader.append('<div style="flex-grow: 1" style="width: 160px">' + GetTextByKey("P_IPT_ST_NOTES", "Notes") + '</div>');
dataheader.append('<div class="question-cell" style="width: 90px;padding-right:20px;"></div>');
content.append(dataheader);
_this.datacontent = $('<div></div>');
content.append(_this.datacontent);
return content;
}
function updateContent(sectiononly) {
if (!sectiononly) {
_this.namecontrol.val(_this.page.Name).data("texts", _this.page.LocalNames);
_this.displaytextcontrol.val(_this.page.DisplayText).data("texts", _this.page.LocalDisplayTexts);
_this.notescontrol.val(_this.page.Notes);
_this.displayonreportcontrol.attr('checked', _this.page.DisplayOnReport);
}
if (_this.page.Sections) {
_this.sectionmodules = [];
_this.datacontent.empty();
for (var i = 0; i < _this.page.Sections.length; i++) {
//var s = new Section(_this.page.Sections[i], null, _this);
addSectionModule(_this.page.Sections[i], i);
}
} else
_this.page.Sections = [];
}
function addSectionModule(s, index) {
var sectionmodule = new Section(s, null, _this, index);
sectionmodule.oncopy = function (section) {
section = JSON.parse(JSON.stringify(section));
section.Id = "";
section.Conditional = null;
if (!section.IsLink && section.Questions) {
for (var j = 0; j < section.Questions.length; j++) {
section.Questions[j].Id = "";
section.Questions[j].Conditional = null;
}
}
_this.page.Sections.push(section);
addSectionModule(section);
};
sectionmodule.ondelete = function (sm) {
_this.page.Sections.splice(_this.page.Sections.indexOf(sm.section), 1);
_this.sectionmodules.splice(_this.sectionmodules.indexOf(sm), 1);
}
_this.sectionmodules.push(sectionmodule);
_this.datacontent.append(sectionmodule.createContent());
}
function setDisabled() {
_this.namecontrol.prop('disabled', true);
_this.displaytextcontrol.prop('disabled', true);
_this.notescontrol.prop('disabled', true);
_this.displayonreportcontrol.prop('disabled', true);
content.find(".iconmultitext").hide();
content.find(".iconimage").hide();
}
function addGlobalSection() {//<2F><><EFBFBD><EFBFBD>Global Section
var dialogSection = new LinkSection().getDialogSection();
dialogSection.onDialogClosed = function () {
}
dialogSection.onOK = function (sections, iscopy) {
if (!iscopy) {
var linkedSectionIds = [];
if (_this.pagesmodule && _this.pagesmodule.pages) {
for (var i = 0; i < _this.pagesmodule.pages.length; i++) {
var page = _this.pagesmodule.pages[i];
if (page.Sections) {
for (var j = 0; j < page.Sections.length; j++) {
var s = page.Sections[j];
if (s.IsLink)
linkedSectionIds.push(s.ReferenceId);
}
}
}
}
if (sections && sections.length > 0) {
for (var i = 0; i < sections.length; i++) {
var it = sections[i];
if (it.Selected && linkedSectionIds.indexOf(it.Id) >= 0) {
showAlert(GetTextByKey("P_IPT_ALREADYLINKED", '{0} has already been linked to current template.').replace("{0}", it.Name), GetTextByKey("P_ADDGLOBALSECTION", "Add Global Section"));
return;
}
}
}
}
onAddGlobalSection(sections, iscopy);
}
//var sections = [];
//for (var i = 0; i < _this.page.Sections.length; i++) {
// if (_this.page.Sections[i].IsLink)
// sections.push(_this.page.Sections[i].ReferenceId);
//}
//dialogSection.exceptSource = sections;
dialogSection.showSelector();
}
function onAddGlobalSection(sections, iscopy) {
if (sections && sections.length > 0) {
var sids = [];
for (var i = 0; i < sections.length; i++) {
var it = sections[i];
if (it.Selected)
sids.push(it.Id);
}
var p = JSON.stringify([teamintelligence, JSON.stringify(sids)]);
inspectionrequest("GetGlobalSectionsByID", p, function (data) {
if (typeof data === 'string')
return;
for (var i = 0; i < data.length; i++) {
var section = data[i];
if (iscopy) {//copy secton<6F><6E>reference its questions
if (section.Questions) {
for (var j = 0; j < section.Questions.length; j++) {
var q = section.Questions[j];
q.Id = '';
//q.ReferenceId = q.Id;
//q.IsLink = true;
}
}
}
else {//reference section
section.ReferenceId = section.Id;
section.IsLink = true;
}
section.Id = '';
_this.page.Sections.push(section);
addSectionModule(section);
}
//updateContent(true);
}, function (err) {
});
}
}
content.append(createPageContent());
updateContent();
if (templatereadonly || !editable)
setDisabled();
return content;
}
p.prototype.getPageValue = function (noalert, nocheck) {
var page = this.page;
page.Name = this.namecontrol.val();
page.DisplayText = this.displaytextcontrol.val();
page.Notes = this.notescontrol.val();
page.DisplayOnReport = this.displayonreportcontrol.prop('checked');
var texts = this.namecontrol.data("texts");
if (texts) {
page.LocalNames = texts;
}
texts = this.displaytextcontrol.data("texts");
if (texts) {
page.LocalDisplayTexts = texts;
}
if (!nocheck) {
var alerttitle = GetTextByKey("P_IPT_PAGE", 'Page');
if (!noalert && (!page.Name || page.Name.length == 0)) {
showAlert(GetTextByKey("P_IPT_PAGENAMENOTBEEMPTY", 'Page Name cannot be empty.'), alerttitle);
return false;
}
if (!noalert && (!page.DisplayText || page.DisplayText.length == 0)) {
showAlert(GetTextByKey("P_IPT_DISPLAYTEXTNOTBEEMPTY", 'Display Text cannot be empty.'), alerttitle);
return false;
}
}
page.Sections = [];
if (this.sectionmodules.length > 0) {
for (var i = 0; i < this.sectionmodules.length; i++) {
var s = this.sectionmodules[i].getSectionValue(noalert, nocheck);
if (!s) return false;
page.Sections.push(s);
}
}
return page;
}
p.prototype.dragOutSection = function (sm) {//called when end drag
var index = this.page.Sections.indexOf(sm.section)
if (index >= 0) {
this.page.Sections.splice(index, 1);
this.sectionmodules.splice(index, 1);
}
};
p.prototype.dragInSection = function (target, sm, after) {//called when end drag
if (!this.page.Sections)
this.page.Sections = [];
if (this.page.Sections.length == 0) {
this.page.Sections.push(sm.section);
this.sectionmodules.push(sm);
return;
}
var tindex = 0;
if (target) {
tindex = this.page.Sections.indexOf(target);
if (after)
tindex = tindex + 1;
}
if (tindex >= 0) {
this.page.Sections.splice(tindex, 0, sm.section);
this.sectionmodules.splice(tindex, 0, sm);
}
};
p.prototype.refreshIdIndex = function () {
var id = 0;
var needRefreshes = [];
var needReloads = [];
for (var s = 0; s < this.sectionmodules.length; s++) {
var section = this.sectionmodules[s];
section.index = s;
section.btncondition.css('display', section.conditionalTree.init() ? '' : 'none');
for (var i = 0; i < section.questionmodules.length; i++) {
var question = section.questionmodules[i];
question.idIndex = ++id;
question.questionholder.children('.question-index').text('#' + String(id));
question.btncondition.css('display', question.conditionalTree.init() ? '' : 'none');
if (question.conditional?.QuestionId != null) {
needReloads.push(question);
}
var root = question.conditionalTree;
if (root == null) {
continue;
}
root.initialized = false;
if (root.tree?.is(':visible')) {
needRefreshes.push(root);
}
}
}
var qs = this.sectionmodules.reduce(function (array, s) { return array.concat(s.questionmodules) }, []);
for (var i = 0; i < needReloads.length; i++) {
var q = needReloads[i];
var conditional = q.conditional;
var r = cleanConditional(conditional, q.idIndex, qs);
if (r) {
q.conditional.QuestionId = '-';
q.conditional.SubItems = [];
}
}
for (var i = 0; i < needRefreshes.length; i++) {
needRefreshes[i].refresh();
}
};
function cleanConditional(c, index, qs) {
if (!['-', 'or', 'and'].includes(c.QuestionId)) {
var cq = qs.find(function (q) { return q.question.Id == c.QuestionId });
if (cq == null) {
return true;
}
if (cq.idIndex >= index) {
return true;
}
}
if (c.SubItems?.length > 0) {
for (var i = c.SubItems.length - 1; i >= 0; i--) {
var r = cleanConditional(c.SubItems[i], index, qs);
if (r) {
c.SubItems.splice(i, 1);
}
}
}
return false;
}
return p;
});