<commit_before>/*
  Map browser based on OpenLayers 4. 
  Popup windows
  
  Copyright (C) 2017 <NAME>, LA7ECA, <EMAIL>
  
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published 
  by the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


// FIXME 
var isMobile = false;


/**
 * @classdesc
 * Popup window manager class. 
 * For now, there is only one active popup at a time. 
 *
 * @constructor
 * @param {polaric.MapBrowser} mb - Map browser instance.
 */

polaric.Popup = function(mb) {
   this.mb            = mb;
   this.onDiv         = document.getElementById("map"); 
   this.activepopup   = null;
   this.psubdiv       = null;
   this.allowedPopups = 1; 
   this.onCallback    = null; 
   this.offCallback   = null;
   this.geoPos        = false;
   this.image         = null;
   var t = this;

   this.mb.view.on('change:center', onChangeCenter);
   this.mb.map.on('click', onClick);
   this.mb.map.on('change:view', onChangeView);
   
   function onChangeCenter() {
       if (t.geoPos && t.geoPos != null) t.setPositionGeo(t.geoPos); 
    }

   function onClick() {
       t.removePopup(); 
   }
   
   function onChangeView() {
       t.removePopup();
       t.mb.view.on('change:center', onChangeCenter);
       // FIXME: Unregister handler on previous view? 
   }
}



/**
 * Deactivate popup.
 */ 
polaric.Popup.prototype.removePopup = function()
{
  if (this.activepopup == null)
    return;
  if (this.offCallback != null)
    this.offCallback(); 
  this.isMenu = false;
  this.allowedPopups++;
                   
  this.activepopup.style.display = "none" ;
  this.activepopup.parentNode.removeChild(this.activepopup);
  this.activepopup = null;
  this.geoPos = null;
}



/**
 * Return true if popup is active. 
 * @returns {boolean}
 */ 

polaric.Popup.prototype.popupActive = function()
   { return (this.activepopup != null); }


   
/**
 * Register callback functions
 * For popup activation (on) and deactivation (off)
 * @param {function} on - Callback to be invoked when popup is activated.
 * @param {function} off - Callback to be invoked when popup is deactivated.
 */

polaric.Popup.prototype.onPopup = function(on, off) 
   { this.onCallback = on; this.offCallback = off; }
   


/**
 * Activate popup. 
 * @param {Object.<string,*>} props - Options
 * @param {string|undefined} props.html - HTML code to render inside the popup.
 * @param {ol.Pixel|undefined} props.pixPos - Pixel position of upper left corner of popup.
 * @param {ol.Coordinate|undefined} props.geoPos - LatLong position of upper left corner of popup.
 * @param {boolean|undefined} props.image - true if we want a cross to be displayed at the position.
 * @param {boolean|undefined} props.draggable - true if we want the popup to be draggable and pinnable.
 * @param {string|undefined} props.id - unique identifier (used as id of element).
 * 
 */

