var ALL_TEXT = "(All)";
var ALL_VALUE = "@all";
$(function () {
// global event
$(document).mousedown(function (e) {
var t = $(e.target);
if (!t.hasClass("div_filter"))
t = t.parents(".div_filter");
if (t.length == 0) $('.div_panel').hide();
var divpanel = t.find(".div_panel");
$('.div_panel').each(function () {
if (!$(this).is(divpanel))
divpanel.hide();
});
});
}());
function showMapAlertLayer(data) {
var opt = $('').val("").text("(None)");
var sel = $("#selMapAlertLayer");
sel.empty();
sel.append(opt);
for (var i = 0; i < data.length; i++) {
var mal = data[i];
opt = $('').val(mal.ID).text(mal.Name).data("mal", mal);
sel.append(opt);
}
if (userParams && userParams.MapAlertLayer) {
sel.val(userParams.MapAlertLayer);
}
showMapAlertLayerDetail();
}
function showMapAlertLayerDetail() {
var mal = $("#selMapAlertLayer").find("option:selected").data("mal");
var tb = $("#tbLegend");
tb.empty();
var haspramameter = false;
if (mal && mal.Layers) {
for (var i = 0; i < mal.Layers.length; i++) {
var layer = mal.Layers[i];
if ((layer.Pivots && layer.Pivots.length > 0))
haspramameter = true;
var tr = $("
").html(harshdrivingtitle[i]).css({ "padding-left": 10, "width": 220 });
tr.append(td);
}
if (haspramameter) {
$('#setparameter').css('display', '');
}
else {
$('#setparameter').css('display', 'none');
}
var right = $('#mapAlertLayerDiv').width() + 100;
$('#filterIconDiv').css('right', right);
}
function setParameterDatimeTimePicker(ipt) {
ipt.datetimepicker({
timepicker: false,
format: 'm/d/Y',
enterLikeTab: false,
onSelectDate: function (v, inp) {
var date = new DateFormatter().formatDate(v, 'm/d/Y 00:00:00');
inp.parent().data('val', [date]);
}
});
}
function getAlertLayerParameters() {
if (parameterinput.length > 0) {//更新参数选择的值
for (var l = 0; l < parameterinput.length; l++) {
var ipt = parameterinput[l];
var param = ipt.data("parameter");
if (ipt.data('commoninput'))
param.ParameterValue = ipt.val();
}
}
var mal = $("#selMapAlertLayer").find("option:selected").data("mal");
if (mal)
return mal.Layers;
return null;
}
var parameterinput = [];
var paramindex = -1;
function setAlertLayerParameters() {
parameterinput = [];
paramindex = -1;
var ul = $("#ul_parameter");
ul.empty();
var pdiv = $("#parameter_right");
pdiv.empty();
var mal = $("#selMapAlertLayer").find("option:selected").data("mal");
var layerHasParam = 0;
if (mal && mal.Layers) {
for (var i = 0; i < mal.Layers.length; i++) {
var layer = mal.Layers[i];
if (layer && layer.Pivots) {
var li = $("").data("layerid", layer.ID).click(layerHasParam++, layerclick);
var img = $("").attr("src", layer.LegendUrl).css({ "width": 18, "vertical-align": "middle" });
var span = $("").text(layer.Title).css({ "margin-left": 10, "vertical-align": "middle" });
li.append(img).append(span);
ul.append(li);
var tb = $("
").data("layerid", layer.ID);
if (layerHasParam == 1)
li.addClass("selected");
else
tb.css('display', 'none');
pdiv.append(tb);
//if (criteriaparam && criteriaparam.length > 0)
// createParameters(criteriaparam, tb, 1);
//if (alertparam && alertparam.length > 0)
// createParameters(alertparam, tb, 2);
if (layer.Pivots && layer.Pivots.length > 0)
createParameters(layer.Pivots, tb);
}
}
}
}
function createParameters(params, tb) {
var criteriaTitle = false;
var alertTitle = false;
for (var j = 0; j < params.length; j++) {
var param = params[j];
if (param.IsCriteriaSQL && !criteriaTitle) {
var tr = $("
").append("
" + GetTextByKey("P_MV_CRITERIA", "Criteria") + "
");
tb.append(tr);
criteriaTitle = true;
}
if (!param.IsCriteriaSQL && !alertTitle) {
var tr = $("
").append("
" + GetTextByKey("P_MV_ALERT", "Alert") + "
");
tb.append(tr);
alertTitle = true;
}
var tr_param = $("
");
tb.append(tr_param);
tr_param.append($("
").addClass('td_paramtype').html(param.Caption));
var td = $("
");
var ipt = createParameterInput(param);
parameterinput.push(ipt);
if (param.DataType == 3 && param.DisplayStyle != 1 && param.DisplayStyle != 2) {
setParameterDatimeTimePicker(ipt);
}
td.append(ipt);
tr_param.append(td);
}
}
function FALSE() { return false; }
var idindex = 0;
function createParameterInput(param) {
var paramvalue = getParameterValue(param.ID, param.IsCriteriaSQL);
if (paramvalue == null)
paramvalue = param.DefaultValue;
if (param.DisplayStyle == 0) {//inputbox
return $("").val(paramvalue).data("parameter", param).data('commoninput', true)
.attr("paramcaption", param.Name.toLowerCase())
.keyup(function () {
$("[paramcaption='" + param.Name.toLowerCase() + "']").val($(this).val());
}).change(function () {
$("[paramcaption='" + param.Name.toLowerCase() + "']").val($(this).val());
});
}
else if (param.DisplayStyle == 1 || param.DisplayStyle == 2) {//Dropdown or Combobox
var defaultcheckall = false;
if (param.IsAllAllowed && paramvalue === ALL_VALUE)
defaultcheckall = true;
if (param.IsField && param.MutipleSelect) {//多选
var divcontrol = $('').data("parameter", param);
var seldiv = $('');
var span = $('');
var spanimg = $('');
var divpanel = $('');
var ul = $('
');
divpanel.append(ul).mousedown(FALSE);
seldiv.append(span).append(spanimg).append(divpanel);
seldiv.mousedown(function (e) {
$('.div_panel').each(function () {
var t = $(this);
if (!t.is(divpanel))
t.css('display', 'none');
});
var display = divpanel.css('display');
if (display == 'none') {
var t = $(e.target);
divpanel.css('top', seldiv.offset().top + seldiv.height());
if (seldiv.offset().top + seldiv.height() + divpanel.height() > $(document).height())
divpanel.css({ 'bottom': 0 });
else
divpanel.css({ 'bottom': 'unset' });
divpanel.css({ 'display': 'block' });
} else {
divpanel.css('display', 'none');
}
return false;
});
var itemclick = function (isall, checked) {
var chks = ul.find('input');
if (isall) {
chks.prop('checked', checked);
if (checked) {
span.attr('title', ALL_TEXT).text(ALL_TEXT);
param.ParameterValue = ALL_VALUE;
}
else {
span.attr('title', "").text("");
param.ParameterValue = "";
}
return;
}
else {
if (chkall)
chkall.prop('checked', false);//(ALL)取消选中
}
var texts = [];
var paramvalues = [];
for (var i = 0; i < chks.length; i++) {
var c = $(chks[i]);
if (c.prop('checked')) {
var text = c.next('label').text();
if (!text) text = c.val();
texts.push(text);
paramvalues.push(c.val());
}
}
texts = texts.join(', ');
span.attr('title', texts).text(texts);
paramvalues = paramvalues.join('^');
param.ParameterValue = paramvalues;
};
var chkall = undefined;
if (param.IsAllAllowed) {
var liall = $('');
chkall = $('');
if (defaultcheckall)
chkall.attr('checked', true);
chkall.change(function () {//全选
itemclick(true, $(this).prop('checked'));
});
liall.append(chkall);
liall.append($('').text(ALL_TEXT));
ul.append(liall);
}
var ds = getLookupDatasource(param.LookupDatasourceID)
if (ds && ds.Items) {
var paramvalueAry = paramvalue.split("^");
var texts = [];
for (var s = 0; s < ds.Items.length; s++) {
var item = ds.Items[s];
var txt = param.DisplayCaptionField ? item.Value : item.Key;
var li = $('');
var chk = $('').val(item.Key).attr("id", "chkp" + ++paramindex);
chk.change(function () {
itemclick(false, $(this).prop('checked'));
});
li.append(chk);
li.append($('').text(txt));
ul.append(li);
if (defaultcheckall || paramvalueAry.indexOf(item.Key) > -1) {//选中默认值
chk.attr('checked', true);
var text = chk.next('label').text();
if (!text) text = chk.val();
texts.push(text);
}
}
if (defaultcheckall)
texts = ALL_TEXT;
else
texts = texts.join(', ');
span.attr('title', texts).text(texts);
param.ParameterValue = paramvalue;
}
divcontrol.append(seldiv);
return divcontrol;
}
else {
var sel = $("").data("parameter", param).data('commoninput', true)
.attr("paramcaption", param.Name.toLowerCase())
.change(function () {
$("[paramcaption='" + param.Name.toLowerCase() + "']").val($(this).val());
});
var ds = getLookupDatasource(param.LookupDatasourceID)
if (ds && ds.Items) {
if (param.IsField && param.IsAllAllowed) {
var opall = $("").val(ALL_VALUE).text(ALL_TEXT);
sel.append(opall);
}
for (var s = 0; s < ds.Items.length; s++) {
var item = ds.Items[s];
var opt = $("").val(item.Key);
var txt = param.DisplayCaptionField ? item.Value : item.Key;
opt.text(txt);
sel.append(opt);
}
sel.val(paramvalue);
}
return sel;
}
}
}
function getLookupDatasource(dsid) {
var mal = $("#selMapAlertLayer").find("option:selected").data("mal");
if (mal && mal.LookupDataSources) {
for (var i = 0; i < mal.LookupDataSources.length; i++) {
var ds = mal.LookupDataSources[i];
if (ds.ID.toLowerCase() == dsid.toLowerCase())
return ds;
}
}
return null;
}
function layerclick(e) {
$('#ul_parameter li').removeClass("selected");
$(this).addClass("selected");
var index = e.data;
var tbs = $('.tb_parameter');
tbs.hide();
$(tbs[index]).show();
}
function getParameterValue(paramid, isCriteria) {
if (parametervalues && parametervalues.length > 0) {
for (var i = 0; i < parametervalues.length; i++) {
var layer = parametervalues[i];
var params = [];
for (var j = 0; j < layer.Pivots.length; j++) {
if (layer.Pivots[j].IsCriteriaSQL == isCriteria)
params.push(layer.Pivots[j])
}
if (layer && (params || params)) {
for (var k = 0; k < params.length; k++) {
var param = params[k];
if (param.ID == paramid)
return param.ParameterValue;
}
}
}
}
return null;
}
function openSetAlertLayerParameters() {
setAlertLayerParameters();
$('#mask_bg').show();
$('#dialog_parameter .dialog-title span.title').text(GetTextByKey("P_MV_PIVOTS", 'Pivots'));
//$('#mask_bg').show();
$('#dialog_parameter')
.attr('act', 'edit')
.css({
'width': 570,
'top': (document.documentElement.clientHeight - $('#dialog_parameter').height()) / 2,
'left': (document.documentElement.clientWidth - $('#dialog_parameter').width()) / 2
})
.showDialogfixed();
pivotsDialogOpend = true;
}
var parametervalues;
function setParameterCompleted() {
parametervalues = getAlertLayerParameters();
assetObject.searchMachine(false);
$('#dialog_parameter').hideDialog();
$('#mask_bg').hide();
pivotsDialogOpend = false;
}