Permalink
Browse files

Refactored defaultCallback function.

  • Loading branch information...
coderifous committed Jun 15, 2012
1 parent a7a9838 commit e24f02d9e65c2d8022aa92d920a251aacdadbce9
Showing with 95 additions and 58 deletions.
  1. +55 −30 build/jquery.localize.js
  2. +40 −28 src/jquery.localize.coffee
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -18,9 +18,9 @@ normaliseLang = (lang) ->
$.defaultLanguage = normaliseLang(navigator.language || navigator.userLanguage)
$.localize = (pkg, options = {}) ->
- wrappedSet = this
+ wrappedSet = this
intermediateLangData = {}
- fileExtension = options.fileExtension || "json"
+ fileExtension = options.fileExtension || "json"
loadLanguage = (pkg, lang, level = 1) ->
switch level
@@ -59,36 +59,44 @@ $.localize = (pkg, options = {}) ->
ajaxOptions.error = (xhr) -> successFunc($.parseJSON(xhr.responseText))
$.ajax(ajaxOptions)
+ notifyDelegateLanguageLoaded = (data) ->
+ if options.callback?
+ options.callback(data, defaultCallback)
+ else
+ defaultCallback(data)
+
defaultCallback = (data) ->
$.localize.data[pkg] = data
wrappedSet.each ->
- elem = $(this)
- key = elem.data("localize")
+ elem = $(this)
+ key = elem.data("localize")
key ||= elem.attr("rel").match(/localize\[(.*?)\]/)[1]
value = valueForKey(key, data)
- if elem.is('input')
- if elem.is("[placeholder]")
- elem.attr("placeholder", value)
- else
- elem.val(value)
- else if elem.is('optgroup')
- elem.attr("label", value)
- else if elem.is('img')
- applyValueToAttribute(key, "alt", data, elem)
- applyValueToAttribute(key, "src", data, elem)
- else unless $.isPlainObject(value)
- elem.html(value)
-
- if $.isPlainObject(value)
- applyValueToAttribute(key, "title", data, elem)
- value = valueForKey("#{key}.text", data)
- elem.text(value) if value?
+ localizeElement(elem, key, value)
- notifyDelegateLanguageLoaded = (data) ->
- if options.callback?
- options.callback(data, defaultCallback)
+ localizeElement = (elem, key, value) ->
+ if elem.is('input') then localizeInputElement(elem, key, value)
+ else if elem.is('img') then localizeImageElement(elem, key, value)
+ else if elem.is('optgroup') then localizeOptgroupElement(elem, key, value)
+ else unless $.isPlainObject(value) then elem.html(value)
+ localizeForSpecialKeys(elem, value) if $.isPlainObject(value)
+
+ localizeInputElement = (elem, key, value) ->
+ if elem.is("[placeholder]")
+ elem.attr("placeholder", value)
else
- defaultCallback(data)
+ elem.val(value)
+
+ localizeForSpecialKeys = (elem, value) ->
+ setAttrFromValueForKey(elem, "title", value)
+ setTextFromValueForKey(elem, "text", value)
+
+ localizeOptgroupElement = (elem, key, value) ->
+ elem.attr("label", value)
+
+ localizeImageElement = (elem, key, value) ->
+ setAttrFromValueForKey(elem, "alt", value)
+ setAttrFromValueForKey(elem, "src", value)
valueForKey = (key, data) ->
keys = key.split(/\./)
@@ -97,9 +105,13 @@ $.localize = (pkg, options = {}) ->
value = if value? then value[key] else null
value
- applyValueToAttribute = (key, attribute, data, elem) ->
- value = valueForKey("#{key}.#{attribute}", data)
- elem.attr(attribute, value) if value?
+ setAttrFromValueForKey = (elem, key, value) ->
+ value = valueForKey(key, value)
+ elem.attr(key, value) if value?
+
+ setTextFromValueForKey = (elem, key, value) ->
+ value = valueForKey(key, value)
+ elem.text(value) if value?
regexify = (string_or_regex_or_array) ->
if typeof(string_or_regex_or_array) == "string"

0 comments on commit e24f02d

Please sign in to comment.