utils.coffee | |
---|---|
iOrder | |
Private variables |
|
Mapping for internationalization handlers. |
i18nHandlers =
|
Replace the HTML content of |
'i18n-content': (element, value, subMap) ->
subs = i18nSubs element, value, subMap
element.innerHTML = utils.i18n value, subs
|
Replace the value of the properties and/or attributes of |
'i18n-values': (element, value, subMap) ->
subs = i18nSubs element, value, subMap
parts = value.replace(/\s/g, '').split ';'
for part in parts
prop = part.match /^([^:]+):(.+)$/
if prop
propName = prop[1]
propExpr = prop[2]
if propName.indexOf('.') is 0
path = propName.slice(1).split '.'
obj = element
obj = obj[path.shift()] while obj and path.length > 1
if obj
obj[path] = utils.i18n value, subs
i18nProcess element, subMap if path is 'innerHTML'
else
element.setAttribute propName, utils.i18n propExpr, subs
|
List of internationalization attributes/handlers available. |
i18nAttributes = []
i18nAttributes.push key for key of i18nHandlers
|
Selector containing the available internationalization attributes/handlers
which is used by |
i18nSelector = "[#{i18nAttributes.join '],['}]"
|
Private functions |
|
Find all elements to be internationalized and call their corresponding handler(s). |
i18nProcess = (node, subMap) ->
for element in node.querySelectorAll i18nSelector
for name in i18nAttributes
attribute = element.getAttribute name
i18nHandlers[name] element, attribute, subMap if attribute?
|
Find an array of substitution strings using the element's ID and the message key as the mapping. |
i18nSubs = (element, value, subMap) ->
if subMap
for prop of subMap when subMap.hasOwnProperty(prop) and prop is element.id
for subProp of subMap[prop] when subMap[prop].hasOwnProperty subProp
if subProp is value
subs = subMap[prop][subProp]
break
break
return subs
|
Utilities setup |
utils = window.utils =
|
Data functions |
|
Determine whether or not the specified key exists in |
exists: (key) ->
return localStorage.hasOwnProperty key
|
Retrieve the value associated with the specified key from |
get: (key) ->
value = localStorage[key]
return if value? then JSON.parse value else value
|
Initialize the value of the specified key in |
init: (key, defaultValue) ->
value = utils.get key
return utils.set key, value ? defaultValue
|
Remove the specified key from |
remove: (key) ->
exists = utils.exists key
delete localStorage[key]
return exists
|
Copy the value of the existing key to that of the new key then remove the
old key from |
rename: (oldKey, newKey, defaultValue) ->
if utils.exists oldKey
utils.init newKey, utils.get oldKey
utils.remove oldKey
else
utils.init newKey, defaultValue
|
Set the value of the specified key in |
set: (key, value) ->
oldValue = utils.get key
localStorage[key] = if value? then JSON.stringify value else value
return oldValue
|
Internationalization functions |
|
Convenient shorthand for |
i18n: ->
chrome.i18n.getMessage.apply chrome.i18n, arguments
|
Internationalize the specified attribute of all the selected elements. |
i18nAttribute: (selector, attribute, value, subs) ->
elements = document.querySelectorAll selector
|
Ensure the substitution string(s) are in an array. |
subs = [subs] if typeof subs is 'string'
for element in elements
element.setAttribute attribute, utils.i18n value, subs
|
Internationalize the contents of all the selected elements. |
i18nContent: (selector, value, subs) ->
elements = document.querySelectorAll selector
|
Ensure the substitution string(s) are in an array. |
subs = [subs] if typeof subs is 'string'
element.innerHTML = utils.i18n value, subs for element in elements
|
Perform all internationalization setup required for the current page. |
i18nSetup: (subMap) ->
i18nProcess document, subMap
|