Utilities

Pack of usefull utilities used by other components.

Importing modules

MetaJS provides dynamic module imports.

Set import url

window.Meta.config.importUrl = '/path/to/%module%.html';

Meta.config.importUrl

Variable which specifies import url pattern. String %module% is replaced by module name.

Default value: modules/%module%.html

Import modules

window.Meta.Utils.import("my-module", function(){
    console.log("Imported!");
}, function(err){
    console.log("Import error", err);
});

Meta.Utils.import(module, onReady onError)

Imports specified module.

If onReady callback is provided then it is called when module has been sucessfully imported.

If onError callback is provided then it is called when module import failed.

Callback onError accepts error as first argument.

Import multiple modules

window.Meta.Utils.importMany([
    "my-module1",
    "my-module2",
    "my-module3"
], function(){
    console.log("Imported!");
}, function(err){
    console.log("Import error", err);
});

Meta.Utils.importMany(moduleList, onReady, onError)

Same as Meta.Utils.import but accepts moduleList argument as array of modules to import.

Other functions

Batch processing

var items = ["a", "b", "c"];

var process = function(el, next){
    console.log("Item:", el);
    next();
};

Meta.Utils.Batch(items, process, function(err){
    if(err)
        console.log("Error", err);
    else
        console.log("Done");
});

Meta.Utils.batch(list, processingFn, callback)

Function calls processingFn for each item in list array.

Argument processingFn must be a function which accepts list element as first argument and next function as second argument. It must call next function to continue batch processing.

Function next accepts error as first argument. When error is passed then batch is stopped and callback is called with error as first argument.

Otherwise callback is called when all items has been processed without error.

Creating hashmap

HTML:

<div id="parent">
    <p id="p1">P 1</p>
    <p id="p2">P 2</p>
</div>

JavaScript:

var map = Meta.Utils.idHashmap(document.getElementById('parent'));

map.p1.innerHTML === "P 1";
map.p2.innerHTML === "P 1";

Meta.Utils.idHashmap(targetElement)

Function goes throught targetElement child nodes and associates them to result object by it's ID attribute if ID set.

Sanitizing HTML contents

var html = '<p>Lorem ipsum</p>';
var text = Meta.Utils.sanitizeHtml(html);

text === '&lt;p&gt;Lorem ipsum&lt;/p&gt;';

Meta.Utils.sanitizeHtml(input)

Function returns input where html open and close tags are converted to HTML entities.

Traversing object by string path

var obj = {
    "abc": { "def": 123 }
}

Meta.Utils.traverse(obj, "abc.def") === 123;

Meta.Utils.traverse(o, path)

Function traverses object o by path array.

Formatting date

var now = Math.round(new Date("2015-01-02 12:43:10").getTime() / 1000);

Meta.Utils.formatDate("d. m. Y", now) === "2. 1. 2015";

Meta.Utils.formatDate(format, unixtime)

Port of PHP's date function.