Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typings #16

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/domini-core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/domini-highlight.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/domini-xhttp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/domini.js

Large diffs are not rendered by default.

235 changes: 235 additions & 0 deletions domini.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
declare module "domini" {
type ElementWithFields = Element&{[otherFields: string]: unknown;};

interface DOMini extends Array<ElementWithFields> {
(selector?: string|Element): this;
fn: {
_: (selector: string) => Array<ElementWithFields>,

ajax: (args: {
'url': string,
'method'?: XMLHttpRequest.HttpMethod,
'cors'?: 'cors'|'no-cors', // cors, no-cors
'data'?: unknown,
'success'?: (response_text: string) => void,
'fail'?: (request: XMLHttpRequest) => void,
'accept'?: string,
'contentType'?: XMLHttpRequest.ContentType
} = {
'url': '',
'method': 'GET',
'cors': 'cors', // cors, no-cors
'data': {},
'success': null,
'fail': null,
'accept': 'text/html',
'contentType': 'application/x-www-form-urlencoded; charset=UTF-8'
}) => void,
},
_fn: {
plugin: (name: string, object: Object) => this,

bodyTransform: () => {
top: number,
left: number,
},

bodyTransformY: () => number,

bodyTransformX: () => number,

hasFixedParent: (element: Element) => boolean,

hasEventListener: (element: Element, event_type: string, func: Function) => boolean,

allDescendants: (element: Element) => Array<Element>,

createElementsFromHTML: (html_string: string) => Array<Element>,

elementArrayFromAny: (anything: string|Element|Element[]|DOMini) => Array<Element>,

absolutePosition: (element: Element) => {
top: number,
left: number,
},
}
add: (selector: string|Element) => this,

css: {
(properties: Record<string, unknown>): this;
(property: string, value: unknown): this;
},

hasClass: (className: string) => boolean,

addClass: (className: string) => this,

removeClass: (className: string) => this,

/**
* Checks if element is visible
*/
isVisible: () => boolean,

val: {
(value: string|number|string[]|number[]): this;
(): string|number|string[]|number[];
},

attr: {
(attributes: Record<string, unknown>): this;
(attribute: string, value: unknown): this;
(attribute: string): unknown;
},

removeAttr: (attribute: string) => this,

prop: {
(attribute: string, value: unknown): this;
(attribute: string): unknown;
},

data: {
(key: string, value: unknown): this;
(key: string): string;
},

html: {
(html: string): this;
(): string;
},

text: {
(text: string): this;
(): string;
},

extend: (...args: Object) => Object,

each: (callback: (index?: number, node?: ElementWithFields, array?: ElementWithFields[])=>unknown) => this

forEach: (callback: (node?: ElementWithFields, index?: number, array?: ElementWithFields[])=>unknown) => this

get: (n: number) => ElementWithFields,

offset: ()=> {
top: number,
left: number,
},

position: () => {
top: number,
left: number,
},

outerWidth: (includeMargin?: boolean = false) => number,
outerHeight: (includeMargin?: boolean = false) => number,

noPaddingWidth: (includeMargin?: boolean = false) => number,
noPaddingHeight: (includeMargin?: boolean = false) => number,

innerWidth: () => number,
innerHeight: () => number,

/**
* Same as `outerWidth(false)`
*/
width: () => number,

/**
* Same as `outerHeight(false)`
*/
height: () => number,

on: {
(events: string, callback: (event:Event)=>void): this,
(events: string, selector: string, callback: (event:Event)=>void): this,
},

off: {
(): this,
(events: string): this,
(events: string, callback: Function): this
},

offForced: ()=> this,

trigger: (event: string, args:unknown[], native: boolean = false, jquery: boolean = false) => this,

/**
* Removes all DoMini related event listeners
*/
clear: () => this,

clone: () => this,

detach: (context?: string|Element) => this,

remove: (context?: string|Element) => this,

prepend: (prepend: string|Element|Element[]|DOMini) => this,

append: (prepend: string|Element|Element[]|DOMini) => this,

is: (selectors: string) => boolean,

parent: (selectors?: string) => this,

copy: (source: Object<unknown>|Array<unknown>, deep?: boolean = false) => typeof source,

first: () => this,

last: () => this,

prev: (selectors?: string) => this,

next: (selectors?: string) => this,

closest: (selectors?: string|Element|DOMini) => this,

find: (selectors?: string) => this,

animate: {
(props: false|Record<string, number>, duration?: number = 200, easing?: keyof DOMini.animate.easing),
easing: Record<string, (x: number)=>number>,
},

unhighlight: (options?: {
className?: string,
element?: 'span'
} = {
className: 'highlight',
element: 'span'
}) => this,

highlight: (word: string, options?: {
className?: string,
element?: string,
caseSensitive?: boolean,
wordsOnly?: boolean,
excludeParents?: string
} = {
className: 'highlight',
element: 'span',
caseSensitive: false,
wordsOnly: false,
excludeParents: '.excludeFromHighlight'
}) => this,

serialize: () => string,

serializeObject: (object: Object<unknown>, prefix: string) => string,

inViewPort: (tolerance: number = 0, viewport: Element = window) => boolean,

/**
* Addons and other possibly dynamically added fields
*/
[otherFields: string]: (...args:unknown)=>this | unknown;
}

import {DOMini} from "./types";
declare const DoMini: DOMini;

export=DoMini;
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "domini",
"version": "0.2.4",
"version": "0.2.5",
"description": "Minimalistic DOM manipulation tool",
"main": "dist/domini.js",
"types": "domini.d.ts",
"scripts": {
"build": "webpack --mode=production --node-env=production",
"test": "nodemon ./test/server.js",
Expand All @@ -23,6 +24,8 @@
],
"files": [
"dist",
"types.d.ts",
"domini.d.ts",
"examples.html",
"readme.md"
],
Expand Down
14 changes: 9 additions & 5 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
* @returns {DoMini|void}
*/

let DoMini;

// Prevent overloading
if ( typeof window.DoMini == 'undefined' ) {

var DoMini = function(selector, node) {
DoMini = function(selector, node) {
// Actual constructor when new Domini() is called
if ( typeof arguments[2] !== 'undefined' ) {
return this.constructor.call(this, selector, node);
}

//if ( arguments.length >= 1 ) {
// Case of DoMini(function($){..})
if ( arguments.length == 1 && typeof arguments[0] == 'function' ) {
if ( arguments.length === 1 && typeof arguments[0] == 'function' ) {
if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") {
arguments[0].apply(this, [DoMini]);
} else {
Expand All @@ -44,7 +46,7 @@ if ( typeof window.DoMini == 'undefined' ) {
return DoMini(node).find(s);
}
} else {
if (typeof s == "string" && s != '') {
if (typeof s == "string" && s !== '') {
this.push(...this._(s));
} else {
if ( s instanceof DoMini ) {
Expand Down Expand Up @@ -76,16 +78,18 @@ if ( typeof window.DoMini == 'undefined' ) {
push: Array.prototype.push,
pop: Array.prototype.pop,
sort: Array.prototype.sort,
splice: Array.prototype.splice
splice: Array.prototype.splice,
}

// Define the iterator symbol to the array iterator, allows "for of.." and derivates
DoMini.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];

// Utility functions container
DoMini._fn = {};

DoMini.version = "0.2.5";
} else {
var DoMini = window.DoMini;
DoMini = window.DoMini;
}

export default DoMini;
8 changes: 4 additions & 4 deletions src/core/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DoMini from "../base";

DoMini.fn.css = function(prop, v) {
for ( const el of this ) {
if ( arguments.length == 1 ) {
if ( arguments.length === 1 ) {
if ( typeof prop == "object" ) {
Object.keys(prop).forEach(function(k){
el.style[k] = prop[k];
Expand Down Expand Up @@ -67,9 +67,9 @@ DoMini.fn.isVisible = function() {
while (el !== null) {
style = window.getComputedStyle(el);
if (
style['display'] == 'none' ||
style['visibility'] == 'hidden' ||
style['opacity'] == 0
style['display'] === 'none' ||
style['visibility'] === 'hidden' ||
parseInt(style['opacity']) === 0
) {
visible = false;
break;
Expand Down
Loading