Skip to content

Commit bffa2c0

Browse files
authored
fix(mvc.$): add missing filter() method, fix html() to accept numbers (#2468)
1 parent 5a6bd96 commit bffa2c0

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

packages/joint-core/src/mvc/Dom/Dom.mjs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ $.fn.addBack = function() {
121121
return this.add(this.prevObject);
122122
};
123123

124+
$.fn.filter = function(selector) {
125+
const matches = [];
126+
for (let i = 0; i < this.length; i++) {
127+
const node = this[i];
128+
if (!node.matches(selector)) continue;
129+
matches.push(node);
130+
}
131+
return this.pushStack(matches);
132+
};
133+
124134
// A simple way to check for HTML strings
125135
// Prioritize #id over <tag> to avoid XSS via location.hash (trac-9521)
126136
// Strict HTML recognition (trac-11290: must start with <)
@@ -200,8 +210,11 @@ $.event = {
200210
special: Object.create(null),
201211
};
202212

203-
$.event.has = function(elem) {
204-
return dataPriv.has(elem, 'events');
213+
$.event.has = function(elem, eventType) {
214+
const events = dataPriv.get(elem, 'events');
215+
if (!events) return false;
216+
if (!eventType) return true;
217+
return Array.isArray(events[eventType]) && events[eventType].length > 0;
205218
};
206219

207220
$.event.on = function(elem, types, selector, data, fn, one) {

packages/joint-core/src/mvc/Dom/methods.mjs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import { dataPriv, cleanNodesData } from './vars.mjs';
44

55
// Manipulation
66

7-
export function remove() {
8-
for (let i = 0; i < this.length; i++) {
9-
const node = this[i];
10-
dataPriv.remove(node);
7+
function removeNodes(nodes, removeData) {
8+
for (let i = 0; i < nodes.length; i++) {
9+
const node = nodes[i];
10+
removeData && dataPriv.remove(node);
1111
if (node.parentNode) {
1212
node.parentNode.removeChild(node);
1313
}
1414
}
1515
}
1616

17+
export function remove() {
18+
removeNodes(this, true);
19+
return this;
20+
}
21+
22+
export function detach() {
23+
removeNodes(this, false);
24+
return this;
25+
}
26+
1727
export function empty() {
1828
for (let i = 0; i < this.length; i++) {
1929
const node = this[i];
@@ -31,7 +41,7 @@ export function html(html) {
3141
if (!el) return null;
3242
if (!html) return el.innerHTML;
3343
cleanNodesData(dataPriv, el.getElementsByTagName('*'));
34-
if (typeof string === 'string') {
44+
if (typeof string === 'string' || typeof string === 'number') {
3545
el.innerHTML = html;
3646
} else {
3747
el.innerHTML = '';

packages/joint-core/types/joint.d.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ export namespace config {
1010

1111
type NativeEvent = Event;
1212

13-
/* A JQuery-like object */
14-
type Dom = any;
15-
1613
export namespace dia {
1714

1815
type Event = mvc.TriggeredEvent;
@@ -755,13 +752,13 @@ export namespace dia {
755752

756753
presentationAttributes(): CellView.PresentationAttributes;
757754

758-
highlight(el?: SVGElement | Dom | string, opt?: { [key: string]: any }): this;
755+
highlight(el?: mvc.$SVGElement, opt?: { [key: string]: any }): this;
759756

760-
unhighlight(el?: SVGElement | Dom | string, opt?: { [key: string]: any }): this;
757+
unhighlight(el?: mvc.$SVGElement, opt?: { [key: string]: any }): this;
761758

762759
can(feature: string): boolean;
763760

764-
findMagnet(el: SVGElement | Dom | string): SVGElement | undefined;
761+
findMagnet(el: mvc.$SVGElement): SVGElement | undefined;
765762

766763
findNode(selector: string): SVGElement | HTMLElement | null;
767764

@@ -821,7 +818,7 @@ export namespace dia {
821818

822819
isDefaultInteractionPrevented(evt: dia.Event): boolean;
823820

824-
protected findBySelector(selector: string, root?: SVGElement | Dom | string): SVGElement[];
821+
protected findBySelector(selector: string, root?: SVGElement): SVGElement[];
825822

826823
protected removeHighlighters(): void;
827824

@@ -1499,7 +1496,7 @@ export namespace dia {
14991496

15001497
getContentBBox(opt?: { useModelGeometry: boolean }): g.Rect;
15011498

1502-
findView<T extends ElementView | LinkView>(element: string | Dom | SVGElement): T;
1499+
findView<T extends ElementView | LinkView>(element: mvc.$SVGElement): T;
15031500

15041501
findViewByModel<T extends ElementView | LinkView>(model: Cell | Cell.ID): T;
15051502

@@ -2430,7 +2427,7 @@ export namespace util {
24302427
export function getElementBBox(el: Element): dia.BBox;
24312428

24322429
export function sortElements(
2433-
elements: Element[] | string | Dom,
2430+
elements: mvc.$Element,
24342431
comparator: (a: Element, b: Element) => number
24352432
): Element[];
24362433

@@ -2773,6 +2770,13 @@ export namespace layout {
27732770

27742771
export namespace mvc {
27752772

2773+
type Dom = unknown;
2774+
// The following types represent the DOM elements that can be passed to the
2775+
// $() function.
2776+
type $Element<T extends Element = Element> = string | T | T[] | Dom;
2777+
type $HTMLElement = $Element<HTMLElement>;
2778+
type $SVGElement = $Element<SVGElement>;
2779+
27762780
interface Event {
27772781
// Event
27782782
bubbles: boolean | undefined;
@@ -3160,7 +3164,7 @@ export namespace mvc {
31603164
model?: TModel | undefined;
31613165
// TODO: quickfix, this can't be fixed easy. The collection does not need to have the same model as the parent view.
31623166
collection?: Collection<any> | undefined; // was: Collection<TModel>;
3163-
el?: TElement | Dom | string | undefined;
3167+
el?: $Element<TElement> | string | undefined;
31643168
id?: string | undefined;
31653169
attributes?: Record<string, any> | undefined;
31663170
className?: string | undefined;
@@ -3196,7 +3200,7 @@ export namespace mvc {
31963200
// A conditional type used here to prevent `TS2532: Object is possibly 'undefined'`
31973201
model: TModel extends Model ? TModel : undefined;
31983202
collection: Collection<any>;
3199-
setElement(element: TElement | Dom): this;
3203+
setElement(element: $Element<TElement>): this;
32003204
id?: string | undefined;
32013205
cid: string;
32023206
className?: string | undefined;
@@ -3216,7 +3220,7 @@ export namespace mvc {
32163220
undelegate(eventName: string, selector?: string, listener?: ViewBaseEventListener): this;
32173221

32183222
protected _removeElement(): void;
3219-
protected _setElement(el: TElement | Dom): void;
3223+
protected _setElement(el: $Element<TElement>): void;
32203224
protected _createElement(tagName: string): void;
32213225
protected _ensureElement(): void;
32223226
protected _setAttributes(attributes: Record<string, any>): void;

0 commit comments

Comments
 (0)