polaric.Popup.prototype.showPopup = function (props) 
{
    var t = this;
    var x, y; 
    if (props.id && props.id != null && document.getElementById(props.id) != null) {
        $('#'+props.id).effect('bounce');
        return null;
    }
    
    var pdiv = ((props.elem && props.elem!=null)  
        ? props.elem : document.createElement('div'));
    if (props.html)
        pdiv.innerHTML = props.html;
    if (props.vnode)
        m.mount(pdiv, props.vnode);
    
    if (props.id && props.id != null) pdiv.id = props.id;
  
    pdiv.className = 'POPUP' + 
       ((props.cclass && props.cclass != null) ? " "+props.cclass : ""); 
  
    setTimeout( function() {
       if (props.geoPos && props.geoPos != null) {
           t.geoPos = props.geoPos;
           props.pixPos = t.mb.map.getPixelFromCoordinate
             (ol.proj.fromLonLat(props.geoPos, t.mb.view.getProjection()));
       }      
       else
           t.geoPos = null;
       
       if (props.pixPos && props.pixPos != null)
          { x = props.pixPos[0]; y=props.pixPos[1]; }
       t.popup_(pdiv, x, y, props.image);
    }, 200);
    
    if (props.draggable) {
       if (props.pinned)
           pdiv._pinned = true;
       var pinimage = document.createElement('img');
       pinimage.className = "popup_pin";
       if (pdiv._pinned) { 
           pinimage.src = "images/pin-red.png";
           t.allowedPopups++;
           t.activepopup = null;
       } else
           pinimage.src = "images/pin-green.png";
       pdiv.appendChild(pinimage);
 
       pinimage.onclick = function(e) {
          pdiv._pinned = (pdiv._pinned ? false : true); 
	      if (props.pin)
	   	     props.pin(pdiv._pinned); // Pin callback
          if (pdiv._pinned) {
             pinimage.src = "images/pin-red.png";
             t.activepopup = null;
             t.allowedPopups++;
          } 
          else {
             pinimage.src = "images/pin-green.png";
       //      t.removePopup();
             t.activepopup = pdiv;
             t.allowedPopups--;
          }
       };
    }
    
    pdiv.onmousedown = function(e) 
       { e = (e)?e:((event)?event:null); e.stopPropagation(); return null; };
    pdiv.onmouseup = function(e) 
       { e = (e)?e:((event)?event:null); return null; };
    pdiv.onclick = function(e)   
       { e = (e)?e:((event)?event:null); e.stopPropagation(); return null; }; 
   
   if (props.resizable) 
       $(pdiv).resizable();
   if (props.draggable) 
       $(pdiv).draggable({ delay: 100, opacity: 0.7, start: props.dragStart, stop: props.dragStop }  );
   return pdiv;

}



/**
 * Activate popup with content from remote server. 
 * @param {string} url - URL of remote content. 
 * @param {Object.<string,*>} props - Options, see showPopup()
 */
// FIXME: Needs testing.

