Advertisement
dav4530

Openlayers Maptext Javascript

Dec 18th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($) {
  2.  
  3.   Drupal.behaviors.openlayers_plus_behavior_maptext = {
  4.       attach : function(context, setting) {
  5.         var toggle_setting;
  6.  
  7.         $('a.toggle_button').click(function (e) {
  8.           e.stopImmediatePropagation();
  9.           if($.cookie('block_titles') != null && $.cookie('block_titles').indexOf(',') == -1) {
  10.  
  11.             $(this).toggleClass("down");
  12.  
  13.             $('div#' + $(this).attr('id')).parent().toggle();
  14.             if ($(this).attr('class').indexOf('down') != -1) {
  15.               $.cookie($(this).html(), 'down');
  16.  
  17.             }else {
  18.               $.cookie($(this).html(), 'up');
  19.  
  20.             }
  21.           }else {
  22.  
  23.             var blocks = $.cookie('block_titles').split(',');
  24.  
  25.             for(var y = 0; y < blocks.length; y++) {
  26.               if(blocks[y] != "" && blocks[y].indexOf('#') === -1 && blocks[y].indexOf('context') === -1) {
  27.                 if(blocks[y] == $(this).attr('id')) {
  28.                   $('div#' + blocks[y]).parent().toggle();
  29.                   $('a#' + blocks[y]).toggleClass("down");
  30.                 }else if (
  31.                     $('div#' + blocks[y]).is(":visible")) {
  32.                   $('a#' + blocks[y]).toggleClass("down");
  33.                   $('div#' + blocks[y]).parent().toggle();
  34.                 }
  35.                 if($('a#' + blocks[y]).attr('class').indexOf('down') != -1) {
  36.                   $.cookie($('a#' + blocks[y]).html(), 'down');
  37.  
  38.                 }else {
  39.                   $.cookie($('a#' + blocks[y]).html(), 'up');
  40.  
  41.                 }
  42.               }
  43.             }
  44.           }
  45.  
  46.         });
  47.  
  48.         $('.maptext a.popup-close').click(function(e){
  49.           e.stopImmediatePropagation();
  50.           var klass = $(this).parent().attr('id');
  51.           $('a#' + klass).toggleClass("down");
  52.           $("div#" + klass).parent().toggle();
  53.           $.cookie($('a#' + klass).html(), 'down');
  54.           return false;
  55.         });
  56.  
  57.         if(Object.size(setting.openlayers_plus.collapse) === 1){
  58.           $('.bntitle').click(function(e){
  59.             e.stopImmediatePropagation();
  60.             $(".bnContainer").slideToggle('slow');
  61.           });
  62.         }
  63.  
  64.         for(var regn in setting.openlayers_plus.drag) {
  65.           if (setting.openlayers_plus.drag.hasOwnProperty(regn)) {
  66.             if(setting.openlayers_plus.drag[regn].fieldset.draggable != 0 && setting.openlayers_plus.maptextregions[regn] != undefined){
  67.               var map;
  68.               var data = $(context).data('openlayers');
  69.               if(data != undefined) {
  70.                 map = data.openlayers;
  71.                 for (var i = 0; i< map.controls.length; i++) {
  72.                   if (map.controls[i].displayClass ==
  73.                   "olControlNavigation") {
  74.                     map.controls[i].dragPan.deactivate();
  75.                   }
  76.                 }
  77.               }
  78.  
  79.               $('.btns').draggable({
  80.                 stop: function(pos){
  81.                   var Stoppos = [];
  82.                   Stoppos = pos;
  83.                   localStorage.posleft = Stoppos['left'];
  84.                   localStorage.postop =  Stoppos['top'];
  85.                 },
  86.                 containment: $('#openlayers-map')
  87.               });
  88.  
  89.             }
  90.             else {
  91.               $.cookie("pos-left", null);
  92.               $.cookie("pos-top", null);
  93.             }
  94.  
  95.           }
  96.  
  97.         }
  98.         var data = $(context).data('openlayers');
  99.         if (data && data.map.behaviors.openlayers_plus_behavior_maptext) {
  100.           var map = data.openlayers;
  101.           toggle_setting = setting.openlayers_plus.Toggle;
  102.           button_title = setting.openlayers_plus.Btn_Titles;
  103.           collapse = setting.openlayers_plus.collapse;
  104.           positions = setting.openlayers_plus.position;
  105.           var maptextpos = '';
  106.  
  107.           regions = setting.openlayers_plus.maptextregions;
  108.           controls = new Array();
  109.           for (region in regions) {
  110.             var region_toggles = new toggles();
  111.  
  112.             for(position in positions){
  113.               if(positions.hasOwnProperty(region) && positions[region] != 0){
  114.                 maptextpos = positions[region];
  115.               }
  116.             }
  117.  
  118.             for (block in regions[region]) {
  119.  
  120.               block_toggle = new togle(
  121.                   block,
  122.                   regions[region][block].title,
  123.                   regions[region][block].title,
  124.                   regions[region][block].markup,
  125.                   0,0,0,0, maptextpos,
  126.                   region, collapse);
  127.               region_toggles.addElement(block_toggle);
  128.             }
  129.             region_toggles.showControlHTML(map, toggle_setting);
  130.             region_toggles.showButtonHTML(map, toggle_setting, button_title, collapse);
  131.  
  132.           }
  133.         }
  134.       }
  135.   };
  136.  
  137.   Object.size = function(obj) {
  138.     var size = 0, key;
  139.     for (key in obj) {
  140.       if (obj.hasOwnProperty(key)) {
  141.         size++;
  142.       }
  143.     }
  144.     return size;
  145.   };
  146.  
  147.   function togle(id,cTitle, blabel, ctrl, lft, rgt, tp, btm, pos, Region, col) {
  148.     this.iD = id;
  149.     this.buttonlabel = blabel;
  150.     this.controlTitle = cTitle;
  151.     this.controlcontent = ctrl;
  152.  
  153.     this.left = lft;
  154.     this.top = tp;
  155.     this.right = rgt;
  156.     this.bottom = btm;
  157.     this.region = Region;
  158.  
  159.     this.draw = function(content, type, indx) {
  160.       var element = new mtext();
  161.  
  162.       element.html = content;
  163.       if(type == 'btn') {
  164.         element.button = true;
  165.         element.maptext = false;
  166.       }else if(type == 'maptext') {
  167.         element.button = false;
  168.         element.maptext = true;
  169.         element.index = indx;
  170.       }
  171.  
  172.       if($.cookie("pos-left") != null && $.cookie("pos-top") != null) {
  173.         element.left = $.cookie("pos-left");
  174.         element.top = $.cookie("pos-top");
  175.       }
  176.       else {
  177.         element.left = this.left;
  178.         element.top = this.top;
  179.       }
  180.  
  181.       element.identifier = this.iD;
  182.       element.bottom = this.bottom;
  183.       element.right = this.right;
  184.       element.position = pos;
  185.  
  186.       return element;
  187.     }
  188.  
  189.   }
  190.  
  191.   function toggles() {
  192.     this.elements = [];
  193.  
  194.     this.addElement = function(instance) {
  195.       if(this.elements.length == 0){
  196.         this.elements[this.elements.length] = instance;
  197.       }else if(this.elements.length >= 1) {
  198.         this.elements[this.elements.length] = instance;
  199.       }
  200.  
  201.     }
  202.  
  203.     this.showControlHTML = function(map, toggl_menu) {
  204.       var mapHTML;
  205.       var toggleObject;
  206.       var title;
  207.  
  208.       for (var toggleObjectkey in this.elements) {
  209.         toggleObject = this.elements[toggleObjectkey];
  210.         if(toggleObject.controlTitle === 'None'){
  211.           title = '<h2 class="popup-title none"><a></a></h2>';
  212.         }
  213.         else
  214.         {
  215.           title = '<h2 class="popup-title"><a>' + toggleObject.controlTitle + '</a></h2>';
  216.         }
  217.         mapHTML = toggleObject.draw('<div class="maptext" id="' + toggleObject.iD + '">' +
  218.             '<a class="popup-close" href="">X</a>' + title +
  219.             '<div class="popup-content">' + toggleObject.controlcontent + '</div>' + '<div class="popup-pager"><ul class="links" style="display: none;">' + '<li><a class="prev disabled" href="#prev">Prev</a></li>' + '<li><a class="next" href="#next">Next</a></li>' + '</ul><span class="count">1 / 0</span></div>' + '</div>','maptext',toggleObjectkey);
  220.  
  221.         map.addControl(mapHTML);
  222.         if ($.cookie(toggleObject.controlTitle) == 'down') {
  223.           $('div#' + toggleObject.iD).parent().toggle();
  224.         }
  225.         else if ($.cookie(toggleObject.controlTitle) == undefined) {
  226.           $('div#' + toggleObject.iD).parent().hide();
  227.         }
  228.  
  229.  
  230.       }
  231.  
  232.     }
  233.  
  234.     this.showButtonHTML = function(map, toggl_menu, btntitle, colapse) {
  235.  
  236.       var bnGroup = "";
  237.       var hideAnchor = [];
  238.       var toggleobject;
  239.       var block_title = '';
  240.       var label;
  241.  
  242.       for (var x = 0; x < toggl_menu.length; x++) {
  243.         block_title = toggl_menu[x] + ',' + block_title;
  244.       }
  245.  
  246.       $.cookie('block_titles', block_title);
  247.  
  248.       for (var toggleobjectkey in this.elements) {
  249.         toggleobject = this.elements[toggleobjectkey];
  250.         if (toggleobject.buttonlabel != "") {
  251.           if(toggleobject.buttonlabel === 'None') {
  252.             label = '';
  253.           }else{
  254.             label = toggleobject.buttonlabel;
  255.           }
  256.           bnGroup = bnGroup + '<div class="' + toggleobject.iD + '"><a class="toggle_button" id="' + toggleobject.iD + '" title="' + label + '">' + label + '</a></div>';
  257.           if ($.cookie(toggleobject.buttonlabel) == 'down') {
  258.             hideAnchor.push(toggleobject.iD);
  259.           }
  260.         }
  261.       }
  262.  
  263.       if (colapse[toggleobject.region] === 1 && btntitle[toggleobject.region] != undefined) {
  264.         group = "<a><b><h3 class='bntitle'>" + btntitle[toggleobject.region] + "</h3></b></a><div class='bnContainer'>" + bnGroup + "</div>";
  265.       }
  266.       else if(btntitle[toggleobject.region] != undefined) {
  267.         group = "<a><b><h3 class='bntitle'>" + btntitle[toggleobject.region] + "</h3></b></a><div class='bnContainer'>" + bnGroup + "</div>";
  268.       }
  269.       else {
  270.         group = "<div class='bnContainer'>" + bnGroup + "</div>";
  271.       }
  272.  
  273.       if (bnGroup != "") {
  274.         map.addControl(toggleobject.draw(group,'btn',toggleobjectkey));
  275.       }
  276.       if ($.cookie(toggleobject.buttonlabel) == undefined) {
  277.         $('a.toggle_button').toggleClass('down');
  278.       }
  279.       else {
  280.         for (var xkey in hideAnchor) {
  281.           var x = hideAnchor[xkey];
  282.           $('div.bnContainer').find('a#' + x).toggleClass('down');
  283.  
  284.         }
  285.       }
  286.     }
  287.   }
  288.  
  289. })(jQuery);
  290.  
  291. mtext = OpenLayers.Class(OpenLayers.Control.Attribution, {
  292.  
  293.   separator: ", ",
  294.   html: "",
  295.   button:"",
  296.   maptext:"",
  297.   index:"",
  298.   top:"",
  299.   right:"",
  300.   bottom:"",
  301.   left:"",
  302.   position:"",
  303.   identifier:"",
  304.  
  305.   destroy: function() {
  306.     this.events.un({
  307.       "removelayer": this.updateAttribution,
  308.       "addlayer": this.updateAttribution,
  309.       "changelayer": this.updateAttribution,
  310.       "changebaselayer": this.updateAttribution,
  311.       scope: this
  312.     });
  313.  
  314.     OpenLayers.Control.prototype.destroy.apply(this, arguments);
  315.   },
  316.   draw: function() {
  317.     OpenLayers.Control.prototype.draw.apply(this, arguments);
  318.  
  319.     this.events.on({
  320.       'changebaselayer': this.updateAttribution,
  321.       'changelayer': this.updateAttribution,
  322.       'addlayer': this.updateAttribution,
  323.       'removelayer': this.updateAttribution,
  324.       scope: this
  325.     });
  326.  
  327.     if (this.button) {
  328.       this.div.classList.add('ui-widget-content');
  329.       this.div.classList.add('btns');
  330.       this.div.classList.add(this.identifier);
  331.  
  332.       if(this.position != "") {
  333.         this.div.classList.add(this.position);
  334.       }
  335.     }
  336.     else if (this.maptext) {
  337.       this.div.classList.add('maptext_' + this.index + '_' + this.identifier);
  338.     }
  339.  
  340.     this.updateAttribution();
  341.  
  342.     return this.div;
  343.   },
  344.   updateAttribution: function() {
  345.     if (this.map && this.map.layers) {
  346.       this.div.innerHTML = this.html;
  347.  
  348.       if(this.left != 0 && this.top != 0) {
  349.         this.div.style.left = this.left + 'px';
  350.         this.div.style.top = this.top + 'px';
  351.       }
  352.     }
  353.  
  354.   },
  355.  
  356.   CLASS_NAME: "OpenLayers.Control.Maptext"
  357. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement