util.js

CandyChats are not dead yet.

Authors

Copyright

© 2011 Amiado Group AG.  All rights reserved.  © 2012-2014 Patrick Stadler & Michael Weibel.  All rights reserved.

Summary
util.js
Candy.UtilCandy utils
Functions
jidToIdTranslates a jid to a MD5-Id
escapeJidEscapes a jid (node & resource get escaped)
unescapeJidUnescapes a jid (node & resource get unescaped)
cropCrop a string with the specified length
parseAndCropXhtmlParses the XHTML and applies various Candy related filters to it.
setCookieSets a new cookie
cookieExistsTests if a cookie with the given name exists
getCookieReturns the cookie value if there’s one with this name, otherwise returns undefined
deleteCookieDeletes a cookie with the given name
getPosLeftAccordingToWindowBoundsFetches the window width and element width and checks if specified position + element width is bigger than the window width.
getPosTopAccordingToWindowBoundsFetches the window height and element height and checks if specified position + element height is bigger than the window height.
localizedTimeLocalizes ISO-8610 Date with the time/dateformat specified in the translation.
iso8610toDateParses a ISO-8610 Date to a Date-Object.
isEmptyObjectIE7 doesn’t work with jQuery.isEmptyObject (<=1.5.1), workaround.
forceRedrawFix IE7 not redrawing under some circumstances.
getIeVersionReturns local variable `ie` which you can use to detect which IE version is available.
Candy.Util.ParserParser for emoticons, links and also supports escaping.
Functions
setEmoticonPathSet emoticons location.
Variables
emoticonsArray containing emoticons to be replaced by their images.
Functions
emotifyReplaces text-emoticons with their image equivalent.
linkifyReplaces URLs with a HTML-link.
escapeEscapes a text using a jQuery function (like htmlspecialchars in PHP)
nl2brreplaces newline characters with a <br/> to make multi line messages look nice
allDoes everything of the parser: escaping, linkifying and emotifying.
createHtmlCopy an HTML DOM element into an XML DOM.

Candy.Util

Candy utils

Parameters

(Candy.Util) selfitself
(jQuery) $jQuery
Summary
Functions
jidToIdTranslates a jid to a MD5-Id
escapeJidEscapes a jid (node & resource get escaped)
unescapeJidUnescapes a jid (node & resource get unescaped)
cropCrop a string with the specified length
parseAndCropXhtmlParses the XHTML and applies various Candy related filters to it.
setCookieSets a new cookie
cookieExistsTests if a cookie with the given name exists
getCookieReturns the cookie value if there’s one with this name, otherwise returns undefined
deleteCookieDeletes a cookie with the given name
getPosLeftAccordingToWindowBoundsFetches the window width and element width and checks if specified position + element width is bigger than the window width.
getPosTopAccordingToWindowBoundsFetches the window height and element height and checks if specified position + element height is bigger than the window height.
localizedTimeLocalizes ISO-8610 Date with the time/dateformat specified in the translation.
iso8610toDateParses a ISO-8610 Date to a Date-Object.
isEmptyObjectIE7 doesn’t work with jQuery.isEmptyObject (<=1.5.1), workaround.
forceRedrawFix IE7 not redrawing under some circumstances.
getIeVersionReturns local variable `ie` which you can use to detect which IE version is available.

Functions

jidToId

self.jidToId = function(jid)

Translates a jid to a MD5-Id

Parameters

(String) jidJid

Returns

MD5-ified jid

escapeJid

self.escapeJid = function(jid)

Escapes a jid (node & resource get escaped)

See

XEP-0106

Parameters

(String) jidJid

Returns

(String)escaped jid

unescapeJid

self.unescapeJid = function(jid)

Unescapes a jid (node & resource get unescaped)

See

XEP-0106

Parameters

(String) jidJid

Returns

(String)unescaped Jid

crop

self.crop = function(str,
len)

Crop a string with the specified length

Parameters

(String) strString to crop
(Integer) lenMax length

parseAndCropXhtml

self.parseAndCropXhtml = function(str,
len)

Parses the XHTML and applies various Candy related filters to it.

  • Ensures it contains only valid XHTML
  • Crops text to a max length
  • Parses the text in order to display html

Parameters

(String) strString containing XHTML
(Integer) lenMax text length

setCookie

self.setCookie = function(name,
value,
lifetime_days)

Sets a new cookie

Parameters

(String) namecookie name
(String) valueValue
(Integer) lifetime_daysLifetime in days

cookieExists

self.cookieExists = function(name)

Tests if a cookie with the given name exists

Parameters

(String) nameCookie name

Returns

(Boolean)true/false

getCookie

self.getCookie = function(name)

Returns the cookie value if there’s one with this name, otherwise returns undefined

Parameters

(String) nameCookie name

Returns

Cookie value or undefined

deleteCookie

self.deleteCookie = function(name)

Deletes a cookie with the given name

Parameters

(String) namecookie name

getPosLeftAccordingToWindowBounds

self.getPosLeftAccordingToWindowBounds = function(elem,
pos)

Fetches the window width and element width and checks if specified position + element width is bigger than the window width.

If this evaluates to true, the position gets substracted by the element width.

Parameters

(jQuery.Element) elemElement to position
(Integer) posPosition left

Returns

Object containing `px` (calculated position in pixel) and `alignment` (alignment of the element in relation to pos, either ‘left’ or ‘right’)

getPosTopAccordingToWindowBounds

self.getPosTopAccordingToWindowBounds = function(elem,
pos)

Fetches the window height and element height and checks if specified position + element height is bigger than the window height.

If this evaluates to true, the position gets substracted by the element height.

Parameters

(jQuery.Element) elemElement to position
(Integer) posPosition top

