Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New arch 1st step: add a plugin name #124

Merged
merged 3 commits into from
Nov 20, 2015
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions js/jquery.mapael.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
}(function ($, Raphael, mousewheel) { // jshint ignore:line

"use strict";

var pluginName = "mapael";

var Mapael = function(options) {

Expand All @@ -41,11 +43,18 @@
});

return this.each(function() {

// Avoid multiple instanciation
if ($.data(this, pluginName)) throw new Error("Mapael already exists on this element.");

// Save instanciation on element
// This allow external access to Mapael using $(".mapcontainer").data("mapael")
$.data(this, pluginName, Mapael);

var $self = $(this)
, $tooltip = $("<div>").addClass(options.map.tooltip.cssClass).css("display", "none")
, $container = $("." + options.map.cssClass, this).empty().append($tooltip)
, mapConf = $.fn.mapael.maps[options.map.name]
, mapConf = $.fn[pluginName].maps[options.map.name]
, paper = new Raphael($container[0], mapConf.width, mapConf.height)
, elemOptions = {}
, resizeTO = 0
Expand All @@ -55,6 +64,9 @@
, zoomCenterX = 0
, zoomCenterY = 0
, previousPinchDist = 0;

// add plugin class name on element
$self.addClass(pluginName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of adding the plugin name as a class name on the element ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows the plugin to be deterministic:

  • declaring externally that mapael is assigned to this element
  • allow specific CSS customization aimed at mapael (without fearing name collision)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the second point, we will be able to define the CSS for mapael.
Example: the .tooltip class. There is high chance this name is already used on a website, or through a framework, with different parameters.
Hence, using .mapael .tooltip will allow specific selectivity.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

Thank for the explanation, it's ok for me. However, regarding the second argument, currently, nothing prevent the user from adding a custom class name on the container in order to allow specific selectivity in the CSS, this is why I was curious about the purpose of the class.


if (options.map.tooltip.css) $tooltip.css(options.map.tooltip.css);
paper.setViewBox(0, 0, mapConf.width, mapConf.height, false);
Expand Down Expand Up @@ -190,7 +202,7 @@

if (Math.abs(pinchDist - previousPinchDist) > 15) {
offset = $container.offset();
initFactor = (options.map.width) ? (Mapael.maps[options.map.name].width / options.map.width) : ($.fn.mapael.maps[options.map.name].width / $container.width());
initFactor = (options.map.width) ? (Mapael.maps[options.map.name].width / options.map.width) : ($.fn[pluginName].maps[options.map.name].width / $container.width());
zoomFactor = 1 / (1 + ($self.data("zoomLevel")) * options.map.zoom.step);
x = zoomFactor * initFactor * (zoomCenterX + $(window).scrollLeft() - offset.left) + $self.data("panX");
y = zoomFactor * initFactor * (zoomCenterY + $(window).scrollTop() - offset.top) + $self.data("panY");
Expand Down Expand Up @@ -1612,8 +1624,6 @@
};

// jQuery access
$.fn.mapael = Mapael;

return $.fn.mapael;
$.fn[pluginName] = Mapael;

}));