Source: temp/jsdocinputdirs/cercalia.control.logistics.js

/**
 * @classdesc Let you create a selector menu with a list of options, and assign a click event to each option<br/> The options to be included by parameter are <br/> Example:<br/> var option1 = {name:'opcio_1',className:'class1',clickFunction:function(){alert('1');}}; var option2 = {name:'opcio_2',className:'class2',clickFunction:function(){alert('2');}}; var customSwitcherControl = new cercalia.Control.CustomSwitcherControl({ name : 'Switcher1', name : 'Custom selector menu', menuOptions : [option1,option2] });
 * @constructor
 * @extends {ol.control.Control}
 */
cercalia.Control.Logistics = function() {

    /**
     * @private
     * @type {string}
     */
    this.name_ = cercalia.MapControls.LogisticsControl;

    /**
     * Class name
     * @private
     * @type {string}
     */
    this.CLASS_NAME_ = "cercalia.Control.Logistics";

    /**
     * @private
     * @type {String}
     */
    this.topBarId_ = null;

    /**
     * Button
     * @private
     * @type {Object}
     */
    this.element_ = null;

    /**
     * @private
     * @type {cercalia.WMS}
     */
    this.wmsLayer_ = null;

    /**
     * Layers
     * @private
     * @type {Array.<string>}
     */
    this.paintedLayers_ = [
    /* 'LWEIGHT', 'LWEIGHTAXL', 'LWIDTH', 'LLENGTH', 'LHEIGHT' */
    ];

    /**
     * Values
     * @private
     * @type {Object}
     */
    this.values_ = {
        'LWEIGHT': 0,
        'LWEIGHTAXL': 0,
        'LWIDTH': 0,
        'LLENGTH': 0,
        'LHEIGHT': 0,
        'LDATE': '',
        'LTIMESTART': '',
        'LTIMEEND': ''
    };

    /**
     * Factors of values
     * @private
     * @type {Object}
     */
    this.factors_ = {
        'LWEIGHT': 1,
        'LWEIGHTAXL': 1,
        'LWIDTH': 100,
        'LLENGTH': 100,
        'LHEIGHT': 100
    }

    /**
     * @private
     * @type {Object}
     */
    this.dialog_ = null;

    this.initialize_();
};
ol.inherits(cercalia.Control.Logistics, ol.control.Control);

/**
 * Initializes the element
 * @private
 */
cercalia.Control.Logistics.prototype.initialize_ = function() {

    this.element_ = document.createElement('button');
    this.element_.className = 'logisticsswicher';
    this.element_.innerHTML = cercalia.i18n._('Logistics');

    cercalia.jQuery(this.element_).button({
        icons: {
            primary: "cercalia-big-icon cercalia-big-icon-truck",
            secondary: "ui-icon-triangle-1-s"
        }
    });

    cercalia.jQuery(this.element_).removeClass("ui-button-text-icons").addClass("ui-button-text-icon-secondary cercalia-tool-button");
    cercalia.jQuery(this.element_).find('.ui-button-icon-secondary').hide();
};

/**
 * Create the button in the tools topbar
 * @private
 * @param {string} idTopBar Div element ID where the control will be embedded
 */
cercalia.Control.Logistics.prototype.setTopBar = function(idTopBar) {

    this.topBarId_ = idTopBar;

    var self = this;
    this.element_.addEventListener('click', function(event) {

        /*if (cercalia.jQuery(self.element_).hasClass("cercalia-tool-button-open")) {

            cercalia.jQuery(self.element_).removeClass("cercalia-tool-button-open").button("refresh");
            cercalia.jQuery('.cercalia-tool-button-open').click().removeClass("cercalia-tool-button-open").button("refresh");

        } else {

            cercalia.jQuery('.cercalia-tool-button-open').click().removeClass("cercalia-tool-button-open").button("refresh");
            cercalia.jQuery(self.element_).addClass("cercalia-tool-button-open");

        }*/

        if (!self.wmsLayer_) {

            self.wmsLayer_ = new cercalia.WMS("logistics", servers.logistics, {
                'authkey': servers.authkey,
                'LAYERS': 'LWEIGHT',
                'FORMAT': 'image/png',
                'SRS': 'EPSG:3857',
                'TRANSPARENT': 'TRUE',
                'PROJECTION': 'EPSG:3857',
                'VERSION': '1.1.1',
                'STYLES': ''
            }, { visible: false });
            self.wmsLayer_.updateParams(self.values_);
            self.getMap().cercalia.addLayer(self.wmsLayer_.getLayer());
            self.wmsLayer_.getLayer().setVisible(false);

        }

        // Si no existe la primera vez lo creamos
        if (!self.dialog_) {

            self.dialog_ = self.createDialog_();

        }

        if (self.dialog_.dialog('isOpen')) {

            self.closeList_();

        } else {

            self.openList_();

        }
    });

    var target = document.getElementById(idTopBar);

    ol.control.Control.call(this, {
        element: self.element_,
        target: target
    });

    // this.createMenuList_(target, idTopBar);
};