Returns

Object containing `px` (calculated position in pixel) and `alignment` (alignment of the element in relation to pos, either ‘top’ or ‘bottom’)

localizedTime

self.localizedTime = function(dateTime)

Localizes ISO-8610 Date with the time/dateformat specified in the translation.

See: libs/dateformat/dateFormat.js See: src/view/translation.js See: jquery-i18n/jquery.i18n.js

Parameters

(String) dateTimeISO-8610 Datetime

Returns

If current date is equal to the date supplied, format with timeFormat, otherwise with dateFormat

iso8610toDate

Parses a ISO-8610 Date to a Date-Object.

Uses a fallback if the client’s browser doesn’t support it.

Quote

ECMAScript revision 5 adds native support for ISO-8601 dates in the Date.parse method, but many browsers currently on the market (Safari 4, Chrome 4, IE 6-8) do not support it.

Credits

Colin Snover

Parameters

(String) dateISO-8610 Date

Returns

Date-Object

isEmptyObject

self.isEmptyObject = function(obj)

IE7 doesn’t work with jQuery.isEmptyObject (<=1.5.1), workaround.

Parameters

(Object) objthe object to test for

Returns

Boolean true or false.

forceRedraw

self.forceRedraw = function(elem)

Fix IE7 not redrawing under some circumstances.

Parameters

(jQuery.element) elemjQuery element to redraw

getIeVersion

self.getIeVersion = function()

Returns local variable `ie` which you can use to detect which IE version is available.

Use e.g. like this: if(Candy.Util.getIeVersion() < 9) alert(‘kaboom’);

Candy.Util.Parser

Parser for emoticons, links and also supports escaping.

Summary
Functions
setEmoticonPathSet emoticons location.
Variables
emoticonsArray containing emoticons to be replaced by their images.
Functions
emotifyReplaces text-emoticons with their image equivalent.
linkifyReplaces URLs with a HTML-link.
escapeEscapes a text using a jQuery function (like htmlspecialchars in PHP)
nl2brreplaces newline characters with a <br/> to make multi line messages look nice
allDoes everything of the parser: escaping, linkifying and emotifying.
createHtmlCopy an HTML DOM element into an XML DOM.

Functions

setEmoticonPath

setEmoticonPath: function(path)

Set emoticons location.

Parameters

(String) pathlocation of emoticons with trailing slash

Variables

emoticons

Array containing emoticons to be replaced by their images.

Can be overridden/extended.

Functions

emotify

emotify: function(text)

Replaces text-emoticons with their image equivalent.

Parameters

(String) textText to emotify

Returns

Emotified text

linkify

linkify: function(text)

Replaces URLs with a HTML-link.

Parameters

(String) textText to linkify

Returns

Linkified text

escape

escape: function(text)

Escapes a text using a jQuery function (like htmlspecialchars in PHP)

Parameters

(String) textText to escape

Returns

Escaped text

nl2br

nl2br: function(text)

replaces newline characters with a <br/> to make multi line messages look nice

Parameters

(String) textText to process

Returns

Processed text

all

all: function(text)

Does everything of the parser: escaping, linkifying and emotifying.

Parameters

(String) textText to parse

Returns

(String) Parsed text

createHtml

self.createHtml = function(elem,
maxLength,
currentLength)

Copy an HTML DOM element into an XML DOM.

This function copies a DOM element and all its descendants and returns the new copy.

It’s a function copied & adapted from [Strophe.js core.js](https://github.com/strophe/strophejs/blob/master/src/core.js).

Parameters

(HTMLElement) elemA DOM element.
(Integer) maxLengthMax length of text
(Integer) currentLengthCurrent accumulated text length

Returns

A new, copied DOM element tree.

self.jidToId = function(jid)
Translates a jid to a MD5-Id
self.escapeJid = function(jid)
Escapes a jid (node & resource get escaped)
self.unescapeJid = function(jid)
Unescapes a jid (node & resource get unescaped)
self.crop = function(str,
len)
Crop a string with the specified length
self.parseAndCropXhtml = function(str,
len)
Parses the XHTML and applies various Candy related filters to it.
self.setCookie = function(name,
value,
lifetime_days)
Sets a new cookie
self.cookieExists = function(name)
Tests if a cookie with the given name exists
self.getCookie = function(name)
Returns the cookie value if there’s one with this name, otherwise returns undefined
self.deleteCookie = function(name)
Deletes a cookie with the given name
self.getPosLeftAccordingToWindowBounds = function(elem,
pos)
Fetches the window width and element width and checks if specified position + element width is bigger than the window width.
self.getPosTopAccordingToWindowBounds = function(elem,
pos)
Fetches the window height and element height and checks if specified position + element height is bigger than the window height.
self.localizedTime = function(dateTime)
Localizes ISO-8610 Date with the time/dateformat specified in the translation.
self.isEmptyObject = function(obj)
IE7 doesn’t work with jQuery.isEmptyObject (<=1.5.1), workaround.
self.forceRedraw = function(elem)
Fix IE7 not redrawing under some circumstances.
self.getIeVersion = function()
Returns local variable `ie` which you can use to detect which IE version is available.
setEmoticonPath: function(path)
Set emoticons location.
emotify: function(text)
Replaces text-emoticons with their image equivalent.
linkify: function(text)
Replaces URLs with a HTML-link.
escape: function(text)
Escapes a text using a jQuery function (like htmlspecialchars in PHP)
nl2br: function(text)
replaces newline characters with a br/ to make multi line messages look nice
all: function(text)
Does everything of the parser: escaping, linkifying and emotifying.
self.createHtml = function(elem,
maxLength,
currentLength)
Copy an HTML DOM element into an XML DOM.
Close