API Docs for: 0.0.8-0
Show:

File: javascript\src\RAMP\Modules\basemapselector.js

/*global define, $, esri, tmpl */
/*jslint white: true */

/**
*
* @module RAMP
*/

/**
* Populates the BasemapGallery widget located in the maps toolbar with items found in the application configuration. 
* This module also handles all the event  needed to change the map basemap and update the UI
*
*
* @class BaseMapSelector
* @static
* @uses dojo/_base/array
* @uses dojo/_base/lang
* @uses dojo/dom-attr
* @uses dojo/query
* @uses dojo/topic
* @uses templates/basemap_selector_template.json
* @uses ramp/globalStorage
* @uses esri/dijit/BasemapGallery
* @uses PopupManager
*/

define([
// Dojo
    "dojo/_base/array", "dojo/_base/lang", "dojo/dom-attr", "dojo/query", "dojo/topic",
// Templates
    "dojo/text!./templates/basemap_selector_template.json",
// Ramp
    "ramp/globalStorage", "ramp/map", "ramp/eventManager",
// Esri
    "esri/dijit/BasemapGallery",
// Util
    "utils/popupManager", "utils/tmplHelper"],

function (
// Dojo
dojoArray, dojoLang, domAttr, query, topic,
// Templates
          basemapselectorTemplate,
// Ramp
          GlobalStorage, RampMap, EventManager,
// Esri
          BasemapGallery,
// Util
          popupManager, tmplHelper) {
    "use strict";

    var basemapGallery,
        basemaps = [],
        config,

        placementAnchorId = "basemapGallery",

        baseMapControls,
        baseMapToggle,
        basemapGalleryNode,

        cssButtonPressedClass = "button-pressed",

        ui = {
            /**
            * Initiates additional UI components of the widget, setting listeners and registering the popup functionality
            *
            * @method init
            * @private
            * @return {object} itself
            * 
            */
            init: function () {
                baseMapControls = $("#basemapControls");
                baseMapToggle = $("#baseMapToggle");
                basemapGalleryNode = $("#basemapGallery");

                // Set alt text for selector thumbnails
                dojoArray.forEach(config.basemaps, function (basemap) {
                    domAttr.set(query(String.format("#galleryNode_{0} img", basemap.id))[0], "alt", basemap.altText);
                });

                // load JSON templates for basemap and skin every node under the basemap selector
                tmpl.templates = JSON.parse(tmplHelper.stringifyTemplate(basemapselectorTemplate));
                dojoArray.forEach($(".esriBasemapGalleryNode"), function (node, i) {
                    $(node).html(tmpl(config.siteTemplate.basemapTemplate, tmplHelper.dataBuilder(config.basemaps[i])));
                });

                // turn on the opening and closing of the basemape selector section
                popupManager.registerPopup(baseMapControls, "hoverIntent",
                    function (d) {
                        basemapGalleryNode.slideDown("fast", function () { d.resolve(); });
                    },
                    {
                        activeClass: cssButtonPressedClass,
                        target: basemapGalleryNode,
                        closeHandler: function (d) {
                            basemapGalleryNode.slideUp("fast", function () { d.resolve(); });
                        },
                        timeout: 500
                    }
                );

                topic.publish(EventManager.BasemapSelector.UI_COMPLETE, { title: basemaps[0].title });

                return this;
            },
            /*
             * Changes the text shown on the toolbar to match the currently selected basemap's title
             * @method updateToggleLabel
             * @private
             * 
             */
            updateToggleLabel: function () {
                baseMapToggle.find("span:first").text(basemapGallery.getSelected().title);
            }
        };

    /**
    * Initializes functions that publish events.
    *
    * @method initTopics
    * @private
    */
    function initTopics() {
        /* PUBLISH */
        basemapGallery.on("selection-change", function () {
            var basemap = basemapGallery.getSelected();

            ui.updateToggleLabel();
            topic.publish(EventManager.BasemapSelector.BASEMAP_CHANGED, {
                id: basemap.id,
                title: basemap.title,
                cssStyle: basemap.scaleCssClass
            });
        });
    }

    /**
    * Initializes class listeners.
    *
    * @method initListeners
    * @private
    */
    function initListeners() {
        /* SUBSCRIBE */
        topic.subscribe(EventManager.BasemapSelector.TOGGLE, function (eventArg) {
            basemapGallery.select(eventArg.id);
        });
    }

    return {
        /*
         * Adds all of the basemaps specified in the application configuration to the basemap selector widget and then calls function to initializes event handling 
         * @method init
         * @constructor
         * 
         */
        init: function () {
            config = GlobalStorage.config;

            dojoArray.forEach(config.basemaps, function (basemap) {
                var layerDijit, basemapDijit;

                layerDijit = new esri.dijit.BasemapLayer({ url: basemap.url });
                basemapDijit = new esri.dijit.Basemap({
                    id: basemap.id,
                    layers: [layerDijit],
                    title: String.format("{0} ({1})", basemap.name, basemap.type),
                    thumbnailUrl: basemap.thumbnail
                });
                basemapDijit.scaleCssClass = basemap.scaleCssClass;

                basemaps.push(basemapDijit);
            });

            //Create and start the selector
            basemapGallery = new BasemapGallery({
                "showArcGISBasemaps": false,
                "basemaps": basemaps,
                "map": RampMap.getMap()
            }, placementAnchorId);
            basemapGallery.startup();
            basemapGallery.select(basemaps[0].id);

            //take over the click
            $(".esriBasemapGalleryNode").on("mousedown", function (evt) {
                var curr_id = basemapGallery.getSelected().id;
                var selected_node = evt.currentTarget.id;
                var selected_id = selected_node.slice(selected_node.indexOf("_") + 1);

                if (curr_id === selected_id) {
                    return false; //prevent the basemap from changing
                } else { //didnt select the same basemap
                    basemapGallery.select(selected_id);
                }
            });

            initTopics();
            initListeners();

            ui
                .init()
                .updateToggleLabel();
        }
    };
});