Skip to content

Commit cd709a8

Browse files
committed
v2.0.0 - publish a separate jquery distribution, fix #14
1 parent 57fdf6b commit cd709a8

18 files changed

+1146
-990
lines changed

README.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $ npm install image-map
2121
$ bower install image-map
2222
```
2323

24-
### CDN
24+
### [CDN](https://unpkg.com/browse/image-map/dist/)
2525
| Module (`.es.js`) | Main (`.js`) | Main (compressed) `.min.js` |
2626
| :-------------------------------------------------------: | :----------------------------------------------------: | :--------------------------------------------------------: |
2727
| [🔗](https://unpkg.com/image-map/dist/image-map.es.js) | [🔗](https://unpkg.com/image-map/dist/image-map.js) | [🔗](https://unpkg.com/image-map/dist/image-map.min.js) |
@@ -44,24 +44,54 @@ Add an **image-map** to your html page (either create one yourself or try the [o
4444
</map>
4545
```
4646

47-
### jQuery
48-
To use this plugin with jQuery, include (or import) both the [jQuery](https://jquery.com/) and `image-map.js` libraries, then simply call:
47+
### JavaScript
48+
49+
To use this plugin with *only* JavaScript, first include (or import) the `image-map.js` library:
50+
4951
```js
50-
$('img[usemap]').imageMap();
52+
// using `import`
53+
import ImageMap from "image-map";
5154
```
5255

53-
The default debounce rate is **500**ms. To customize this value, pass a numeric *wait* value as the first argument.
5456
```js
55-
$('img[usemap]').imageMap(500);
57+
// using `require`
58+
var ImageMap = require("image-map");
59+
```
60+
61+
```html
62+
<!-- using `script` -->
63+
<script src="https://unpkg.com/image-map/dist/image-map.js"></script>
5664
```
5765

58-
### JavaScript (Vanilla JS)
59-
To use this plugin with *only* JavaScript, include (or import) the `image-map.js` library, then simply call:
66+
Next, simply call the `ImageMap` constructor:
67+
6068
```js
6169
ImageMap('img[usemap]')
6270
```
6371

6472
The default debounce rate is **500**ms. To customize this value, pass a numeric *wait* value as the second argument.
73+
6574
```js
6675
ImageMap('img[usemap]', 500)
6776
```
77+
78+
### jQuery
79+
80+
To use this plugin with jQuery, first include both the [jQuery](https://jquery.com/) and `image-map.jquery.js` libraries:
81+
82+
```html
83+
<!-- using `script` -->
84+
<script src="https://unpkg.com/jquery/dist/jquery.js"></script>
85+
<script src="https://unpkg.com/image-map/dist/image-map.jquery.js"></script>
86+
```
87+
88+
Next, simply call the `imageMap` plugin:
89+
90+
```js
91+
$('img[usemap]').imageMap();
92+
```
93+
94+
The default debounce rate is **500**ms. To customize this value, pass a numeric *wait* value as the first argument.
95+
```js
96+
$('img[usemap]').imageMap(500);
97+
```

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "image-map",
3-
"version": "1.1.7",
3+
"version": "2.0.0",
44
"author": {
55
"name": "Travis Clarke",
66
"email": "[email protected]",

dist/image-map.es.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/**
2-
* Copyright (c) 2018, Travis Clarke (https://www.travismclarke.com/)
2+
* Copyright (c) 2020, Travis Clarke (https://www.travismclarke.com/)
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import $ from 'jquery';
9-
108
function _classCallCheck(instance, Constructor) {
119
if (!(instance instanceof Constructor)) {
1210
throw new TypeError("Cannot call a class as a function");
@@ -29,7 +27,7 @@ function _createClass(Constructor, protoProps, staticProps) {
2927
return Constructor;
3028
}
3129

32-
var version = "1.1.7";
30+
var version = "2.0.0";
3331

3432
var RESIZE = "resize";
3533
var LOAD = "load";
@@ -44,9 +42,7 @@ var COMPLETE = "complete";
4442
* @constructor
4543
*/
4644

47-
var ImageMap =
48-
/*#__PURE__*/
49-
function () {
45+
var ImageMap = /*#__PURE__*/function () {
5046
function ImageMap(selector, wait) {
5147
_classCallCheck(this, ImageMap);
5248

@@ -142,12 +138,6 @@ function () {
142138
return ImageMap;
143139
}();
144140

145-
if ($ !== undefined && $.fn) {
146-
$.fn.imageMap = function (wait) {
147-
return new ImageMap(this.toArray(), wait);
148-
};
149-
}
150-
151141
function _ImageMap(selector, wait) {
152142
return new ImageMap(selector, wait);
153143
}

dist/image-map.jquery.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* Copyright (c) 2020, Travis Clarke (https://www.travismclarke.com/)
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
(function ($) {
9+
'use strict';
10+
11+
$ = $ && Object.prototype.hasOwnProperty.call($, 'default') ? $['default'] : $;
12+
13+
function _classCallCheck(instance, Constructor) {
14+
if (!(instance instanceof Constructor)) {
15+
throw new TypeError("Cannot call a class as a function");
16+
}
17+
}
18+
19+
function _defineProperties(target, props) {
20+
for (var i = 0; i < props.length; i++) {
21+
var descriptor = props[i];
22+
descriptor.enumerable = descriptor.enumerable || false;
23+
descriptor.configurable = true;
24+
if ("value" in descriptor) descriptor.writable = true;
25+
Object.defineProperty(target, descriptor.key, descriptor);
26+
}
27+
}
28+
29+
function _createClass(Constructor, protoProps, staticProps) {
30+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
31+
if (staticProps) _defineProperties(Constructor, staticProps);
32+
return Constructor;
33+
}
34+
35+
var version = "2.0.0";
36+
37+
var RESIZE = "resize";
38+
var LOAD = "load";
39+
var USEMAP = "usemap";
40+
var COORDS = "coords";
41+
var COMPLETE = "complete";
42+
/**
43+
* ImageMap main library constructor
44+
*
45+
* @param selector {string} CSS selector
46+
* @param wait {number} [wait=500] debounce wait interval
47+
* @constructor
48+
*/
49+
50+
var ImageMap = /*#__PURE__*/function () {
51+
function ImageMap(selector, wait) {
52+
_classCallCheck(this, ImageMap);
53+
54+
this.selector = selector instanceof Array ? selector : document.querySelectorAll(selector);
55+
if (document.readyState !== COMPLETE) window.addEventListener(LOAD, this.update.bind(this));else this.update();
56+
window.addEventListener(RESIZE, this.debounce(this.update, wait).bind(this));
57+
}
58+
59+
_createClass(ImageMap, [{
60+
key: "update",
61+
62+
/**
63+
* Update
64+
*/
65+
value: function update() {
66+
var imgs = this.selector;
67+
68+
for (var i = 0; i < imgs.length; i++) {
69+
var img = imgs[i];
70+
if (img.getAttribute(USEMAP) === undefined) return;
71+
var newImg = img.cloneNode();
72+
newImg.addEventListener(LOAD, this.handleImageLoad(img.offsetWidth, img.offsetHeight));
73+
newImg.src = img.src; // required for IE
74+
}
75+
}
76+
/**
77+
* Debounce
78+
*
79+
* @param {function} func
80+
* @param {number} [wait=500]
81+
*/
82+
83+
}, {
84+
key: "debounce",
85+
value: function debounce(func) {
86+
var _this = this;
87+
88+
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
89+
var timeout;
90+
return function () {
91+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
92+
args[_key] = arguments[_key];
93+
}
94+
95+
window.clearTimeout(timeout);
96+
timeout = window.setTimeout(function (ctx) {
97+
return func.apply(ctx, args);
98+
}, wait, _this);
99+
};
100+
}
101+
/**
102+
* handleImageLoad
103+
*
104+
* @param {number} [offsetWidth=0]
105+
* @param {number} [offsetHeight=0]
106+
*/
107+
108+
}, {
109+
key: "handleImageLoad",
110+
value: function handleImageLoad() {
111+
var offsetWidth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
112+
var offsetHeight = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
113+
return function (e) {
114+
var w = e.target.width;
115+
var h = e.target.height;
116+
var wPercent = offsetWidth / 100;
117+
var hPercent = offsetHeight / 100;
118+
var mapName = e.target.getAttribute(USEMAP).replace(/^#/, "");
119+
var areas = document.querySelectorAll(ImageMap.genAreaSelector(mapName));
120+
121+
var _loop = function _loop(i) {
122+
var area = areas[i];
123+
var coordsString = area.dataset[COORDS] = area.dataset[COORDS] || area.getAttribute(COORDS);
124+
var coordsArrayOld = coordsString.split(",");
125+
var coordsArrayNew = coordsArrayOld.map(function (_, i) {
126+
return i % 2 === 0 ? Number(coordsArrayOld[i] / w * 100 * wPercent) : Number(coordsArrayOld[i] / h * 100 * hPercent);
127+
});
128+
area.setAttribute(COORDS, coordsArrayNew.toString());
129+
};
130+
131+
for (var i = 0; i < areas.length; i++) {
132+
_loop(i);
133+
}
134+
};
135+
}
136+
}], [{
137+
key: "genAreaSelector",
138+
value: function genAreaSelector(mapName) {
139+
return "map[name=\"".concat(mapName, "\"] area");
140+
}
141+
}]);
142+
143+
return ImageMap;
144+
}();
145+
146+
function _ImageMap(selector, wait) {
147+
return new ImageMap(selector, wait);
148+
}
149+
150+
_ImageMap.VERSION = version;
151+
152+
if ($ !== undefined && $.fn) {
153+
$.fn.imageMap = function (wait) {
154+
return new _ImageMap(this.toArray(), wait);
155+
};
156+
}
157+
158+
}($));

dist/image-map.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
/**
2-
* Copyright (c) 2018, Travis Clarke (https://www.travismclarke.com/)
2+
* Copyright (c) 2020, Travis Clarke (https://www.travismclarke.com/)
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

88
(function (global, factory) {
9-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
10-
typeof define === 'function' && define.amd ? define(['jquery'], factory) :
11-
(global = global || self, global.ImageMap = factory(global.$));
12-
}(this, function ($) { 'use strict';
13-
14-
$ = $ && $.hasOwnProperty('default') ? $['default'] : $;
9+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
10+
typeof define === 'function' && define.amd ? define(factory) :
11+
(global = global || self, global.ImageMap = factory());
12+
}(this, (function () { 'use strict';
1513

1614
function _classCallCheck(instance, Constructor) {
1715
if (!(instance instanceof Constructor)) {
@@ -35,7 +33,7 @@
3533
return Constructor;
3634
}
3735

38-
var version = "1.1.7";
36+
var version = "2.0.0";
3937

4038
var RESIZE = "resize";
4139
var LOAD = "load";
@@ -50,9 +48,7 @@
5048
* @constructor
5149
*/
5250

53-
var ImageMap =
54-
/*#__PURE__*/
55-
function () {
51+
var ImageMap = /*#__PURE__*/function () {
5652
function ImageMap(selector, wait) {
5753
_classCallCheck(this, ImageMap);
5854

@@ -148,12 +144,6 @@
148144
return ImageMap;
149145
}();
150146

151-
if ($ !== undefined && $.fn) {
152-
$.fn.imageMap = function (wait) {
153-
return new ImageMap(this.toArray(), wait);
154-
};
155-
}
156-
157147
function _ImageMap(selector, wait) {
158148
return new ImageMap(selector, wait);
159149
}
@@ -162,4 +152,4 @@
162152

163153
return _ImageMap;
164154

165-
}));
155+
})));

dist/image-map.min.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

example/example-1000-area-tags.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
<area shape="poly" alt="orange" title="orange" onclick="selectRegion(this, 'orange')" coords="802,27,1096,27,1096,174,802,174,838,101">
2323
</map>
2424

25-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
25+
<!-- JQUERY -->
26+
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>-->
27+
<!--<script src="../dist/image-map.jquery.js"></script>-->
28+
29+
<!-- JAVASCRIPT -->
2630
<script src="../dist/image-map.js"></script>
31+
2732
<script>
2833

2934
const map = $("map");

example/example.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@
3838
coords="802,27,1096,27,1096,174,802,174,838,101">
3939
</map>
4040

41-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
42-
<!--<script src="./image-map.min.js"></script>-->
41+
<!-- JQUERY -->
42+
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>-->
43+
<!--<script src="../dist/image-map.jquery.js"></script>-->
44+
45+
<!-- JAVASCRIPT -->
4346
<script src="../dist/image-map.js"></script>
47+
4448
<script>
4549

4650
/* JavaScript */

example/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var ImageMap = require("image-map");
2+
3+
console.log(ImageMap);

example/main.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import ImageMap from 'image-map';
2+
3+
console.log(ImageMap);

0 commit comments

Comments
 (0)