cercalia.Control.Logistics.prototype.createDialog_ = function() {

    var dialog = cercalia.jQuery('<div />').addClass('cercalia-control-logistics-dialog').dialog({
        height: "auto",
        width: "auto",
        draggable: true,
        autoOpen: false,
        title: cercalia.i18n._("Logistics title"),
        resizable: false,
        position: {
            my: "right top",
            at: "right-10px top+64px",
            of: this.getMap().getViewport()
        },
        appendTo: this.topBarId_ ? "#" + this.topBarId_ : "body"
    });

    var self = this;
    var elementInfo = null;
    var elementPesoMaximoTotal = cercalia.jQuery('<div />').addClass('cercalia-logistics-element cercalia-logistics-element-odd').appendTo(dialog);
    var chkPesoMaximo = cercalia.jQuery('<input />').attr('layer', 'LWEIGHT').attr('type', 'checkbox').addClass('cercalia-logistics-check').appendTo(elementPesoMaximoTotal).change({ dialog: dialog }, function(ev) {
        self.updateLayers_(cercalia.jQuery(this));
        if(ev.data.dialog.find(".cercalia-logistics-check:checked").length >= 1) elementInfo.show();
        else elementInfo.hide();
    });
    cercalia.jQuery("<span />").addClass("ui-icon cercalia-logistics-icon cercalia-logistics-icon-weight").appendTo(elementPesoMaximoTotal);
    cercalia.jQuery('<span />').html(cercalia.i18n._('Total maximum weight')).addClass('cercalia-logistics-text').appendTo(elementPesoMaximoTotal);
    cercalia.jQuery('<input />').attr('title', cercalia.i18n._('tonnes')).attr('factor', 'LWEIGHT').attr('type', 'text').addClass('cercalia-logistics-value').appendTo(elementPesoMaximoTotal).change(function() {
        self.updateFactors_(cercalia.jQuery(this));
        if(!chkPesoMaximo[0].checked) chkPesoMaximo.click();
        if(chkPesoMaximo[0].checked && (this.value == null || this.value.length <= 0)) chkPesoMaximo.click();
    });
    cercalia.jQuery('<span />').html('tn').addClass('cercalia-logistics-units').appendTo(elementPesoMaximoTotal);

    var elementPesoMaximoEje = cercalia.jQuery('<div />').addClass('cercalia-logistics-element').appendTo(dialog);
    var chkPesoMaximoEje = cercalia.jQuery('<input />').attr('layer', 'LWEIGHTAXL').attr('type', 'checkbox').addClass('cercalia-logistics-check').appendTo(elementPesoMaximoEje).change({ dialog: dialog }, function(ev) {
        self.updateLayers_(cercalia.jQuery(this));
        if(ev.data.dialog.find(".cercalia-logistics-check:checked").length >= 1) elementInfo.show();
        else elementInfo.hide();
    });
    cercalia.jQuery("<span />").addClass("ui-icon cercalia-logistics-icon cercalia-logistics-icon-weightaxl").appendTo(elementPesoMaximoEje);
    cercalia.jQuery('<span />').html(cercalia.i18n._('Maximum axle weight')).addClass('cercalia-logistics-text').appendTo(elementPesoMaximoEje);
    cercalia.jQuery('<input />').attr('title', cercalia.i18n._('tonnes')).attr('factor', 'LWEIGHTAXL').attr('type', 'text').addClass('cercalia-logistics-value').appendTo(elementPesoMaximoEje).change(function() {
        self.updateFactors_(cercalia.jQuery(this));
        if(!chkPesoMaximoEje[0].checked) chkPesoMaximoEje.click();
        if(chkPesoMaximoEje[0].checked && (this.value == null || this.value.length <= 0)) chkPesoMaximoEje.click();
    });
    cercalia.jQuery('<span />').html('tn').addClass('cercalia-logistics-units').appendTo(elementPesoMaximoEje);
    cercalia.jQuery('<br />').css("clear", "both").appendTo(dialog);

    var elementAltoMaximo = cercalia.jQuery('<div />').addClass('cercalia-logistics-element cercalia-logistics-element-odd').appendTo(dialog);
    var chkAltoMaximo = cercalia.jQuery('<input />').attr('layer', 'LHEIGHT').attr('type', 'checkbox').addClass('cercalia-logistics-check').appendTo(elementAltoMaximo).change({ dialog: dialog }, function(ev) {
        self.updateLayers_(cercalia.jQuery(this));
        if(ev.data.dialog.find(".cercalia-logistics-check:checked").length >= 1) elementInfo.show();
        else elementInfo.hide();
    });
    cercalia.jQuery("<span />").addClass("ui-icon cercalia-logistics-icon cercalia-logistics-icon-height").appendTo(elementAltoMaximo);
    cercalia.jQuery('<span />').html(cercalia.i18n._('Maximum height')).addClass('cercalia-logistics-text').appendTo(elementAltoMaximo);
    cercalia.jQuery('<input />').attr('title', cercalia.i18n._('meters')).attr('factor', 'LHEIGHT').attr('type', 'text').addClass('cercalia-logistics-value').appendTo(elementAltoMaximo).change(function() {
        self.updateFactors_(cercalia.jQuery(this));
        if(!chkAltoMaximo[0].checked) chkAltoMaximo.click();
        if(chkAltoMaximo[0].checked && (this.value == null || this.value.length <= 0)) chkAltoMaximo.click();
    });
    cercalia.jQuery('<span />').html('m').addClass('cercalia-logistics-units').appendTo(elementAltoMaximo);

    var elementLongitudMaxima = cercalia.jQuery('<div />').addClass('cercalia-logistics-element').appendTo(dialog);
    var chkLongitudMaxima = cercalia.jQuery('<input />').attr('layer', 'LLENGTH').attr('type', 'checkbox').addClass('cercalia-logistics-check').appendTo(elementLongitudMaxima).change({ dialog: dialog }, function(ev) {
        self.updateLayers_(cercalia.jQuery(this));
        if(ev.data.dialog.find(".cercalia-logistics-check:checked").length >= 1) elementInfo.show();
        else elementInfo.hide();
    });
    cercalia.jQuery("<span />").addClass("ui-icon cercalia-logistics-icon cercalia-logistics-icon-length").appendTo(elementLongitudMaxima);
    cercalia.jQuery('<span />').html(cercalia.i18n._('Maximum length')).addClass('cercalia-logistics-text').appendTo(elementLongitudMaxima);
    cercalia.jQuery('<input />').attr('title', cercalia.i18n._('meters')).attr('factor', 'LLENGTH').attr('type', 'text').addClass('cercalia-logistics-value').appendTo(elementLongitudMaxima).change(function() {
        self.updateFactors_(cercalia.jQuery(this));
        if(!chkLongitudMaxima[0].checked) chkLongitudMaxima.click();
        if(chkLongitudMaxima[0].checked && (this.value == null || this.value.length <= 0)) chkLongitudMaxima.click();
    });
    cercalia.jQuery('<span />').html('m').addClass('cercalia-logistics-units').appendTo(elementLongitudMaxima);
    cercalia.jQuery('<br />').css("clear", "both").appendTo(dialog);

    var elementAnchoMaximo = cercalia.jQuery('<div />').addClass('cercalia-logistics-element cercalia-logistics-element-odd').appendTo(dialog);
    var chkAnchoMaximo = cercalia.jQuery('<input />').attr('layer', 'LWIDTH').attr('type', 'checkbox').addClass('cercalia-logistics-check').appendTo(elementAnchoMaximo).change({ dialog: dialog }, function(ev) {
        self.updateLayers_(cercalia.jQuery(this));
        if(ev.data.dialog.find(".cercalia-logistics-check:checked").length >= 1) elementInfo.show();
        else elementInfo.hide();
    });
    cercalia.jQuery("<span />").addClass("ui-icon cercalia-logistics-icon cercalia-logistics-icon-width").appendTo(elementAnchoMaximo);
    cercalia.jQuery('<span />').html(cercalia.i18n._('Maximum width')).addClass('cercalia-logistics-text').appendTo(elementAnchoMaximo);
    cercalia.jQuery('<input />').attr('title', cercalia.i18n._('meters')).attr('factor', 'LWIDTH').attr('type', 'text').addClass('cercalia-logistics-value').appendTo(elementAnchoMaximo).change(function() {
        self.updateFactors_(cercalia.jQuery(this));
        if(!chkAnchoMaximo[0].checked) chkAnchoMaximo.click();
        if(chkAnchoMaximo[0].checked && (this.value == null || this.value.length <= 0)) chkAnchoMaximo.click();
    });
    cercalia.jQuery('<span />').html('m').addClass('cercalia-logistics-units').appendTo(elementAnchoMaximo);

    var elementMercanciasPeligrosas = cercalia.jQuery('<div />').addClass('cercalia-logistics-element').appendTo(dialog);
    cercalia.jQuery('<input />').attr('layer', 'RIMP').attr('type', 'checkbox').addClass('cercalia-logistics-check').appendTo(elementMercanciasPeligrosas).change({ dialog: dialog }, function(ev) {
        self.updateLayers_(cercalia.jQuery(this));
        if(ev.data.dialog.find(".cercalia-logistics-check:checked").length >= 1) elementInfo.show();
        else elementInfo.hide();
    });
	cercalia.jQuery("<span />").addClass("ui-icon cercalia-logistics-icon cercalia-logistics-icon-dangerous-goods").appendTo(elementMercanciasPeligrosas);
    cercalia.jQuery('<span />').html(cercalia.i18n._('Dangerous goods')).addClass('cercalia-logistics-text').appendTo(elementMercanciasPeligrosas);
    cercalia.jQuery('<br />').css("clear", "both").appendTo(dialog);

    var elementRestriccionesFecha = cercalia.jQuery('<div />').addClass('cercalia-logistics-element cercalia-logistics-element-odd').appendTo(dialog);
    cercalia.jQuery('<input />').attr('type', 'checkbox').addClass('cercalia-logistics-check').appendTo(elementRestriccionesFecha).change({ dialog: dialog }, function(ev) {
        if(this.checked) {
            elementRestriccionesFechaOpciones.slideDown();
        } else {
            elementRestriccionesFechaOpciones.slideUp();
        }
        
        if(ev.data.dialog.find(".cercalia-logistics-check:checked").length >= 1) elementInfo.show();
        else elementInfo.hide();
        var currentDate = dayValue.datepicker("getDate");
        self.updateLayerTime_(elementRestriccionesFecha.find('input'), currentDate, hoursValue.slider("option", "values"));
        
    });
    cercalia.jQuery('<span />').html(cercalia.i18n._('Restrictions by date')).addClass('cercalia-logistics-text cercalia-logistics-text-noblank').appendTo(elementRestriccionesFecha);
    
    elementInfo = cercalia.jQuery('<div />').addClass('cercalia-logistics-element cercalia-logistics-legend').appendTo(dialog).hide();
    cercalia.jQuery('<div />').addClass('cercalia-logistics-legend-tramo').appendTo(elementInfo);
    cercalia.jQuery('<div />').html(cercalia.i18n._('Tramo de via prohibida')).addClass('cercalia-logistics-legend-text').appendTo(elementInfo);
    cercalia.jQuery('<br />').css("clear", "both").appendTo(dialog);
    
    var elementRestriccionesFechaOpciones = cercalia.jQuery('<div />').addClass('cercalia-logistics-element-options').appendTo(dialog);
    cercalia.jQuery('<span />').html(cercalia.i18n._('Day')+':').appendTo(elementRestriccionesFechaOpciones);
    var dayValue = cercalia.jQuery('<input />').attr('type', 'text').addClass('cercalia-logistics-element-option-input').appendTo(elementRestriccionesFechaOpciones).datepicker({
        minDate: 0,
        showOn: "button",
        yearRange: (new Date()).getFullYear() + ":" + (new Date()).getFullYear(),
        buttonImage: cercaliaGlobals.css + "/images/cal-icon-24.png",
        buttonText: cercalia.i18n._('Select day')
    }).change(function () {
        
        var currentDate = dayValue.datepicker("getDate");
        self.updateLayerTime_(elementRestriccionesFecha.find('input'), currentDate, hoursValue.slider("option", "values"));
        
    });
    dayValue.datepicker('setDate', new Date());
    cercalia.jQuery('<br />').css("clear", "both").appendTo(elementRestriccionesFechaOpciones);
    
    cercalia.jQuery('<div />').addClass('cercalia-logistics-element-option-hoursTitle').html(cercalia.i18n._('Hour')+':').appendTo(elementRestriccionesFechaOpciones);
    var hoursLabel = cercalia.jQuery('<div />').addClass('cercalia-logistics-element-option-hoursValue').html('00:00 - 23:59').appendTo(elementRestriccionesFechaOpciones);
    cercalia.jQuery('<br />').appendTo(elementRestriccionesFechaOpciones);
    var hoursValue = cercalia.jQuery('<div />').appendTo(elementRestriccionesFechaOpciones).slider({
        range: true,
        min: 0,
        max: 1439,
        step: 1,
        values: [0, 1439],
        slide: function (e, ui) {

            hoursLabel.html(self.formatHM_(ui.values[0]) + ' - ' + self.formatHM_(ui.values[1]));
            
        }, stop: function (e, ui) {            
            var currentDate = dayValue.datepicker("getDate");
            self.updateLayerTime_(elementRestriccionesFecha.find('input'), currentDate, ui.values);
        }
    });

    //dialog.parent().find(".ui-dialog-titlebar-close").detach();

    return dialog;
};

cercalia.Control.Logistics.prototype.updateLayers_ = function(input) {

    if (input[0].checked) {
        this.paintedLayers_.push(input.attr("layer"));
    } else {
        var index = this.paintedLayers_.indexOf(input.attr("layer"));
        this.paintedLayers_.splice(index, 1);
    }
    
    if(this.paintedLayers_.length > 0) {
        this.wmsLayer_.getLayer().setVisible(true);
        this.wmsLayer_.updateParams({
            'LAYERS': this.paintedLayers_.join(",")
        });
        
    } else {
        this.wmsLayer_.getLayer().setVisible(false);
    }

};

cercalia.Control.Logistics.prototype.updateFactors_ = function(input) {

    var newValue = input.val();
    if (newValue) {

        newValue = parseInt(input.val());
        if (!newValue || isNaN(newValue)) {
            newValue = 0;
        }

    } else {
        newValue = 0;
    }

    this.values_[input.attr("factor")] = newValue * this.factors_[input.attr("factor")];
    this.wmsLayer_.updateParams(this.values_);
    
    if(this.paintedLayers_.length > 0) {
        this.wmsLayer_.getLayer().setVisible(true);
    } else {
        this.wmsLayer_.getLayer().setVisible(false);
    }

};

cercalia.Control.Logistics.prototype.updateLayerTime_ = function(input, currentDate, values) {

    if (input[0].checked && this.paintedLayers_.indexOf('LTIMERESTRICTIONS') < 0) {
        this.paintedLayers_.push('LTIMERESTRICTIONS');        
    } else if ( ! input[0].checked ) {
        var index = this.paintedLayers_.indexOf('LTIMERESTRICTIONS');
        this.paintedLayers_.splice(index, 1);
    }
    
    currentDate = currentDate ? currentDate : new Date();
    this.values_['LDATE'] = currentDate.getFullYear() * 10000 + (currentDate.getMonth() + 1) * 100 + currentDate.getDate();
    this.values_['LTIMESTART'] = this.formatHMS_(values[0]);
    this.values_['LTIMEEND'] = this.formatHMS_(values[1]);

    this.wmsLayer_.updateParams(this.values_);
    this.wmsLayer_.updateParams({
        'LAYERS': this.paintedLayers_.join(",")
    });
    
    if(this.paintedLayers_.length > 0) {
        this.wmsLayer_.getLayer().setVisible(true);
    } else {
        this.wmsLayer_.getLayer().setVisible(false);
    }

};

