-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpx-map-behavior-popup.es6.js
581 lines (511 loc) · 17.9 KB
/
px-map-behavior-popup.es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
* @license
* Copyright (c) 2018, General Electric
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
'use strict';
/****************************************************************************
* BEHAVIORS
****************************************************************************/
/* Ensures the behavior namespace is created */
window.PxMapBehavior = (window.PxMapBehavior || {});
/**
*
* @polymerBehavior PxMapBehavior.Popup
*/
PxMapBehavior.PopupImpl = {
properties: {
/**
* If set to `true`, the popup will be automatically closed when the user
* interacts with any control buttons (e.g. zoom buttons or locate button).
* By default, the popup only closes when the user clicks the map.
*/
closeOnControlInteract: {
type: Boolean,
value: false
},
/**
* Max width of popup container
*/
maxWidth: {
type: Number,
value: 400,
observer: 'shouldUpdateInst'
},
/**
* Min width of popup container
*/
minWidth: {
type: Number,
value: 300,
observer: 'shouldUpdateInst'
},
/**
* Programatically opens and closes the popup. Is updated when the popup
* is opened by the user through DOM interaction.
*/
opened: {
type: Boolean,
value: false,
observer: 'shouldUpdateInst',
notify: true
}
},
addInst(parent) {
if (parent && parent.getPopup && (parent.getPopup() !== this.elementInst)) {
parent.bindPopup(this.elementInst);
if (this.opened) {
parent.openPopup();
}
// Bind custom events for this popup. Events will be unbound automatically.
const controlClickFn = this._handleControlClick.bind(this, parent);
this.bindEvents({
'controlclick' : controlClickFn
}, parent._mapToAdd);
const popupOpenFn = this._handlePopupOpenChange.bind(this, true);
const popupCloseFn = this._handlePopupOpenChange.bind(this, false);
this.bindEvents({
'popupopen' : popupOpenFn,
'popupclose' : popupCloseFn
}, parent);
}
},
_handlePopupOpenChange(opened) {
this.opened = opened;
},
removeInst(parent) {
if (parent && parent.getPopup && (parent.getPopup() === this.elementInst)) {
parent.unbindPopup(this.elementInst);
}
},
updateInst(lastOptions, nextOptions) {
if (lastOptions.opened !== nextOptions.opened) {
if (nextOptions.opened && !this.elementInst.isOpen()) {
this.elementInst._source.openPopup();
} else if (!nextOptions.opened && this.elementInst.isOpen()) {
this.elementInst._source.closePopup();
}
}
if (nextOptions.minWidth !== lastOptions.minWidth || nextOptions.maxWidth !== lastOptions.maxWidth) {
const minWidth = nextOptions.minWidth || lastOptions.minWidth;
const maxWidth = nextOptions.maxWidth || lastOptions.maxWidth;
this.elementInst.updateSettings({minWidth, maxWidth})
}
},
getInstOptions() {
return {
opened: this.opened,
minWidth: this.minWidth,
maxWidth: this.maxWidth,
};
},
_handleControlClick(parent) {
if (this.closeOnControlInteract && parent && parent.closePopup) {
parent.closePopup();
}
}
};
/* Bind Popup behavior */
/** @polymerBehavior */
PxMapBehavior.Popup = [
PxMapBehavior.Layer,
PxMapBehavior.PopupImpl
];
/**
*
* @polymerBehavior PxMapBehavior.InfoPopup
*/
PxMapBehavior.InfoPopupImpl = {
properties: {
/**
* Title text to display in bold at the top of the popup. Should be kept
* relatively short (25 characters or less) to keep the popup from
* growing too large.
*
* @type {String}
*/
title: {
type: String,
observer: 'shouldUpdateInst'
},
/**
* Description text to place in the body of the popup. Try to keep the
* description to a reasonable size to keep the popup from growing
* too large.
*
* To display more information, bind to the popup's parent layer (e.g. a marker)
* tapped event and display more information in the UI of your application.
*
* @type {String}
*/
description: {
type: String,
observer: 'shouldUpdateInst'
},
/**
* The URL for an image to be placed inside the popup. Use a small, square
* thumbnail-style image (e.g. 75px by 75px). You may use any image type
* that you would put in an html
* <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img"
* target="_blank">`<img>`</a> tag.
*
* @type {String}
*/
imgSrc: {
type: String,
observer: 'shouldUpdateInst'
}
},
createInst(options) {
return new PxMap.InfoPopup(options);
},
updateInst(lastOptions, nextOptions) {
PxMapBehavior.PopupImpl.updateInst.call(this, lastOptions, nextOptions);
let updates = {};
if (lastOptions.title !== nextOptions.title) {
updates.title = nextOptions.title;
}
if (lastOptions.description !== nextOptions.description) {
updates.description = nextOptions.description;
}
if (lastOptions.imgSrc !== nextOptions.imgSrc) {
updates.imgSrc = nextOptions.imgSrc;
}
if (Object.keys(updates).length) {
this.elementInst.updateSettings(updates);
}
},
getInstOptions() {
const opts = PxMapBehavior.PopupImpl.getInstOptions.call(this);
return Object.assign({}, opts, {
title: this.title,
description: this.description,
imgSrc: this.imgSrc,
styleScope: this.isShadyScoped() ? this.getShadyScope() : undefined
});
}
};
/* Bind InfoPopup behavior */
/** @polymerBehavior */
PxMapBehavior.InfoPopup = [
PxMapBehavior.Popup,
PxMapBehavior.InfoPopupImpl
];
/**
*
* @polymerBehavior PxMapBehavior.DataPopup
*/
PxMapBehavior.DataPopupImpl = {
properties: {
/**
* Title text to display in bold at the top of the popup. Should be kept
* relatively short (25 characters or less) to keep the popup from
* growing too large.
*/
title: {
type: String,
observer: 'shouldUpdateInst'
},
/**
* A list of key/value pairs to display in a data table. Must be in the
* format of an object with human-readable keys and associated values.
*
* For example, to show the name and location of a place, set this
* attribute to:
* '{ "Name" : "Tokyo", "Location" : "Japan" }'
*
* @type {Object}
*/
data: {
type: Object,
value: function() { return {}; },
observer: 'shouldUpdateInst'
}
},
canAddInst() {
return this.data && typeof this.data === 'object' && Object.keys(this.data).length;
},
createInst(options) {
return new PxMap.DataPopup(options);
},
updateInst(lastOptions, nextOptions) {
PxMapBehavior.PopupImpl.updateInst.call(this, lastOptions, nextOptions);
let updates = {};
if (lastOptions.title !== nextOptions.title) {
updates.title = nextOptions.title;
}
if (lastOptions.dataHash !== nextOptions.dataHash) {
updates.data = nextOptions.data;
}
if (Object.keys(updates).length) {
this.elementInst.updateSettings(updates);
}
},
getInstOptions() {
const opts = PxMapBehavior.PopupImpl.getInstOptions.call(this);
let data = this._getValidData();
return Object.assign({}, opts, {
title: this.title,
data: data,
dataHash: JSON.stringify(data),
styleScope: this.isShadyScoped() ? this.getShadyScope() : undefined
});
},
_getValidData() {
if (typeof this.data !== 'object') {
console.log(`PX-MAP CONFIGURATION ERROR:
You entered an invalid \`data\` attribute for ${this.is}. You must pass a valid object.
An attribute of type \`${typeof this.data}\` was passed.`);
return {};
}
return this.data;
}
};
/* Bind DataPopup behavior */
/** @polymerBehavior */
PxMapBehavior.DataPopup = [
PxMapBehavior.Popup,
PxMapBehavior.DataPopupImpl
];
/****************************************************************************
* KLASSES
****************************************************************************/
/* Ensures the klass namespace is created */
window.PxMap = (window.PxMap || {});
/**
*
* @class PxMap.InfoPopup
*/
class InfoPopup extends L.Popup {
constructor(settings={}) {
super();
this._createPopup(settings);
return this;
}
onAdd(map) {
if (map.__addShadyScope && !Polymer.Element) {
// We need to monkey patch the node returned by `getPane().appendChild`
// so we can ensure that we apply the right CSS scope if we are in
// shady DOM. By doing this, we effectively wrap the node (which is
// fetched in the L.DivOverlay.onAdd method) in a function that scopes
// the child nodes before they are added to the map. If we don't do this,
// Leaflet will measure the popup before its CSS classes are applied and
// pan the map far too much to fit it. This is lame. :(
// @TODO: Remove when shady DOM support is deprecated
var srcPane = this.getPane();
var srcFn = srcPane.appendChild;
srcPane.appendChild = function(el) {
map.__addShadyScope(el, false);
let d = Polymer.dom(srcPane);
d.appendChild(el);
}
}
L.Popup.prototype.onAdd.call(this, map);
if (map.__addShadyScope && !Polymer.Element) {
// Restore monkey patched function
srcPane.appendChild = srcFn;
}
}
_updateContent() {
if (this._map && this._map.__addShadyScope && this._content.length) {
// We need to monkey patch the srcNode's `innerHTML` setter to ensure
// that our popup is scoped before it is drawn if we are in shady DOM.
// If we don't do this, Leaflet will measure the popup before its CSS
// classes are applied and pan the map far too much to fit it.
// This is also lame. :(
// @TODO: Remove when shady DOM support is deprecated
var srcNode = this._contentNode;
var fakeNode = {
innerHTML: null
};
this._contentNode = fakeNode;
}
L.DivOverlay.prototype._updateContent.call(this);
if (this._map && this._map.__addShadyScope && this._content.length) {
if (typeof fakeNode.innerHTML === 'string') {
Polymer.dom(srcNode).innerHTML = fakeNode.innerHTML;
}
// Restore monkey patched function
this._contentNode = srcNode;
}
}
// Note `createPopup` is an internet explorer native method, but deprecated
// so hopefully it won't cause grief
_createPopup({title = null,
description = null,
imgSrc = null,
styleScope = null,
minWidth = PxMapBehavior.PopupImpl.properties.minWidth.value,
maxWidth = PxMapBehavior.PopupImpl.properties.maxWidth.value}={}) {
// Assign settings and create content
this.settings = { title, description, imgSrc, styleScope, minWidth, maxWidth };
const content = this._generatePopupContent(title, description, imgSrc);
const className = `map-popup-info ${styleScope||''}`
this.initialize({ className, maxWidth, minWidth });
this.setContent(content);
}
_generatePopupContent(title, description, imgSrc) {
let tmplFnIf = (fn, ...vals) =>
vals.length && vals[0] != null ? fn.call(this, ...vals) : '';
let imgTmpl = (imgSrc) => `
<div class="map-box-info__image">
<img src="${imgSrc}" />
</div>
`;
let titleTmpl = (title) => `
<p class="map-box-info__title">${title}</p>
`;
let descriptionTmpl = (description) => `
<p class="map-box-info__description">${description}</p>
`;
return `
<section class="map-box-info">
${tmplFnIf(imgTmpl, imgSrc)}
<div class="map-box-info__content">
${tmplFnIf(titleTmpl, title)}
${tmplFnIf(descriptionTmpl, description)}
</div>
</section>
`;
}
updateSettings(settings={}) {
Object.assign(this.settings, settings);
const { title, description, imgSrc, minWidth, maxWidth } = this.settings;
Object.assign(this.options, { minWidth, maxWidth });
const content = this._generatePopupContent(title, description, imgSrc);
this.setContent(content);
this.update();
}
};
/* Bind InfoPopup klass */
PxMap.InfoPopup = InfoPopup;
/**
*
* @class PxMap.DataPopup
*/
class DataPopup extends L.Popup {
constructor(settings={}) {
super();
this._createPopup(settings);
return this;
}
onAdd(map) {
// Don't open empty data popups
if (typeof this.settings.data !== 'object' || Object.keys(this.settings.data).length === 0) {
return;
}
if (map.__addShadyScope && !Polymer.Element) {
// We need to monkey patch the node returned by `getPane().appendChild`
// so we can ensure that we apply the right CSS scope if we are in
// shady DOM. By doing this, we effectively wrap the node (which is
// fetched in the L.DivOverlay.onAdd method) in a function that scopes
// the child nodes before they are added to the map. If we don't do this,
// Leaflet will measure the popup before its CSS classes are applied and
// pan the map far too much to fit it. This is lame. :(
// @TODO: Remove when shady DOM support is deprecated
var srcPane = this.getPane();
var srcFn = srcPane.appendChild;
srcPane.appendChild = function(el) {
map.__addShadyScope(el, false);
Polymer.dom(srcPane).appendChild(el);
}
}
L.Popup.prototype.onAdd.call(this, map);
if (map.__addShadyScope && !Polymer.Element) {
// Restore monkey patched function
srcPane.appendChild = srcFn;
}
}
_updateContent() {
if (this._map && this._map.__addShadyScope && this._content.length) {
// We need to monkey patch the srcNode's `innerHTML` setter to ensure
// that our popup is scoped before it is drawn if we are in shady DOM.
// If we don't do this, Leaflet will measure the popup before its CSS
// classes are applied and pan the map far too much to fit it.
// This is also lame. :(
// @TODO: Remove when shady DOM support is deprecated
var srcNode = this._contentNode;
var fakeNode = {
innerHTML: null
};
this._contentNode = fakeNode;
}
L.DivOverlay.prototype._updateContent.call(this);
if (this._map && this._map.__addShadyScope && this._content.length) {
if (typeof fakeNode.innerHTML === 'string') {
Polymer.dom(srcNode).innerHTML = fakeNode.innerHTML;
}
// Restore monkey patched function
this._contentNode = srcNode;
}
}
// Note `createPopup` is an internet explorer native method, but deprecated
// so hopefully it won't cause grief
_createPopup({title = null,
data = null,
styleScope = null,
minWidth = PxMapBehavior.PopupImpl.properties.minWidth.value,
maxWidth = PxMapBehavior.PopupImpl.properties.maxWidth.value}={}) {
// Merge defaults into settings param
this.settings = { title, data, styleScope, minWidth, maxWidth };
const content = this._generatePopupContent(title, data);
const className = `map-popup-data ${styleScope||''}`
this.initialize({ className, maxWidth, minWidth });
this.setContent(content);
}
_generatePopupContent(title, data) {
let tmplFnIf = (fn, ...vals) =>
vals.length && vals[0] != null ? fn.call(this, ...vals) : '';
let titleTmpl = (title) => `
<div class="map-data-box__header">
<h3 class="map-data-box__header__text">${title}</h3>
</div>
`;
let dataTmpl = (data) => {
let dataList = Object.keys(data).reduce((accum, key) => {
return accum.concat([dataItemTmpl(key, data[key])]);
}, []).join('');
return `
<div class="map-data-box__table">
${dataList}
</div>
`;
};
let dataItemTmpl = (label, value) => `
<div class="map-data-box__table__cell"><p>${label}</p></div>
<div class="map-data-box__table__cell"><p>${value}</p></div>
`;
return `
<section class="map-box-data">
${tmplFnIf(titleTmpl, title)}
${tmplFnIf(dataTmpl, data)}
</section>
`;
}
updateSettings(settings={}) {
Object.assign(this.settings, settings);
const { title, data, minWidth, maxWidth } = this.settings;
Object.assign(this.options, { minWidth, maxWidth });
const content = this._generatePopupContent(title, data);
if (this.isOpen() && Object.keys(data).length === 0) {
this._close();
}
this.setContent(content);
this.update();
}
};
/* Bind DataPopup klass */
PxMap.DataPopup = DataPopup;
})();