define(['modules/sections/question', 'modules/sections/addsection', 'modules/sections/addquestion', 'common', 'modules/templates/picture'] , function (Question, AddSection, AddQuestion, Common, Picture) { var s = function (section, gsm, pm) { this.section = section; this.content = null; this.holder = null; this.gsmodule = gsm; this.pagemodule = pm; this.txtName = null; this.txtDisplayText = null; if (!this.section.Questions) this.section.Questions = []; this.questionmodules = []; }; s.prototype.moduletype = "section"; s.prototype.description = "Section Module"; s.prototype.version = "1.0.0.0"; s.prototype.ondelete = null; s.prototype.createContent = function () { var content = $('
'); this.content = content; var holder = $('
'); content.append(holder); var _this = this; if (sectiontype === 0) { if (!_this.section.IssueId || _this.section.IssueId == "") editable = true; else editable = false; } if (sectiontype === 0) { var package = $('
'); holder.append(package); } if (sectiontype == 1) { var drag = $('
'); holder.append(drag); if (sectiontype == 1) { drag.attr('draggable', true); holder.bind('dragstart', function (e) { //var data = e.originalEvent.dataTransfer; draggingobj = _this; }); holder.bind('dragend', function (e) { draggingobj = null; }); holder.bind('dragover', function (e) { e.originalEvent.preventDefault() }); holder.bind('drop', function (e) { if (!draggingobj || _this === draggingobj) return; if (draggingobj.moduletype == "question") { if (_this.section.IsLink) return; $(this).after(draggingobj.holder); draggingobj.sectionmodule.dragOutQuestion(draggingobj); _this.dragInQuestion(null, draggingobj, true); } else { var t = $(this); var after = e.originalEvent.clientY > t.offset().top + t.height() / 2; if (after) t.parent().after(draggingobj.holder.parent()); else t.parent().before(draggingobj.holder.parent()); draggingobj.pagemodule.dragOutSection(draggingobj); _this.pagemodule.dragInSection(_this.section, draggingobj, after); } }); } else drag.children().remove(); if ((templatereadonly || !editable)) drag.hide(); } var btnsection = $('
'); btnsection.click(function () { var icon = btnsection.find('.spanbtn'); if (icon.hasClass('iconangleright')) { icon.removeClass('iconangleright').addClass('iconangledown'); if (sectiontype == 0) { _this.getQuestions(); } else _this.updateQuestions(_this.section.Questions); } else { if (sectiontype == 1) _this.getSectionValue(true, true); icon.removeClass('iconangledown').addClass('iconangleright'); _this.content.children('.questionitem').remove(); } }); holder.append(btnsection); this.txtName = $(''); this.txtName.change(function () { _this.onsave(); }); var addiText = $(''); addiText.click(function () { Common.createMultiTextDialog(_this.txtName, _this.section); }); holder.append($('
').append(this.txtName).append(addiText)); this.txtDisplayText = $(''); this.txtDisplayText.change(function () { _this.onsave(); }); addiText = $(''); addiText.click(function () { Common.createMultiTextDialog(_this.txtDisplayText, _this.section); }); var imgicon = $(''); imgicon.click(function (e) { var picturemodule = new Picture(false, _this.section, function () { _this.onsave() }); picturemodule.createContent(); }); holder.append($('
').append(this.txtDisplayText).append(addiText).append(imgicon)); if (sectiontype === 0) holder.append($('
')); var funcs = $('
'); holder.append(funcs); //holder.find('.section-name span').click(function () { // _this.onedit(); //}); //holder.find('.section-display span').click(function () { // _this.onedit(); //}); if (_this.section.IsLink) { holder.find('.section-name input').before(''); holder.find('input').prop('disabled', true); holder.find('.iconmultitext').remove(); holder.find('.iconimage').remove(); } if (!templatereadonly && editable) { if (!_this.section.IsLink) { funcs.append($('').click(function () { //var aq = new AddQuestion(_this, _this.section.Id); //if (sectiontype == 0) { // $('#right_popup').empty().append(aq.createContent()); // showRightPopup(true); //} //else { // aq.onsave = function (question) { // if (!_this.section.Questions) // _this.section.Questions = []; // _this.section.Questions.push(question); // _this.updateQuestions(_this.section.Questions); // } // $('#right_popup1').empty().append(aq.createContent()).show(); //} var question = { QuestionType: '0', IsRequired: false, CanComment: false, IsImportant: false, SeverityLevel: 0, LookupSource: 0, SelectItems: [] }; _this.section.Questions.push(question); _this.addQuestionModule(question); }).attr('title', GetTextByKey("P_IPT_ADDQUESTION", 'Add Question'))); } if (sectiontype == 1 && !_this.section.IsLink) { funcs.append($('').click(function () { if (_this.oncopy) _this.oncopy(_this.getSectionValue(true, true)); }).attr('title', GetTextByKey("P_IPT_COPYSECTION", 'Copy Section'))); } //if (!_this.section.IsLink) { // funcs.append($('').click(function () { // _this.onedit(); // }).attr('title', 'Edit Section')); //} } if ((sectiontype === 0 && !templatereadonly) || (sectiontype === 1 && !templatereadonly && editable)) { funcs.append($('').click(function () { if (sectiontype == 0) { _this.delete(); } else { showConfirm(GetTextByKey("P_IPT_AREYOUSUREYOUWANTTODELETETHISSECTION", 'Are you sure you want to delete this section?'), GetTextByKey("P_IPT_DELETESECTION", 'Delete Section'), function () { if (_this.ondelete) { _this.content.remove(); _this.ondelete(_this); } }); } }).attr('title', GetTextByKey("P_IPT_DELETESECTION", 'Delete Section'))); } this.holder = holder; this.updateContent(this.section); if (sectiontype == 1) btnsection.click(); //this.holder.find('input').attr('draggable', true).bind('dragstart', function (e) {//½ûÖ¹ÊäÈë¿òÍ϶¯ // e.originalEvent.preventDefault(); //}); if (templatereadonly || !editable) { this.txtName.prop('disabled', true); this.txtDisplayText.prop('disabled', true); this.holder.find(".iconmultitext").hide(); this.holder.find(".iconimage").hide(); } return content; }; s.prototype.onedit = function () { var _this = this; var s = new AddSection(_this.gsmodule, _this.section); if (sectiontype == 0) { $('#right_popup').empty().append(s.createContent()); showRightPopup(true); } else { s.onsave = function (section) { _this.section.Name = section.Name; _this.section.DisplayText = section.DisplayText; _this.section.Notes = section.Notes; _this.section.LocalNames = section.LocalNames; _this.section.LocalDisplayTexts = section.LocalDisplayTexts; _this.updateContent(_this.section); } $('#right_popup1').empty().append(s.createContent()).show(); } } s.prototype.updateContent = function (section) { if (section) { if (sectiontype === 0) { if (!section.IssueId || section.IssueId == "") { this.holder.find('.section-packages em').hide(); this.holder.find('.section-createdby span').text(''); } else { this.holder.find('.section-packages em').show(); this.holder.find('.section-createdby span').text(section.IssueName); } } this.txtName.val(section.Name).data("texts", section.LocalNames); this.txtDisplayText.val(section.DisplayText).data("texts", section.LocalDisplayTexts);; } }; s.prototype.getSectionValue = function (noalert, nocheck) { var section = this.section; section.Name = this.txtName.val(); section.DisplayText = this.txtDisplayText.val(); var alerttitle = GetTextByKey("P_IPT_SECTION", "Section"); if (!nocheck) { if (!section.Name || section.Name.length == 0) { if (!noalert) showAlert(GetTextByKey("P_IPT_SETIONNAMENOTBEEMPTY", 'Section Name cannot be empty.'), alerttitle); return false; } if (!section.DisplayText || section.DisplayText.length == 0) { if (!noalert) showAlert(GetTextByKey("P_IPT_SECTIONDISPLAYTEXTNOTBEEMPTY", 'Section Display Text cannot be empty.'), alerttitle); return false; } } var texts = this.txtName.data("texts"); if (texts) { section.LocalNames = texts; } texts = this.txtDisplayText.data("texts"); if (texts) { section.LocalDisplayTexts = texts; } section.Questions = []; if (this.questionmodules.length > 0) { for (var i = 0; i < this.questionmodules.length; i++) { var q = this.questionmodules[i].getQuestionValue(noalert, nocheck); if (!q) return false; section.Questions.push(q); } } return section; }; s.prototype.updateQuestions = function (questions) { if (questions == null) { return; } this.questionmodules = []; this.content.children('.question-holder').remove(); for (var i = 0; i < questions.length; i++) { this.addQuestionModule(questions[i]); } }; s.prototype.addQuestionModule = function (q) { var _this = this; if (this.section.IsLink) q.IsLink = true; var qmodule = new Question(this, q, this.section.Questions.indexOf(q)); qmodule.oncopy = function (question) { _this.oncopyquestion(question); } qmodule.ondelete = function (qm) {//qm:question module _this.section.Questions.splice(_this.section.Questions.indexOf(qm.question), 1); _this.questionmodules.splice(_this.questionmodules.indexOf(qm), 1); } this.questionmodules.push(qmodule); this.content.append(qmodule.createContent()); }; s.prototype.oncopyquestion = function (question) { question = JSON.parse(JSON.stringify(question)); question.Id = ""; //question.IsLink = false; //question.ReferenceId = ""; if (!this.section.Questions) this.section.Questions = []; this.section.Questions.push(question); this.addQuestionModule(question); }; s.prototype.getQuestions = function () { var _this = this; var p = JSON.stringify([teamintelligence, htmlencode(this.section.Id)]); inspectionrequest("GetGlobalQuestions", p, function (data) { if (data) { _this.section.Questions = data; _this.content.children('.questionitem').remove(); _this.updateQuestions(data); } }, function (err) { }); }; s.prototype.delete = function () { var _this = this; showConfirm(GetTextByKey("P_IPT_AREYOUSUREYOUWANTTODELETETHISSECTION", 'Are you sure you want to delete this section?'), GetTextByKey("P_IPT_DELETESECTION", 'Delete Section'), function () { var p = JSON.stringify([teamintelligence, htmlencode(_this.section.Id)]); inspectionrequest("DeleteGlobalSection", p, function (data) { if (data !== 'OK') showAlert(data, GetTextByKey("P_IPT_DELETESECTION", 'Delete Section')); else if (_this.gsmodule && typeof _this.gsmodule.refresh === "function") _this.gsmodule.refresh(); }, function (err) { showAlert(GetTextByKey("P_IPT_FAILEDTODELETESECTION", 'Failed to delete section.'), GetTextByKey("P_IPT_DELETESECTION", 'Delete Section')); }); }); }; s.prototype.refresh = function () { var icon = this.content.find('.spanbtn'); if (icon.hasClass('iconangledown')) { this.getQuestions(); } }; s.prototype.dragOutQuestion = function (qm) {//called when question dragged end at question var index = this.section.Questions.indexOf(qm.question) if (index >= 0) { this.section.Questions.splice(index, 1); this.questionmodules.splice(index, 1); } }; s.prototype.dragInQuestion = function (target, qm, after) {//called when question dragged end at question qm.sectionmodule = this; qm.section = this.section; if (!this.section.Questions) this.section.Questions = []; if (this.section.Questions.length == 0) { this.section.Questions.push(qm.question); this.questionmodules.push(qm); return; } var tindex = 0; if (target) { tindex = this.section.Questions.indexOf(target); if (after) tindex = tindex + 1; } if (tindex >= 0) { this.section.Questions.splice(tindex, 0, qm.question); this.questionmodules.splice(tindex, 0, qm); } }; s.prototype.dragInSection = function () {//called when section dragged end at question this.holder.parent().after(draggingobj.holder.parent()); draggingobj.pagemodule.dragOutSection(draggingobj); this.pagemodule.dragInSection(this.section, draggingobj, true); }; s.prototype.onsave = function () { if (sectiontype == 1) return; var item = this.getSectionValue(true); if (!item) return; var param = JSON.stringify(item); param = htmlencode(param); var p = JSON.stringify([teamintelligence, param]); var _this = this; inspectionrequest("SaveGlobalSection", p, function (data) { if (typeof (data) === "string") { showAlert(data, GetTextByKey("P_IPT_SAVESECTION", 'Save Section')); } else { if (!_this.section) _this.section = {}; _this.section.Id = data[0]; } }, function (err) { showAlert(GetTextByKey("P_IPT_FAILEDTOSAVESECTION", 'Failed to save Section.'), GetTextByKey("P_IPT_SAVESECTION", 'Save Section')); }); } return s; });