joosy 0.1.0.alpha → 1.0.0.RC1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/.codoopts +5 -0
  2. data/.gitignore +2 -0
  3. data/Gemfile +15 -2
  4. data/Gemfile.lock +102 -81
  5. data/Guardfile +16 -16
  6. data/LICENSE +22 -0
  7. data/MIT-LICENSE +2 -2
  8. data/README.md +118 -0
  9. data/app/assets/javascripts/joosy/core/application.js.coffee +53 -0
  10. data/app/assets/javascripts/joosy/core/form.js.coffee +338 -0
  11. data/app/assets/javascripts/joosy/core/helpers/form.js.coffee +72 -0
  12. data/app/assets/javascripts/joosy/core/helpers/view.js.coffee +42 -0
  13. data/app/assets/javascripts/joosy/core/helpers/widgets.js.coffee +14 -0
  14. data/app/assets/javascripts/joosy/core/joosy.js.coffee +184 -0
  15. data/app/assets/javascripts/joosy/core/layout.js.coffee +168 -0
  16. data/app/assets/javascripts/joosy/core/modules/container.js.coffee +124 -0
  17. data/app/assets/javascripts/joosy/core/modules/events.js.coffee +122 -0
  18. data/app/assets/javascripts/joosy/core/modules/filters.js.coffee +39 -0
  19. data/app/assets/javascripts/joosy/core/modules/log.js.coffee +36 -0
  20. data/app/assets/javascripts/joosy/core/modules/module.js.coffee +117 -0
  21. data/app/assets/javascripts/joosy/core/modules/renderer.js.coffee +200 -0
  22. data/app/assets/javascripts/joosy/core/modules/time_manager.js.coffee +46 -0
  23. data/app/assets/javascripts/joosy/core/modules/widgets_manager.js.coffee +87 -0
  24. data/app/assets/javascripts/joosy/core/page.js.coffee +387 -0
  25. data/app/assets/javascripts/joosy/core/preloader.js.coffee +13 -0
  26. data/app/assets/javascripts/joosy/core/resource/collection.js.coffee +175 -0
  27. data/app/assets/javascripts/joosy/core/resource/generic.js.coffee +303 -0
  28. data/app/assets/javascripts/joosy/core/resource/rest.js.coffee +244 -0
  29. data/app/assets/javascripts/joosy/core/resource/rest_collection.js.coffee +24 -0
  30. data/app/assets/javascripts/joosy/core/router.js.coffee +201 -0
  31. data/app/assets/javascripts/joosy/core/templaters/rails_jst.js.coffee +37 -0
  32. data/app/assets/javascripts/joosy/core/widget.js.coffee +85 -0
  33. data/app/assets/javascripts/joosy/preloaders/caching.js.coffee +169 -0
  34. data/app/assets/javascripts/joosy/preloaders/inline.js.coffee +56 -0
  35. data/{vendor → app}/assets/javascripts/joosy.js.coffee +0 -1
  36. data/app/helpers/joosy/sprockets_helper.rb +39 -12
  37. data/joosy.gemspec +4 -3
  38. data/lib/joosy/rails/engine.rb +12 -1
  39. data/lib/joosy/rails/version.rb +2 -2
  40. data/lib/joosy.rb +9 -0
  41. data/lib/rails/generators/joosy/application_generator.rb +15 -3
  42. data/lib/rails/generators/joosy/joosy_base.rb +2 -2
  43. data/lib/rails/generators/joosy/layout_generator.rb +8 -1
  44. data/lib/rails/generators/joosy/page_generator.rb +21 -6
  45. data/lib/rails/generators/joosy/preloader_generator.rb +14 -7
  46. data/lib/rails/generators/joosy/resource_generator.rb +29 -0
  47. data/lib/rails/generators/joosy/templates/app/helpers/application.js.coffee +4 -0
  48. data/lib/rails/generators/joosy/templates/app/layouts/application.js.coffee +1 -0
  49. data/lib/rails/generators/joosy/templates/app/layouts/template.js.coffee +1 -1
  50. data/lib/rails/generators/joosy/templates/app/pages/application.js.coffee +1 -1
  51. data/lib/rails/generators/joosy/templates/app/pages/template.js.coffee +3 -3
  52. data/lib/rails/generators/joosy/templates/app/pages/welcome/index.js.coffee +22 -0
  53. data/lib/rails/generators/joosy/templates/app/resources/template.js.coffee +2 -0
  54. data/lib/rails/generators/joosy/templates/app/routes.js.coffee +7 -1
  55. data/lib/rails/generators/joosy/templates/app/templates/layouts/application.jst.hamlc +2 -0
  56. data/lib/rails/generators/joosy/templates/app/templates/pages/welcome/index.jst.hamlc +7 -0
  57. data/lib/rails/generators/joosy/templates/app/widgets/template.js.coffee +2 -2
  58. data/lib/rails/generators/joosy/templates/app.js.coffee +4 -0
  59. data/lib/rails/generators/joosy/templates/app_preloader.js.coffee.erb +9 -12
  60. data/lib/rails/generators/joosy/templates/app_resources_predefiner.js.coffee.erb +11 -0
  61. data/lib/rails/generators/joosy/templates/preload.html.erb +5 -6
  62. data/lib/rails/generators/joosy/templates/preload.html.haml +3 -5
  63. data/lib/rails/generators/joosy/widget_generator.rb +8 -1
  64. data/lib/rails/resources_with_joosy.rb +11 -0
  65. data/spec/javascripts/helpers/spec_helper.js.coffee +25 -0
  66. data/spec/javascripts/joosy/core/application_spec.js.coffee +40 -0
  67. data/spec/javascripts/joosy/core/form_spec.js.coffee +200 -0
  68. data/spec/javascripts/joosy/core/helpers/forms_spec.js.coffee +103 -0
  69. data/spec/javascripts/joosy/core/helpers/view_spec.js.coffee +10 -0
  70. data/spec/javascripts/joosy/core/joosy_spec.js.coffee +97 -0
  71. data/spec/javascripts/joosy/core/layout_spec.js.coffee +50 -0
  72. data/spec/javascripts/joosy/core/modules/container_spec.js.coffee +32 -27
  73. data/spec/javascripts/joosy/core/modules/events_spec.js.coffee +55 -18
  74. data/spec/javascripts/joosy/core/modules/filters_spec.js.coffee +28 -27
  75. data/spec/javascripts/joosy/core/modules/log_spec.js.coffee +3 -3
  76. data/spec/javascripts/joosy/core/modules/module_spec.js.coffee +6 -15
  77. data/spec/javascripts/joosy/core/modules/renderer_spec.js.coffee +203 -0
  78. data/spec/javascripts/joosy/core/modules/time_manager_spec.js.coffee +12 -7
  79. data/spec/javascripts/joosy/core/modules/widget_manager_spec.js.coffee +31 -17
  80. data/spec/javascripts/joosy/core/page_spec.js.coffee +178 -0
  81. data/spec/javascripts/joosy/core/resource/collection_spec.js.coffee +84 -0
  82. data/spec/javascripts/joosy/core/resource/generic_spec.js.coffee +149 -0
  83. data/spec/javascripts/joosy/core/resource/rest_collection_spec.js.coffee +31 -0
  84. data/spec/javascripts/joosy/core/resource/rest_spec.js.coffee +171 -0
  85. data/spec/javascripts/joosy/core/router_spec.js.coffee +143 -0
  86. data/spec/javascripts/joosy/core/templaters/rails_jst_spec.js.coffee +25 -0
  87. data/spec/javascripts/joosy/core/widget_spec.js.coffee +40 -0
  88. data/spec/javascripts/joosy/preloaders/caching_spec.js.coffee +36 -0
  89. data/spec/javascripts/joosy/preloaders/inline_spec.js.coffee +16 -0
  90. data/spec/javascripts/support/assets/coolface.jpg +0 -0
  91. data/spec/javascripts/support/assets/okay.jpg +0 -0
  92. data/spec/javascripts/support/assets/test.js +1 -0
  93. data/spec/javascripts/support/sinon-ie-1.3.1.js +82 -0
  94. data/vendor/assets/javascripts/jquery.form.js +978 -963
  95. data/vendor/assets/javascripts/metamorph.js +409 -0
  96. data/vendor/assets/javascripts/sugar.js +1057 -366
  97. metadata +95 -50
  98. data/README.rdoc +0 -3
  99. data/lib/joosy/forms.rb +0 -47
  100. data/lib/joosy-rails.rb +0 -5
  101. data/lib/rails/generators/joosy/templates/preload.html.slim +0 -21
  102. data/tmp/javascripts/.gitignore +0 -1
  103. data/tmp/spec/javascripts/helpers/.gitignore +0 -1
  104. data/vendor/assets/javascripts/base64.js +0 -135
  105. data/vendor/assets/javascripts/inflection.js +0 -656
  106. data/vendor/assets/javascripts/joosy/core/application.js.coffee +0 -26
  107. data/vendor/assets/javascripts/joosy/core/form.js.coffee +0 -87
  108. data/vendor/assets/javascripts/joosy/core/joosy.js.coffee +0 -62
  109. data/vendor/assets/javascripts/joosy/core/layout.js.coffee +0 -38
  110. data/vendor/assets/javascripts/joosy/core/modules/container.js.coffee +0 -51
  111. data/vendor/assets/javascripts/joosy/core/modules/events.js.coffee +0 -17
  112. data/vendor/assets/javascripts/joosy/core/modules/filters.js.coffee +0 -39
  113. data/vendor/assets/javascripts/joosy/core/modules/log.js.coffee +0 -12
  114. data/vendor/assets/javascripts/joosy/core/modules/module.js.coffee +0 -28
  115. data/vendor/assets/javascripts/joosy/core/modules/time_manager.js.coffee +0 -23
  116. data/vendor/assets/javascripts/joosy/core/modules/widgets_manager.js.coffee +0 -45
  117. data/vendor/assets/javascripts/joosy/core/page.js.coffee +0 -136
  118. data/vendor/assets/javascripts/joosy/core/resource/rest.js.coffee +0 -69
  119. data/vendor/assets/javascripts/joosy/core/resource/rest_collection.js.coffee +0 -42
  120. data/vendor/assets/javascripts/joosy/core/router.js.coffee +0 -75
  121. data/vendor/assets/javascripts/joosy/core/widget.js.coffee +0 -35
  122. data/vendor/assets/javascripts/joosy/preloader/development.js.coffee +0 -27
  123. data/vendor/assets/javascripts/joosy/preloader/production.js.coffee +0 -84
