This is a wiki for a reason. Anyone can contribute. If you see something that is inaccurate or can be improved, don't ask that it be fixed--just improve it.
[ Disclaimer, Create new user --- Wiki markup help, Install P99 ]

Difference between revisions of "MediaWiki:LocMaps.js"

From Project 1999 Wiki
Jump to: navigation, search
Line 1: Line 1:
try {
+
  try {
(function() {
+
  (function() {
  
  // TODO: Add support for cropping maps that have multiple maps.
+
    // TODO: Add support for cropping maps that have multiple maps.
  // For instance, the following CSS styles show only one of the three maps for  
+
    // For instance, the following CSS styles show only one of the three maps for  
  // Erudin Palace:
+
    // Erudin Palace:
  // background-image:url(/images/Erudinpalace.jpg);  
+
    // background-image:url(/images/Erudinpalace.jpg);  
  // background-position: 250px 0px; width: 275px; height: 272px
+
    // background-position: 250px 0px; width: 275px; height: 272px
  // Figure out how to make the Xs line up with those styles added
+
    // Figure out how to make the Xs line up with those styles added
  
  var locIsWithinAlternateData = function(loc, alternateData) {
+
    var locIsWithinAlternateData = function(loc, alternateData) {
    return loc.x < alternateData.maxX &&
+
      return loc.x < alternateData.maxX &&
    loc.x > alternateData.minX &&
+
      loc.x > alternateData.minX &&
    loc.y < alternateData.maxY &&
+
      loc.y < alternateData.maxY &&
    loc.y > alternateData.minY;
+
      loc.y > alternateData.minY;
  };
+
    };
  
  
  /**
+
    /**
  * Determines if the provided name contains any of the provided words
+
    * Determines if the provided name contains any of the provided words
  */
+
    */
  // TODO: Add a some polyfill to make this a lot simpler
+
    // TODO: Add a some polyfill to make this a lot simpler
  var containsAny = function(name/* word1, word2, etc. */) {
+
    var containsAny = function(name/* word1, word2, etc. */) {
    for(var arg = 1; arg < arguments.length; ++ arg) {
+
      for(var arg = 1; arg < arguments.length; ++ arg) {
      var word = arguments[arg];
+
        var word = arguments[arg];
      if (name.includes(word)) return true;
+
        if (name.includes(word)) return true;
    }
+
      }
    return false;
+
      return false;
  };
+
    };
  
  var containsAnyNumber = function(name, number, suffix) {
+
    var containsAnyNumber = function(name, number, suffix) {
    return containsAny.call(null, name,  
+
      return containsAny.call(null, name,  
    number + suffix,
+
      number + suffix,
    'level ' + number,
+
      'level ' + number,
    'level: ' + number
+
      'level: ' + number
    );
+
      );
  };
+
    };
  
  var getZoneLevelData = function(levels, text) {
+
    var getZoneLevelData = function(levels, text) {
    // Check for part "level" aliases (eg. "1st floor" vs. "Level One")
+
      // Check for part "level" aliases (eg. "1st floor" vs. "Level One")
    text = text.toLowerCase();
+
      text = text.toLowerCase();
    if (containsAnyNumber(text, 0, 'th') || containsAny(text, 'basement', 'underground')) return levels[0];
+
      if (containsAnyNumber(text, 0, 'th') || containsAny(text, 'basement', 'underground')) return levels[0];
    if (containsAnyNumber(text, 1, 'st') || containsAny(text, 'one', 'first', 'ground')) return levels[1];
+
      if (containsAnyNumber(text, 1, 'st') || containsAny(text, 'one', 'first', 'ground')) return levels[1];
    if (containsAnyNumber(text, 2, 'nd') || containsAny(text, 'two', 'second')) return levels[2];
+
      if (containsAnyNumber(text, 2, 'nd') || containsAny(text, 'two', 'second')) return levels[2];
    if (containsAnyNumber(text, 3, 'rd') || containsAny(text, 'three', 'third')) return levels[3];
+
      if (containsAnyNumber(text, 3, 'rd') || containsAny(text, 'three', 'third')) return levels[3];
    if (containsAnyNumber(text, 4, 'th') || containsAny(text, 'four', 'fourth')) return levels[4];
+
      if (containsAnyNumber(text, 4, 'th') || containsAny(text, 'four', 'fourth')) return levels[4];
    if (containsAnyNumber(text, 5, 'th') || containsAny(text, 'five', 'fifth')) return levels[5];
+
      if (containsAnyNumber(text, 5, 'th') || containsAny(text, 'five', 'fifth')) return levels[5];
    if (containsAnyNumber(text, 6, 'th') || containsAny(text, 'six', 'sixth')) return levels[6];
+
      if (containsAnyNumber(text, 6, 'th') || containsAny(text, 'six', 'sixth')) return levels[6];
    if (containsAnyNumber(text, 7, 'th') || containsAny(text, 'seven', 'seventh')) return levels[7];
+
      if (containsAnyNumber(text, 7, 'th') || containsAny(text, 'seven', 'seventh')) return levels[7];
    if (containsAnyNumber(text, 8, 'th') || containsAny(text, 'eight', 'eighth')) return levels[8];
+
      if (containsAnyNumber(text, 8, 'th') || containsAny(text, 'eight', 'eighth')) return levels[8];
    if (containsAnyNumber(text, 9, 'th') || containsAny(text, 'nine', 'ninth')) return levels[9];
+
      if (containsAnyNumber(text, 9, 'th') || containsAny(text, 'nine', 'ninth')) return levels[9];
  
    var levelKeys = Object.keys(levels);
+
      var levelKeys = Object.keys(levels);
    var keyOfLastLevel = levelKeys[levelKeys.length - 1];
+
      var keyOfLastLevel = levelKeys[levelKeys.length - 1];
    if (containsAny(text, 'top')) return levels[keyOfLastLevel];
+
      if (containsAny(text, 'top')) return levels[keyOfLastLevel];
  
    // If we still couldn't match, and there is a ground/1st floor, use it
+
      // If we still couldn't match, and there is a ground/1st floor, use it
    return levels[1];
+
      return levels[1];
  };
+
    };
  
 
+
   
  
  var findZoneData = function(zoneName, locs, nonLocParts) {
+
    var findZoneData = function(zoneName, locs, nonLocParts) {
    var zone = zoneData[zoneName];
+
      var zone = zoneData[zoneName];
  
  // Handle aliases (eg. "North Ro" instead of "Northern Desert of Ro")
+
    // Handle aliases (eg. "North Ro" instead of "Northern Desert of Ro")
  if (typeof(zone) === 'string') zone = zoneData[zone];
+
    if (typeof(zone) === 'string') zone = zoneData[zone];
  
  if (!zone) return null;
+
    if (!zone) return null;
  
  // Handle Multi-Level Zones (with different maps for 1st, 2nd, etc. floor)
+
    // Handle Multi-Level Zones (with different maps for 1st, 2nd, etc. floor)
  if (zone.levels) return getZoneLevelData(zone.levels, nonLocParts);
+
    if (zone.levels) return getZoneLevelData(zone.levels, nonLocParts);
  
  // Handle Zones with alternate maps on the same level (eg. Kelethin in GFay)
+
    // Handle Zones with alternate maps on the same level (eg. Kelethin in GFay)
  if (!zone.alternateMaps) return zone;
+
    if (!zone.alternateMaps) return zone;
  
  for (var i = 0; i < zone.alternateMaps.length; i++) {
+
    for (var i = 0; i < zone.alternateMaps.length; i++) {
    var alternateData = zone.alternateMaps[i];
+
      var alternateData = zone.alternateMaps[i];
    var allLocsAreWithin = true;
+
      var allLocsAreWithin = true;
    // wish I had ES6 [].every
+
      // wish I had ES6 [].every
    $.each(locs, function(i, loc) {
+
      $.each(locs, function(i, loc) {
      allLocsAreWithin = allLocsAreWithin &&  
+
        allLocsAreWithin = allLocsAreWithin &&  
      locIsWithinAlternateData(loc, alternateData);
+
        locIsWithinAlternateData(loc, alternateData);
    });
+
      });
    if (allLocsAreWithin) return alternateData;
+
      if (allLocsAreWithin) return alternateData;
 +
    };
 +
    return zone;
 
   };
 
   };
  return zone;
 
};
 
  
var addImageUrl = function(zoneData) {
+
  var addImageUrl = function(zoneData) {
  zoneData.imageUrl = '/images/' + zoneData.image;
+
    zoneData.imageUrl = '/images/' + zoneData.image;
  return zoneData;
+
     return zoneData;
};
+
 
+
var getZoneData = function(zoneName, locs, nonLocParts) {
+
  var zoneData = findZoneData(zoneName, locs, nonLocParts);
+
  if (!zoneData) return null;
+
  zoneData.zoneName = zoneName;
+
  return addImageUrl(zoneData);
+
};
+
 
+
  var build$MapImage = function(zoneData) {
+
     return $(
+
      '<img alt="Map of ' + zoneData.zoneName + '" ' +
+
      '    class="thumbborder" ' +
+
      '    height="'+ zoneData.height + '" ' +
+
      '    src="' + zoneData.imageUrl + '" ' +
+
      '    title="Map of ' + zoneData.zoneName + '"' +
+
      '    width="' + zoneData.width + '" ' +
+
      '>'
+
      );
+
  };``
+
 
+
  var build$XContainer = function() {
+
    return $('<div class="x-container"></div>')
+
      .css({ position: 'relative' });
+
  }
+
 
+
  var build$WrappedMap = function(zoneData) {
+
    return build$XContainer().append(build$MapImage(zoneData));
+
 
   };
 
   };
  
   var build$LightboxFramedMap = function(zoneData) {
+
   var getZoneData = function(zoneName, locs, nonLocParts) {
     var $map = build$WrappedMap(zoneData);
+
     var zoneData = findZoneData(zoneName, locs, nonLocParts);
     return addFramingStyles($map, zoneData.width, zoneData.height)
+
     if (!zoneData) return null;
 +
    zoneData.zoneName = zoneName;
 +
    return addImageUrl(zoneData);
 
   };
 
   };
  
  var addFramingStyles = function($el, width, height) {
+
    var build$MapImage = function(zoneData) {
    return $el.css({
+
      return $(
         left: '50%',
+
         '<img alt="Map of ' + zoneData.zoneName + '" ' +
         marginLeft: '-' + (width / 2) + 'px', // centering
+
         '     class="thumbborder" ' +
         marginTop: '-' + (height / 2) + 'px',
+
        '    height="'+ zoneData.height + '" ' +
         opacity: 1,
+
         '     src="' + zoneData.imageUrl + '" ' +
         top: '50%'
+
         '    title="Map of ' + zoneData.zoneName + '"' +
      });
+
         '     width="' + zoneData.width + '" ' +
  }
+
        '>'
 +
        );
 +
    };``
  
  var build$MapFrame = function() {
+
    var build$XContainer = function() {
    return $('<div class="map-wrapper"></div>')
+
      return $('<div class="x-container"></div>')
            .css({ position: 'absolute' });
+
        .css({ position: 'relative' });
  };
+
    }
  
  var removeOpenMap = function(e) {
+
    var build$WrappedMap = function(zoneData) {
    // Do nothing if there's no open map
+
      return build$XContainer().append(build$MapImage(zoneData));
    if (!window.$openImg) return false;
+
    };
  
     // Do nothing if the user clicked to open a map, then moused out of an area
+
     var build$LightboxFramedMap = function(zoneData) {
    // (otherwise it can close immediately)
+
      var $map = build$WrappedMap(zoneData);
    var position = window.$openImg.css('position');
+
      return addFramingStyles($map, zoneData.width, zoneData.height)
    if (e && e.type === 'mouseleave' && position === 'fixed') return false;
+
    };
  
     window.$openImg.remove();
+
     var addFramingStyles = function($el, width, height) {
    return false;
+
      return $el.css({
  }
+
          left: '50%',
 +
          marginLeft: '-' + (width / 2) + 'px', // centering
 +
          marginTop: '-' + (height / 2) + 'px',
 +
          opacity: 1,
 +
          top: '50%'
 +
        });
 +
    }
  
 +
    var build$MapFrame = function() {
 +
      return $('<div class="map-wrapper"></div>')
 +
              .css({ position: 'absolute' });
 +
    };
  
  var build$FullScreenFrame = function() {
+
    var removeOpenMap = function(e) {
    return build$MapFrame()
+
       // Do nothing if there's no open map
       .css({
+
       if (!window.$openImg) return false;
        background: 'rgba(0,0,0,0.8)',
+
        height: '100%',
+
        left: 0,
+
        position: 'fixed',
+
        top: 0,
+
        width: '100%',
+
        zIndex: 4 // one higher than #p-search's 3
+
      })
+
       .click(removeOpenMap);
+
  }
+
  
  var build$FullMap = function(zoneData) {
+
      // Do nothing if the user clicked to open a map, then moused out of an area
    var $frame = build$FullScreenFrame();
+
      // (otherwise it can close immediately)
    if (zoneData) {
+
      var position = window.$openImg.css('position');
       $frame.html(build$LightboxFramedMap(zoneData));
+
      if (e && e.type === 'mouseleave' && position === 'fixed') return false;
       $frame.zoneData = zoneData;
+
 
 +
       window.$openImg.remove();
 +
       return false;
 
     }
 
     }
    return $frame;
 
  };
 
  
  var build$SmallMap = function(zoneData) {
 
    return build$MapFrame().append(build$WrappedMap(zoneData));
 
  }
 
  
 +
    var build$FullScreenFrame = function() {
 +
      return build$MapFrame()
 +
        .css({
 +
          background: 'rgba(0,0,0,0.8)',
 +
          height: '100%',
 +
          left: 0,
 +
          position: 'fixed',
 +
          top: 0,
 +
          width: '100%',
 +
          zIndex: 4 // one higher than #p-search's 3
 +
        })
 +
        .click(removeOpenMap);
 +
    }
  
var buildX = function(left, top, sizeInEm) {
+
    var build$FullMap = function(zoneData) {
  sizeInEm = sizeInEm || 2.5;
+
      var $frame = build$FullScreenFrame();
  return $('<div class="x">x</div>')
+
      if (zoneData) {
    .css({
+
        $frame.html(build$LightboxFramedMap(zoneData));
      color: 'red',
+
        $frame.zoneData = zoneData;
       fontSize: sizeInEm + 'em',
+
       }
       fontWeight: 'bold',
+
       return $frame;
      left: left,
+
     };
      position: 'absolute',
+
      top: top
+
     })
+
}
+
  
/**
+
    var build$SmallMap = function(zoneData) {
* Draws a red "X" on the map at the provided coordinate
+
      return build$MapFrame().append(build$WrappedMap(zoneData));
*/
+
    }
var addX = function($xContainer, zoneData, x, y, xSize) {
+
  var left = (zoneData.zeroX || 0) + x * -1 * (zoneData.zoomX || 0.1);
+
  var top = (zoneData.zeroY || 0) + y * -1 * (zoneData.zoomY || 0.1);
+
  $xContainer.append(buildX(left, top, xSize));
+
}
+
  
var addXs = function($xContainer, zoneData, locs, xSize) {
 
  $.each(locs, function(i, loc) {
 
    addX($xContainer, zoneData, loc.x, loc.y, xSize);
 
  });
 
}
 
  
 
+
  var buildX = function(left, top, sizeInEm) {
var parseLoc = function(locText) {
+
    sizeInEm = sizeInEm || 2.5;
  if (typeof locText !== 'string') return locText;
+
    return $('<div class="x">x</div>')
 
+
      .css({
  var match = locText.match(/\(? *([\+\-]?\d+\.?\d*), *([\+\-]?\d+\.?\d*)\)?/);
+
        color: 'red',
  return {x: parseFloat(match[2]), y: parseFloat(match[1]) };
+
        fontSize: sizeInEm + 'em',
}
+
        fontWeight: 'bold',
 
+
        left: left,
var isLoc = function(locBit) {
+
        position: 'absolute',
    // If we can't split the string by its comma and find a number on either side, it's not a loc
+
        top: top
    try {
+
       })
      return locBit.split(',')[0].match(/\d+/) && locBit.split(',')[1].match(/\d+/);
+
    } catch (err) {
+
       return false;
+
    }
+
 
   }
 
   }
  
 
   /**
 
   /**
   * Helper function to power parseLocs/extractNonLocText, since they both use
+
   * Draws a red "X" on the map at the provided coordinate
  * the same logic.
+
  *
+
  * SIDE NOTE: If only the wiki didn't have to use 1999 JS, and could
+
  *            destructure return values with ES2015, the two related functions
+
  *            wouldn't even need to exist :(
+
 
   */
 
   */
   var parseLocString = function(locString) {
+
   var addX = function($xContainer, zoneData, x, y, xSize) {
     var nonLocParts = '';
+
     var left = (zoneData.zeroX || 0) + x * -1 * (zoneData.zoomX || 0.1);
     var bits = locString.split(/([\+\-]?\d+\.?\d*\D*,\D*[\+\-]?\d+\.?\d*)/g);
+
     var top = (zoneData.zeroY || 0) + y * -1 * (zoneData.zoomY || 0.1);
     var relevantBits = bits.filter(function(part) {
+
     $xContainer.append(buildX(left, top, xSize));
       // Filter out the non-loc parts, but save them
+
  }
      if (isLoc(part)) return true;
+
 
      nonLocParts += part; // save as it might be something like "1st floor"
+
  var addXs = function($xContainer, zoneData, locs, xSize) {
 +
    $.each(locs, function(i, loc) {
 +
       addX($xContainer, zoneData, loc.x, loc.y, xSize);
 
     });
 
     });
    var locs = relevantBits.map(parseLoc);
+
   }
    return [locs, nonLocParts];
+
   };
+
  
  /**
 
  * Extracts all of the locs (objects with x and y properties) from a provided
 
  * string such as:
 
  *  "500, 200"
 
  *  "(200, 300), (300, 200)"
 
  * and returns an array containing any locs found.
 
  */
 
  var parseLocs = function(locString) {
 
    return parseLocString(locString)[0];
 
  };
 
 
 
  /**
 
  * Extracts the non-loc parts (eg. "Level 1") from a provided string of text
 
  * (presumably from an NPC's location <td> or somewhere similar). Although not
 
  * actually a part of the loc, this text may still be useful in determining
 
  * which map to show for the loc.
 
  */
 
  var extractNonLocText = function(locString) {
 
    return parseLocString(locString)[1];
 
  };
 
  
 +
  var parseLoc = function(locText) {
 +
    if (typeof locText !== 'string') return locText;
  
  var showImage = function($img, $container) {
+
    var match = locText.match(/\(? *([\+\-]?\d+\.?\d*), *([\+\-]?\d+\.?\d*)\)?/);
     if ($container) {
+
     return {x: parseFloat(match[2]), y: parseFloat(match[1]) };
      $img.css({ marginTop: 40 });
+
  }
      var isAboveHalf = $container.position().top < ($('body').height() / 2);
+
 
       var isLeftOfHalf = $container.position().left < ($('body').width() / 2);
+
  var isLoc = function(locBit) {
      $container.css('position', 'relative')
+
      // If we can't split the string by its comma and find a number on either side, it's not a loc
                .prepend($img);
+
       try {
       // TODO: Do the same thing with left/right
+
        return locBit.split(',')[0].match(/\d+/) && locBit.split(',')[1].match(/\d+/);
      $img.css({  
+
       } catch (err) {
         [isAboveHalf  ? 'top' : 'bottom']: '1.5em',
+
         return false;
        [isLeftOfHalf  ? 'left' : 'right']: '1.5em'
+
       }
       })
+
 
     }
 
     }
    else $('body').append($img);
 
  
     window.$openImg = $img;
+
     /**
    return $img;
+
    * Helper function to power parseLocs/extractNonLocText, since they both use
  };
+
    * the same logic.
 +
    *
 +
    * SIDE NOTE: If only the wiki didn't have to use 1999 JS, and could
 +
    *            destructure return values with ES2015, the two related functions
 +
    *            wouldn't even need to exist :(
 +
    */
 +
    var parseLocString = function(locString) {
 +
      var nonLocParts = '';
 +
      var bits = locString.split(/([\+\-]?\d+\.?\d*\D*,\D*[\+\-]?\d+\.?\d*)/g);
 +
      var relevantBits = bits.filter(function(part) {
 +
        // Filter out the non-loc parts, but save them
 +
        if (isLoc(part)) return true;
 +
        nonLocParts += part; // save as it might be something like "1st floor"
 +
      });
 +
      var locs = relevantBits.map(parseLoc);
 +
      return [locs, nonLocParts];
 +
    };
  
  /**
+
    /**
  * Adds a loc map image to the page.
+
    * Extracts all of the locs (objects with x and y properties) from a provided
  */
+
    * string such as:
  var showMapWithLocs = function(zoneData, locs, $container) {
+
    *  "500, 200"
    removeOpenMap();
+
    *  "(200, 300), (300, 200)"
     zoneData = addImageUrl(zoneData);
+
    * and returns an array containing any locs found.
     var $map = $container ? build$SmallMap(zoneData) : build$FullMap(zoneData);
+
    */
 +
    var parseLocs = function(locString) {
 +
      return parseLocString(locString)[0];
 +
     };
 +
   
 +
    /**
 +
    * Extracts the non-loc parts (eg. "Level 1") from a provided string of text
 +
    * (presumably from an NPC's location <td> or somewhere similar). Although not
 +
    * actually a part of the loc, this text may still be useful in determining
 +
    * which map to show for the loc.
 +
    */
 +
     var extractNonLocText = function(locString) {
 +
      return parseLocString(locString)[1];
 +
    };
  
    var $xContainer = $map.find('.x-container');
 
    $map.find('img').load(function() {
 
      addXs($xContainer, zoneData, locs);
 
    });
 
    return showImage($map, $container);
 
  };
 
  
  // Handle "Where to obtain" rows for spells
+
    var showImage = function($img, $container) {
  var handleWhereToObtainRows = function() {
+
      if ($container) {
    $('table:has(th:contains("Area"))' +
+
        $img.css({ marginTop: 40 });
      ':has(th:contains("Location"), th:contains("Loc"))' +
+
        var isAboveHalf = $container.position().top < ($('body').height() / 2);
      ':has(th:contains("Zone"))')
+
         var isLeftOfHalf = $container.position().left < ($('body').width() / 2);
      .each(function(i, table) {
+
         $container.css('position', 'relative')
        var $table = $(table);
+
                  .prepend($img);
         var areaColumnIndex = $table.find('th:contains("Area")').index();
+
         // TODO: Do the same thing with left/right
         var locationColumnIndex =
+
        $img.css({
          $table.find('th:contains("Location"), th:contains("Loc")').index();
+
          [isAboveHalf  ? 'top' : 'bottom']: '1.5em',
         var zoneColumnIndex = $table.find('th:contains("Zone")').index();
+
          [isLeftOfHalf  ? 'left' : 'right']: '1.5em'
 +
        })
 +
      }
 +
      else $('body').append($img);
  
        $table.find('tr').each(function(i, tr) {
+
      window.$openImg = $img;
          var $tr = $(tr);
+
      return $img;
          var $loc = $tr.find('td:eq(' + locationColumnIndex + ')');
+
    };
          var loc = $loc.text().trim();
+
 
          // Include the area in case it contains relevant non-loc location info
+
    /**
          // (such as a floor number)
+
    * Adds a loc map image to the page.
          var nonLoc = $tr.find('td:eq(' + areaColumnIndex + ')').text().trim();
+
    */
          var zone = $tr.find('td:eq(' + zoneColumnIndex + ')').text().trim();
+
    var showMapWithLocs = function(zoneData, locs, $container) {
          var $locLink = $('<a href="' + zone + '" ' +
+
      removeOpenMap();
                          '  data-loc="' + nonLoc + ' ' + loc + '" ' +
+
      zoneData = addImageUrl(zoneData);
                          '  data-zone="' + zone + '"' +
+
      var $map = $container ? build$SmallMap(zoneData) : build$FullMap(zoneData);
                          '>' + loc + '</a>');
+
 
          $loc.html($locLink);
+
      var $xContainer = $map.find('.x-container');
        });
+
      $map.find('img').load(function() {
 +
        addXs($xContainer, zoneData, locs);
 
       });
 
       });
  };
+
      return showImage($map, $container);
 +
    };
  
  var getZoneName = function() {
+
    // Handle "Where to obtain" rows for spells
    try {
+
    var handleWhereToObtainRows = function() {
       return $('b:contains("Zone:")').parent().text().split('Zone:')[1].trim();
+
       $('table:has(th:contains("Area"))' +
    } catch (err) {
+
        ':has(th:contains("Location"), th:contains("Loc"))' +
      return null;
+
        ':has(th:contains("Zone"))')
 +
        .each(function(i, table) {
 +
          var $table = $(table);
 +
          var areaColumnIndex = $table.find('th:contains("Area")').index();
 +
          var locationColumnIndex =
 +
            $table.find('th:contains("Location"), th:contains("Loc")').index();
 +
          var zoneColumnIndex = $table.find('th:contains("Zone")').index();
 +
 
 +
          $table.find('tr').each(function(i, tr) {
 +
            var $tr = $(tr);
 +
            var $loc = $tr.find('td:eq(' + locationColumnIndex + ')');
 +
            var loc = $loc.text().trim();
 +
            // Include the area in case it contains relevant non-loc location info
 +
            // (such as a floor number)
 +
            var nonLoc = $tr.find('td:eq(' + areaColumnIndex + ')').text().trim();
 +
            var zone = $tr.find('td:eq(' + zoneColumnIndex + ')').text().trim();
 +
            var $locLink = $('<a href="' + zone + '" ' +
 +
                            '  data-loc="' + nonLoc + ' ' + loc + '" ' +
 +
                            '  data-zone="' + zone + '"' +
 +
                            '>' + loc + '</a>');
 +
            $loc.html($locLink);
 +
          });
 +
        });
 
     };
 
     };
  };
 
  
  /**
+
    var getZoneName = function() {
  * Determines whether the page has a "Location" <td> (as all NPC pages do)
+
      try {
  * with parsable locs inside it (as only some do).  Also checks that
+
        return $('b:contains("Zone:")').parent().text().split('Zone:')[1].trim();
  * "loc-mapped" zone data exists for the NPC's zone.
+
      } catch (err) {
  */
+
        return null;
  var getZoneDataIfThereIsALocBox = function($locTd, zoneName) {
+
      };
    if (!$locTd.length) return false; // page isn't an NPC page (no loc box)
+
    };
   
+
    var locs = parseLocs($locTd.text());
+
    if (!locs.length) return false; // no locs existed (or could be parsed)
+
  
     var nonLocParts = extractNonLocText($locTd.text());
+
    /**
    return !!getZoneData(zoneName, locs, nonLocParts); // does zone data exist?
+
    * Determines whether the page has a "Location" <td> (as all NPC pages do)
  };
+
    * with parsable locs inside it (as only some do).  Also checks that
 +
    * "loc-mapped" zone data exists for the NPC's zone.
 +
    */
 +
     var getZoneDataIfThereIsALocBox = function($locTd, zoneName) {
 +
      if (!$locTd.length) return false; // page isn't an NPC page (no loc box)
 +
     
 +
      var locs = parseLocs($locTd.text());
 +
      if (!locs.length) return false; // no locs existed (or could be parsed)
  
  var handleLocBoxes = function() {
+
      var nonLocParts = extractNonLocText($locTd.text());
    var zoneName = getZoneName();
+
      return !!getZoneData(zoneName, locs, nonLocParts); // does zone data exist?
    var $locTd = $('b:contains("Location:")').parent();
+
     };
    var zoneData = getZoneDataIfThereIsALocBox($locTd, zoneName);
+
    if (!zoneData) return;
+
   
+
    // Get the mob's loc(s)
+
    var locs = parseLocs($locTd.text());
+
    var nonLocParts = extractNonLocText($locTd.text());
+
   
+
    // Do we have data for that zone's map?
+
    var zoneData = getZoneData(zoneName, locs, nonLocParts);
+
     if (!zoneData) return; // If not, stop here
+
  
     // Add the mouse-enter link
+
     var handleLocBoxes = function() {
    var $link = $(' <a href="#">(Map)</a>')
+
      var zoneName = getZoneName();
       // When it's moused-over, show the map nearby
+
      var $locTd = $('b:contains("Location:")').parent();
       .on('mouseenter', function(e) {
+
      var zoneData = getZoneDataIfThereIsALocBox($locTd, zoneName);
        // If there is already a map "open" (because of a click), do nothing
+
      if (!zoneData) return;
        if (window.$openImg && window.$openImg.parent().length) return false;
+
     
 +
       // Get the mob's loc(s)
 +
       var locs = parseLocs($locTd.text());
 +
      var nonLocParts = extractNonLocText($locTd.text());
 +
     
 +
      // Do we have data for that zone's map?
 +
      var zoneData = getZoneData(zoneName, locs, nonLocParts);
 +
      if (!zoneData) return; // If not, stop here
  
        var $map = showMapWithLocs(zoneData, locs, $locTd);
+
      // Add the mouse-enter link
        $map.parent().one('mouseleave', removeOpenMap);
+
      var $link = $(' <a href="#">(Map)</a>')
      })
+
        // When it's moused-over, show the map nearby
      // When it's clicked, show the map full-screen/lightbox style
+
        .on('mouseenter', function(e) {
      .on('click', function(e) {
+
          // If there is already a map "open" (because of a click), do nothing
        showMapWithLocs(zoneData, locs);
+
          if (window.$openImg && window.$openImg.parent().length) return false;
      });
+
  
    // $locTd.find('b').html($('<span>Location </span>').append($link).append('<span>:</span>'));
+
          var $map = showMapWithLocs(zoneData, locs, $locTd);
    $locTd
+
          $map.parent().one('mouseleave', removeOpenMap);
      .find('b')
+
        })
      .html($('<span>Location </span>')
+
        // When it's clicked, show the map full-screen/lightbox style
      .append($link)
+
        .on('click', function(e) {
      .append('<span>:</span>'));
+
          showMapWithLocs(zoneData, locs);
  };
+
        });
  
  // Setup event listeners (code "starts" here)
+
      // $locTd.find('b').html($('<span>Location </span>').append($link).append('<span>:</span>'));
  var startLocFunctionality = function() {
+
      $locTd
    handleWhereToObtainRows();
+
        .find('b')
    handleLocBoxes();
+
        .html($('<span>Location </span>')
  };
+
        .append($link)
 +
        .append('<span>:</span>'));
 +
    };
  
  // Sometimes zoneData isn't ready when this code is, so retry for 10 secs in hope that it becomes available
+
    // Setup event listeners (code "starts" here)
  var tries = 10;
+
    var startLocFunctionality = function() {
  var timeBetweenTries = 1000; // 1 second
+
      handleWhereToObtainRows();
  var interval = window.setInterval(function() {
+
       handleLocBoxes();
    if (window.zoneData) {
+
     };
      window.clearInterval(interval);
+
      startLocFunctionality();
+
    } else {
+
       tries -= 1;
+
      if (!tries) window.clearInterval(interval); // Failure :(
+
     }
+
  }, timeBetweenTries);
+
  
  var alertNoData = function(zone) {
+
    // Sometimes zoneData isn't ready when this code is, so retry for 10 secs in hope that it becomes available
    alert('We\'re sorry, but ' + zone + ' has not been loc mapped yet ... ' +
+
    var tries = 10;
          'please see "/Loc_Maps" for more information.');  
+
    var timeBetweenTries = 1000; // 1 second
  }
+
    var interval = window.setInterval(function() {
 
+
      if (window.zoneData) {
  var showMapForData = function(data, $target) {
+
        window.clearInterval(interval);
    if (!data.loc || ! data.zone) return;
+
        startLocFunctionality();
 +
      } else {
 +
        tries -= 1;
 +
        if (!tries) window.clearInterval(interval); // Failure :(
 +
      }
 +
    }, timeBetweenTries);
  
     var locs = [parseLoc(data.loc)];
+
     var alertNoData = function(zone) {
    var zoneData = getZoneData(data.zone, locs);
+
       alert('We\'re sorry, but ' + zone + ' has not been loc mapped yet ... ' +
    if (!zoneData) {
+
            'please see "/Loc_Maps" for more information.');  
       // If it was a click and not a mouseover
+
      if (!$target) alertNoData(data.zone);
+
      return;
+
 
     }
 
     }
    showMapWithLocs(zoneData, locs, $target);
 
  };
 
  
  // TODO: this is in here because fashion links and loc links share global
+
    var showMapForData = function(data, $target) {
  // event watchers, but this file needs to be renamed now
+
      if (!data.loc || ! data.zone) return;
  var showFashionForData = function(data) {
+
    if (!data.file) return;
+
    var $img = addFramingStyles($('<img src="/images/' + data.file + '" />'));
+
    var $div = $('<div style="position: absolute; left: 50%; top: 50%"></div>')
+
                  .html($img)
+
    var $framedImage =  build$FullScreenFrame().html($div)
+
    showImage($framedImage);
+
  };
+
  
  // Hook up loc-link events
+
      var locs = [parseLoc(data.loc)];
  $('body')
+
       var zoneData = getZoneData(data.zone, locs);
    .on('click', '.loc-link, .fashion-link', function() {
+
       if (!zoneData) {
       var data = $(this).data();
+
        // If it was a click and not a mouseover
       showMapForData(data);
+
        if (!$target) alertNoData(data.zone);
      showFashionForData(data);
+
        return;
      return false;
+
      }
    })
+
      showMapWithLocs(zoneData, locs, $target);
    .on('mouseenter', '.loc-link', function(e) {
+
     };
      var $this = $(e.target).on('mouseleave', removeOpenMap);
+
      showMapForData($(this).data(), $this);
+
     });
+
  
      
+
     // TODO: this is in here because fashion links and loc links share global
  // *** HELPER FUNCTIONS ***
+
    // event watchers, but this file needs to be renamed now
  // Define two helper functions for building new zone definitions
+
    var showFashionForData = function(data) {
 +
      if (!data.file) return;
 +
      var $img = $('<img src="/images/' + data.file + '"">');
 +
      var $div = $('<div class="framed-image"></div>')
 +
                    .html($img)
 +
      var $framedImage =  build$FullScreenFrame().html($div)
 +
      showImage($framedImage);
 +
    };
  
  // 1) Use this function to find the correct 0,0 point
+
    // Hook up loc-link events
  window.testZero = window.testZeroZero = function(zoneData) {
+
    $('body')
    removeOpenMap(); // test functions don't clean up properly
+
      .on('click', '.loc-link, .fashion-link', function() {
    var locs = [{ x: 0, y: 0 }];
+
        var data = $(this).data();
    showMapWithLocs(zoneData, locs);
+
        showMapForData(data);
  };
+
        showFashionForData(data);
 +
        return false;
 +
      })
 +
      .on('mouseenter', '.loc-link', function(e) {
 +
        var $this = $(e.target).on('mouseleave', removeOpenMap);
 +
        showMapForData($(this).data(), $this);
 +
      });
  
 +
     
 +
    // *** HELPER FUNCTIONS ***
 +
    // Define two helper functions for building new zone definitions
  
  // 2) Use this function to generate a grid of alignment of X's
+
    // 1) Use this function to find the correct 0,0 point
  window.testGrid = function(zoneData) {
+
    window.testZero = window.testZeroZero = function(zoneData) {
    removeOpenMap(); // test functions don't clean up properly
+
      removeOpenMap(); // test functions don't clean up properly
    var $locTd = $('b:contains("Location:")').parent();
+
      var locs = [{ x: 0, y: 0 }];
    var locs = [];
+
       showMapWithLocs(zoneData, locs);
    for (var x = zoneData.maxX; x >= zoneData.minX; x -= zoneData.interval) {
+
      for (var y = zoneData.maxY; y >= zoneData.minY; y -= zoneData.interval) {
+
        locs.push({ x: x, y: y });
+
       }
+
 
     };
 
     };
    showMapWithLocs(zoneData, locs);
 
  };
 
  
})();
+
 
} catch (err) {
+
    // 2) Use this function to generate a grid of alignment of X's
  console.error(err); // If anything goes wrong, move on but log the error
+
    window.testGrid = function(zoneData) {
}
+
      removeOpenMap(); // test functions don't clean up properly
 +
      var $locTd = $('b:contains("Location:")').parent();
 +
      var locs = [];
 +
      for (var x = zoneData.maxX; x >= zoneData.minX; x -= zoneData.interval) {
 +
        for (var y = zoneData.maxY; y >= zoneData.minY; y -= zoneData.interval) {
 +
          locs.push({ x: x, y: y });
 +
        }
 +
      };
 +
      showMapWithLocs(zoneData, locs);
 +
    };
 +
 
 +
  })();
 +
  } catch (err) {
 +
    console.error(err); // If anything goes wrong, move on but log the error
 +
  }

Revision as of 05:33, 14 September 2019

  try {
  (function() {

    // TODO: Add support for cropping maps that have multiple maps.
    // For instance, the following CSS styles show only one of the three maps for 
    // Erudin Palace:
    // background-image:url(/images/Erudinpalace.jpg); 
    // background-position: 250px 0px; width: 275px; height: 272px
    // Figure out how to make the Xs line up with those styles added

    var locIsWithinAlternateData = function(loc, alternateData) {
      return loc.x < alternateData.maxX &&
      loc.x > alternateData.minX &&
      loc.y < alternateData.maxY &&
      loc.y > alternateData.minY;
    };


    /**
     * Determines if the provided name contains any of the provided words
     */
    // TODO: Add a some polyfill to make this a lot simpler
    var containsAny = function(name/* word1, word2, etc. */) {
      for(var arg = 1; arg < arguments.length; ++ arg) {
        var word = arguments[arg];
        if (name.includes(word)) return true;
      }
      return false;
    };

    var containsAnyNumber = function(name, number, suffix) {
      return containsAny.call(null, name, 
       number + suffix,
       'level ' + number,
       'level: ' + number
       );
    };

    var getZoneLevelData = function(levels, text) {
      // Check for part "level" aliases (eg. "1st floor" vs. "Level One")
      text = text.toLowerCase();
      if (containsAnyNumber(text, 0, 'th') || containsAny(text, 'basement', 'underground')) return levels[0];
      if (containsAnyNumber(text, 1, 'st') || containsAny(text, 'one', 'first', 'ground')) return levels[1];
      if (containsAnyNumber(text, 2, 'nd') || containsAny(text, 'two', 'second')) return levels[2];
      if (containsAnyNumber(text, 3, 'rd') || containsAny(text, 'three', 'third')) return levels[3];
      if (containsAnyNumber(text, 4, 'th') || containsAny(text, 'four', 'fourth')) return levels[4];
      if (containsAnyNumber(text, 5, 'th') || containsAny(text, 'five', 'fifth')) return levels[5];
      if (containsAnyNumber(text, 6, 'th') || containsAny(text, 'six', 'sixth')) return levels[6];
      if (containsAnyNumber(text, 7, 'th') || containsAny(text, 'seven', 'seventh')) return levels[7];
      if (containsAnyNumber(text, 8, 'th') || containsAny(text, 'eight', 'eighth')) return levels[8];
      if (containsAnyNumber(text, 9, 'th') || containsAny(text, 'nine', 'ninth')) return levels[9];

      var levelKeys = Object.keys(levels);
      var keyOfLastLevel = levelKeys[levelKeys.length - 1];
      if (containsAny(text, 'top')) return levels[keyOfLastLevel];

      // If we still couldn't match, and there is a ground/1st floor, use it
      return levels[1];
    };

    

    var findZoneData = function(zoneName, locs, nonLocParts) {
      var zone = zoneData[zoneName];

    // Handle aliases (eg. "North Ro" instead of "Northern Desert of Ro")
    if (typeof(zone) === 'string') zone = zoneData[zone];

    if (!zone) return null;

    // Handle Multi-Level Zones (with different maps for 1st, 2nd, etc. floor)
    if (zone.levels) return getZoneLevelData(zone.levels, nonLocParts);

    // Handle Zones with alternate maps on the same level (eg. Kelethin in GFay)
    if (!zone.alternateMaps) return zone;

    for (var i = 0; i < zone.alternateMaps.length; i++) {
      var alternateData = zone.alternateMaps[i];
      var allLocsAreWithin = true;
      // wish I had ES6 [].every
      $.each(locs, function(i, loc) {
        allLocsAreWithin = allLocsAreWithin && 
        locIsWithinAlternateData(loc, alternateData);
      });
      if (allLocsAreWithin) return alternateData;
    };
    return zone;
  };

  var addImageUrl = function(zoneData) {
    zoneData.imageUrl = '/images/' + zoneData.image;
    return zoneData;
  };

  var getZoneData = function(zoneName, locs, nonLocParts) {
    var zoneData = findZoneData(zoneName, locs, nonLocParts);
    if (!zoneData) return null;
    zoneData.zoneName = zoneName;
    return addImageUrl(zoneData);
  };

    var build$MapImage = function(zoneData) {
      return $(
        '<img alt="Map of ' + zoneData.zoneName + '" ' +
        '     class="thumbborder" ' +
        '     height="'+ zoneData.height + '" ' +
        '     src="' + zoneData.imageUrl + '" ' +
        '     title="Map of ' + zoneData.zoneName + '"' +
        '     width="' + zoneData.width + '" ' +
        '>'
        );
    };``

    var build$XContainer = function() {
      return $('<div class="x-container"></div>')
        .css({ position: 'relative' });
    }

    var build$WrappedMap = function(zoneData) {
      return build$XContainer().append(build$MapImage(zoneData));
    };

    var build$LightboxFramedMap = function(zoneData) {
      var $map = build$WrappedMap(zoneData);
      return addFramingStyles($map, zoneData.width, zoneData.height)
    };

    var addFramingStyles = function($el, width, height) {
      return $el.css({
          left: '50%',
          marginLeft: '-' + (width / 2) + 'px', // centering
          marginTop: '-' + (height / 2) + 'px',
          opacity: 1,
          top: '50%'
        });
    }

    var build$MapFrame = function() {
      return $('<div class="map-wrapper"></div>')
              .css({ position: 'absolute' });
    };

    var removeOpenMap = function(e) {
      // Do nothing if there's no open map
      if (!window.$openImg) return false;

      // Do nothing if the user clicked to open a map, then moused out of an area
      // (otherwise it can close immediately)
      var position = window.$openImg.css('position');
      if (e && e.type === 'mouseleave' && position === 'fixed') return false;

      window.$openImg.remove();
      return false;
    }


    var build$FullScreenFrame = function() {
      return build$MapFrame()
        .css({
          background: 'rgba(0,0,0,0.8)',
          height: '100%',
          left: 0,
          position: 'fixed',
          top: 0,
          width: '100%',
          zIndex: 4 // one higher than #p-search's 3
        })
        .click(removeOpenMap);
    }

    var build$FullMap = function(zoneData) {
      var $frame = build$FullScreenFrame();
      if (zoneData) {
        $frame.html(build$LightboxFramedMap(zoneData));
        $frame.zoneData = zoneData;
      }
      return $frame;
    };

    var build$SmallMap = function(zoneData) {
      return build$MapFrame().append(build$WrappedMap(zoneData));
    }


  var buildX = function(left, top, sizeInEm) {
    sizeInEm = sizeInEm || 2.5;
    return $('<div class="x">x</div>')
      .css({
        color: 'red',
        fontSize: sizeInEm + 'em',
        fontWeight: 'bold',
        left: left,
        position: 'absolute',
        top: top
      })
  }

  /**
   * Draws a red "X" on the map at the provided coordinate
   */
  var addX = function($xContainer, zoneData, x, y, xSize) {
    var left = (zoneData.zeroX || 0) + x * -1 * (zoneData.zoomX || 0.1);
    var top = (zoneData.zeroY || 0) + y * -1 * (zoneData.zoomY || 0.1);
    $xContainer.append(buildX(left, top, xSize));
  }

  var addXs = function($xContainer, zoneData, locs, xSize) {
    $.each(locs, function(i, loc) {
      addX($xContainer, zoneData, loc.x, loc.y, xSize);
    });
  }


  var parseLoc = function(locText) {
    if (typeof locText !== 'string') return locText;

    var match = locText.match(/\(? *([\+\-]?\d+\.?\d*), *([\+\-]?\d+\.?\d*)\)?/);
    return {x: parseFloat(match[2]), y: parseFloat(match[1]) };
  }

  var isLoc = function(locBit) {
      // If we can't split the string by its comma and find a number on either side, it's not a loc
      try {
        return locBit.split(',')[0].match(/\d+/) && locBit.split(',')[1].match(/\d+/);
      } catch (err) {
        return false;
      }
    }

    /**
     * Helper function to power parseLocs/extractNonLocText, since they both use
     * the same logic.
     *
     * SIDE NOTE: If only the wiki didn't have to use 1999 JS, and could
     *            destructure return values with ES2015, the two related functions
     *            wouldn't even need to exist :(
     */
    var parseLocString = function(locString) {
      var nonLocParts = '';
      var bits = locString.split(/([\+\-]?\d+\.?\d*\D*,\D*[\+\-]?\d+\.?\d*)/g);
      var relevantBits = bits.filter(function(part) {
        // Filter out the non-loc parts, but save them
        if (isLoc(part)) return true;
        nonLocParts += part; // save as it might be something like "1st floor"
      });
      var locs = relevantBits.map(parseLoc);
      return [locs, nonLocParts];
    };

    /**
     * Extracts all of the locs (objects with x and y properties) from a provided
     * string such as:
     *   "500, 200"
     *   "(200, 300), (300, 200)"
     * and returns an array containing any locs found.
     */
     var parseLocs = function(locString) {
      return parseLocString(locString)[0];
    };
    
    /**
     * Extracts the non-loc parts (eg. "Level 1") from a provided string of text
     * (presumably from an NPC's location <td> or somewhere similar). Although not
     * actually a part of the loc, this text may still be useful in determining
     * which map to show for the loc.
     */
    var extractNonLocText = function(locString) {
      return parseLocString(locString)[1];
    };


    var showImage = function($img, $container) {
      if ($container) {
        $img.css({ marginTop: 40 });
        var isAboveHalf = $container.position().top < ($('body').height() / 2);
        var isLeftOfHalf = $container.position().left < ($('body').width() / 2);
        $container.css('position', 'relative')
                  .prepend($img);
        // TODO: Do the same thing with left/right
        $img.css({ 
          [isAboveHalf  ? 'top' : 'bottom']: '1.5em', 
          [isLeftOfHalf  ? 'left' : 'right']: '1.5em' 
        })
      }
      else $('body').append($img);

      window.$openImg = $img;
      return $img;
    };

    /**
     * Adds a loc map image to the page.
     */
    var showMapWithLocs = function(zoneData, locs, $container) {
      removeOpenMap();
      zoneData = addImageUrl(zoneData);
      var $map = $container ? build$SmallMap(zoneData) : build$FullMap(zoneData);

      var $xContainer = $map.find('.x-container');
      $map.find('img').load(function() {
        addXs($xContainer, zoneData, locs);
      });
      return showImage($map, $container);
    };

    // Handle "Where to obtain" rows for spells
    var handleWhereToObtainRows = function() {
      $('table:has(th:contains("Area"))' +
        ':has(th:contains("Location"), th:contains("Loc"))' +
        ':has(th:contains("Zone"))')
        .each(function(i, table) {
          var $table = $(table);
          var areaColumnIndex = $table.find('th:contains("Area")').index();
          var locationColumnIndex =
            $table.find('th:contains("Location"), th:contains("Loc")').index();
          var zoneColumnIndex = $table.find('th:contains("Zone")').index();

          $table.find('tr').each(function(i, tr) {
            var $tr = $(tr);
            var $loc = $tr.find('td:eq(' + locationColumnIndex + ')');
            var loc = $loc.text().trim();
            // Include the area in case it contains relevant non-loc location info
            // (such as a floor number)
            var nonLoc = $tr.find('td:eq(' + areaColumnIndex + ')').text().trim();
            var zone = $tr.find('td:eq(' + zoneColumnIndex + ')').text().trim();
            var $locLink = $('<a href="' + zone + '" ' +
                             '   data-loc="' + nonLoc + ' ' + loc + '" ' +
                             '   data-zone="' + zone + '"' +
                             '>' + loc + '</a>');
            $loc.html($locLink);
          });
        });
    };

    var getZoneName = function() {
      try {
        return $('b:contains("Zone:")').parent().text().split('Zone:')[1].trim();
      } catch (err) {
        return null;
      };
    };

    /**
     * Determines whether the page has a "Location" <td> (as all NPC pages do)
     * with parsable locs inside it (as only some do).  Also checks that
     * "loc-mapped" zone data exists for the NPC's zone.
     */
    var getZoneDataIfThereIsALocBox = function($locTd, zoneName) {
      if (!$locTd.length) return false; // page isn't an NPC page (no loc box)
      
      var locs = parseLocs($locTd.text());
      if (!locs.length) return false; // no locs existed (or could be parsed)

      var nonLocParts = extractNonLocText($locTd.text());
      return !!getZoneData(zoneName, locs, nonLocParts); // does zone data exist?
    };

    var handleLocBoxes = function() {
      var zoneName = getZoneName();
      var $locTd = $('b:contains("Location:")').parent();
      var zoneData = getZoneDataIfThereIsALocBox($locTd, zoneName);
      if (!zoneData) return;
      
      // Get the mob's loc(s)
      var locs = parseLocs($locTd.text());
      var nonLocParts = extractNonLocText($locTd.text());
      
      // Do we have data for that zone's map?
      var zoneData = getZoneData(zoneName, locs, nonLocParts);
      if (!zoneData) return;  // If not, stop here

      // Add the mouse-enter link
      var $link = $(' <a href="#">(Map)</a>')
        // When it's moused-over, show the map nearby
        .on('mouseenter', function(e) {
          // If there is already a map "open" (because of a click), do nothing
          if (window.$openImg && window.$openImg.parent().length) return false;

          var $map = showMapWithLocs(zoneData, locs, $locTd);
          $map.parent().one('mouseleave', removeOpenMap);
        })
        // When it's clicked, show the map full-screen/lightbox style
        .on('click', function(e) {
          showMapWithLocs(zoneData, locs);
        });

      // $locTd.find('b').html($('<span>Location </span>').append($link).append('<span>:</span>'));
      $locTd
        .find('b')
        .html($('<span>Location </span>')
        .append($link)
        .append('<span>:</span>'));
    };

    // Setup event listeners (code "starts" here)
    var startLocFunctionality = function() {
      handleWhereToObtainRows();
      handleLocBoxes();
    };

    // Sometimes zoneData isn't ready when this code is, so retry for 10 secs in hope that it becomes available
    var tries = 10;
    var timeBetweenTries = 1000; // 1 second
    var interval = window.setInterval(function() {
      if (window.zoneData) {
         window.clearInterval(interval);
         startLocFunctionality();
      } else {
        tries -= 1;
        if (!tries) window.clearInterval(interval); // Failure :(
      }
    }, timeBetweenTries);

    var alertNoData = function(zone) {
      alert('We\'re sorry, but ' + zone + ' has not been loc mapped yet ... ' +
            'please see "/Loc_Maps" for more information.'); 
    }

    var showMapForData = function(data, $target) {
      if (!data.loc || ! data.zone) return;

      var locs = [parseLoc(data.loc)];
      var zoneData = getZoneData(data.zone, locs);
      if (!zoneData) {
        // If it was a click and not a mouseover
        if (!$target) alertNoData(data.zone);
        return;
      }
      showMapWithLocs(zoneData, locs, $target);
    };

    // TODO: this is in here because fashion links and loc links share global
    // event watchers, but this file needs to be renamed now
    var showFashionForData = function(data) {
       if (!data.file) return;
       var $img = $('<img src="/images/' + data.file + '"">');
       var $div = $('<div class="framed-image"></div>')
                    .html($img)
       var $framedImage =  build$FullScreenFrame().html($div)
       showImage($framedImage);
    };

    // Hook up loc-link events
    $('body')
      .on('click', '.loc-link, .fashion-link', function() {
        var data = $(this).data();
        showMapForData(data);
        showFashionForData(data);
        return false;
      })
      .on('mouseenter', '.loc-link', function(e) {
        var $this = $(e.target).on('mouseleave', removeOpenMap);
        showMapForData($(this).data(), $this);
      });

      
    // *** HELPER FUNCTIONS ***
    // Define two helper functions for building new zone definitions

    // 1) Use this function to find the correct 0,0 point
    window.testZero = window.testZeroZero = function(zoneData) {
      removeOpenMap(); // test functions don't clean up properly
      var locs = [{ x: 0, y: 0 }];
      showMapWithLocs(zoneData, locs);
    };


    // 2) Use this function to generate a grid of alignment of X's
    window.testGrid = function(zoneData) {
      removeOpenMap(); // test functions don't clean up properly
      var $locTd = $('b:contains("Location:")').parent();
      var locs = [];
      for (var x = zoneData.maxX; x >= zoneData.minX; x -= zoneData.interval) {
        for (var y = zoneData.maxY; y >= zoneData.minY; y -= zoneData.interval) {
          locs.push({ x: x, y: y });
        }
      };
      showMapWithLocs(zoneData, locs);
    };

  })();
  } catch (err) {
    console.error(err); // If anything goes wrong, move on but log the error
  }