A collection of helper functions used by o-comments and o-chat.
This module provides a useful way to handle application level configurations. It supports setting and reading configurations, also overriding existing values (helpful when the application should be working on different environments with partially different configuration).
In order to create a configuration, you should create an instance first:
var config1 = new oCommentUtilities.EnvConfig();
In order to keep an isolated configuration object of your application, which is accessible within your whole application, the following method can be used:
var EnvConfig = require('js-env-config');
module.exports = new EnvConfig();
Gets the whole configuration if without parameter, or a field if a field is given as parameter.
Getting the whole configuration object:
config.get();
Getting a field of the configuration object:
config.get('aField');
Sets the configuration. The current configuration is merged with this object. Existing fields that have primitive type values and the same field has value in the object given to set, are overwritten.
Setting an object:
config.set({
'configField': 'value'
});
Setting a key/value:
config.set('aField', {
'configField': 'value'
});
This is equivalent with the following object:
{
"aField": {
"configField": "value"
}
}
This module helps creating custom events, listening on custom events and triggering them. It is similar to jQuery's event system (except that it doesn't support namespacing).
The constructor has no parameter.
var myEvent = new Events();
Registers a new event handler to an event.
myEvent.on(event, handler)
Where:
Registers a new event handler to an event. Similar to 'on', but the handler is called only once.
myEvent.one(event, handler)
Removes one or more event handlers.
myEvent.off(event, handler)
Where:
If handler is omitted, all event handlers from the event specified are removed.
If both event and handler are omitted, all event handlers are removed.
Triggers an event: it causes calling all the event handlers that are listening for the event name.
myEvent.trigger(event, customData)
Where:
This module provides a similar solution for JSONP communication as jQuery. For more information on JSONP, visit https://www.w3schools.com/js/js_json_jsonp.asp .
The module is actually a single function. It should be called the following way:
commentUtilitis.jsonp({
url: "URL here",
data: "Data here" //optional
}, function (err, data) {
if (err) {
throw err;
}
// use the data
});
Both parameters (configuration object and callback) are required. Also, the configuration should contain the 'url' field.
If data should be sent, it can be part of the URL directly, or can be provided as the data field within the configuration object. The data field should be an object with key-value pairs.
The callback has a Node.js inspired form. The first parameter is the error parameter, while the second one is the data parameter received from the server as a response.
This module provides a similar solution for loading a Javascript file asynchronously as jQuery's $.getScript() (http://api.jquery.com/jquery.getscript/).
The module is actually a single function. It can be called the following ways:
1.
oCommentUtilities.scriptLoader({
url: "URL here",
charset: "utf-8" //optional
}, function (err) {
if (err) {
throw err;
}
// loaded successfully
});
2.
oCommentUtilities.scriptLoader("URL here", function (err) {
if (err) {
throw err;
}
// loaded successfully
});
Both parameters (configuration object/URL and callback) are required. Also, if the configuration is an object, it should contain the 'url' field.
The callback has a Node.js inspired form. The parameter is either 'null' or an Error instance. If it doesn't contain an error, the loading finished with success.
Wrapper around localStorage and sessionStorage, but enhanced with automatic type conversion.
Automatic type conversion means the followin: for example, if you store an object which can be serialized into a JSON string, when you read it back you will get the same Javascript object and not just a plain string.
The module exposes two main fields, both having the same API:
oCommentUtilities.storageWrapper.localStorage //wrapper around native localStorage
oCommentUtilities.storageWrapper.sessionStorage //wrapper around native sessionStorage
Both submodules has the same public API. The difference is only the place the data is saved (as it is in the native API).
Available methods:
Saves an item in the storage. It has the following form:
setItem(key, value);
The value is saved in the storage with the key. The value can be looked up using the key provided.
The item is saved in the following format:
{type}|{value converted/serialized to string}
For example, a boolean "true" value is saved in the following way:
boolean|true
Objects that can be serialized are saved with the type "json".
Reads the item and returns it in the original type that was saved.
getItem(key);
For example, if a boolean true was saved, it is not returned as a string, but as a boolean type.
Returns true or false and it says if there is an item with that key.
hasItem(key);
Removes the item saved with the key provided.
removeItem(key);
Clears all entries for the current domain.
Returns the native localStorage or sessionStorage object.
Logging helper which uses the native "console" to log. It also extends IE8 logging capabilities by stringifying complex objects.
Where console is not available, the logger fails silently.
The module supports the following methods:
The logger can be configured with the following: enable/disable, set the minimum level that is logged.
By default is logging disabled (recommended settings for production).
Default level of logging is 'warn'.
Enables the logging.
logger.enable();
Disables the logging.
logger.disable();
Sets the minimum level that should be logged. The levels are the following:
logger.setLevel(level);
Where level can be:
Available functions:
This submodule is meant to generate a callback only when all functions provided finished their execution. This is achieved by passing a callback as parameter to the functions that are executed.
The submodule itself is a single function and can be called in the following way:
oCommentUtilities.functionSync.parallel({
func1: function (callback) {},
func2: function (callback) {},
func3: {
args: ['param1', 'param2'],
func: function (callback, param1, param2) {}
}
}, function (dataAggregate) {
// where data aggregate is:
// {
// func1: 'dataFromFunc1',
// func2: 'dataFromFunc2',
// func2: 'dataFromFunc2'
// }
}
});
Functions provided within the object can be in the following forms:
For more information on the technical side, please visit the detailed documentation (docs/index.html).
This module is able to instantiate classes extended from o-comment-ui/Widget.js using markup in the DOM.
This feature reads the DOM for certain types of elements. An example element:
<div data-o-component="o-chat" id="commentWidget" data-o-chat-config-title="o-chat-test-closed3" data-o-chat-config-url="http://ftalphaville.ft.com/marketslive-test.html" data-o-chat-config-articleId="marketslive-test" data-o-chat-config-order="inverted"></div>
Key parts of the DOM element:
data-o-component
: defines the type of Widget element, in this example o-chat
.data-o-{modulename}-config-{key}
: configuration options that are passed to the Widget constructor. {key}
has the following rule: --
means new object level, -
means camel case. Example: data-o-comments-config-livefyre--data-format--absolute="value"
is transformed to: {"livefyre": {"dataFormat": {"absolute": "value"}}}
.In order to start the DOM construction, the oCommentUtilities.initDomConstruct
function should be called with a configuration object, which has the following fields:
data-o-component="{modulename}"
. If none is specified, it falls back to document.body
.'o-chat'
. It is used as class and also as part of the data attributes.'oChat'
.oChat
.data-{namespace}-auto-init="false"
(e.g. data-o-chat-init="false") will be considered. This is useful when there are two phases of initialization: one on o.DOMContentLoaded
and one on demand (lazy load). On o.DOMContentLoaded
there will be loaded only the widgets which don't have this attribute, while the others only on explicit call.Example:
var initDomConstructOnDemand = function () {
oCommentUtilities.initDomConstruct({
classNamespace: 'o-chat',
eventNamespace: 'oChat',
moduleRef: oChat,
classRef: oChat.Widget
});
}
document.addEventListener('o.DOMContentLoaded', function () {
oCommentUtilities.initDomConstruct({
classNamespace: 'o-chat',
eventNamespace: 'oChat',
moduleRef: oChat,
classRef: oChat.Widget,
auto: true
});
});
In some cases this module should be called on demand by the product, so it could be exposed as a public API as part of the module which uses it.
Example: (from o-chat/main.js)
var Widget = require('./src/javascripts/Widget.js');
exports.init = function (el) {
return oCommentUtilities.initDomConstruct({
context: el,
classNamespace: 'o-chat',
eventNamespace: 'oChat',
moduleRef: this,
classRef: Widget
});
};
This way all the configurations are abstracted, the product should not care about setting them.
If you want to obtain a reference of the created Class instances, you should listen on the body to the event {namespace}.ready
(e.g. oChat.ready), which will have the following details:
Example:
document.body.addEventListener('oChat.ready', function (evt) {
//evt.detail.id and evt.detail.instance are available
});
The instance that is already created will have a flag that says that it shouldn't be created again, even if initDomConstruct is called again. The flag that is set is data-{namespace}-built="true"
(e.g. data-o-chat-built="true"). If you are creating instances in another way (programatically), the container element should have this flag set.
The return value is an array with all the instances of Class
(e.g. oChat.Widget) created.
Helpers for working with cookies.
Reads a cookie by name.
oCommentUtilities.cookie.get('name');
Sets a cookie.
oCommentUtilities.cookie.set('name', 'value', 36 /*days */);
Removes a cookie
oCommentUtilities.cookie.remove('name');
ftUser is a helper for obtaining information about the FT user, information like logged in status, user ID, email address, session ID.
Methods exposed that can be used: