2023-04-28 12:22:26 +08:00

340 lines
13 KiB
JavaScript

/// <reference path="../../js/jquery-3.6.0.min.js" />
/// <reference path="../../js/utility.js" />
_sc = {};
(function () {
_sc.focusIntervalItem = function (e) {
if (e.data) {
return;
}
var tr = $(this).parents('tr:first');
tr.find('input[type="text"]').addClass('focused');
tr.find('textarea').addClass('focused');
tr.find('select').addClass('focused');
tr.find('.general').css('display', 'none');
tr.find('.editing').css('display', 'block');
};
_sc.changeIntervalUom = function (e) {
var td = $(e.target).parent().next();
td.find('span').text($(e.target).val());
};
_sc.addIntervalItem = function (schid) {
$('#dig_name').val('');
$('#dig_interval').val('');
$('#dig_period').val('');
$('#dig_expectedcost').val('');
$('#dig_recurring').prop('checked', false).unbind('change').change(function () {
$('#dig_priority').prop('disabled', !$(this).prop('checked'));
});
$('#dig_priority').prop('disabled', true).val('');
$('#dig_servicedec').val('');
showmaskbg(true);
$('#dialog_interval')
.attr('data-id', schid)
.css({
'top': ($(window).height() - $('#dialog_interval').height()) / 3,
'left': ($(window).width() - $('#dialog_interval').width()) / 3
})
.showDialog();
$('#dig_name').focus();
};
_sc.deleteIntervalItem = function (e) {
var tr = $(this).parents('tr:first');
var iid = tr.attr('data-id');
var alerttitle = GetTextByKey("P_PM_DELETE", 'Delete');
var comfirmmsg = GetTextByKey("P_PM_DELETETHISINTERVAL", 'Do you want to delete this interval?');
if (e.data || !iid) {
showConfirm(comfirmmsg, alerttitle, function (e) {
tr.remove();
});
} else {
showConfirm(comfirmmsg, alerttitle, function (e) {
pmquery('DeletePmInterval', iid, function (data) {
if (!data || data.length == 0) {
//tr.remove();
$('#refresh_button').click();
} else {
showAlert(data, alerttitle);
}
});
});
}
};
_sc.checkIntervalItem = function (item) {
var alerttitle = GetTextByKey("P_PM_UPDATEINTERVAL", 'Update Interval');
if (!item.ServiceName || item.ServiceName.trim().length == 0) {
showAlert(GetTextByKey("P_PM_SERVICENAMENOTBEEMPTY", 'Service Name cannot be empty.'), alerttitle);
return false;
}
item.ServiceName = item.ServiceName.trim();
if (isNaN(item.Interval)) {
showAlert(GetTextByKey("P_PM_INTERVALMUSTBEANUMBER", 'Interval must be a number.'), alerttitle);
return false;
}
if (isNaN(item.NotificationPeriod)) {
showAlert(GetTextByKey("P_PM_NOTIFICATIONPERIODMUSTBEANUMBER", 'Notification Period must be a number.'), alerttitle);
return false;
}
if (item.NotificationPeriod >= item.Interval) {
showAlert(GetTextByKey("P_PM_NOTIFICATIONPERIODMUSTLESSOREQUALINTERVAL", 'Notification period must be less than or equal to the Interval.'), alerttitle);
return false;
}
if (item.ExpectedCost && item.ExpectedCost.length > 0) {
if (isNaN(item.ExpectedCost) || item.ExpectedCost < 0) {
showAlert(GetTextByKey("P_PM_EXPECTEDCOSTFORMATERROR", 'Expected Cost format error.'), alerttitle);
return false;
}
}
else
item.ExpectedCost = 0;
if (item.Recurring) {
item.Priority = parseInt(item.Priority);
if (isNaN(item.Priority)) {
showAlert(GetTextByKey("P_PM_PRIORITYUSTBEANUMBER", 'Priority must be a number.'), alerttitle);
return false;
} else if (item.Priority < 0) {
showAlert(GetTextByKey("P_PM_PRIORITYNOTLESS0", 'Priority cannot be less than 0.'), alerttitle);
return false;
}
} else {
item.Priority = 0;
}
return true;
};
_sc.saveIntervalItem = function (e) {
var tr = $(this).parents('tr:first');
function next() {
tr.find('input[type="text"]').removeClass('focused');
tr.find('textarea').removeClass('focused');
tr.find('select').removeClass('focused');
tr.find('.general').css('display', 'block');
tr.find('.editing').css('display', 'none');
var inps = tr.find('input[type="text"]');
for (var i = 0; i < inps.length; i++) {
var inp = $(inps[i]);
inp.attr('data-ori', inp.val());
}
}
var item = {
'ScheduleId': tr.attr('data-scheduleid'),
'PmIntervalID': tr.attr('data-id'),
'Interval': parseInt(tr.find('.inp_interval').val()),
'NotificationPeriod': parseInt(tr.find('.inp_period').val()),
'ServiceName': tr.find('.inp_name').val(),
'Recurring': tr.find('.inp_recurring').prop('checked'),
'Priority': tr.find('.inp_priority').val(),
'ServiceDescription': tr.find('.textarea_desc').val(),
'ExpectedCost': tr.find('.ipt_expectedcost').val()
};
if (!_sc.checkIntervalItem(item)) {
return;
}
tr.find('.inp_name').val(item.ServiceName);
tr.find('.inp_interval').val(item.Interval);
tr.find('.inp_period').val(item.NotificationPeriod);
tr.find('.textarea_desc').val(item.ServiceDescription);
tr.find('.ipt_expectedcost').val(item.ExpectedCost);
pmquery('UpdatePmInterval', htmlencode(JSON.stringify(item)), function (data) {
if (!data || data.length == 0) {
next();
} else {
showAlert(data, GetTextByKey("P_PM_UPDATEINTERVAL", 'Update Interval'));
}
});
};
_sc.cancelIntervalSave = function (e) {
var tr = $(this).parents('tr:first');
tr.find('input[type="text"]').removeClass('focused');
tr.find('textarea').removeClass('focused');
tr.find('select').removeClass('focused');
tr.find('.general').css('display', 'block');
tr.find('.editing').css('display', 'none');
var inps = tr.find('input');
for (var i = 0; i < inps.length; i++) {
var inp = $(inps[i]);
var data = inp.attr('data-ori');
if (inp.attr('type') == 'text') {
inp.val(data || '');
} else if (inp.attr('type') == 'checkbox') {
inp.prop('checked', eval(data));
}
}
var recurring = tr.find('.inp_recurring').prop('checked');
var inp = tr.find('.inp_priority');
inp.prop('disabled', !recurring);
if (recurring) {
inp.parent('td').removeClass('gray_field');
} else {
inp.parent('td').addClass('gray_field');
}
var textarea = tr.find('.textarea_desc');
var desc = $(textarea).attr('data-ori');
$(textarea).val(desc);
};
var interval_uom = "";
_sc.createIntervalTr = function (item, flag) {
function changeEnable(e) {
var flag = $(this).prop('checked');
var inp = $(this).parents('tr:first').find('.inp_priority');
inp.prop('disabled', !flag);
if (flag) {
inp.parent('td').removeClass('gray_field');
} else {
inp.parent('td').addClass('gray_field');
}
}
var tr = $('<tr></tr>').attr('data-id', item.PmIntervalID).attr('data-scheduleid', item.ScheduleId);
var td;
td = $('<td></td>');
td.append($('<input type="text" class="inp_name" maxlength="50"/>')
.css('width', '100%')
.attr('data-ori', item.ServiceName)
.val(item.ServiceName)
.focus(flag, _sc.focusIntervalItem));
tr.append(td);
td = $('<td></td>');
td.append($('<input type="text" class="inp_interval" maxlength="9"/>')
.attr('data-ori', item.Interval)
.val(item.Interval)
.focus(flag, _sc.focusIntervalItem)
.number());
/*interval uom */
interval_uom = interval_label;
td.append("<span class='span_uom' style='margin-left:8px;'> " + interval_uom + "</span>");
tr.append(td);
td = $('<td></td>');
td.append($('<input type="text" class="inp_period" maxlength="9"/>')
.attr('data-ori', item.NotificationPeriod)
.val(item.NotificationPeriod)
.focus(flag, _sc.focusIntervalItem)
.number());
td.append("<span class='span_uom' style='margin-left:8px;'> " + interval_uom + "</span>");
tr.append(td);
td = $('<td></td>');
td.append($('<input type="checkbox" class="inp_recurring"/>')
.attr('data-ori', item.Recurring)
.prop('checked', item.Recurring)
.change(changeEnable)
.focus(flag, _sc.focusIntervalItem));
tr.append(td);
//td = $('<td class="gray_field"></td>');
//tr.append(td);
//td = $('<td class="gray_field"></td>');
//tr.append(td);
//desc
td = $('<td></td>');
td.append($('<textarea class="textarea_desc" maxlength="3000"></textarea>')
.css('width', '98%').css('height', '28px').css('resize', 'none')
.attr('data-ori', item.ServiceDescription)
.val(item.ServiceDescription)
.focus(flag, _sc.focusIntervalItem));
tr.append(td);
//Expected Cost
td = $('<td></td>');
td.append($('<input type="text" class="ipt_expectedcost" maxlength="12"/>')
.css('width', '100%')
.attr('data-ori', item.ExpectedCost)
.val(item.ExpectedCost)
.focus(flag, _sc.focusIntervalItem));
tr.append(td);
td = $('<td></td>');
if (!item.Recurring) {
td.addClass('gray_field');
}
td.append($('<input type="text" class="inp_priority" maxlength="5"/>')
.attr('data-ori', item.Recurring ? item.Priority : '')
.val(item.Recurring ? item.Priority : '')
.prop('disabled', !item.Recurring)
.focus(flag, _sc.focusIntervalItem)
.number());
tr.append(td);
td = $('<td class="td_funcs"></td>');
// 常规操作
var div = $('<div class="general"></div>');
div.append($('<a onclick="return false;">' + GetTextByKey("P_PM_DELETE", "Delete") + '</a>').click(flag, _sc.deleteIntervalItem));
td.append(div);
div = $('<div class="editing"></div>');
div.append($('<input type="button" value="' + GetTextByKey("P_PM_SAVE", "Save") + '"/>').click(_sc.saveIntervalItem));
div.append($('<input type="button" value="' + GetTextByKey("P_PM_CANCEL", "Cancel") + '"/>').click(_sc.cancelIntervalSave));
td.append(div);
tr.append(td);
return tr;
};
_sc.createIntervalReadonlyTr = function (item) {
var tr = $('<tr></tr>').attr('data-id', item.PmIntervalID);
var td;
td = $('<td style="width: 150px;"></td>');
td.append($('<span class="inp_name"></span>').text(item.ServiceName));
tr.append(td);
/*interval uom */
interval_uom = interval_label;
td = $('<td></td>');
td.append($('<span class="inp_interval"></span>').text(item.Interval));
td.append('<span> ' + interval_uom + '</span>');
tr.append(td);
td = $('<td></td>');
td.append($('<span class="inp_period"></span>').text(item.NotificationPeriod));
td.append('<span> ' + interval_uom + '</span>');
tr.append(td);
td = $('<td></td>');
td.append($('<span class="inp_recurring icon"></span>').html(item.Recurring ? '&#xe605;' : ''));
tr.append(td);
//td = $('<td></td>');
//tr.append(td);
//td = $('<td></td>');
//tr.append(td);
//desc
td = $('<td></td>');
td.append($('<div class="inp_desc"></div>').css('max-width', '200px').css('max-height', '24px')
.html(getText(item.ServiceDescription)).attr('title', item.ServiceDescription));
tr.append(td);
td = $('<td></td>');
td.append($('<span class="ipt_expectedcost"></span>').text(item.ExpectedCost));
tr.append(td);
td = $('<td></td>');
td.append($('<span class="inp_priority"></span>').text(item.Recurring ? item.Priority : ''));
tr.append(td);
return tr;
};
})();