@@ -1,87 +0,0 @@
1
- #= require joosy/core/joosy
2
- #= require joosy/core/modules/module
3
- #= require joosy/core/modules/log
4
- #= require joosy/core/modules/events
5
- #= require joosy/core/modules/container
6
-
7
- class Joosy.Form extends Joosy.Module
8
- @include Joosy.Modules.Log
9
- @include Joosy.Modules.Events
10
- @include Joosy.Modules.Container
11
-
12
- invalidationClass: 'field_with_errors'
13
- substitutions: {}
14
-
15
- elements:
16
- 'fields': 'input,select,textarea'
17
-
18
- constructor: (form, opts={}) ->
19
- if Object.isFunction opts
20
- @success = opts
21
- else
22
- Object.extended(opts).each (key, value) =>
23
- @[key] = value
24
-
25
- @container = $(form)
26
- @refreshElements()
27
- @__delegateEvents()
28
-
29
- if (method = @container.attr('method')?.toLowerCase()) && !['get', 'post'].has(method)
30
- @__markMethod method
31
- @container.attr 'method', 'POST'
32
-
33
- @container.ajaxForm
34
- dataType: 'json'
35
- beforeSend: => @__before(arguments...)
36
- success: => @__success(arguments...)
37
- error: => @__error(arguments...)
38
- xhr: =>
39
- xhr = $.ajaxSettings.xhr()
40
- if xhr.upload? && @progress
41
- xhr.upload.onprogress = (event) =>
42
- @progress (event.position/event.total*100).round(2) if event.lengthComputable
43
- xhr
44
-
45
- fill: (resource, decorator) ->
46
- e = if decorator? then decorator(resource.e) else resource.e
47
- Object.each e, (key, val) =>
48
- key = resource.constructor.entityName()+"[#{key}]"
49
- @fields.filter("[name='#{key.underscore()}']:not(:file),[name='#{key.camelize(false)}']:not(:file)").val(val)
50
-
51
- @container.attr 'action', resource.constructor.__buildSource(extension: resource.id)
52
- @__markMethod() if resource.id
53
- @container.attr 'method', 'POST'
54
-
55
- __success: (response, status, xhr) ->
56
- if xhr
57
- @success(response)
58
- else if response.status == 200
59
- @success(response.json)
60
- else
61
- @__error(response.json)
62
-
63
- __before: (xhr, settings) ->
64
- if !@before? || @before(arguments...) is true
65
- @fields.removeClass(@invalidationClass)
66
-
67
- __error: (data) ->
68
- errors = if data.responseText
69
- try
70
- Object.extended(jQuery.parseJSON(data.responseText))
71
- catch error
72
- Object.extended()
73
- else
74
- Object.extended(data)
75
-
76
- if !@error? || @error(errors) is true
77
- errors.each (field, notifications) =>
78
- field = @substitutions[field] if @substitutions[field]?
79
- input = @fields.filter("[name='#{field}']").addClass(@invalidationClass)
80
- @notification?(input, notifications)
81
-
82
- __markMethod: (method='PUT') ->
83
- method = $ '<input/>',
84
- type: 'hidden'
85
- name: '_method'
86
- value: method
87
- @container.append(method)
@@ -1,62 +0,0 @@
1
- @Joosy = Object.extended(if @Joosy? then @Joosy else {})
2
-
3
- @Joosy.merge
4
- debug: false
5
- Modules: {}
6
- Resource: {}
7
-
8
- Joosy.Beautifier =
9
- beautifiers: []
10
-
11
- add: (callback) -> @beautifiers.push(callback)
12
- go: -> b() for b in @beautifiers
13
-
14
- Joosy.namespace = (name, generator=false) ->
15
- name = name.split('.')
16
- space = window
17
- space = space[part] ?= {} for part in name
18
-
19
- generator = generator.apply(space) if generator?
20
-
21
- Joosy.test = ->
22
- text = "Hi :). I'm Joosy. And everything is just fine!"
23
-
24
- if console
25
- console.log text
26
- else
27
- alert text
28
-
29
- Joosy.uuid = ->
30
- 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace /[xy]/g, (c) ->
31
- r = Math.random() * 16 | 0
32
- v = if c is 'x' then r else r & 3 | 8
33
- v.toString 16
34
- .toUpperCase()
35
-
36
- Joosy.preloadImages = (images, callback) ->
37
- callback() if images.length == 0
38
-
39
- ticks = images.length
40
- result = []
41
-
42
- for p in images
43
- result.push $('<img/>').load(->
44
- callback?() if (ticks -= 1) == 0
45
- ).attr('src', p)
46
-
47
- return result
48
-
49
- Joosy.buildUrl = (url, params) ->
50
- paramsString = []
51
- Object.each params, (key, value) -> paramsString.push("#{key}=#{value}")
52
- paramsString = paramsString.join('&')
53
-
54
- hash = url.match(/(\#.*)?$/)[0]
55
-
56
- url = url.replace(/\#.*$/, '')
57
-
58
- url = url + "?" if paramsString != '' && url.indexOf("?") == -1
59
-
60
- url = (if paramsString == '' || url.last() == '?' then url+paramsString else url+'&'+paramsString) + hash
61
-
62
- return url
@@ -1,38 +0,0 @@
1
- #= require joosy/core/joosy
2
- #= require joosy/core/modules/module
3
- #= require joosy/core/modules/log
4
- #= require joosy/core/modules/events
5
- #= require joosy/core/modules/container
6
- #= require joosy/core/modules/time_manager
7
- #= require joosy/core/modules/widgets_manager
8
- #= require joosy/core/modules/filters
9
-
10
- class Joosy.Layout extends Joosy.Module
11
- @include Joosy.Modules.Log
12
- @include Joosy.Modules.Events
13
- @include Joosy.Modules.Container
14
- @include Joosy.Modules.TimeManager
15
- @include Joosy.Modules.WidgetsManager
16
- @include Joosy.Modules.Filters
17
-
18
- constructor: ->
19
- @view ||= JST['app/templates/layouts/default']
20
-
21
- navigate: (args...) -> Joosy.Router.navigate(args...)
22
-
23
- __load: (@container) ->
24
- @refreshElements()
25
- @__delegateEvents()
26
- @__setupWidgets()
27
- @__runAfterLoads()
28
-
29
- __unload: ->
30
- @clearTime()
31
- @__unloadWidgets()
32
- @__runAfterUnloads()
33
-
34
- yield: ->
35
- @uuid = Joosy.uuid()
36
-
37
- content: ->
38
- $("##{@uuid}")
@@ -1,51 +0,0 @@
1
- Joosy.Modules.Container =
2
- events: false
3
- elements: false
4
-
5
- eventSplitter: /^(\S+)\s*(.*)$/
6
-
7
- $: (selector) -> $(selector, @container)
8
-
9
- refreshElements: ->
10
- @__collectElements().each (key, value) => @[key] = @$(value)
11
-
12
- swapContainer: (container, data) ->
13
- realContainer = container.clone().html(data)
14
- container.replaceWith realContainer
15
- return realContainer
16
-
17
- __collectElements: ->
18
- elements = Object.extended(@elements || {})
19
- klass = @
20
- while klass = klass.constructor.__super__
21
- elements.merge(klass.elements, false)
22
- elements
23
-
24
- __collectEvents: ->
25
- events = Object.extended(@events || {})
26
- klass = @
27
- while klass = klass.constructor.__super__
28
- events.merge(klass.events, false)
29
- events
30
-
31
- __extractSelector: (selector) ->
32
- if r = selector.match(/\$([A-z]+)/)
33
- selector = @__collectElements()[r[1]]
34
- selector
35
-
36
- __delegateEvents: ->
37
- module = @
38
- events = @__collectEvents()
39
-
40
- events.each (key, method) =>
41
- method = @[method] unless typeof(method) is 'function'
42
- callback = (event) -> method.call(module, @, event)
43
-
44
- match = key.match(@eventSplitter)
45
- eventName = match[1]
46
- selector = @__extractSelector(match[2])
47
-
48
- if selector is ''
49
- @container.bind(eventName, callback)
50
- else
51
- @container.on(eventName, selector, callback)
@@ -1,17 +0,0 @@
1
- Joosy.Modules.Events =
2
- wait: (events, callback) ->
3
- events = events.split(' ')
4
- @__eventWaiters ||= []
5
-
6
- @__eventWaiters.push [events, callback]
7
-
8
- trigger: (event) ->
9
- return unless @__eventWaiters
10
-
11
- @__eventWaiters.each (entry, i) =>
12
- index = entry[0].indexOf(event)
13
- delete entry[0][index] if index >= 0
14
-
15
- if entry[0].compact().length == 0
16
- delete @__eventWaiters[i] and @__eventWaiters = @__eventWaiters.compact()
17
- entry[1].call()
@@ -1,39 +0,0 @@
1
- Joosy.Modules.Filters =
2
- included: ->
3
- @beforeLoad = (callback) ->
4
- unless @::hasOwnProperty('__beforeLoads')
5
- @::__beforeLoads = [].concat @.__super__.__beforeLoads || []
6
- @::__beforeLoads.push callback
7
-
8
- @afterLoad = (callback) ->
9
- unless @::hasOwnProperty('__afterLoads')
10
- @::__afterLoads = [].concat @.__super__.__afterLoads || []
11
- @::__afterLoads.push callback
12
-
13
- @afterUnload = (callback) ->
14
- unless @::hasOwnProperty('__afterUnloads')
15
- @::__afterUnloads = [].concat @.__super__.__afterUnloads || []
16
- @::__afterUnloads.push callback
17
-
18
- __runBeforeLoads: (opts...) ->
19
- return true unless @__beforeLoads?.length > 0
20
-
21
- flag = true
22
-
23
- for filter in @__beforeLoads
24
- filter = @[filter] unless typeof(filter) is 'function'
25
- flag = flag && filter.apply(@, opts)
26
-
27
- return flag
28
-
29
- __runAfterLoads: (opts...) ->
30
- if @__afterLoads?
31
- for filter in @__afterLoads
32
- filter = @[filter] unless typeof(filter) is 'function'
33
- filter.apply(@, opts)
34
-
35
- __runAfterUnloads: (opts...) ->
36
- if @__afterUnloads?
37
- for filter in @__afterUnloads
38
- filter = @[filter] unless typeof(filter) is 'function'
39
- filter.apply(@, opts)
@@ -1,12 +0,0 @@
1
- Joosy.Modules.Log =
2
- log: (args...) ->
3
- return if typeof console is 'undefined'
4
-
5
- if console.log.apply?
6
- args.unshift "Joosy>"
7
- console.log(args...)
8
- else
9
- console.log(args.first())
10
-
11
- debug: (args...) ->
12
- @log(args...) if Joosy.debug
@@ -1,28 +0,0 @@
1
- moduleKeywords = ['included', 'extended']
2
-
3
- class Joosy.Module
4
- @hasAncestor = (what, klass) ->
5
- [ what, klass ] = [ what.prototype, klass.prototype ]
6
- while what
7
- return true if what == klass
8
- what = what.constructor?.__super__
9
- false
10
-
11
- @include: (obj) ->
12
- throw('include(obj) requires obj') unless obj
13
- Object.extended(obj).each (key, value) =>
14
- if key not in moduleKeywords
15
- @::[key] = value
16
- obj.included?.apply(@)
17
- @
18
-
19
- @extend: (obj) ->
20
- throw('extend(obj) requires obj') unless obj
21
- Object.extended(obj).each (key, value) =>
22
- if key not in moduleKeywords
23
- @[key] = value
24
- obj.extended?.apply(@)
25
- @
26
-
27
- constructor: ->
28
- @init?(arguments...)
@@ -1,23 +0,0 @@
1
- Joosy.Modules.TimeManager =
2
- setTimeout: (timeout, action) ->
3
- @__timeouts ||= []
4
-
5
- timer = window.setTimeout (=> action()), timeout
6
- @__timeouts.push timer
7
- timer
8
-
9
- setInterval: (delay, action) ->
10
- @__intervals ||= []
11
-
12
- timer = window.setInterval (=> action()), delay
13
- @__intervals.push timer
14
- timer
15
-
16
- clearTime: ->
17
- if @__intervals
18
- for entry in @__intervals
19
- window.clearInterval entry
20
-
21
- if @__timeouts
22
- for entry in @__timeouts
23
- window.clearTimeout entry
@@ -1,45 +0,0 @@
1
- Joosy.Modules.WidgetsManager =
2
- registerWidget: (container, widget) ->
3
- @__activeWidgets ||= []
4
-
5
- if Joosy.Module.hasAncestor(widget, Joosy.Widget)
6
- widget = new widget()
7
-
8
- @__activeWidgets.push widget.__load(@, container)
9
- return widget
10
-
11
- unregisterWidget: (widget) ->
12
- widget.__unload()
13
- delete @__activeWidgets[@__activeWidgets.indexOf(widget)]
14
-
15
- __collectWidgets: ->
16
- widgets = Object.extended(@widgets || {})
17
- klass = @
18
- while klass = klass.constructor.__super__
19
- widgets.merge(klass.widgets, false)
20
- widgets
21
-
22
- __setupWidgets: ->
23
- widgets = @__collectWidgets()
24
-
25
- widgets.each (selector, widget) =>
26
- if selector == '$container'
27
- selector = @container
28
- else
29
- if r = selector.match(/\$([A-z]+)/)
30
- selector = @elements[r[1]]
31
- selector = $(selector, @container)
32
-
33
- Joosy.Modules.Log.debug "Widget registered at '#{selector.selector}'. Elements: #{selector.length}"
34
-
35
- selector.each (elem) =>
36
- if Joosy.Module.hasAncestor(widget, Joosy.Widget)
37
- w = new widget(@)
38
- else
39
- w = widget.apply(@, [elem])
40
-
41
- @registerWidget($(elem), w)
42
-
43
- __unloadWidgets: ->
44
- if @__activeWidgets
45
- widget?.__unload() for widget in @__activeWidgets
@@ -1,136 +0,0 @@
1
- #= require joosy/core/joosy
2
- #= require joosy/core/modules/module
3
- #= require joosy/core/modules/log
4
- #= require joosy/core/modules/events
5
- #= require joosy/core/modules/container
6
- #= require joosy/core/modules/time_manager
7
- #= require joosy/core/modules/widgets_manager
8
- #= require joosy/core/modules/filters
9
-
10
- class Joosy.Page extends Joosy.Module
11
- @include Joosy.Modules.Log
12
- @include Joosy.Modules.Events
13
- @include Joosy.Modules.Container
14
- @include Joosy.Modules.TimeManager
15
- @include Joosy.Modules.WidgetsManager
16
- @include Joosy.Modules.Filters
17
-
18
- layout: false
19
- previous: false
20
- params: false
21
- source: false
22
- data: false
23
-
24
- @fetch: (callback) -> @::__fetch = callback
25
- @scroll: (element, options={}) ->
26
- @::__scrollElement = element
27
- @::__scrollSpeed = options.speed || 500
28
- @::__scrollMargin = options.margin || 0
29
-
30
- @beforePageRender: (callback) -> @::__beforePageRender = callback
31
- @afterPageRender: (callback) -> @::__afterPageRender = callback
32
- @onPageRender: (callback) -> @::__onPageRender = callback
33
-
34
- @beforeLayoutRender: (callback) -> @::__beforeLayoutRender = callback
35
- @afterLayoutRender: (callback) -> @::__afterLayoutRender = callback
36
- @onLayoutRender: (callback) -> @::__onLayoutRender = callback
37
-
38
- constructor: (@params, @previous) ->
39
- @layout ||= ApplicationLayout
40
-
41
- if @__runBeforeLoads(@params, @previous)
42
- if @previous?.layout not instanceof @layout
43
- @__renderLayout()
44
- else
45
- @__render()
46
-
47
- navigate: (args...) -> Joosy.Router.navigate(args...)
48
-
49
- __load: ->
50
- @refreshElements()
51
- @__delegateEvents()
52
- @__setupWidgets()
53
- @__runAfterLoads(@params, @previous)
54
- if @__scrollElement
55
- scroll = $(@__extractSelector(@__scrollElement)).offset()?.top + @__scrollMargin
56
- $('html, body').animate({scrollTop: scroll}, @__scrollSpeed)
57
-
58
- __unload: ->
59
- @clearTime()
60
- @__unloadWidgets()
61
- @__runAfterUnloads(@params, @previous)
62
-
63
- __render: ->
64
- @layout = @previous.layout
65
-
66
- @wait "stageClear dataReceived", =>
67
- @previous?.__unload()
68
-
69
- render = =>
70
- @swapContainer @layout.content(), @view(@data)
71
- @container = @layout.content()
72
-
73
- @__load()
74
- Joosy.Beautifier.go()
75
-
76
- if @__afterPageRender?
77
- @__afterPageRender @layout.content()
78
-
79
- return @layout.content()
80
-
81
- if @__onPageRender?
82
- @__onPageRender @layout.content(), render
83
- else
84
- render()
85
-
86
- if @__beforePageRender?
87
- @__beforePageRender @layout.content(), =>
88
- @trigger 'stageClear'
89
- else
90
- @trigger 'stageClear'
91
-
92
- if @__fetch?
93
- @__fetch =>
94
- @trigger 'dataReceived'
95
- else
96
- @trigger 'dataReceived'
97
-
98
- __renderLayout: ->
99
- @layout = new @layout
100
-
101
- @wait "stageClear dataReceived", =>
102
-
103
- @previous?.layout?.__unload?()
104
- @previous?.__unload()
105
-
106
- render = =>
107
- @swapContainer Joosy.Application.content(), @layout.view(@data)
108
- @swapContainer @layout.content(), @view(@data)
109
- @container = @layout.content()
110
-
111
- @layout.__load Joosy.Application.content()
112
- @__load()
113
- Joosy.Beautifier.go()
114
-
115
- if @__afterLayoutRender?
116
- @__afterLayoutRender Joosy.Application.content()
117
-
118
- return Joosy.Application.content()
119
-
120
- if @__onLayoutRender?
121
- @__onLayoutRender Joosy.Application.content(), render
122
- else
123
- render()
124
-
125
-
126
- if @__beforeLayoutRender?
127
- @__beforeLayoutRender Joosy.Application.content(), =>
128
- @trigger 'stageClear'
129
- else
130
- @trigger 'stageClear'
131
-
132
- if @__fetch?
133
- @__fetch =>
134
- @trigger 'dataReceived'
135
- else
136
- @trigger 'dataReceived'
@@ -1,69 +0,0 @@
1
- #= require ./rest_collection
2
-
3
- class Joosy.Resource.REST extends Joosy.Module
4
- @include Joosy.Modules.Log
5
- @include Joosy.Modules.Events
6
-
7
- __primaryKey: 'id'
8
-
9
- @entity: (name) -> @__entityName = name
10
- @primary: (primary) -> @::__primaryKey = primary
11
- @source: (source) -> @__source = source
12
- @beforeLoad: (action) -> @::__beforeLoad = action
13
-
14
- constructor: (description) ->
15
- if @constructor.__isId(description)
16
- @id = description
17
- else
18
- @__fillData(description)
19
- @id = @e[@__primaryKey]
20
-
21
- @entityName: -> @__entityName ?= @name.underscore()
22
-
23
- # Returns single entity if int/string given
24
- # Returns collection if no value or Object (with parameters) given
25
- @find: (description, callback, options) ->
26
- if @__isId(description)
27
- resource = new @(description)
28
- resource.fetch callback, options
29
- resource
30
- else
31
- resources = new Joosy.Resource.RESTCollection(@, description)
32
- resources.fetch callback, options
33
- resources
34
-
35
- fetch: (callback, options) ->
36
- @constructor.__ajax 'get', @constructor.__buildSource(extension: @id), options, (e) =>
37
- @__fillData(e)
38
- callback?(@)
39
- @
40
-
41
- save: ->
42
-
43
- destroy: (callback, options) ->
44
- @constructor.__ajax 'delete', @constructor.__buildSource(extension: @id), options, (e) =>
45
- callback?(@)
46
- @
47
-
48
- @__isId: (something) -> Object.isNumber(something) || Object.isString(something)
49
-
50
- @__ajax: (method, url, options={}, callback) ->
51
- $.ajax url, Object.extended(
52
- type: method
53
- success: callback
54
- cache: false
55
- dataType: 'json'
56
- ).merge options
57
-
58
- @__buildSource: (options) ->
59
- @__source ?= "/"+@entityName().pluralize()
60
- source = Joosy.buildUrl("#{@__source}/#{options.extension || ''}", options.params)
61
-
62
- __fillData: (data) ->
63
- data = Object.extended(data)
64
- data = @__beforeLoad(data) if @__beforeLoad?
65
-
66
- @e = if Object.isObject(data) && data[@constructor.entityName()] && data.keys().length == 1
67
- data[@constructor.entityName()]
68
- else
69
- data
@@ -1,42 +0,0 @@
1
- class Joosy.Resource.RESTCollection extends Joosy.Module
2
- @include Joosy.Modules.Log
3
- @include Joosy.Modules.Events
4
-
5
- data: []
6
- pages: Object.extended()
7
-
8
- constructor: (@model, params={}) -> @params = Object.extended(params)
9
-
10
- # Clears the storage and atempts to import given JSON
11
- reset: (entities) ->
12
- @data = data = @modelize entities
13
- @pages = Object.extended().merge 1: data
14
- @
15
-
16
- # Clears the storage and gets new data from server
17
- fetch: (callback, options) ->
18
- @model.__ajax 'get', @model.__buildSource(params: @params), options, (data) =>
19
- @reset(data)
20
- callback?(@)
21
- @
22
-
23
- # Returns the subset for requested page. Requests with &page=x if not found localy.
24
- page: (number, callback=false) ->
25
- if @pages[number]?
26
- callback?(@pages[number])
27
- return @
28
-
29
- @model.__ajax 'get', @model.__buildSource(params: @params.merge(page: number)), (data) =>
30
- @pages[number] = @modelize data
31
-
32
- @data = []
33
- @pages.keys().sort().each (x) => @data.add @pages[x]
34
-
35
- callback?(@pages[number])
36
- @
37
-
38
- modelize: (collection) ->
39
- if collection instanceof Array
40
- collection.map (x) => new @model(x)
41
- else
42
- []