≡
altersoftware.IT
altersoftware.IT
altersoftware.IT

jslibs/js-as-core

AS object, exposes methods

Basic JavaScript library from AlterSoftware, used also by other JavaScript modules.

Load

Basic usage, loading from Altersoftware server:

<script type="text/javascript" src="https://altersoftware.it/products/jslibs/js-as-core/as-core.js" charset="UTF-8"></script>

Download

All downloads are refreshed daily from the latest stable branch of Altersoftware GIT repository.

Core types prototype extension

Array

Array().clone()

Creates a clone of a data array, including neted arrays and/or objects.

Note: Array shouldn't contain any code reference, leafs can only be string or numbers.

Number

Number().smartSize()

Converts bytes integer into smart IT representations

console.log(Number(12334445566).smartSize());
// returns --> "11.5 GB"

console.log(Number(4200).smartSize());
// returns --> "4.1 kB"

String

String().dehtml()

Returns the string removing HTML tags and quotes, if any.

Use:

console.log('Hello, <b>today</b> 3 > 2 is "true"!'.dehtml());

// returns --> "Hello, today 3 > 2 is true !"

String().escape()

Returns the string escaping HTML tags, if any.

Use:

console.log('Hello, <b>today</b> 3 > 2 is "true"!'.escape());

// returns --> "Hello, &lt;b&gt;today&lt;/b&gt; 3 &gt; 2 is \"true\"!"

String().escapeDate()

Returns the string parsing it as a SQL timestamp, in a smart format.

Use:

console.log('2021-01-15 03:05:10'.escapeDate());

// returns --> "15.1.21, 3:05"

String().shorten()

Returns the string shortened.

A single object parameter is allowed, for options in the form of key/value.
Options and all keys are optional, and have a default value.

console.log('Hello, <b>today</b> 3 > 2 is "true"!'.shorten());
// returns --> "Hello, <b>today</b> 3 > 2 is \"true\"!"

