-
Notifications
You must be signed in to change notification settings - Fork 4
V.5. Functions
- lord.isAudioType
- lord.isImageType
- lord.isVideoType
- lord.getCookie
- lord.setCookie
- lord.deleteCookie
- lord.getLocalObject
- lord.setLocalObject
- lord.removeLocalObject
- lord.getSessionObject
- lord.setSessionObject
- lord.removeSessionObject
- lord.hasOwnProperties
- lord.gently
- lord.regexp
- lord.id
- lord.queryAll
- lord.queryOne
- lord.nameAll
- lord.nameOne
- lord.node
- lord.toCenter
- lord.reloadPage
- lord.showPopup
- lord.showNotification
- lord.text
- lord.deviceType
- lord.showDialog
- lord.generateImageHash
- lord.getPlainText
- lord.activateTab
- lord.notificationsEnabled
- lord.soundEnabled
- lord.playSound
- lord.hash
- lord.data
- lord.scriptWorkaround
- lord.addTemplate
- lord.template
- lord.createDocumentFragment
- lord.createStylesheetLink
- lord.createScript
- lord.compareRegisteredUserLevelsfunction
- lord.compareRatings
- lord.model
- lord.get
- lord.api
- lord.post
- lord.now
- lord.settings
- lord.setSettings
- lord.checkError
- lord.handleError
- lord.readAs
- lord.series
Note: Functions that are used internally are not described here.
Returns true if mimeType is one of MIME-types for an audio (starts with audio/). Otherwise returns false.
Returns true if mimeType is one of MIME-types for an image (starts with image/). Otherwise returns false.
Returns true if mimeType is one of MIME-types for a video (starts with video/). Otherwise returns false.
Returns a cookie with the name name. If the cookie with this name does not exists, returns defValue (if specified), or undefined. The cookie is returned as a string (as it is stored by the browser).
Stored a cookie with name name and value value as a string. If options contains the expires property, the property is converted from a Date object to a string. See this article for details.
Deletes a cookie with the name name.
Returns the value stored in localStorage under the key key. If there is no such value, defValue is returned, if specified, otherwise undefined is returned. All errors thrown are catched, you do not need to catch them manually. Note: JSON.parse() is applied to the value before it is returned.
Stores value in localStorage under key key. Returns true on success, otherwise returns false. All errors thrown are catched, you do not need to catch them manually. Note: JSON.stringify() is applied to the value before it is stored.
Removes the value stored in localStorage under key key. Returns true on success, otherwise returns false. All errors thrown are catched, you do not need to catch them manually.
Returns the value stored in sessionStorage under the key key. If there is no such value, defValue is returned, if specified, otherwise undefined is returned. All errors thrown are catched, you do not need to catch them manually. Note: JSON.parse() is applied to the value before it is returned.
Stores value in sessionStorage under key key. Returns true on success, otherwise returns false. All errors thrown are catched, you do not need to catch them manually. Note: JSON.stringify() is applied to the value before it is stored.
Removes the value stored in sessionStorage under key key. Returns true on success, otherwise returns false. All errors thrown are catched, you do not need to catch them manually.
Returns true if the object obj has at least one own property (Object.prototype.hasOwnProperties() method is used to perform the chech). Otherwise returns false.
Applies the function f to every value of object/array obj. If obj is an array, the index of an element is passed to f as the second argument. Otherwise, the corresponding key is passed.
The application is done by portions with some timeout to prevent UI blocking.
Possible options:
-
delayThe delay between data portions processing. Default:undefined(no delay). -
nCount of elements to be processed at one time. Default:1. -
promiseIf casts tobooleantrue, it is assumed thatfreturns a promise, so.then()will be called on it's result every time. Default:false.
Returns a promise that resolves when all processing is finished.
Parses a string representation of a RegExp. Returs the resulting RegExp.
Example:
var rxParsed = lord.regexp("/^(test)\s(text)/gi");
/*Equivalent to:*/
var rxNative = /^(test)\s(text)/gi;
A shortcut for document.getElementById(id).
A shortcut for (parent || doucment).querySelectorAll(query). The only difference is that the result is always an instance of Array.
A shortcut for (parent || doucment).querySelectorAll(query).
A shortcut for lord.queryAll("[name='" + name + "']").
A shortcut for lord.queryOne("[name='" + name + "']").
A shortcut for document.createElement(type). If the value of type is "text", a shortcut for document.createTextNode(text || "").
Places the element element at the center of the window. If sizeHintX or sizeHintY are not valid numbers, element.offsetWidth and element.offsetHeight values are used. If border is a valid number, it's doubled value is added to element's width and height when calculating the position.
A shortcut for document.location.reload(true).
Creates an instance of lord.PopupMessage with the values text and options being passed to it's constructor. Then, calls the created object's .show() method and returns the object.
Requests permission to show desktop notifications, then shows a notification with title, body and icon values being passed to it's constructor.
A wrapper for lord.model("tr").tr[id]. Returns a translation with identifier id, if exists. Otherwise, returns the identifier itself.
If expected is a string, retunrs true if current device type matchs that string (either "desktop" or "mobile"). If expected casts to boolean false, returns the device type as a sting.
Example:
lord.deviceType("mobile") //true on mobile devices, false on desktop devices
lord.deviceType() //"mobile" on mobile devices, "desktop" on desktop devices
Shows a modal dialog using jQuery UI. Returns a promise that resolves with a boolean value, indicating whether the dialog action was accepted (true) or rejected (false). body must be a DOM node (it does not have to be in the DOM tree of the page).
options is an object-map of options. Possible values:
-
titleThe title that will be shown in the dialog header. May be a translation identifier. -
buttonsAn array containing special button names ("ok","cancel","close"), or objects describing the buttons with the following fields:
-
textThe text of a button. May be a translation identifier. -
actionA function that is executed when the button is clicked. If a function returns abooleanvalue, the dialog is accepted/rejected, depending on the value.
-
afterShowA function that will be called immediately after the dialog is shown. May be used, for example, to adjust some sizes, which is not possible until the dialog is appended to the DOM tree.
Generates a special hash for the ArrayBuffer imageData. sizeX and sizeY must be valid numbers (image dimensions). The algorithm is taken form the Dollchan Extension Tools script.
Returns the plain text contents of the element node without the HTML markup. End of lines are treated as expected, unlike the native .textContent. This method is much slower than .textContent, though.
Note: The code for this function was found somwhere on the Internet, I did not write it myself.
An alternative to jQuery UI Tabs.
Example:
<!--HTML-->
<ul>
<li data-index="0" class="activated"><a onclick="lord.activateTab(this); return false;">Tab 1</a></li>
<li data-index="1"><a onclick="lord.activateTab(this); return false;">Tab 2</a></li>
<li data-index="2"><a onclick="lord.activateTab(this); return false;">Tab 3</a></li>
</ul>
<div>
<div data-index="0">
Content 1
</div>
<div data-index="1" style="display: none;">
Content 2
</div>
<div data-index="2" style="display: none;">
Content 3
</div>
</div>
A shortcut for lord.getLocalObject("showAutoUpdateDesktopNotifications", true).
A shortcut for lord.getLocalObject("playAutoUpdateSound", false).
Creates an audio element and plays a special notification sound (public/audio/signal.ogg). The element is cached, so this functon is used to do the caching.
If hash is undefined, returns current page hash without the # symbol. Otherwise, sets the page hash to hash. If the current hash is the same as hash, the hash is first set to en ampty string, and then back to hash (this hack makes the page scroll anyway).
Kind of shortcut for (el || document.body).dataset[key]. If bubble casts to boolean true, and the current element does not have key in it's dataset, the search is performed in the element's parent node until document.body is reached.
A workaround for scripts elements with an async attribute, appended to the page. Queries all the elements inside parent (or document, if not specified) and replaces them with a non-async version.
Note: Normally, you should not use this method explicitly.
Adds a doT.js template html with name name to the template map.
Note: Normally, you should not use this method explicitly.
If model is not specified, returns a doT.js with the name templateName. Otherwise, applies the template to model, generating HTML. If noparse casts to boolean true, the HTML is returned as is, otherwise a DOM node is created and returned.
Creates a document fragment and inserts nodes created from html into it. Using this method does not block the UI as badly as inserting the html into the page directly (but it may still be slow on large amount of nodes).
Creates a stylesheet link element with source href, and inserts it into the document.head. If prefix casts to boolean true, the href is prefixed with standard site prefix.
Example:
/*Assuming a CSS file is in the public/css dir:*/
lord.createStylesheetLink("my-style.css", true);
/*Assuming a CSS file is an external file:*/
lord.createStylesheetLink("//site.com/files/external-style.css");
Creates a script element with source src, and inserts it into the document.head. If prefix casts to boolean true, the href is prefixed with standard site prefix.
Example:
/*Assuming a JS file is in the public/js dir:*/
lord.createScript("my-script", true);
/*Assuming a JS file is an external file:*/
lord.createScript("//site.com/files/external-script.js");
Returns -1 if the registered level l1 is less than l2, 1 if l1 is greated than l2, and 0 if the levels are equal. The levels are: "USER", "MODER", "ADMIN", "SUPERUSER".
Returns -1 if the file rating r1 is less than r2, 1 if r1 is greated than r2, and 0 if the ratings are equal. The ratings are: "SFW", "R-15", "R-18", "R-18G".
If modelName is a string, returns the model. If it is an Array, returns the models listed in that array merged into one model. Some additional data is added to the model. See the implementation in public/js/api.js for details.
Synchronous GET request. what is the URL. If enableCache does not cast to boolean true, a special "_random_=" + Math.random() query part is appended to prevent caching. The result is returned as a string.
Asynchronous ololord.js GET Web API call. jQuery .ajax() method is used. entity is the requested resource name. If parameters object is specified, it's key-value pairs are converted to the request query. Array values are allowed. If prefix is specified, it is used as a request path prefix, otherwise, "api" is used as a prefix. Note: The ".json" suffix is appended to the path automatically. Do not include it.
Example:
/*Get user IP address*/
lord.api("userIp", { boardName: "b", postNumber: 9000 }).then(function(result) {
//...
});
/*Get last post numbers on the /b/ and /d/ boards*/
lord.api("lastPostNumbers", { boardNames: ["b", "d"] }).then(function(result) {
//...
});
/*Get the thread with number 9000 on the /b/ board*/
lord.api(9000, {}, "b/res").then(function(result) {
//...
});
Asynchronous ololord.js POST Web API call. jQuery .post() method is used. action is the action requestd. formData is a FormData instance.
Warning: progressBarContext and progressBarOptions arguments are to be changed in the next release, so they are not described here. Do not use them.
Example:
/*Sending a private chat message*/
var formData = new FormData();
formData.append("boardName", "b");
formData.append("postNumber", 9000);
formData.append("text", "Hello!");
lord.post("/" + lord.data("sitePathPrefix") + "action/sendChatMessage", formData).then(function(result) {
//...
});
A shortcut for new Date(). More clear.
Returns an object-map of user settings stored in localStorage and in cookies.
Stores user settings, that model object contains, in localStorage and in cookies.
If result is an error, returns true. Otherwise returns false. The algorithm is not described here, because this function is used internally. You may use it at your own risk.
Logs the error and shows the corresponding popup message. The algorithm is a bit tricky, so it is not explained here.
Example:
lord.api(/*Some Web API call*/).then(function() {
//...
}).catch(lord.handleError); //Yep, you do not have to bind the context
try {
//...
} catch(err) {
lord.handleError(err);
}
A promisified version of FileReader.prototype.readAs methods. blob must be an instance of Blob or File. method may take the following values:
-
ArrayBufferThe default. BinaryStringDataURLText
The function creates an instance FileReader, calls FileReader.prototype["readAs" + method]() on that instance, and returns a promise that resolves to the result.
Succesively applies the function f to each elenent of arr, assuming that f returns a promise. Waits for each promise, then applies f to the next element. arr may be either Array, or a map-like object. Returns the last promise.