Skip to content

Commit

Permalink
style: lint lib (visjs#1059)
Browse files Browse the repository at this point in the history
* style: include lib in linting

* style: reformat

* style: manual tweaks

Co-authored-by: Vis Bot <[email protected]>
  • Loading branch information
Thomaash and vis-bot authored Sep 20, 2020
1 parent aae7fb1 commit 7c18684
Show file tree
Hide file tree
Showing 84 changed files with 7,479 additions and 5,242 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

!/docs
!/docs-kr
!/lib
!/src
!/test

Expand Down
124 changes: 73 additions & 51 deletions lib/DOMutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

/**
* this prepares the JSON container for allocating SVG elements
* @param {Object} JSONcontainer
*
* @param {object} JSONcontainer
* @private
*/
export function prepareElements (JSONcontainer) {
export function prepareElements(JSONcontainer) {
// cleanup the redundant svgElements;
for (const elementType in JSONcontainer) {
if (Object.prototype.hasOwnProperty.call(JSONcontainer, elementType)) {
Expand All @@ -19,16 +20,18 @@ export function prepareElements (JSONcontainer) {
* this cleans up all the unused SVG elements. By asking for the parentNode, we only need to supply the JSON container from
* which to remove the redundant elements.
*
* @param {Object} JSONcontainer
* @param {object} JSONcontainer
* @private
*/
export function cleanupElements (JSONcontainer) {
export function cleanupElements(JSONcontainer) {
// cleanup the redundant svgElements;
for (const elementType in JSONcontainer) {
if (Object.prototype.hasOwnProperty.call(JSONcontainer, elementType)) {
if (JSONcontainer[elementType].redundant) {
for (let i = 0; i < JSONcontainer[elementType].redundant.length; i++) {
JSONcontainer[elementType].redundant[i].parentNode.removeChild(JSONcontainer[elementType].redundant[i]);
JSONcontainer[elementType].redundant[i].parentNode.removeChild(
JSONcontainer[elementType].redundant[i]
);
}
JSONcontainer[elementType].redundant = [];
}
Expand All @@ -38,9 +41,10 @@ export function cleanupElements (JSONcontainer) {

/**
* Ensures that all elements are removed first up so they can be recreated cleanly
* @param {Object} JSONcontainer
*
* @param {object} JSONcontainer
*/
export function resetElements (JSONcontainer) {
export function resetElements(JSONcontainer) {
prepareElements(JSONcontainer);
cleanupElements(JSONcontainer);
prepareElements(JSONcontainer);
Expand All @@ -51,108 +55,118 @@ export function resetElements (JSONcontainer) {
* the JSON container and the SVG container have to be supplied so other svg containers (like the legend) can use this.
*
* @param {string} elementType
* @param {Object} JSONcontainer
* @param {Object} svgContainer
* @param {object} JSONcontainer
* @param {object} svgContainer
* @returns {Element}
* @private
*/
export function getSVGElement (elementType, JSONcontainer, svgContainer) {
export function getSVGElement(elementType, JSONcontainer, svgContainer) {
let element;
// allocate SVG element, if it doesnt yet exist, create one.
if (Object.prototype.hasOwnProperty.call(JSONcontainer, elementType)) { // this element has been created before
if (Object.prototype.hasOwnProperty.call(JSONcontainer, elementType)) {
// this element has been created before
// check if there is an redundant element
if (JSONcontainer[elementType].redundant.length > 0) {
element = JSONcontainer[elementType].redundant[0];
JSONcontainer[elementType].redundant.shift();
}
else {
} else {
// create a new element and add it to the SVG
element = document.createElementNS('http://www.w3.org/2000/svg', elementType);
element = document.createElementNS(
"http://www.w3.org/2000/svg",
elementType
);
svgContainer.appendChild(element);
}
}
else {
} else {
// create a new element and add it to the SVG, also create a new object in the svgElements to keep track of it.
element = document.createElementNS('http://www.w3.org/2000/svg', elementType);
JSONcontainer[elementType] = {used: [], redundant: []};
element = document.createElementNS(
"http://www.w3.org/2000/svg",
elementType
);
JSONcontainer[elementType] = { used: [], redundant: [] };
svgContainer.appendChild(element);
}
JSONcontainer[elementType].used.push(element);
return element;
}


/**
* Allocate or generate an SVG element if needed. Store a reference to it in the JSON container and draw it in the svgContainer
* the JSON container and the SVG container have to be supplied so other svg containers (like the legend) can use this.
*
* @param {string} elementType
* @param {Object} JSONcontainer
* @param {object} JSONcontainer
* @param {Element} DOMContainer
* @param {Element} insertBefore
* @returns {*}
*/
export function getDOMElement (elementType, JSONcontainer, DOMContainer, insertBefore) {
export function getDOMElement(
elementType,
JSONcontainer,
DOMContainer,
insertBefore
) {
let element;
// allocate DOM element, if it doesnt yet exist, create one.
if (Object.prototype.hasOwnProperty.call(JSONcontainer, elementType)) { // this element has been created before
if (Object.prototype.hasOwnProperty.call(JSONcontainer, elementType)) {
// this element has been created before
// check if there is an redundant element
if (JSONcontainer[elementType].redundant.length > 0) {
element = JSONcontainer[elementType].redundant[0];
JSONcontainer[elementType].redundant.shift();
}
else {
} else {
// create a new element and add it to the SVG
element = document.createElement(elementType);
if (insertBefore !== undefined) {
DOMContainer.insertBefore(element, insertBefore);
}
else {
} else {
DOMContainer.appendChild(element);
}
}
}
else {
} else {
// create a new element and add it to the SVG, also create a new object in the svgElements to keep track of it.
element = document.createElement(elementType);
JSONcontainer[elementType] = {used: [], redundant: []};
JSONcontainer[elementType] = { used: [], redundant: [] };
if (insertBefore !== undefined) {
DOMContainer.insertBefore(element, insertBefore);
}
else {
} else {
DOMContainer.appendChild(element);
}
}
JSONcontainer[elementType].used.push(element);
return element;
}




/**
* Draw a point object. This is a separate function because it can also be called by the legend.
* The reason the JSONcontainer and the target SVG svgContainer have to be supplied is so the legend can use these functions
* as well.
*
* @param {number} x
* @param {number} y
* @param {Object} groupTemplate: A template containing the necessary information to draw the datapoint e.g., {style: 'circle', size: 5, className: 'className' }
* @param {Object} JSONcontainer
* @param {Object} svgContainer
* @param {Object} labelObj
* @param {object} groupTemplate: A template containing the necessary information to draw the datapoint e.g., {style: 'circle', size: 5, className: 'className' }
* @param groupTemplate
* @param {object} JSONcontainer
* @param {object} svgContainer
* @param {object} labelObj
* @returns {vis.PointItem}
*/
export function drawPoint (x, y, groupTemplate, JSONcontainer, svgContainer, labelObj) {
export function drawPoint(
x,
y,
groupTemplate,
JSONcontainer,
svgContainer,
labelObj
) {
let point;
if (groupTemplate.style == 'circle') {
point = getSVGElement('circle', JSONcontainer, svgContainer);
if (groupTemplate.style == "circle") {
point = getSVGElement("circle", JSONcontainer, svgContainer);
point.setAttributeNS(null, "cx", x);
point.setAttributeNS(null, "cy", y);
point.setAttributeNS(null, "r", 0.5 * groupTemplate.size);
}
else {
point = getSVGElement('rect', JSONcontainer, svgContainer);
} else {
point = getSVGElement("rect", JSONcontainer, svgContainer);
point.setAttributeNS(null, "x", x - 0.5 * groupTemplate.size);
point.setAttributeNS(null, "y", y - 0.5 * groupTemplate.size);
point.setAttributeNS(null, "width", groupTemplate.size);
Expand All @@ -165,9 +179,8 @@ export function drawPoint (x, y, groupTemplate, JSONcontainer, svgContainer, lab
point.setAttributeNS(null, "class", groupTemplate.className + " vis-point");
//handle label


if (labelObj) {
const label = getSVGElement('text', JSONcontainer, svgContainer);
const label = getSVGElement("text", JSONcontainer, svgContainer);
if (labelObj.xOffset) {
x = x + labelObj.xOffset;
}
Expand All @@ -180,7 +193,7 @@ export function drawPoint (x, y, groupTemplate, JSONcontainer, svgContainer, lab
}

if (labelObj.className) {
label.setAttributeNS(null, "class", labelObj.className + " vis-label");
label.setAttributeNS(null, "class", labelObj.className + " vis-label");
}
label.setAttributeNS(null, "x", x);
label.setAttributeNS(null, "y", y);
Expand All @@ -197,17 +210,26 @@ export function drawPoint (x, y, groupTemplate, JSONcontainer, svgContainer, lab
* @param {number} width
* @param {number} height
* @param {string} className
* @param {Object} JSONcontainer
* @param {Object} svgContainer
* @param {object} JSONcontainer
* @param {object} svgContainer
* @param {string} style
*/
export function drawBar (x, y, width, height, className, JSONcontainer, svgContainer, style) {
export function drawBar(
x,
y,
width,
height,
className,
JSONcontainer,
svgContainer,
style
) {
if (height != 0) {
if (height < 0) {
height *= -1;
y -= height;
}
const rect = getSVGElement('rect',JSONcontainer, svgContainer);
const rect = getSVGElement("rect", JSONcontainer, svgContainer);
rect.setAttributeNS(null, "x", x - 0.5 * width);
rect.setAttributeNS(null, "y", y);
rect.setAttributeNS(null, "width", width);
Expand Down
33 changes: 18 additions & 15 deletions lib/hammerUtil.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
/**
* Register a touch event, taking place before a gesture
*
* @param {Hammer} hammer A hammer instance
* @param {function} callback Callback, called as callback(event)
* @param {Function} callback Callback, called as callback(event)
*/
export function onTouch (hammer, callback) {
export function onTouch(hammer, callback) {
callback.inputHandler = function (event) {
if (event.isFirst) {
callback(event);
}
};

hammer.on('hammer.input', callback.inputHandler);
hammer.on("hammer.input", callback.inputHandler);
}

/**
* Register a release event, taking place after a gesture
*
* @param {Hammer} hammer A hammer instance
* @param {function} callback Callback, called as callback(event)
* @param {Function} callback Callback, called as callback(event)
* @returns {*}
*/
export function onRelease (hammer, callback) {
export function onRelease(hammer, callback) {
callback.inputHandler = function (event) {
if (event.isFinal) {
callback(event);
}
};

return hammer.on('hammer.input', callback.inputHandler);
return hammer.on("hammer.input", callback.inputHandler);
}


/**
* Unregister a touch event, taking place before a gesture
*
* @param {Hammer} hammer A hammer instance
* @param {function} callback Callback, called as callback(event)
* @param {Function} callback Callback, called as callback(event)
*/
export function offTouch (hammer, callback) {
hammer.off('hammer.input', callback.inputHandler);
export function offTouch(hammer, callback) {
hammer.off("hammer.input", callback.inputHandler);
}

/**
* Unregister a release event, taking place before a gesture
*
* @param {Hammer} hammer A hammer instance
* @param {function} callback Callback, called as callback(event)
* @param {Function} callback Callback, called as callback(event)
*/
export const offRelease = offTouch;

Expand All @@ -53,12 +56,12 @@ export const offRelease = offTouch;
* Yeah ... this is quite a hack ... see https://github.com/hammerjs/hammer.js/issues/932
*
* @param {Hammer.Pinch} pinchRecognizer
* @return {Hammer.Pinch} returns the pinchRecognizer
* @returns {Hammer.Pinch} returns the pinchRecognizer
*/
export function disablePreventDefaultVertically (pinchRecognizer) {
const TOUCH_ACTION_PAN_Y = 'pan-y';
export function disablePreventDefaultVertically(pinchRecognizer) {
const TOUCH_ACTION_PAN_Y = "pan-y";

pinchRecognizer.getTouchAction = function() {
pinchRecognizer.getTouchAction = function () {
// default method returns [TOUCH_ACTION_NONE]
return [TOUCH_ACTION_PAN_Y];
};
Expand Down
2 changes: 1 addition & 1 deletion lib/index-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const network = {
gephiParser,
allOptions,
convertDot: dotparser.DOTToGraph,
convertGephi: parseGephi
convertGephi: parseGephi,
};

// utils
Expand Down
6 changes: 3 additions & 3 deletions lib/module/hammer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function hammerMock() {
destroy: noop,
emit: noop,

get: function(m) { //eslint-disable-line no-unused-vars
get() {
return {
set: noop
set: noop,
};
}
},
};
}

Expand Down
Loading

0 comments on commit 7c18684

Please sign in to comment.