polaric.Popup.prototype.remotePopup = function(url, props)
{
    var d =  this.showPopup(props);
    call(url, null, function(txt) { d.innerHTML = txt; } );<commit_msg>Popup fix<commit_after>/*
  Map browser based on OpenLayers 4. 
  Popup windows
  
  Copyright (C) 2017 <NAME>, LA7ECA, <EMAIL>
  
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published 
  by the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


// FIXME 
var isMobile = false;


/**
 * @classdesc
 * Popup window manager class. 
 * For now, there is only one active popup at a time. 
 *
 * @constructor
 * @param {polaric.MapBrowser} mb - Map browser instance.
 */

polaric.Popup = function(mb) {
   this.mb            = mb;
   this.onDiv         = document.getElementById("map"); 
   this.activepopup   = null;
   this.psubdiv       = null;
   this.allowedPopups = 1; 
   this.onCallback    = null; 
   this.offCallback   = null;
   this.geoPos        = false;
   this.image         = null;
   var t = this;

   this.mb.view.on('change:center', onChangeCenter);
   this.mb.map.on('click', onClick);
   this.mb.map.on('change:view', onChangeView);
   
   function onChangeCenter() {
       if (t.geoPos && t.geoPos != null) t.setPositionGeo(t.geoPos); 
    }

   function onClick() {
       t.removePopup(); 
   }
   
   function onChangeView() {
       t.removePopup();
       t.mb.view.on('change:center', onChangeCenter);
       // FIXME: Unregister handler on previous view? 
   }
}



/**
 * Deactivate popup.
 */ 
polaric.Popup.prototype.removePopup = function()
{
  if (this.activepopup == null)
    return;
  if (this.offCallback != null)
    this.offCallback(); 
  this.isMenu = false;
  this.allowedPopups++;
                   
  this.activepopup.style.display = "none" ;
  this.activepopup.parentNode.removeChild(this.activepopup);
  this.activepopup = null;
  this.geoPos = null;
}



/**
 * Return true if popup is active. 
 * @returns {boolean}
 */ 

polaric.Popup.prototype.popupActive = function()
   { return (this.activepopup != null); }


   
/**
 * Register callback functions
 * For popup activation (on) and deactivation (off)
 * @param {function} on - Callback to be invoked when popup is activated.
 * @param {function} off - Callback to be invoked when popup is deactivated.
 */

polaric.Popup.prototype.onPopup = function(on, off) 
   { this.onCallback = on; this.offCallback = off; }
   


/**
 * Activate popup. 
 * @param {Object.<string,*>} props - Options
 * @param {string|undefined} props.html - HTML code to render inside the popup.
 * @param {ol.Pixel|undefined} props.pixPos - Pixel position of upper left corner of popup.
 * @param {ol.Coordinate|undefined} props.geoPos - LatLong position of upper left corner of popup.
 * @param {boolean|undefined} props.image - true if we want a cross to be displayed at the position.
 * @param {boolean|undefined} props.draggable - true if we want the popup to be draggable and pinnable.
 * @param {string|undefined} props.id - unique identifier (used as id of element).
 * 
 */

polaric.Popup.prototype.showPopup = function (props) 
{
    var t = this;
    var x, y; 
    if (props.id && props.id != null && document.getElementById(props.id) != null) {
        $('#'+props.id).effect('bounce');
        return null;
    }
    
    var pdiv = ((props.elem && props.elem!=null)  
        ? props.elem : document.createElement('div'));
    if (props.html)
        pdiv.innerHTML = props.html;
    if (props.vnode)
        m.mount(pdiv, props.vnode);
    
    if (props.id && props.id != null) pdiv.id = props.id;
  
    pdiv.className = 'POPUP' + 
       ((props.cclass && props.cclass != null) ? " "+props.cclass : ""); 
  
    setTimeout( function() {
       if (props.geoPos && props.geoPos != null) {
           t.geoPos = props.geoPos;
           props.pixPos = t.mb.map.getPixelFromCoordinate
             (ol.proj.fromLonLat(props.geoPos, t.mb.view.getProjection()));
       }      
       else
           t.geoPos = null;
       
       if (props.pixPos && props.pixPos != null)
          { x = props.pixPos[0]; y=props.pixPos[1]; }
       t.popup_(pdiv, x, y, props.image);

    
       if (props.draggable) {
          if (props.pinned)
              pdiv._pinned = true;
          var pinimage = document.createElement('img');
          pinimage.className = "popup_pin";
          if (pdiv._pinned) { 
              pinimage.src = "images/pin-red.png";
              t.allowedPopups++;
              t.activepopup = null;
          } else
             pinimage.src = "images/pin-green.png";
          pdiv.appendChild(pinimage);
 
         pinimage.onclick = function(e) {
             pdiv._pinned = (pdiv._pinned ? false : true); 
	         if (props.pin)
                props.pin(pdiv._pinned); // Pin callback
             if (pdiv._pinned) {
                pinimage.src = "images/pin-red.png";
                t.activepopup = null;
                t.allowedPopups++;
             } 
             else {
                pinimage.src = "images/pin-green.png";
                t.removePopup();
                t.activepopup = pdiv;
                t.allowedPopups--;
             }
         };
    }  }, 200);
    
    pdiv.onmousedown = function(e) 
       { e = (e)?e:((event)?event:null); e.stopPropagation(); return null; };
    pdiv.onmouseup = function(e) 
       { e = (e)?e:((event)?event:null); return null; };
    pdiv.onclick = function(e)   
       { e = (e)?e:((event)?event:null); e.stopPropagation(); return null; }; 
   
   if (props.resizable) 
       $(pdiv).resizable();
   if (props.draggable) 
       $(pdiv).draggable({ delay: 100, opacity: 0.7, start: props.dragStart, stop: props.dragStop }  );
   return pdiv;

}



/**
 * Activate popup with content from remote server. 
 * @param {string} url - URL of remote content. 
 * @param {Object.<string,*>} props - Options, see showPopup()
 */
// FIXME: Needs testing.

polaric.Popup.prototype.remotePopup = function(url, props)
{
    var d =  this.showPopup(props);
    call(url, null, function(txt) { d.innerHTML = txt; } );
{'fraction_non_alphanumeric': 0.13114241001564944, 'fraction_numerical': 0.0028169014084507044, 'mean_word_length': 2.3740760295670538, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5068484', 'n_tokens_mistral': 4117, 'n_tokens_neox': 3991, 'n_words': 1067}