Skip to content

Commit

Permalink
Add support for all methods for DomTokenList
Browse files Browse the repository at this point in the history
  • Loading branch information
AliyanH committed Mar 9, 2023
1 parent 3e686f5 commit f360179
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 52 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = function(grunt) {
'dist/mapml.js': ['<%= rollup.main.dest %>'],
'dist/web-map.js': ['src/web-map.js'],
'dist/mapml-viewer.js': ['src/mapml-viewer.js'],
'dist/controlsListDomTokenList.js': ['src/controlsListDomTokenList.js'],
'dist/map-caption.js': ['src/map-caption.js'],
'dist/map-area.js': ['src/map-area.js'],
'dist/layer.js': ['src/layer.js'],
Expand Down
95 changes: 95 additions & 0 deletions src/controlsListDomTokenList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
export default class DOMTokenList {
#mapEl; // create mapEl as a private property
#valueSet;
constructor (initialValue, mapEl) {
// create donor/host div to extract DomTokenList from
const hostingElement = document.createElement('div');
this.controlsList = hostingElement.classList;

// to check if value is being set, protects from infinite recursion
// from attributeChangedCallback of mapml-viewer and web-map
this.#valueSet = false;
this.controlsList.value = initialValue ?? '';
this.#mapEl = mapEl;
}

get valueSet () {
return this.#valueSet;
}

get length () {
return this.controlsList.length;
}

get value () {
return this.controlsList.value;
}
set value (val) {
this.controlsList.value = val.toLowerCase();
this.#valueSet = true;
this.#mapEl.setAttribute("controlslist", this.controlsList.value);
this.#valueSet = false;
}

item (index) {
return this.controlsList.item(index);
}

contains(token) {
return this.controlsList.contains(token);
}

// Modified default behavior
add (token) {
this.controlsList.add(token);
this.#mapEl.setAttribute("controlslist", this.controlsList.value);
}

// Modified default behavior
remove (token) {
this.controlsList.remove(token);
this.#mapEl.setAttribute("controlslist", this.controlsList.value);
}

// Modified default behavior
replace (oldToken, newToken) {
this.controlsList.replace(oldToken, newToken);
this.#mapEl.setAttribute("controlslist", this.controlsList.value);
}

// Modified default behavior
supports (token) {
let supported = ["nolayer", "nofullscreen", "noreload", "nozoom"];
if (supported.includes(token)) {
return true;
} else {
return false;
}
}

// Modified default behavior
//https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/toggle
toggle (token, force) {
this.controlsList.toggle(token, force);
this.#mapEl.setAttribute("controlslist", this.controlsList.value);
}

entries () {
return this.controlsList.entries();
}

//https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/forEach
forEach (callback, thisArg) {
this.controlsList.forEach(callback, thisArg);
}

//https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/keys
keys () {
return this.controlsList.keys();
}

//https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/values
values () {
return this.controlsList.values();
}
}
33 changes: 7 additions & 26 deletions src/mapml-viewer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './leaflet.js'; // bundled with proj4, proj4leaflet, modularized
import './mapml.js';
import DOMTokenList from './controlsListDomTokenList.js';
import { MapLayer } from './layer.js';
import { MapCaption } from './map-caption.js';

Expand All @@ -23,13 +24,6 @@ export class MapViewer extends HTMLElement {
this.removeAttribute('controls');
}
}
get controlslist() {
return this._controlslist;
}
set controlslist(val) {
this._controlslist.value = val.toLowerCase();
this.setAttribute("controlslist", val);
}
get width() {
return (window.getComputedStyle(this).width).replace('px','');
}
Expand Down Expand Up @@ -125,9 +119,7 @@ export class MapViewer extends HTMLElement {
connectedCallback() {
if (this.isConnected) {

// TODO - add DOMTokenList as a class
// Add a DOMTokenList for controlslist
this._controlslist = this._controlslistDOMTokenList(this.getAttribute("controlslist"));
this.controlsList = new DOMTokenList(this.getAttribute("controlslist"), this);

let tmpl = document.createElement('template');
tmpl.innerHTML = `<link rel="stylesheet" href="${new URL("mapml.css", import.meta.url).href}">`; // jshint ignore:line
Expand Down Expand Up @@ -303,8 +295,10 @@ export class MapViewer extends HTMLElement {
} */
switch(name) {
case 'controlslist':
if (this._controlslist) {
this._controlslist.value = newValue;
if (this.controlsList) {
if (this.controlsList.valueSet === false) {
this.controlsList.value = newValue;
}
this._setControls();
}
break;
Expand Down Expand Up @@ -366,7 +360,7 @@ export class MapViewer extends HTMLElement {
this._setControlsVisibility("reload",false);
this._setControlsVisibility("zoom",false);

this._controlslist.forEach((value, key, listObj) => {
this.controlsList.forEach((value) => {
switch(value.toLowerCase()) {
case 'nofullscreen':
this._setControlsVisibility("fullscreen",true);
Expand Down Expand Up @@ -461,19 +455,6 @@ export class MapViewer extends HTMLElement {
}
}
}

// creating a DOMTokenList for controlslist, as shown here:
// https://marchbox.com/articles/2023-01/using-domtokenlist/
// TODO - implement this as a Class
_controlslistDOMTokenList(initialValue) {
if (initialValue) {
initialValue = initialValue.toLowerCase();
}
const hostingElement = document.createElement('div');
const controlslist = hostingElement.classList;
controlslist.value = initialValue ?? '';
return controlslist;
}

_dropHandler(event) {
event.preventDefault();
Expand Down
33 changes: 7 additions & 26 deletions src/web-map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './leaflet.js'; // a lightly modified version of Leaflet for use as browser module
import './mapml.js'; // refactored URI usage, replaced with URL standard
import DOMTokenList from './controlsListDomTokenList.js';
import { MapLayer } from './layer.js';
import { MapArea } from './map-area.js';
import { MapCaption } from './map-caption.js';
Expand All @@ -24,13 +25,6 @@ export class WebMap extends HTMLMapElement {
this.removeAttribute('controls');
}
}
get controlslist() {
return this._controlslist;
}
set controlslist(val) {
this._controlslist.value = val.toLowerCase();
this.setAttribute("controlslist", val);
}
get width() {
return (window.getComputedStyle(this).width).replace('px','');
}
Expand Down Expand Up @@ -130,9 +124,7 @@ export class WebMap extends HTMLMapElement {
connectedCallback() {
if (this.isConnected) {

// TODO - add DOMTokenList as a class
// Add a DOMTokenList for controlslist
this._controlslist = this._controlslistDOMTokenList(this.getAttribute("controlslist"));
this.controlsList = new DOMTokenList(this.getAttribute("controlslist"), this);

let tmpl = document.createElement('template');
tmpl.innerHTML = `<link rel="stylesheet" href="${new URL("mapml.css", import.meta.url).href}">`; // jshint ignore:line
Expand Down Expand Up @@ -343,8 +335,10 @@ export class WebMap extends HTMLMapElement {
} */
switch(name) {
case 'controlslist':
if (this._controlslist) {
this._controlslist.value = newValue;
if (this.controlsList) {
if (this.controlsList.valueSet === false) {
this.controlsList.value = newValue;
}
this._setControls();
}
break;
Expand Down Expand Up @@ -406,7 +400,7 @@ export class WebMap extends HTMLMapElement {
this._setControlsVisibility("reload",false);
this._setControlsVisibility("zoom",false);

this._controlslist.forEach((value, key, listObj) => {
this.controlsList.forEach((value) => {
switch(value.toLowerCase()) {
case 'nofullscreen':
this._setControlsVisibility("fullscreen",true);
Expand Down Expand Up @@ -504,19 +498,6 @@ export class WebMap extends HTMLMapElement {
}
}

// creating a DOMTokenList for controlslist, as shown here:
// https://marchbox.com/articles/2023-01/using-domtokenlist/
// TODO - implement this as a Class
_controlslistDOMTokenList(initialValue) {
if (initialValue) {
initialValue = initialValue.toLowerCase();
}
const hostingElement = document.createElement('div');
const controlslist = hostingElement.classList;
controlslist.value = initialValue ?? '';
return controlslist;
}

_dropHandler(event) {
event.preventDefault();
let text = event.dataTransfer.getData("text");
Expand Down

0 comments on commit f360179

Please sign in to comment.