console.log('Hello, <b>today</b> 3 > 2 is "true"!'.shorten({html:true, max:16});
// returns --> "Hello, today 3…"

String().trim()

Returns the string trimming leading and trailing white spaces, if any.

Methods always available

AS.addClass( obj, className )

Adds a class to one or more DOM nodes.

Requires two parameters:

  1. obj (mandatory) — a DOM node, an array of DOM nodes or a CSS selector string.
  2. className (String, mandatory) — the name of the class to be added to the node(s).

Note: in case jQuery is loaded this method relies on $(obj).addClass(className)

AS.args2array( argumentsOrNodeList )

Returns the arguments of a function or a NodeList as a regular array.

function delayer() {
    // Call a funcion in timeout
    // first argument: function name
    // then every other argument.
    let ags = AS.args2array(arguments);
    let funcName = ags.shift();
    return setTimeout( function(){ window[funcName].apply(window,ags); }, 100);
}
// or the equivalent of AS.getNodes('div'):
let arrayOfNodes = AS.args2array( document.querySelectorAll('div') );

AS.def.xxx( obj )

Returns default value for a tested object.

AS.getClasses( obj )

Accepts single parameter, a DOM node.

Returns an array of class names.

AS.getNodes( param )

Accepts single parameter, string, mandatory, a CSS selector. Returns an array of DOM nodes.

If a DOM node is passed as parameter, returns an array of one DOM node.

AS.generateId( [base] )

Return a random generated ID, optionally with a “base”.

console.log(AS.generateId());
// returns --> "asTempId7254287696242913"

console.log(AS.generateId('tempDiv'));
// returns --> "tempDiv5632452879070909"

AS.path( key )

Set path strings, or return specific path strings.

Default values are:

{
    webroot : '/',
    currentstyle : '/style:default',
    allstyles : '/style:'
}

If the argument is a string it returns the corresponding path.
If the argument is a key/value Oject it's used to set path values, and returns the path Object.

path values can be defined in the page also using META tags, e.g.:

<meta name="AS.path.webroot" content="/myapp"/>
<meta name="AS.path.currentstyle" content="/myapp/styles/mystyle"/>
<meta name="AS.path.allstyles" content="/myapp/styles/"/>
<meta name="AS.path.labels" content="/myapp/locales/mypage-" />

AS.rmClass( obj, className )

Removes a class from one or more DOM nodes.

Requires two parameters:

  1. obj (mandatory) — a DOM node, an array of DOM nodes or a CSS selector string.
  2. className (String, mandatory) — the name of the class to be removed from the node(s).

Note: in case jQuery is loaded this method relies on $(obj).removeClass(className)

AS.toggleClass( obj, className )

Toggles a class from one or more DOM nodes.

Requires two parameters:

  1. obj (mandatory) — a DOM node, an array of DOM nodes or a CSS selector string.
  2. className (String, mandatory) — the name of the class to be toggled in the node(s).

Note: in case jQuery is loaded this method relies on $(obj).toggleClass(className)

AS.test.xxx( obj )

Tests if a variable is a specific format, returns a Boolean.

Please note:

Dealing with events

AS object offers a few methods wrapping standard events command:

AS.addEvent( DOMnode, eventType, function, [key] )

Adds an event listener to DOMnode, if possible.

Works exactly like DOMnode.addEventListener( eventType, function ), actually wraps it, in addition:

Notes:

AS.addEventIfAny( DOMnode, eventType, function, [key] )

Adds an event listener to DOMnode, if possible, only if there’s not an event of type eventType already defined for DOMnode with any key.

Notes:

AS.addEventIfNot( DOMnode, eventType, function, [key] )

Adds an event listener to DOMnode, if possible, only if there’s not an event of type eventType already defined for DOMnode with key.

Notes:

AS.listEvents( DOMnode, [eventType], [key] )

If eventType and key are provided returns an array of function event handlers defined for that DOMnode on eventType for key.

If eventType is provided returns an object: keys are key properties, each value is the related above mentioned array.

In case eventType and key aren’t provided returns an object: keys are eventTypes, each value is the related above mentioned object.

Notes:

AS.triggerEvent( DOMnode, eventType, [data] )

Triggers an event, standard or custom, on DOMnode.

Custom events must contain the ':' character to be recognized, it’s good practice to use it as a kind of "namespace" (e.g.: as:change).

In case a CustomEvent is triggered it can optionally carry additional data, that will be received by event handlers in event.detail property.

Methods available if jQuery is loaded before as-core.js

$.postJSON( url, [data], [callback] )

Extends jQuery with the $.postJSON() method.

This method invokes a remote method and passes in the body of the request the serialized JSON of parameters, rather than a regular form.
contentType of the request is set to application/json,

Parameters are:

  1. url (string, mandatory) — the URL of the resource to be POSTed
  2. data (object, optional) — the key/value object of parameters
  3. callback (function, optional) — if defined the responde will be passed to such function

AS.id( obj )

Returns the ID of an object, in case it hasn't an ID attribute it's generated on the fly and returned.

Accepts one or two parameters:

  1. object (DOM or jQuery object, mandatory) — the object.
  2. idBase (string, optional) — In case the ID should be generated this will be the ID base passed to AS.generateId().

AS.toj( param, [context] )

Returns a jQuery object (if exists) or false.

Accepts one or two parameters:

  1. identifier (string, or DOM or jQuery object, mandatory) — In case of string it's expected to be the ID of the node, with or without the leading #.
  2. context (string, or DOM or jQuery object, optional) — The scope of the object search is limited to this object context. In case of String follows the same rules as identifier.

AS.ws( uri, [data], [callback], [type] )

POSTs to a remote WebService and returns the response to a callback.

Parameters accepted are up to 4, in whatever order:

  1. URI (String, mandarory) — The URI of the remote WebService (relative is fine).
  2. data (key/value object, optional) — The JavaScript object with parameters. In case data is a String with serialized FORM data provide it only after the URI.
  3. callback (function, optional, default: no callback) — The function object that will receive the response.
  4. type (String, optional, default: type:post) — Only accepts literal values type:post, type:get or type:json. Use this parameter with type:json to force the use of $.postJSON().

In case data is undefined then a rnd parameter is added (to avoid cache issues) populated with a random number.

Note: for both POST and GET data will be a plain key/value representation, like in a form.
When data parameter is a JavaScript object, then in case one of its keys starts with "j" and its value is an object (rather than to a scalar value, Number or String), then the related value will become the JSON String of the object.
This can be very useful if you want to pass to the remote WebService a parameter containing a JSON object.

AS.wslist( array )

Accepts an array of array.

Each array item is an array with up to 4 entries (AS.ws() parameters) that will be used to invoke sequentially AS.ws().

When a request is fullfilled, including the callback if any, the following WebService is processed.