cercalia.Control.Logistics.prototype.formatHM_ = function(value) {
    
    var hours = Math.floor(value / 60);
    var minutes = value - (hours * 60);

    if(hours < 10) hours = '0' + hours;
    if(minutes < 10) minutes = '0' + minutes;

    return hours + ':' + minutes;
};

cercalia.Control.Logistics.prototype.formatHMS_ = function(value) {
    
    var hours = Math.floor(value / 60);
    var minutes = value - (hours * 60);

    if(hours < 10) hours = '0' + hours;
    if(minutes < 10) minutes = '0' + minutes;

    return hours + ':' + minutes + ':00';
};

/**
 * @private
 */
cercalia.Control.Logistics.prototype.openList_ = function() {
    // Siempre que abramos ajustamos la posición del list respecto el botón
    // cercalia.jQuery(this.elementList_).css('left', cercalia.jQuery(this.element_).position().left);

    // var width = cercalia.jQuery(this.element_).width();
    // var rightPosition = cercalia.jQuery(this.element_).parent().width() - cercalia.jQuery(this.element_).position().left - width + 8;
    // cercalia.jQuery(this.elementList_).css('right', rightPosition);

    // cercalia.jQuery(this.elementList_).fadeIn();
    this.dialog_.dialog('open');
    cercalia.jQuery(this.element_).find('.ui-button-icon-secondary').hide()/*.addClass('ui-icon-triangle-1-n').removeClass('ui-icon-triangle-1-s')*/;
};

