init
This commit is contained in:
2
jdenticon-js/src/apis/configure.js
Normal file
2
jdenticon-js/src/apis/configure.js
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
export { configure } from "../common/configuration";
|
||||
28
jdenticon-js/src/apis/drawIcon.js
Normal file
28
jdenticon-js/src/apis/drawIcon.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { iconGenerator } from "../renderer/iconGenerator";
|
||||
import { isValidHash, computeHash } from "../common/hashUtils";
|
||||
import { CanvasRenderer } from "../renderer/canvas/canvasRenderer";
|
||||
import { IS_RENDERED_PROPERTY } from "../common/dom";
|
||||
|
||||
/**
|
||||
* Draws an identicon to a context.
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context on which the icon will be drawn at location (0, 0).
|
||||
* @param {*} hashOrValue - A hexadecimal hash string or any value that will be hashed by Jdenticon.
|
||||
* @param {number} size - Icon size in pixels.
|
||||
* @param {Object|number=} config - Optional configuration. If specified, this configuration object overrides any
|
||||
* global configuration in its entirety. For backward compatibility a padding value in the range [0.0, 0.5) can be
|
||||
* specified in place of a configuration object.
|
||||
*/
|
||||
export function drawIcon(ctx, hashOrValue, size, config) {
|
||||
if (!ctx) {
|
||||
throw new Error("No canvas specified.");
|
||||
}
|
||||
|
||||
iconGenerator(new CanvasRenderer(ctx, size),
|
||||
isValidHash(hashOrValue) || computeHash(hashOrValue),
|
||||
config);
|
||||
|
||||
const canvas = ctx.canvas;
|
||||
if (canvas) {
|
||||
canvas[IS_RENDERED_PROPERTY] = true;
|
||||
}
|
||||
}
|
||||
18
jdenticon-js/src/apis/jquery.js
vendored
Normal file
18
jdenticon-js/src/apis/jquery.js
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
import { update } from "./update";
|
||||
|
||||
/**
|
||||
* Renders an identicon for all matching supported elements.
|
||||
*
|
||||
* @param {*} hashOrValue - A hexadecimal hash string or any value that will be hashed by Jdenticon. If not
|
||||
* specified the `data-jdenticon-hash` and `data-jdenticon-value` attributes of each element will be
|
||||
* evaluated.
|
||||
* @param {Object|number=} config - Optional configuration. If specified, this configuration object overrides any global
|
||||
* configuration in its entirety. For backward compatibility a padding value in the range [0.0, 0.5) can be
|
||||
* specified in place of a configuration object.
|
||||
*/
|
||||
export function jdenticonJqueryPlugin(hashOrValue, config) {
|
||||
this["each"](function (index, el) {
|
||||
update(el, hashOrValue, config);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
24
jdenticon-js/src/apis/toPng.js
Normal file
24
jdenticon-js/src/apis/toPng.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import canvasRenderer from "canvas-renderer";
|
||||
import { iconGenerator } from "../renderer/iconGenerator";
|
||||
import { isValidHash, computeHash } from "../common/hashUtils";
|
||||
import { CanvasRenderer } from "../renderer/canvas/canvasRenderer";
|
||||
|
||||
/**
|
||||
* Draws an identicon as PNG.
|
||||
* @param {*} hashOrValue - A hexadecimal hash string or any value that will be hashed by Jdenticon.
|
||||
* @param {number} size - Icon size in pixels.
|
||||
* @param {Object|number=} config - Optional configuration. If specified, this configuration object overrides any
|
||||
* global configuration in its entirety. For backward compatibility a padding value in the range [0.0, 0.5) can be
|
||||
* specified in place of a configuration object.
|
||||
* @returns {Buffer} PNG data
|
||||
*/
|
||||
export function toPng(hashOrValue, size, config) {
|
||||
const canvas = canvasRenderer.createCanvas(size, size);
|
||||
const ctx = canvas.getContext("2d");
|
||||
|
||||
iconGenerator(new CanvasRenderer(ctx, size),
|
||||
isValidHash(hashOrValue) || computeHash(hashOrValue),
|
||||
config);
|
||||
|
||||
return canvas.toPng({ "Software": "Jdenticon" });
|
||||
}
|
||||
21
jdenticon-js/src/apis/toSvg.js
Normal file
21
jdenticon-js/src/apis/toSvg.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { iconGenerator } from "../renderer/iconGenerator";
|
||||
import { isValidHash, computeHash } from "../common/hashUtils";
|
||||
import { SvgRenderer } from "../renderer/svg/svgRenderer";
|
||||
import { SvgWriter } from "../renderer/svg/svgWriter";
|
||||
|
||||
/**
|
||||
* Draws an identicon as an SVG string.
|
||||
* @param {*} hashOrValue - A hexadecimal hash string or any value that will be hashed by Jdenticon.
|
||||
* @param {number} size - Icon size in pixels.
|
||||
* @param {Object|number=} config - Optional configuration. If specified, this configuration object overrides any
|
||||
* global configuration in its entirety. For backward compatibility a padding value in the range [0.0, 0.5) can be
|
||||
* specified in place of a configuration object.
|
||||
* @returns {string} SVG string
|
||||
*/
|
||||
export function toSvg(hashOrValue, size, config) {
|
||||
const writer = new SvgWriter(size);
|
||||
iconGenerator(new SvgRenderer(writer),
|
||||
isValidHash(hashOrValue) || computeHash(hashOrValue),
|
||||
config);
|
||||
return writer.toString();
|
||||
}
|
||||
149
jdenticon-js/src/apis/update.js
Normal file
149
jdenticon-js/src/apis/update.js
Normal file
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* Jdenticon
|
||||
* https://github.com/dmester/jdenticon
|
||||
* Copyright © Daniel Mester Pirttijärvi
|
||||
*/
|
||||
|
||||
import { iconGenerator } from "../renderer/iconGenerator";
|
||||
import { isValidHash, computeHash } from "../common/hashUtils";
|
||||
import { ATTRIBUTES, ICON_SELECTOR, IS_RENDERED_PROPERTY, documentQuerySelectorAll } from "../common/dom";
|
||||
import { SvgRenderer } from "../renderer/svg/svgRenderer";
|
||||
import { SvgElement } from "../renderer/svg/svgElement";
|
||||
import { CanvasRenderer } from "../renderer/canvas/canvasRenderer";
|
||||
import { ICON_TYPE_CANVAS, ICON_TYPE_SVG, getIdenticonType } from "../common/dom";
|
||||
|
||||
|
||||
/**
|
||||
* Updates all canvas elements with the `data-jdenticon-hash` or `data-jdenticon-value` attribute.
|
||||
*/
|
||||
export function updateAll() {
|
||||
if (documentQuerySelectorAll) {
|
||||
update(ICON_SELECTOR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates all canvas elements with the `data-jdenticon-hash` or `data-jdenticon-value` attribute that have not already
|
||||
* been rendered.
|
||||
*/
|
||||
export function updateAllConditional() {
|
||||
if (documentQuerySelectorAll) {
|
||||
/** @type {NodeListOf<HTMLElement>} */
|
||||
const elements = documentQuerySelectorAll(ICON_SELECTOR);
|
||||
|
||||
for (let i = 0; i < elements.length; i++) {
|
||||
const el = elements[i];
|
||||
if (!el[IS_RENDERED_PROPERTY]) {
|
||||
update(el);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the identicon in the specified `<canvas>` or `<svg>` elements.
|
||||
* @param {(string|Element)} el - Specifies the container in which the icon is rendered as a DOM element of the type
|
||||
* `<svg>` or `<canvas>`, or a CSS selector to such an element.
|
||||
* @param {*=} hashOrValue - Optional hash or value to be rendered. If not specified, the `data-jdenticon-hash` or
|
||||
* `data-jdenticon-value` attribute will be evaluated.
|
||||
* @param {Object|number=} config - Optional configuration. If specified, this configuration object overrides any
|
||||
* global configuration in its entirety. For backward compability a padding value in the range [0.0, 0.5) can be
|
||||
* specified in place of a configuration object.
|
||||
*/
|
||||
export function update(el, hashOrValue, config) {
|
||||
renderDomElement(el, hashOrValue, config, function (el, iconType) {
|
||||
if (iconType) {
|
||||
return iconType == ICON_TYPE_SVG ?
|
||||
new SvgRenderer(new SvgElement(el)) :
|
||||
new CanvasRenderer(/** @type {HTMLCanvasElement} */(el).getContext("2d"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the identicon in the specified `<canvas>` elements.
|
||||
* @param {(string|Element)} el - Specifies the container in which the icon is rendered as a DOM element of the type
|
||||
* `<canvas>`, or a CSS selector to such an element.
|
||||
* @param {*=} hashOrValue - Optional hash or value to be rendered. If not specified, the `data-jdenticon-hash` or
|
||||
* `data-jdenticon-value` attribute will be evaluated.
|
||||
* @param {Object|number=} config - Optional configuration. If specified, this configuration object overrides any
|
||||
* global configuration in its entirety. For backward compability a padding value in the range [0.0, 0.5) can be
|
||||
* specified in place of a configuration object.
|
||||
*/
|
||||
export function updateCanvas(el, hashOrValue, config) {
|
||||
renderDomElement(el, hashOrValue, config, function (el, iconType) {
|
||||
if (iconType == ICON_TYPE_CANVAS) {
|
||||
return new CanvasRenderer(/** @type {HTMLCanvasElement} */(el).getContext("2d"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the identicon in the specified `<svg>` elements.
|
||||
* @param {(string|Element)} el - Specifies the container in which the icon is rendered as a DOM element of the type
|
||||
* `<svg>`, or a CSS selector to such an element.
|
||||
* @param {*=} hashOrValue - Optional hash or value to be rendered. If not specified, the `data-jdenticon-hash` or
|
||||
* `data-jdenticon-value` attribute will be evaluated.
|
||||
* @param {Object|number=} config - Optional configuration. If specified, this configuration object overrides any
|
||||
* global configuration in its entirety. For backward compability a padding value in the range [0.0, 0.5) can be
|
||||
* specified in place of a configuration object.
|
||||
*/
|
||||
export function updateSvg(el, hashOrValue, config) {
|
||||
renderDomElement(el, hashOrValue, config, function (el, iconType) {
|
||||
if (iconType == ICON_TYPE_SVG) {
|
||||
return new SvgRenderer(new SvgElement(el));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the identicon in the specified canvas or svg elements.
|
||||
* @param {(string|Element)} el - Specifies the container in which the icon is rendered as a DOM element of the type
|
||||
* `<svg>` or `<canvas>`, or a CSS selector to such an element.
|
||||
* @param {*} hashOrValue - Optional hash or value to be rendered. If not specified, the `data-jdenticon-hash` or
|
||||
* `data-jdenticon-value` attribute will be evaluated.
|
||||
* @param {Object|number|undefined} config
|
||||
* @param {function(Element,number):import("../renderer/renderer").Renderer} rendererFactory - Factory function for creating an icon renderer.
|
||||
*/
|
||||
function renderDomElement(el, hashOrValue, config, rendererFactory) {
|
||||
if (typeof el === "string") {
|
||||
if (documentQuerySelectorAll) {
|
||||
const elements = documentQuerySelectorAll(el);
|
||||
for (let i = 0; i < elements.length; i++) {
|
||||
renderDomElement(elements[i], hashOrValue, config, rendererFactory);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Hash selection. The result from getValidHash or computeHash is
|
||||
// accepted as a valid hash.
|
||||
const hash =
|
||||
// 1. Explicit valid hash
|
||||
isValidHash(hashOrValue) ||
|
||||
|
||||
// 2. Explicit value (`!= null` catches both null and undefined)
|
||||
hashOrValue != null && computeHash(hashOrValue) ||
|
||||
|
||||
// 3. `data-jdenticon-hash` attribute
|
||||
isValidHash(el.getAttribute(ATTRIBUTES.HASH)) ||
|
||||
|
||||
// 4. `data-jdenticon-value` attribute.
|
||||
// We want to treat an empty attribute as an empty value.
|
||||
// Some browsers return empty string even if the attribute
|
||||
// is not specified, so use hasAttribute to determine if
|
||||
// the attribute is specified.
|
||||
el.hasAttribute(ATTRIBUTES.VALUE) && computeHash(el.getAttribute(ATTRIBUTES.VALUE));
|
||||
|
||||
if (!hash) {
|
||||
// No hash specified. Don't render an icon.
|
||||
return;
|
||||
}
|
||||
|
||||
const renderer = rendererFactory(el, getIdenticonType(el));
|
||||
if (renderer) {
|
||||
// Draw icon
|
||||
iconGenerator(renderer, hash, config);
|
||||
el[IS_RENDERED_PROPERTY] = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user