define(['modules/templates/pages', 'modules/templates/filters', 'modules/templates/picture'], function (Pages, Filters, Picture) { var s = function (gs, template) { this.template = template || {}; currenttemplate = this.template; this.gsmodule = gs; this.datasaved = false; this.content = null; }; s.prototype.description = "Add or edit template"; s.prototype.version = "1.0.0.0"; var namecontrol = undefined; var locationenabledcontrol = undefined; var needsignaturecontrol = undefined; var forworkordercontrol = undefined; var displaycommittimecontrol = undefined; var displaycommitbycontrol = undefined; var displayinspectiontitlecontrol = undefined; var layoutcontrol = undefined; var lockedcontrol = undefined; var notescontrol = undefined; var otheremailcontrol = undefined; var pages = undefined; var filtertd = undefined; var filtersmodule = undefined; var picturesdiv = undefined; var picturesadd = undefined; var grid_contactdt; var pagesicon = undefined; var pagescontent = undefined; s.prototype.createContent = function () { var content = $('
'); this.content = content; var _this = this; function createHeader() { var header = $('
'); var func = $('
'); if (!templatereadonly) { var btn = $('' + GetTextByKey("P_IPT_SAVE", "Save") + '').click(function () { onSave(0, 0); }); func.append(btn); btn = $('' + GetTextByKey("P_IPT_SAVE1", "Save and Exit") + '').click(function () { onSave(1, 0); }); func.append(btn); if ((templatestatus == 0 || (!_this.template.Id && templatestatus == 1)) && (!_this.template.Id || _this.template.Editable || IsForesight)) { btn = $('' + GetTextByKey("P_IPT_SAVEANDPUBLISH", "Save and Publish") + '').click(function () { onSave(1, 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); header.append(func) return header; } function createTemplateContent() { var div_main = $('
'); var div_content = $('
'); div_main.append(div_content); var tb = $('
'); div_content.append(tb); var tr = $(''); tb.append(tr); var td = $(''); tr.append(td); //tr.append('Name:*'); var btnsummary = $('' + GetTextByKey("P_IPT_SUMMARY", "Summary") + ''); btnsummary.click(function () { var icon = btnsummary.find('.spanbtn'); if (icon.hasClass('iconangledown')) { icon.removeClass('iconangledown').addClass('iconangleright'); _this.content.find('.templatetr').hide(); } else { icon.removeClass('iconangleright').addClass('iconangledown'); _this.content.find('.templatetr').show(); } }); td.append(btnsummary).append('' + GetTextByKey("P_IPT_NAME_COLON", "Name:") + '*'); namecontrol = $(''); tr.append($('').append(namecontrol)); tr = $(''); tb.append(tr); tr.append('' + GetTextByKey("P_IPT_LOCATIONENABLED_COLON", "Location Enabled:") + ''); locationenabledcontrol = $(''); needsignaturecontrol = $(''); var td = $('').append( locationenabledcontrol, $('').text(GetTextByKey("P_IPT_SIGNATUREREQUIRED_COLON", "Signature Required:")), needsignaturecontrol ); if (!teamintelligence) { td.append($('').text(GetTextByKey("P_IPT_FORWORKORDER_COLON", "For Work Order:"))); forworkordercontrol = $(''); td.append(forworkordercontrol); } tr.append(td); tr = $(''); tb.append(tr); tr.append('' + GetTextByKey("P_IPT_XXXXX_COLON", "Display Commit Time:") + ''); displaycommittimecontrol = $(''); displaycommitbycontrol = $(''); displayinspectiontitlecontrol = $(''); var td = $('').append( displaycommittimecontrol, $('').text(GetTextByKey("P_IPT_XXXXXX_COLON", "Display Commit By:")), displaycommitbycontrol, $('').text(GetTextByKey("P_IPT_XXXXXX_COLON", "Display Inspection Title:")), displayinspectiontitlecontrol ); tr.append(td); tr = $(''); tb.append(tr); tr.append($('').text(GetTextByKey("P_LAYOUT_COLON", "Layout:"))); layoutcontrol = $(''); tr.append($('').append(layoutcontrol)); if (!teamintelligence) { tr = $(''); tb.append(tr); tr.append('' + GetTextByKey("P_IPT_FILTER_COLON", "Filter:") + '') .attr("title", GetTextByKey("P_IPT_FILTERTOOLTIPS", "Multiple filters can be applied that act as independent assignments")); filtertd = $(''); tr.append(filtertd); } picturesdiv = $('
'); tr = $(''); tb.append(tr); tr.append('' + GetTextByKey("P_IPT_PICTURE_COLON", "Picture:") + ''); picturesadd = $('' + GetTextByKey("P_IPT_ADD", "Add") + '').click(function () { var picturemodule = new Picture(true, _this.template, setPictures); picturemodule.createContent(); }); tr.append($('').append(picturesadd)); tr = $(''); tb.append(tr); tr.append(''); tr.append($('').append(picturesdiv)); if (IsForesight) { tr = $(''); tb.append(tr); tr.append('' + GetTextByKey("P_IPT_LOCKED_COLON", "Locked:") + ''); lockedcontrol = $(''); lockedcontrol.prop('checked', false); tr.append($('').append(lockedcontrol)); lockedcontrol.click(function () { editable = !$(this).prop("checked"); _this.template.Editable = editable; updateContent(); }); } tr = $(''); tb.append(tr); tr.append('' + GetTextByKey("P_IPT_NOTES_COLON", "Notes:") + ''); notescontrol = $(''); tr.append($('').append(notescontrol)); //Email tr = $(''); tb.append(tr); td = $(''); tr.append(td); var btnemail = $('' + GetTextByKey("P_IPT_EMAIL", "Email") + ''); btnemail.click(function () { var icon = btnemail.find('.spanbtn'); if (icon.hasClass('iconangledown')) { icon.removeClass('iconangledown').addClass('iconangleright'); _this.content.find('.emailtd').hide(); } else { icon.removeClass('iconangleright').addClass('iconangledown'); _this.content.find('.emailtd').show(); } }); td.append(btnemail); tr.append($('').append(createEmailContent())); return div_main; } function createEmailContent() { var tb = $('
'); var tr = $(''); tb.append(tr); tr.append($('' + GetTextByKey('P_IPT_SENDTHISTO', 'Who do you want to send this to? Select from existing relationships or manual entry.') + '')); tr = $(''); tb.append(tr); tr.append($('').append(InitContactGridData())); tr = $(''); tb.append(tr); tr.append($('' + GetTextByKey("P_IPT_EMAILADDRESSESTIPS", "Separate multiple manually entered email addresses with a semi-colon (;).") + '')); tr = $(''); tb.append(tr); otheremailcontrol = $(''); tr.append($('' + GetTextByKey("P_IPT_OTHEREMAILADDRESS", "Other Email Address") + '').append(otheremailcontrol)); td = $(''); tr.append(td); return tb; } function InitContactGridData() { var div_grid = $('
'); grid_contactdt = new GridView(div_grid); grid_contactdt.lang = { all: GetTextByKey("P_GRID_ALL", "(All)"), ok: GetTextByKey("P_GRID_OK", "OK"), reset: GetTextByKey("P_GRID_RESET", "Reset") }; var list_columns = [ { name: 'UserName', caption: GetTextByKey("P_IPT_CONTACTNAME", "Contact Name"), valueIndex: 'UserName', css: { 'width': 180, 'text-align': 'left' } }, { name: 'ContactType', caption: GetTextByKey("P_IPT_CONTACTTYPE", "Contact Type"), valueIndex: 'ContactType', css: { 'width': 180, 'text-align': 'left' } }, //{ name: 'Text', caption: "Text", valueIndex: 'Text', type: 3, css: { 'width': 45, 'text-align': 'center' } }, { name: 'Selected', caption: GetTextByKey("P_IPT_EMAIL", "Email"), valueIndex: 'Selected', type: 3, css: { 'width': 60, 'text-align': 'center' } } ]; var columns = []; // head for (var hd in list_columns) { var col = {}; col.name = list_columns[hd].name; col.caption = list_columns[hd].caption; col.visible = true; col.sortable = true; col.width = list_columns[hd].css.width; col.align = list_columns[hd].css["text-align"] col.key = list_columns[hd].valueIndex; if (list_columns[hd].type) { col.type = list_columns[hd].type; } columns.push(col); if (col.name === "Email") { col.attrs = function (item) { } } } grid_contactdt.canMultiSelect = false; grid_contactdt.columns = columns; grid_contactdt.init(); grid_contactdt.selectedrowchanged = function (rowindex) { var rowdata = grid_contactdt.source[rowindex]; if (rowdata) { } } return div_grid; } function GetInspectEmailList() { inspectionrequest("GetInspectEmailList", teamintelligence, function (data) { if (typeof (data) !== "string") { showInspectEmailList(data); } }, function (err) { }); } function getReportLayouts() { inspectionrequest('GetInspectLayouts', JSON.stringify([teamintelligence ? 1 : 0, '']), function (data) { if ($.isArray(data)) { var sid = _this.template && _this.template.ReportLayoutId; layoutcontrol.empty().append($('').prop('selected', sid == null || sid == '')); for (var l of data) { layoutcontrol.append($('').val(l.Id).text(l.Name).prop('selected', sid == l.Id)); } } }, function () { }); } function showInspectEmailList(data) { var rows = []; for (var i = 0; i < data.length; i++) { var r = data[i]; r.Selected = false; if (_this.template && _this.template.EmailList) { for (var j = 0; j < _this.template.EmailList.length; j++) { if (r.UserIID.toLowerCase() == _this.template.EmailList[j].UserIID.toLowerCase()) { r.Selected = true; break; } } } var fr = { Values: r }; rows.push(fr); } grid_contactdt.setData(rows); } function getTemplateInfo(id) { var p = JSON.stringify([teamintelligence, htmlencode(id)]); inspectionrequest("GetTemplate", p, function (data) { if (typeof (data) !== "string") { _this.template = data; currenttemplate = _this.template; getReportLayouts(); GetInspectEmailList(); updateContent(); } }, function (err) { }); } function updateContent() { if (pagesicon) { pagesicon.remove(); pagesicon = undefined; } if (pagescontent) { pagescontent.remove(); pagescontent = undefined; } if (filtertd) filtertd.empty(); pagesicon = $('
' + GetTextByKey("P_IPT_PAGES", "Pages") + '
'); content.append(pagesicon); var template = _this.template; if (template.Id) { if ((!template.IssueId || template.IssueId == "") && (template.Editable)) editable = true; else editable = false; namecontrol.val(template.Name); locationenabledcontrol.prop('checked', template.LocationEnabled); needsignaturecontrol.prop('checked', template.NeedSignature); if (forworkordercontrol) forworkordercontrol.prop('checked', template.ForWorkOrder); displaycommittimecontrol.prop('checked', template.DisplayCommitTime); displaycommitbycontrol.prop('checked', template.DisplayCommitBy); displayinspectiontitlecontrol.prop('checked', template.DisplayInspectionTitle); layoutcontrol.val(template.ReportLayoutId); if (IsForesight) { lockedcontrol.prop('checked', !template.Editable); if (template.IssueId && template.IssueId != "") lockedcontrol.prop('disabled', true); } notescontrol.val(template.Notes); otheremailcontrol.val(template.Emails); setPictures(); var next = function () { pages = new Pages(template.Pages); pagescontent = pages.createContent(); content.append(pagescontent); if (!teamintelligence) { filtersmodule = new Filters(template.Filters) filtertd.append(filtersmodule.createContent(template)); } }; inspectionrequest("GetFuelTypes", '', function (data) { if ($.isArray(data)) { window.fuelTypes = data; } next(); }, function () { // TODO: network error }); } else { var next = function () { pages = new Pages(); pagescontent = pages.createContent(); content.append(pagescontent); if (!teamintelligence) { filtersmodule = new Filters() filtertd.append(filtersmodule.createContent()); } }; inspectionrequest("GetFuelTypes", '', function (data) { if ($.isArray(data)) { window.fuelTypes = data; } next(); }, function () { // TODO: network error }); } setEditable(); } function setPictures() { $(picturesdiv).empty(); if (_this.template.StaticPictures && _this.template.StaticPictures.length > 0) { for (var i = 0; i < _this.template.StaticPictures.length; i++) { var p = _this.template.StaticPictures[i]; var img = $('').attr('src', p.Url).attr('title', p.Name); $(picturesdiv).append(img); } } } function setEditable() { if (editable) { namecontrol.prop('disabled', false); locationenabledcontrol.prop('disabled', false); needsignaturecontrol.prop('disabled', false); if (forworkordercontrol) forworkordercontrol.prop('disabled', false); displaycommittimecontrol.prop('disabled', false); displaycommitbycontrol.prop('disabled', false); displayinspectiontitlecontrol.prop('disabled', false); layoutcontrol.prop('disabled', false); notescontrol.prop('disabled', false); picturesadd.show(); } else { namecontrol.prop('disabled', true); locationenabledcontrol.prop('disabled', true); needsignaturecontrol.prop('disabled', true); if (forworkordercontrol) forworkordercontrol.prop('disabled', true); displaycommittimecontrol.prop('disabled', true); displaycommitbycontrol.prop('disabled', true); displayinspectiontitlecontrol.prop('disabled', true); layoutcontrol.prop('disabled', true); notescontrol.prop('disabled', true); picturesadd.hide(); } } function setDisabled() { namecontrol.prop('disabled', true); locationenabledcontrol.prop('disabled', true); needsignaturecontrol.prop('disabled', true); if (forworkordercontrol) forworkordercontrol.prop('disabled', true); displaycommittimecontrol.prop('disabled', true); displaycommitbycontrol.prop('disabled', true); displayinspectiontitlecontrol.prop('disabled', true); layoutcontrol.prop('disabled', true); notescontrol.prop('disabled', true); otheremailcontrol.prop('disabled', true); if (IsForesight) lockedcontrol.prop('disabled', true); } function CheckEmail(mail) { var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (mail.length == 0) return true; return filter.test(mail); } function getSubTypeName(type) { var typename = ""; if (type === "0") typename = GetTextByKey("P_IPT_ST_TRANSACTIONDATE", "Transaction Date"); if (type === "1") typename = GetTextByKey("P_IPT_ST_TICKETRECEIPTNUMBER", "Ticket/Receipt Number"); if (type === "2") typename = GetTextByKey("P_IPT_ST_DRIVERNAME", "Driver Name"); else if (type === "3") typename = GetTextByKey("P_IPT_ST_RETAILERNAME", "Retailer Name"); else if (type === "4") typename = GetTextByKey("P_IPT_ST_RETAILERADDRESS", "Retailer Address"); else if (type === "5") typename = GetTextByKey("P_IPT_ST_CITY", "City"); else if (type === "6") typename = GetTextByKey("P_IPT_ST_STATE", "State"); else if (type === "7") typename = GetTextByKey("P_IPT_ST_ZIP", "Zip"); else if (type === "8") typename = GetTextByKey("P_IPT_ST_ODOMETER", "Odometer"); else if (type === "9") typename = GetTextByKey("P_IPT_ST_FUELTYPE", "Fuel Type"); else if (type === "10") typename = GetTextByKey("P_IPT_ST_QUANTITY", "Quantity"); else if (type === "11") typename = GetTextByKey("P_IPT_ST_UOMCOST", "Unit Cost"); else if (type === "12") typename = GetTextByKey("P_IPT_ST_TOTALCOST", "Total Cost"); else if (type === "13") typename = GetTextByKey("P_IPT_ST_BRANDNAME", "Brand Name"); else if (type === "14") typename = GetTextByKey("P_IPT_ST_NOTES", "Notes"); else if (type === "15") typename = GetTextByKey("P_IPT_ST_PICTURE", "Picture"); else if (type === "16") typename = GetTextByKey("P_IPT_ST_DISTRIBUTEDBY", "Distributed By"); return typename; } function onSave(exit, publish) { var item = { 'Name': namecontrol.val(), 'LocationEnabled': locationenabledcontrol.prop('checked'), 'NeedSignature': needsignaturecontrol.prop('checked'), 'ReportLayoutId': layoutcontrol.val(), 'DisplayCommitTime': displaycommittimecontrol.prop('checked'), 'DisplayCommitBy': displaycommitbycontrol.prop('checked'), 'DisplayInspectionTitle': displayinspectiontitlecontrol.prop('checked'), 'Notes': notescontrol.val() }; if (forworkordercontrol) item.ForWorkOrder = forworkordercontrol.prop('checked'); if (teamintelligence) item.Target = 1; var alerttitle; if (_this.template.Id) { item.Id = _this.template.Id; item.Editable = _this.template.Editable; item.IssueId = _this.template.IssueId; alerttitle = GetTextByKey("P_IPT_EDITTEMPLATE", "Edit Template"); } else { item.Id = -1; item.Editable = true; alerttitle = GetTextByKey("P_IPT_ADDTEMPLATE", "Add Template"); } if (IsForesight) item.Editable = !lockedcontrol.prop('checked'); if (publish === 1) item.Status = 1; else if (_this.template && _this.template.Status == 1) item.Status = 1; else item.Status = 0; if (!item.Name || item.Name.length == 0) { showAlert(GetTextByKey("P_IPT_NAMENOTBEEMPTY", 'Name cannot be empty.'), alerttitle); return; } //email var emailaddress = []; var otheremailaddressstr = otheremailcontrol.val(); if (otheremailaddressstr !== "") emailaddress = otheremailaddressstr.split(';'); item.Emails = ""; for (var i = 0; i < emailaddress.length; i++) { var email = $.trim(emailaddress[i]); if (!CheckEmail(email)) { showAlert(GetTextByKey('P_IPT_OTHEREMAILADDRESSISINVALID', 'The other email address {0} is invalid.').replace('{0}', emailaddress[i]), alerttitle); return; } if (item.Emails === "") item.Emails = email; else item.Emails = item.Emails + ";" + email; } item.Emails = otheremailaddressstr; item.EmailList = []; for (var i = 0; i < grid_contactdt.source.length; i++) { var ct = grid_contactdt.source[i].Values; if (ct.Selected) item.EmailList.push(ct); } if (pages) { item.Pages = pages.getPagesValue(); if (!item.Pages) return; var subtypes = []; for (var i = 0; i < item.Pages.length; i++) { var page = item.Pages[i]; for (var j = 0; j < page.Sections.length; j++) { var section = page.Sections[j]; for (var k = 0; k < section.Questions.length; k++) { var question = section.Questions[k]; if (question.QuestionType == "15" && question.SubType !== "15") { var type = question.SubType; if (subtypes.indexOf(type) >= 0) { var typename = getSubTypeName(type); showAlert(GetTextByKey("P_IPT_ONLYONEQUESTIONCANBEINCLUDEDINTHETEMPLATE", "Only one {0} question can be included in the template.").replace('{0}', typename), GetTextByKey("P_IPT_SAVETEMPLATE", 'Save Template')); return; } else subtypes.push(type); } if (question.QuestionType == "17" && question.IsRequired == true && (!question.TextToCompare || question.TextToCompare.length == 0)) { showAlert(GetTextByKey("P_IPT_TEXTTPCOMPARENOTBEEMPTY", "Text to compare cannot be empty."), GetTextByKey("P_IPT_SAVETEMPLATE", 'Save Template')); return; } } } } } item.Filters = []; if (!teamintelligence && filtersmodule) { item.Filters = filtersmodule.getFiltersValue(); } item.StaticPictures = _this.template.StaticPictures; var param = JSON.stringify(item); var p = JSON.stringify([teamintelligence, htmlencode(param)]); inspectionrequest("SaveTemplate", p, function (data) { if (typeof (data) === "string") { showAlert(data, GetTextByKey("P_IPT_SAVETEMPLATE", 'Save Template')); } else { if (!_this.template.Id) { _this.template.Id = data[0]; } if (exit == 0) { showAlert(GetTextByKey("P_IPT_SAVSUCCESSFULLY", 'Saved successfully.'), GetTextByKey("P_IPT_SAVETEMPLATE", 'Save Template')); _this.datasaved = true; } if (exit == 1) { _this.gsmodule.refresh(); showRightPopup(false); } } }, function (err) { showAlert(GetTextByKey("P_IPT_FAILEDTOSAVETEMPLATE", 'Failed to save Template.'), GetTextByKey("P_IPT_SAVETEMPLATE", 'Save Template')); }); } content.append(createHeader()); content.append(createTemplateContent()); if (this.template.Id) getTemplateInfo(this.template.Id); else { getReportLayouts(); GetInspectEmailList(); updateContent(); } if (templatereadonly) setDisabled(); return content; } return s; });