Skip to content

Commit 4c6f1a6

Browse files
author
潘卓然ParnDeedlit
committed
【SDK】【服务】【增加通用事件工具类】
1 parent 67a237c commit 4c6f1a6

File tree

3 files changed

+240
-1
lines changed

3 files changed

+240
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"fast-xml-parser": "^3.17.6",
7575
"html-webpack-plugin": "^4.3.0",
7676
"jsdoc": "^3.6.3",
77+
"leaflet": "^1.7.1",
7778
"mapv": "^2.0.40",
7879
"ol": "5.3.3",
7980
"proj4": "2.3.15",

src/service/common/EventUtil.js

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/*
2+
* @namespace Util
3+
*
4+
* Various utility functions, used by Leaflet internally.
5+
*/
6+
7+
// @function extend(dest: Object, src?: Object): Object
8+
// Merges the properties of the `src` object (or multiple objects) into `dest` object and returns the latter. Has an `L.extend` shortcut.
9+
export function extend(dest) {
10+
var i, j, len, src;
11+
12+
for (j = 1, len = arguments.length; j < len; j++) {
13+
src = arguments[j];
14+
for (i in src) {
15+
dest[i] = src[i];
16+
}
17+
}
18+
return dest;
19+
}
20+
21+
// @function create(proto: Object, properties?: Object): Object
22+
// Compatibility polyfill for [Object.create](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/create)
23+
export var create = Object.create || (function () {
24+
function F() {}
25+
return function (proto) {
26+
F.prototype = proto;
27+
return new F();
28+
};
29+
})();
30+
31+
// @function bind(fn: Function, …): Function
32+
// Returns a new function bound to the arguments passed, like [Function.prototype.bind](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
33+
// Has a `L.bind()` shortcut.
34+
export function bind(fn, obj) {
35+
var slice = Array.prototype.slice;
36+
37+
if (fn.bind) {
38+
return fn.bind.apply(fn, slice.call(arguments, 1));
39+
}
40+
41+
var args = slice.call(arguments, 2);
42+
43+
return function () {
44+
return fn.apply(obj, args.length ? args.concat(slice.call(arguments)) : arguments);
45+
};
46+
}
47+
48+
// @property lastId: Number
49+
// Last unique ID used by [`stamp()`](#util-stamp)
50+
export var lastId = 0;
51+
52+
// @function stamp(obj: Object): Number
53+
// Returns the unique ID of an object, assigning it one if it doesn't have it.
54+
export function stamp(obj) {
55+
/*eslint-disable */
56+
obj._leaflet_id = obj._leaflet_id || ++lastId;
57+
return obj._leaflet_id;
58+
/* eslint-enable */
59+
}
60+
61+
// @function throttle(fn: Function, time: Number, context: Object): Function
62+
// Returns a function which executes function `fn` with the given scope `context`
63+
// (so that the `this` keyword refers to `context` inside `fn`'s code). The function
64+
// `fn` will be called no more than one time per given amount of `time`. The arguments
65+
// received by the bound function will be any arguments passed when binding the
66+
// function, followed by any arguments passed when invoking the bound function.
67+
// Has an `L.throttle` shortcut.
68+
export function throttle(fn, time, context) {
69+
var lock, args, wrapperFn, later;
70+
71+
later = function () {
72+
// reset lock and call if queued
73+
lock = false;
74+
if (args) {
75+
wrapperFn.apply(context, args);
76+
args = false;
77+
}
78+
};
79+
80+
wrapperFn = function () {
81+
if (lock) {
82+
// called too soon, queue to call later
83+
args = arguments;
84+
85+
} else {
86+
// call and lock until later
87+
fn.apply(context, arguments);
88+
setTimeout(later, time);
89+
lock = true;
90+
}
91+
};
92+
93+
return wrapperFn;
94+
}
95+
96+
// @function wrapNum(num: Number, range: Number[], includeMax?: Boolean): Number
97+
// Returns the number `num` modulo `range` in such a way so it lies within
98+
// `range[0]` and `range[1]`. The returned value will be always smaller than
99+
// `range[1]` unless `includeMax` is set to `true`.
100+
export function wrapNum(x, range, includeMax) {
101+
var max = range[1],
102+
min = range[0],
103+
d = max - min;
104+
return x === max && includeMax ? x : ((x - min) % d + d) % d + min;
105+
}
106+
107+
// @function falseFn(): Function
108+
// Returns a function which always returns `false`.
109+
export function falseFn() { return false; }
110+
111+
// @function formatNum(num: Number, digits?: Number): Number
112+
// Returns the number `num` rounded to `digits` decimals, or to 6 decimals by default.
113+
export function formatNum(num, digits) {
114+
var pow = Math.pow(10, (digits === undefined ? 6 : digits));
115+
return Math.round(num * pow) / pow;
116+
}
117+
118+
// @function trim(str: String): String
119+
// Compatibility polyfill for [String.prototype.trim](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
120+
export function trim(str) {
121+
return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
122+
}
123+
124+
// @function splitWords(str: String): String[]
125+
// Trims and splits the string on whitespace and returns the array of parts.
126+
export function splitWords(str) {
127+
return trim(str).split(/\s+/);
128+
}
129+
130+
// @function setOptions(obj: Object, options: Object): Object
131+
// Merges the given properties to the `options` of the `obj` object, returning the resulting options. See `Class options`. Has an `L.setOptions` shortcut.
132+
export function setOptions(obj, options) {
133+
if (!Object.prototype.hasOwnProperty.call(obj, 'options')) {
134+
obj.options = obj.options ? create(obj.options) : {};
135+
}
136+
for (var i in options) {
137+
obj.options[i] = options[i];
138+
}
139+
return obj.options;
140+
}
141+
142+
// @function getParamString(obj: Object, existingUrl?: String, uppercase?: Boolean): String
143+
// Converts an object into a parameter URL string, e.g. `{a: "foo", b: "bar"}`
144+
// translates to `'?a=foo&b=bar'`. If `existingUrl` is set, the parameters will
145+
// be appended at the end. If `uppercase` is `true`, the parameter names will
146+
// be uppercased (e.g. `'?A=foo&B=bar'`)
147+
export function getParamString(obj, existingUrl, uppercase) {
148+
var params = [];
149+
for (var i in obj) {
150+
params.push(encodeURIComponent(uppercase ? i.toUpperCase() : i) + '=' + encodeURIComponent(obj[i]));
151+
}
152+
return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
153+
}
154+
155+
var templateRe = /\{ *([\w_-]+) *\}/g;
156+
157+
// @function template(str: String, data: Object): String
158+
// Simple templating facility, accepts a template string of the form `'Hello {a}, {b}'`
159+
// and a data object like `{a: 'foo', b: 'bar'}`, returns evaluated string
160+
// `('Hello foo, bar')`. You can also specify functions instead of strings for
161+
// data values — they will be evaluated passing `data` as an argument.
162+
export function template(str, data) {
163+
return str.replace(templateRe, function (str, key) {
164+
var value = data[key];
165+
166+
if (value === undefined) {
167+
throw new Error('No value provided for variable ' + str);
168+
169+
} else if (typeof value === 'function') {
170+
value = value(data);
171+
}
172+
return value;
173+
});
174+
}
175+
176+
// @function isArray(obj): Boolean
177+
// Compatibility polyfill for [Array.isArray](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
178+
export var isArray = Array.isArray || function (obj) {
179+
return (Object.prototype.toString.call(obj) === '[object Array]');
180+
};
181+
182+
// @function indexOf(array: Array, el: Object): Number
183+
// Compatibility polyfill for [Array.prototype.indexOf](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
184+
export function indexOf(array, el) {
185+
for (var i = 0; i < array.length; i++) {
186+
if (array[i] === el) { return i; }
187+
}
188+
return -1;
189+
}
190+
191+
// @property emptyImageUrl: String
192+
// Data URI string containing a base64-encoded empty GIF image.
193+
// Used as a hack to free memory from unused images on WebKit-powered
194+
// mobile devices (by setting image `src` to this string).
195+
export var emptyImageUrl = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';
196+
197+
// inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/
198+
199+
function getPrefixed(name) {
200+
return window['webkit' + name] || window['moz' + name] || window['ms' + name];
201+
}
202+
203+
var lastTime = 0;
204+
205+
// fallback for IE 7-8
206+
function timeoutDefer(fn) {
207+
var time = +new Date(),
208+
timeToCall = Math.max(0, 16 - (time - lastTime));
209+
210+
lastTime = time + timeToCall;
211+
return window.setTimeout(fn, timeToCall);
212+
}
213+
214+
export var requestFn = window.requestAnimationFrame || getPrefixed('RequestAnimationFrame') || timeoutDefer;
215+
export var cancelFn = window.cancelAnimationFrame || getPrefixed('CancelAnimationFrame') ||
216+
getPrefixed('CancelRequestAnimationFrame') || function (id) { window.clearTimeout(id); };
217+
218+
// @function requestAnimFrame(fn: Function, context?: Object, immediate?: Boolean): Number
219+
// Schedules `fn` to be executed when the browser repaints. `fn` is bound to
220+
// `context` if given. When `immediate` is set, `fn` is called immediately if
221+
// the browser doesn't have native support for
222+
// [`window.requestAnimationFrame`](https://developer.mozilla.org/docs/Web/API/window/requestAnimationFrame),
223+
// otherwise it's delayed. Returns a request ID that can be used to cancel the request.
224+
export function requestAnimFrame(fn, context, immediate) {
225+
if (immediate && requestFn === timeoutDefer) {
226+
fn.call(context);
227+
} else {
228+
return requestFn.call(window, bind(fn, context));
229+
}
230+
}
231+
232+
// @function cancelAnimFrame(id: Number): undefined
233+
// Cancels a previous `requestAnimFrame`. See also [window.cancelAnimationFrame](https://developer.mozilla.org/docs/Web/API/window/cancelAnimationFrame).
234+
export function cancelAnimFrame(id) {
235+
if (id) {
236+
cancelFn.call(window, id);
237+
}
238+
}

src/service/common/Evented.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Util from "./Util";
1+
import * as Util from "./EventUtil";
22

33
/*
44
* @class Evented

0 commit comments

Comments
 (0)