/**
 * @private
 */
cercalia.Control.Logistics.prototype.closeList_ = function() {
    // cercalia.jQuery(this.elementList_).fadeOut();
    this.dialog_.dialog('close');
    cercalia.jQuery(this.element_).find('.ui-button-icon-secondary').hide()/*.addClass('ui-icon-triangle-1-s').removeClass('ui-icon-triangle-1-n')*/;
};

/**
 * @private
 * @param {object} target
 * @param {string} idTopBar
 */
/*
 * cercalia.Control.Logistics.prototype.createMenuList_ = function(target, idTopBar){
 * 
 * var switcherList = document.createElement('div'); switcherList.className = 'customswitcherList';
 * 
 * var ulSwitcherList = document.createElement('ul'); ulSwitcherList.className = 'ui-corner-all ui-widget ui-state-default';
 * 
 * switcherList.appendChild(ulSwitcherList);
 * 
 * target.appendChild(switcherList);
 * 
 * this.elementList_ = switcherList;
 * 
 * for(var i = 0; i < this.menuOptions_.length; i++){ this.assignMenuOptionAction_(this.menuOptions_[i], i); } };
 */

/**
 * @private
 * @param {object} option
 * @param {number} numOption
 */
/*
 * cercalia.Control.Logistics.prototype.assignMenuOptionAction_ = function(option, numOption){ cercalia.jQuery('<li />') .addClass(numOption == 0 ? "ui-corner-top ui-no-corner-bottom" : (numOption == this.menuOptions_.length-1 ? "ui-corner-bottom ui-no-corner-top" : "ui-no-corner-all") ) .addClass("cercalia-box-sizing" + (option.className?" "+option.className : "") ) .text(option.name) .click(function(){ option.clickFunction(); }) .appendTo( cercalia.jQuery(this.elementList_).find('ul') ) .button(); };
 */

/**
 * Returns the HTML element for every created menu.
 * @return {HTMLElement}
 */
/*
 * cercalia.Control.Logistics.prototype.getElementList = function() { return this.elementList_; };
 */

/**
 * @return {string} Returns the label name assigned to the control
 */
cercalia.Control.Logistics.prototype.getLabel = function() {
    return this.label_;
};