Splited into separate files.

This commit is contained in:
Martin Edenhofer 2012-07-15 22:34:14 +02:00
parent 58dc1db5dc
commit 35d7a46ab6
4 changed files with 251 additions and 231 deletions

View file

@ -22,6 +22,9 @@
#not_used= require_tree ./lib
#= require_self
#= require ./lib/ajax.js.coffee
#= require ./lib/auth.js.coffee
#= require ./lib/i18n.js.coffee
#= require_tree ./models
#= require_tree ./controllers
#= require_tree ./views
@ -54,236 +57,6 @@ Config2.set( 'a', 123)
console.log '1112222', Config2.get( 'a')
###
class App.Com
_instance = undefined # Must be declared here to force the closure on the class
@ajax: (args) -> # Must be a static method
if _instance == undefined
_instance ?= new _Singleton
_instance.ajax(args)
_instance
# The actual Singleton class
class _Singleton
defaults:
contentType: 'application/json'
dataType: 'json'
processData: false
headers: {'X-Requested-With': 'XMLHttpRequest'}
cache: false
async: true
queue_list: {}
pending: false
constructor: (@args) ->
ajax: (params, defaults) ->
if params['id']
if @queue_list[ params['id'] ]
@queue_list[ params['id'] ].abort()
@queue_list[ params['id'] ] = $.ajax($.extend({}, @defaults, defaults, params))
else
$.ajax($.extend({}, @defaults, defaults, params))
console.log('AJAX', params['url'] )
class App.Auth
@login: (params) ->
console.log 'login(...)', params
App.Com.ajax(
id: 'login',
# params,
type: 'POST',
url: '/signin',
data: JSON.stringify(params.data),
success: params.success,
error: params.error,
)
@loginCheck: ->
console.log 'loginCheck(...)'
App.Com.ajax(
id: 'login_check',
async: false,
type: 'GET',
url: '/signshow',
success: (data, status, xhr) =>
console.log 'logincheck:success', data
# if session is not valid
if data.error
# update config
for key, value of data.config
window.Config[key] = value
# empty session
window.Session = {}
# rebuild navbar with new navbar items
Spine.trigger 'navrebuild'
return false;
# set avatar
if !data.session.image
data.session.image = 'http://placehold.it/48x48'
# update config
for key, value of data.config
window.Config[key] = value
# store user data
for key, value of data.session
window.Session[key] = value
# refresh/load default collections
for key, value of data.default_collections
App[key].refresh( value, options: { clear: true } )
# rebuild navbar with new navbar items
Spine.trigger 'navrebuild', data.session
# rebuild navbar with updated ticket count of overviews
Spine.trigger 'navupdate_remote'
error: (xhr, statusText, error) =>
console.log 'loginCheck:error'#, error, statusText, xhr.statusCode
# empty session
window.Session = {}
)
@logout: ->
console.log 'logout(...)'
App.Com.ajax(
id: 'logout',
type: 'DELETE',
url: '/signout',
)
class App.i18n
constructor: ->
@locale = 'de'
@set( @locale )
window.T = @translate_content
window.Ti = @translate_inline
# $('.translation [contenteditable]')
$('body')
.delegate '.translation', 'focus', (e) =>
$this = $(e.target)
$this.data 'before', $this.html()
# console.log('11111current', $this.html())
return $this
# .delegate '.translation', 'blur keyup paste', (e) =>
.delegate '.translation', 'blur', (e) =>
$this = $(e.target)
source = $this.attr('data-text')
# get new translation
translation_new = $this.html()
translation_new = ('' + translation_new)
.replace(/<.+?>/g, '')
# set new translation
$this.html(translation_new)
# update translation
return if $this.data('before') is translation_new
console.log 'Translation Update', translation_new, $this.data 'before'
$this.data 'before', translation_new
# update runtime translation map
@map[ source ] = translation_new
# replace rest in page
$(".translation[data-text='#{source}']").html( translation_new )
# update permanent translation map
translation = App.Translation.findByAttribute( 'source', source )
if translation
translation.updateAttribute( 'target', translation_new )
else
translation = new App.Translation
translation.load(
locale: @locale,
source: source,
target: translation_new,
)
translation.save()
return $this
set: (locale) =>
@map = {}
App.Com.ajax(
id: 'i18n-set-' + locale,
type: 'GET',
url: '/translations/lang/' + locale,
async: false,
success: (data, status, xhr) =>
# load translation collection
for object in data
# set runtime lookup table
@map[ object[1] ] = object[2]
# load in collection if needed
App.Translation.refresh( { id: object[0], source: object[1], target: object[2], locale: @locale }, options: { clear: true } )
error: (xhr, statusText, error) =>
console.log 'error', error, statusText, xhr.statusCode
)
translate_inline: (string, args...) =>
@translate(string, args...)
translate_content: (string, args...) =>
translated = @translate(string, args...)
# replace = '<span class="translation" contenteditable="true" data-text="' + @escape(string) + '">' + translated + '<span class="icon-edit"></span>'
if window.Config['Translation']
replace = '<span class="translation" contenteditable="true" data-text="' + @escape(string) + '">' + translated + ''
# if !@_translated
# replace += '<span class="missing">XX</span>'
replace += '</span>'
else
translated
translate: (string, args...) =>
# return '' on undefined
return '' if string is undefined
# return translation
if @map[string] isnt undefined
@_translated = true
translated = @map[string]
else
@_translated = false
translated = string
# search %s
for arg in args
translated = translated.replace(/%s/, arg)
# escape
translated = @escape(translated)
# return translated string
return translated
escape: (string) ->
string = ('' + string)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\x22/g, '&quot;')
class App.Run extends Spine.Controller
constructor: ->
@ -308,7 +81,7 @@ class App.Run extends Spine.Controller
# bind to fill selected text into
$(@el).bind('mouseup', =>
window.Session['UISeletion'] = @getSelected() + ''
window.Session['UISelection'] = @getSelected() + ''
)
# @ws = new WebSocket("ws://localhost:3001/");

View file

@ -0,0 +1,48 @@
$ = jQuery.sub()
class App.Com
_instance = undefined # Must be declared here to force the closure on the class
@ajax: (args) -> # Must be a static method
if _instance == undefined
_instance ?= new _Singleton
_instance.ajax(args)
_instance
# The actual Singleton class
class _Singleton
defaults:
contentType: 'application/json'
dataType: 'json'
processData: false
headers: {'X-Requested-With': 'XMLHttpRequest'}
cache: false
async: true
queue_list: {}
count: 0
constructor: (@args) ->
ajax: (params, defaults) ->
data = $.extend({}, @defaults, defaults, params)
@count++
@_show_spinner()
# console.log( 'START', @count )
if params['id']
if @queue_list[ params['id'] ]
@queue_list[ params['id'] ].abort()
@queue_list[ params['id'] ] = $.ajax( data ).always( @_hide_spinner )
else
$.ajax( data ).always( @_hide_spinner )
console.log('AJAX', params['url'] )
_show_spinner: =>
$('.spinner').show()
_hide_spinner: =>
@count--
if @count == 0
$('.spinner').hide()

View file

@ -0,0 +1,78 @@
$ = jQuery.sub()
class App.Auth
@login: (params) ->
console.log 'login(...)', params
App.Com.ajax(
id: 'login',
# params,
type: 'POST',
url: '/signin',
data: JSON.stringify(params.data),
success: params.success,
error: params.error,
)
@loginCheck: ->
console.log 'loginCheck(...)'
App.Com.ajax(
id: 'login_check',
async: false,
type: 'GET',
url: '/signshow',
success: (data, status, xhr) =>
console.log 'logincheck:success', data
# if session is not valid
if data.error
# update config
for key, value of data.config
window.Config[key] = value
# empty session
window.Session = {}
# rebuild navbar with new navbar items
Spine.trigger 'navrebuild'
return false;
# set avatar
if !data.session.image
data.session.image = 'http://placehold.it/48x48'
# update config
for key, value of data.config
window.Config[key] = value
# store user data
for key, value of data.session
window.Session[key] = value
# refresh/load default collections
for key, value of data.default_collections
App[key].refresh( value, options: { clear: true } )
# rebuild navbar with new navbar items
Spine.trigger 'navrebuild', data.session
# rebuild navbar with updated ticket count of overviews
Spine.trigger 'navupdate_remote'
error: (xhr, statusText, error) =>
console.log 'loginCheck:error'#, error, statusText, xhr.statusCode
# empty session
window.Session = {}
)
@logout: ->
console.log 'logout(...)'
App.Com.ajax(
id: 'logout',
type: 'DELETE',
url: '/signout',
)

View file

@ -0,0 +1,121 @@
$ = jQuery.sub()
class App.i18n
constructor: ->
@locale = 'de'
@set( @locale )
window.T = @translate_content
window.Ti = @translate_inline
# $('.translation [contenteditable]')
$('body')
.delegate '.translation', 'focus', (e) =>
$this = $(e.target)
$this.data 'before', $this.html()
# console.log('11111current', $this.html())
return $this
# .delegate '.translation', 'blur keyup paste', (e) =>
.delegate '.translation', 'blur', (e) =>
$this = $(e.target)
source = $this.attr('data-text')
# get new translation
translation_new = $this.html()
translation_new = ('' + translation_new)
.replace(/<.+?>/g, '')
# set new translation
$this.html(translation_new)
# update translation
return if $this.data('before') is translation_new
console.log 'Translation Update', translation_new, $this.data 'before'
$this.data 'before', translation_new
# update runtime translation map
@map[ source ] = translation_new
# replace rest in page
$(".translation[data-text='#{source}']").html( translation_new )
# update permanent translation map
translation = App.Translation.findByAttribute( 'source', source )
if translation
translation.updateAttribute( 'target', translation_new )
else
translation = new App.Translation
translation.load(
locale: @locale,
source: source,
target: translation_new,
)
translation.save()
return $this
set: (locale) =>
@map = {}
App.Com.ajax(
id: 'i18n-set-' + locale,
type: 'GET',
url: '/translations/lang/' + locale,
async: false,
success: (data, status, xhr) =>
# load translation collection
for object in data
# set runtime lookup table
@map[ object[1] ] = object[2]
# load in collection if needed
App.Translation.refresh( { id: object[0], source: object[1], target: object[2], locale: @locale }, options: { clear: true } )
error: (xhr, statusText, error) =>
console.log 'error', error, statusText, xhr.statusCode
)
translate_inline: (string, args...) =>
@translate(string, args...)
translate_content: (string, args...) =>
translated = @translate(string, args...)
# replace = '<span class="translation" contenteditable="true" data-text="' + @escape(string) + '">' + translated + '<span class="icon-edit"></span>'
if window.Config['Translation']
replace = '<span class="translation" contenteditable="true" data-text="' + @escape(string) + '">' + translated + ''
# if !@_translated
# replace += '<span class="missing">XX</span>'
replace += '</span>'
else
translated
translate: (string, args...) =>
# return '' on undefined
return '' if string is undefined
# return translation
if @map[string] isnt undefined
@_translated = true
translated = @map[string]
else
@_translated = false
translated = string
# search %s
for arg in args
translated = translated.replace(/%s/, arg)
# escape
translated = @escape(translated)
# return translated string
return translated
escape: (string) ->
string = ('' + string)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\x22/g, '&quot;')