nali 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +19 -14
- data/lib/client/javascripts/nali/application.js.coffee +59 -0
- data/lib/client/javascripts/nali/collection.js.coffee +188 -0
- data/lib/{assets → client}/javascripts/nali/connection.js.coffee +32 -32
- data/lib/{assets → client}/javascripts/nali/controller.js.coffee +28 -30
- data/lib/{assets → client}/javascripts/nali/cookie.js.coffee +5 -5
- data/lib/client/javascripts/nali/deferred.js.coffee +16 -0
- data/lib/client/javascripts/nali/extensions.js.coffee +18 -0
- data/lib/{assets → client}/javascripts/nali/index.js +2 -1
- data/lib/{assets → client}/javascripts/nali/jbone.min.js +1 -1
- data/lib/{assets → client}/javascripts/nali/model.js.coffee +208 -118
- data/lib/{assets → client}/javascripts/nali/nali.js.coffee +32 -32
- data/lib/client/javascripts/nali/notice.js.coffee +29 -0
- data/lib/{assets → client}/javascripts/nali/router.js.coffee +20 -22
- data/lib/{assets → client}/javascripts/nali/view.js.coffee +63 -60
- data/lib/generator/Gemfile +8 -8
- data/lib/generator/app/{assets → client}/javascripts/application.js.coffee +1 -1
- data/lib/generator/app/{assets → client}/javascripts/controllers/homes.js.coffee +4 -4
- data/lib/generator/app/client/javascripts/models/home.js.coffee +5 -0
- data/lib/generator/app/client/javascripts/views/home/index.js.coffee +9 -0
- data/lib/generator/app/{assets → client}/stylesheets/application.css.sass +1 -1
- data/lib/generator/app/{assets → client}/stylesheets/home/index.css.sass +5 -5
- data/lib/generator/app/{assets → client}/stylesheets/notices/error.css.sass +1 -1
- data/lib/generator/app/{assets → client}/stylesheets/notices/info.css.sass +1 -1
- data/lib/generator/app/{assets → client}/stylesheets/notices/warning.css.sass +1 -1
- data/lib/generator/app/{templates → client/templates}/application.html.erb +1 -1
- data/lib/generator/app/{templates → client/templates}/home/index.html +1 -1
- data/lib/generator/app/client/templates/notice/error.html +1 -0
- data/lib/generator/app/client/templates/notice/info.html +1 -0
- data/lib/generator/app/client/templates/notice/warning.html +1 -0
- data/lib/generator/app/server/clients.rb +15 -0
- data/lib/generator/app/{controllers → server/controllers}/application_controller.rb +2 -2
- data/lib/generator/app/{models → server/models}/access.yml +0 -0
- data/lib/generator/{config → app/server}/routes.rb +3 -3
- data/lib/generator/config/environments/development.rb +2 -2
- data/lib/generator/config/environments/production.rb +4 -4
- data/lib/generator/config/environments/test.rb +5 -5
- data/lib/nali/application.rb +24 -23
- data/lib/nali/connection.rb +14 -31
- data/lib/nali/controller.rb +36 -16
- data/lib/nali/generator.rb +38 -23
- data/lib/nali/helpers.rb +3 -3
- data/lib/nali/model.rb +36 -22
- data/lib/nali/tasks.rb +16 -25
- data/lib/nali/version.rb +2 -2
- metadata +85 -86
- data/lib/assets/javascripts/nali/application.js.coffee +0 -28
- data/lib/assets/javascripts/nali/collection.js.coffee +0 -150
- data/lib/assets/javascripts/nali/extensions.js.coffee +0 -19
- data/lib/assets/javascripts/nali/notice.js.coffee +0 -14
- data/lib/generator/app/assets/javascripts/models/home.js.coffee +0 -3
- data/lib/generator/app/assets/javascripts/views/home/index.js.coffee +0 -1
- data/lib/generator/app/templates/notice/error.html +0 -1
- data/lib/generator/app/templates/notice/info.html +0 -1
- data/lib/generator/app/templates/notice/warning.html +0 -1
- data/lib/generator/config/clients.rb +0 -19
- data/lib/generator/config/environments/development.rb~ +0 -32
- data/lib/generator/config/environments/production.rb~ +0 -80
@@ -1,40 +1,40 @@
|
|
1
1
|
window.__ = ( args... ) -> console.log args...
|
2
2
|
|
3
|
-
window.Nali =
|
4
|
-
|
3
|
+
window.Nali =
|
4
|
+
|
5
5
|
_name: 'Nali'
|
6
|
-
extensions: {}
|
7
|
-
|
6
|
+
extensions: {}
|
7
|
+
|
8
8
|
starting: ->
|
9
9
|
for name, extension of @extensions
|
10
10
|
extension.runExtensions()
|
11
11
|
extension.initialize() if extension.hasOwnProperty 'initialize'
|
12
12
|
@starting.call extension
|
13
13
|
@
|
14
|
-
|
14
|
+
|
15
15
|
extend: ( params ) ->
|
16
16
|
for name, param of params
|
17
17
|
param._name = name
|
18
18
|
@[ name ] = @extensions[ name ] = @child param
|
19
19
|
@[ name ].extensions = {}
|
20
|
-
|
20
|
+
|
21
21
|
clone: ( params = {} ) ->
|
22
22
|
obj = @child params
|
23
23
|
obj.runCloning()
|
24
24
|
obj
|
25
|
-
|
25
|
+
|
26
26
|
child: ( params ) ->
|
27
27
|
obj = Object.create @
|
28
28
|
obj :: = @
|
29
29
|
obj[ name ] = value for name, value of params
|
30
30
|
obj.initObservation()
|
31
31
|
obj
|
32
|
-
|
32
|
+
|
33
33
|
expand: ( obj ) ->
|
34
34
|
if obj instanceof Object then @[ key ] = value for own key, value of obj
|
35
35
|
else console.error "Expand of %O error - argument is not Object", @
|
36
36
|
@
|
37
|
-
|
37
|
+
|
38
38
|
copy: ( obj ) ->
|
39
39
|
copy = {}
|
40
40
|
copy[ property ] = value for own property, value of obj
|
@@ -43,29 +43,29 @@ window.Nali =
|
|
43
43
|
childOf: ( parent ) ->
|
44
44
|
if parent instanceof Object
|
45
45
|
return false unless @::?
|
46
|
-
return true if @:: is parent
|
46
|
+
return true if ( @:: ) is parent
|
47
47
|
else
|
48
48
|
return false unless @::?._name?
|
49
49
|
return true if @::_name is parent
|
50
50
|
@::childOf parent
|
51
|
-
|
51
|
+
|
52
52
|
runExtensions: ( context = @ ) ->
|
53
|
-
@::?.runExtensions context
|
53
|
+
@::?.runExtensions context
|
54
54
|
@extension.call context if @hasOwnProperty 'extension'
|
55
|
-
|
55
|
+
|
56
56
|
runCloning: ( context = @ ) ->
|
57
|
-
@::?.runCloning context
|
57
|
+
@::?.runCloning context
|
58
58
|
@cloning.call context if @hasOwnProperty 'cloning'
|
59
|
-
|
59
|
+
|
60
60
|
getter: ( property, callback ) ->
|
61
61
|
@__defineGetter__ property, callback
|
62
62
|
@
|
63
|
-
|
63
|
+
|
64
64
|
setter: ( property, callback ) ->
|
65
65
|
@__defineSetter__ property, callback
|
66
66
|
@
|
67
67
|
|
68
|
-
access: ( obj
|
68
|
+
access: ( obj ) ->
|
69
69
|
for own property of obj
|
70
70
|
do( property ) =>
|
71
71
|
@getter property, -> obj[ property ]
|
@@ -76,31 +76,31 @@ window.Nali =
|
|
76
76
|
@observers = []
|
77
77
|
@observables = []
|
78
78
|
@
|
79
|
-
|
79
|
+
|
80
80
|
destroyObservation: ->
|
81
81
|
@unsubscribeAll()
|
82
82
|
@unsubscribeFromAll()
|
83
83
|
@
|
84
|
-
|
84
|
+
|
85
85
|
addObservationItem: ( to, obj, event, callback ) ->
|
86
86
|
return @ for item in @[ to ] when item[0] is obj and item[1] is event and item[2] in [ undefined, callback ]
|
87
87
|
@[ to ].push if callback then [ obj, event, callback ] else [ obj, event ]
|
88
88
|
@
|
89
|
-
|
89
|
+
|
90
90
|
removeObservationItem: ( from, obj, event ) ->
|
91
91
|
for item in @[ from ][..] when item[0] is obj and ( item[1] is event or not event )
|
92
92
|
@[ from ].splice @[ from ].indexOf( item ), 1
|
93
93
|
@
|
94
|
-
|
94
|
+
|
95
95
|
subscribe: ( observer, event, callback ) ->
|
96
96
|
@addObservationItem 'observers', observer, event, callback
|
97
97
|
observer.addObservationItem 'observables', @, event, callback
|
98
98
|
@
|
99
|
-
|
99
|
+
|
100
100
|
subscribeTo: ( observable, event, callback ) ->
|
101
101
|
observable.subscribe @, event, callback
|
102
102
|
@
|
103
|
-
|
103
|
+
|
104
104
|
subscribeOne: ( observer, event, callback ) ->
|
105
105
|
callbackOne = ( args... ) =>
|
106
106
|
callback.call observer, args...
|
@@ -111,24 +111,24 @@ window.Nali =
|
|
111
111
|
subscribeOneTo: ( observable, event, callback ) ->
|
112
112
|
observable.subscribeOne @, event, callback
|
113
113
|
@
|
114
|
-
|
114
|
+
|
115
115
|
unsubscribe: ( observer, event ) ->
|
116
116
|
@removeObservationItem 'observers', observer, event
|
117
117
|
observer.removeObservationItem 'observables', @, event
|
118
118
|
@
|
119
|
-
|
120
|
-
|
119
|
+
|
120
|
+
unsubscribeFrom: ( observable, event ) ->
|
121
121
|
observable.unsubscribe @, event
|
122
122
|
@
|
123
|
-
|
123
|
+
|
124
124
|
unsubscribeAll: ( event ) ->
|
125
125
|
@unsubscribe item[0], event for item in @observers[..]
|
126
126
|
@
|
127
|
-
|
127
|
+
|
128
128
|
unsubscribeFromAll: ( event ) ->
|
129
|
-
@
|
129
|
+
@unsubscribeFrom item[0], event for item in @observables[..]
|
130
130
|
@
|
131
|
-
|
131
|
+
|
132
132
|
trigger: ( event, args... ) ->
|
133
|
-
item[2].call item[0], args... for item in @observers[..] when item[1] is event
|
134
|
-
@
|
133
|
+
item[2].call item[0], @, args... for item in @observers[..] when item[1] is event
|
134
|
+
@
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Nali.Model.extend Notice:
|
2
|
+
|
3
|
+
initialize: ->
|
4
|
+
@::::expand Notice: @
|
5
|
+
|
6
|
+
prepare: ( params ) ->
|
7
|
+
params = message: params if typeof params is 'string'
|
8
|
+
params
|
9
|
+
|
10
|
+
info: ( params ) ->
|
11
|
+
@new( @prepare params ).show 'info'
|
12
|
+
|
13
|
+
warning: ( params ) ->
|
14
|
+
@new( @prepare params ).show 'warning'
|
15
|
+
|
16
|
+
error: ( params ) ->
|
17
|
+
@new( @prepare params ).show 'error'
|
18
|
+
|
19
|
+
|
20
|
+
Nali.View.extend
|
21
|
+
|
22
|
+
NoticeInfo:
|
23
|
+
onShow: -> @hide 3000
|
24
|
+
|
25
|
+
NoticeWarning:
|
26
|
+
onShow: -> @hide 3000
|
27
|
+
|
28
|
+
NoticeError:
|
29
|
+
onShow: -> @hide 3000
|
@@ -1,12 +1,11 @@
|
|
1
1
|
Nali.extend Router:
|
2
2
|
|
3
3
|
initialize: ->
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
@::expand redirect: ( args... ) => @go args...
|
5
|
+
@
|
6
|
+
|
8
7
|
routes: {}
|
9
|
-
|
8
|
+
|
10
9
|
start: ->
|
11
10
|
@scanRoutes()
|
12
11
|
@_( window ).on 'popstate', ( event ) =>
|
@@ -14,19 +13,18 @@ Nali.extend Router:
|
|
14
13
|
event.stopPropagation()
|
15
14
|
@saveHistory false
|
16
15
|
@go event.target.location.pathname
|
17
|
-
@go()
|
18
16
|
@
|
19
17
|
|
20
18
|
scanRoutes: ->
|
21
19
|
for name, controller of @Controller.extensions when controller.actions?
|
22
20
|
route = '^'
|
23
|
-
route += name.
|
21
|
+
route += name.lower().replace /s$/, 's*(\/|$)'
|
24
22
|
route += '('
|
25
|
-
route += Object.keys( controller._actions ).join '|'
|
23
|
+
route += Object.keys( controller._actions ).join '|'
|
26
24
|
route += ')?'
|
27
25
|
@routes[ route ] = controller
|
28
26
|
@
|
29
|
-
|
27
|
+
|
30
28
|
go: ( url = window.location.pathname, options = {} ) ->
|
31
29
|
if found = @findRoute @prepare( url ) or @prepare( @Application.defaultUrl )
|
32
30
|
{ controller, action, filters, params } = found
|
@@ -36,39 +34,39 @@ Nali.extend Router:
|
|
36
34
|
@go @Application.notFoundUrl
|
37
35
|
else console.warn "Not exists route to the address %s", url
|
38
36
|
@
|
39
|
-
|
37
|
+
|
40
38
|
prepare: ( url ) ->
|
41
39
|
url = url.replace "http://#{ window.location.host }", ''
|
42
40
|
url = url[ 1.. ] or '' if url and url[ 0...1 ] is '/'
|
43
41
|
url = url[ ...-1 ] or '' if url and url[ -1.. ] is '/'
|
44
42
|
url
|
45
|
-
|
43
|
+
|
46
44
|
findRoute: ( url ) ->
|
47
45
|
for route, controller of @routes when match = url.match new RegExp route, 'i'
|
48
46
|
segments = ( @routedUrl = url ).split( '/' )[ 1... ]
|
49
|
-
if segments[0] in Object.keys( controller._actions )
|
50
|
-
action = segments.shift()
|
51
|
-
else unless action = controller.actions.default
|
47
|
+
if segments[0] in Object.keys( controller._actions )
|
48
|
+
action = segments.shift()
|
49
|
+
else unless action = controller.actions.default
|
52
50
|
console.error 'Unspecified controller action'
|
53
51
|
filters = {}
|
54
52
|
for name in controller._actions[ action ].filters when segments[0]?
|
55
|
-
filters[ name ] = segments.shift()
|
53
|
+
filters[ name ] = segments.shift()
|
56
54
|
params = {}
|
57
|
-
for name in controller._actions[ action ].params
|
58
|
-
params[ name ] = segments.shift()
|
55
|
+
for name in controller._actions[ action ].params
|
56
|
+
params[ name ] = if segments[0]? then segments.shift() else null
|
59
57
|
return controller: controller, action: action, filters: filters, params: params
|
60
58
|
false
|
61
|
-
|
59
|
+
|
62
60
|
saveHistory: ( value ) ->
|
63
61
|
@_saveHistory ?= true
|
64
62
|
if value in [ true, false ]
|
65
63
|
@_saveHistory = value
|
66
64
|
@
|
67
65
|
else @_saveHistory
|
68
|
-
|
69
|
-
|
66
|
+
|
67
|
+
changeUrl: ( url = null ) ->
|
70
68
|
if @saveHistory()
|
71
69
|
@routedUrl = url if url?
|
72
|
-
history.pushState null, null, '/' + ( @url = @routedUrl ) if @routedUrl isnt @url
|
70
|
+
history.pushState null, null, '/' + ( @url = @routedUrl ) if @routedUrl isnt @url
|
73
71
|
else @saveHistory true
|
74
|
-
@
|
72
|
+
@
|
@@ -1,35 +1,38 @@
|
|
1
1
|
Nali.extend View:
|
2
|
-
|
2
|
+
|
3
3
|
extension: ->
|
4
4
|
if @_name isnt 'View'
|
5
|
-
@_shortName = @_name.underscore().split( '_' )[ 1.. ].join '_'
|
5
|
+
@_shortName = @_name.underscore().split( '_' )[ 1.. ].join( '_' ).camel()
|
6
6
|
@parseTemplate()
|
7
|
-
@parseEvents()
|
8
|
-
@
|
9
|
-
|
7
|
+
@parseEvents()
|
8
|
+
@
|
9
|
+
|
10
10
|
cloning: ->
|
11
11
|
@my = @model
|
12
12
|
@
|
13
|
-
|
13
|
+
|
14
14
|
layout: -> null
|
15
|
-
|
15
|
+
|
16
16
|
onSourceUpdated: -> @draw()
|
17
|
-
|
17
|
+
|
18
18
|
onSourceDestroyed: -> @hide()
|
19
|
-
|
19
|
+
|
20
20
|
getOf: ( source, property ) ->
|
21
|
-
@
|
21
|
+
@redrawOn source, "update.#{ property }"
|
22
22
|
source[ property ]
|
23
|
-
|
23
|
+
|
24
|
+
redrawOn: ( source, event ) ->
|
25
|
+
@subscribeTo source, event, @onSourceUpdated
|
26
|
+
|
24
27
|
insertTo: ->
|
25
28
|
if ( layout = @layout() )?.childOf? 'View' then layout.show().element.find '.yield'
|
26
29
|
else @Application.htmlContainer
|
27
|
-
|
30
|
+
|
28
31
|
draw: ->
|
29
32
|
@runAssistants 'draw'
|
30
33
|
@onDraw?()
|
31
34
|
@
|
32
|
-
|
35
|
+
|
33
36
|
show: ( insertTo = @insertTo() ) ->
|
34
37
|
@prepareElement().draw().bindEvents()
|
35
38
|
unless @visible
|
@@ -43,7 +46,7 @@ Nali.extend View:
|
|
43
46
|
@visible = true
|
44
47
|
@runModelCallback 'afterShow'
|
45
48
|
@
|
46
|
-
|
49
|
+
|
47
50
|
hide: ( delay = 0 ) ->
|
48
51
|
if @visible
|
49
52
|
@runModelCallback 'beforeHide'
|
@@ -56,32 +59,32 @@ Nali.extend View:
|
|
56
59
|
@visible = false
|
57
60
|
@runModelCallback 'afterHide'
|
58
61
|
@
|
59
|
-
|
62
|
+
|
60
63
|
hideElement: ->
|
61
64
|
if @hideDelay? then setTimeout ( => @removeElement() ), @hideDelay else @removeElement()
|
62
65
|
@
|
63
|
-
|
66
|
+
|
64
67
|
removeElement: ->
|
65
68
|
@element[0].parentNode.removeChild @element[0]
|
66
69
|
@
|
67
|
-
|
70
|
+
|
68
71
|
runModelCallback: ( type ) ->
|
69
72
|
@model[ type ]?[ @_shortName ]?.call @model
|
70
73
|
@
|
71
|
-
|
74
|
+
|
72
75
|
showRelations: ->
|
73
76
|
for { selector, name, view } in @relationsMap
|
74
|
-
if ( relation = @model[ name ] )?
|
77
|
+
if ( relation = @model[ name ] )?
|
75
78
|
insertTo = @element.find selector
|
76
79
|
if relation.childOf 'Collection'
|
77
80
|
relation.show view, insertTo, true
|
78
|
-
relation.subscribeTo @, 'hide', relation.reset
|
81
|
+
relation.subscribeTo @, 'hide', relation.reset
|
79
82
|
else
|
80
83
|
view = relation.show view, insertTo
|
81
84
|
view.subscribeTo @, 'hide', view.hide
|
82
85
|
else console.warn "Relation %s does not exist of model %O", name, @model
|
83
86
|
@
|
84
|
-
|
87
|
+
|
85
88
|
runLink: ( event ) ->
|
86
89
|
event.preventDefault()
|
87
90
|
@runUrl event.currentTarget.getAttribute 'href'
|
@@ -89,9 +92,9 @@ Nali.extend View:
|
|
89
92
|
|
90
93
|
runForm: ( event ) ->
|
91
94
|
event.preventDefault()
|
92
|
-
@runUrl event.currentTarget.getAttribute( 'action' ), @formToHash event.currentTarget
|
95
|
+
@runUrl event.currentTarget.getAttribute( 'action' ), @formToHash event.currentTarget
|
93
96
|
@
|
94
|
-
|
97
|
+
|
95
98
|
runUrl: ( url, params = {} ) ->
|
96
99
|
if match = url.match /^(@@?)(.+)/
|
97
100
|
[ method, data ] = match[2].split '?'
|
@@ -104,7 +107,7 @@ Nali.extend View:
|
|
104
107
|
else console.warn "Method %s not exists", method
|
105
108
|
else @Router.go url, params
|
106
109
|
@
|
107
|
-
|
110
|
+
|
108
111
|
formToHash: ( form ) ->
|
109
112
|
params = {}
|
110
113
|
for element in form.elements
|
@@ -115,51 +118,51 @@ Nali.extend View:
|
|
115
118
|
target = if target[ key ] instanceof Object then target[ key ] else target[ key ] = {}
|
116
119
|
target[ property ] = element.value
|
117
120
|
params
|
118
|
-
|
121
|
+
|
119
122
|
parseEvents: ->
|
120
123
|
@eventsMap = []
|
121
124
|
if @events
|
122
125
|
@events = [ @events ] if typeof @events is 'string'
|
123
126
|
for event in @events
|
124
127
|
try
|
125
|
-
[ handlers, type, other ] = event.split /\s+(on|one)\s+/
|
126
|
-
[ events, selector ] = other.split /\s+at\s+/
|
128
|
+
[ handlers, type, other ] = event.split /\s+(on|one)\s+/
|
129
|
+
[ events, selector ] = other.split /\s+at\s+/
|
127
130
|
handlers = handlers.split /\s*,\s*/
|
128
131
|
events = events.replace /\s*,\s*/, ' '
|
129
132
|
throw true unless type and events.length and handlers.length
|
130
|
-
catch
|
133
|
+
catch
|
131
134
|
console.warn "Events parsing error: \"%s\" of %O", event, @
|
132
135
|
error = true
|
133
136
|
if error then error = false else @eventsMap.push [ selector, type, events, handlers ]
|
134
137
|
@
|
135
138
|
|
136
139
|
bindEvents: ->
|
137
|
-
unless @
|
140
|
+
unless @_eventsBinded?
|
138
141
|
@element.find( 'a' ).on 'click', ( event ) => @runLink event
|
139
142
|
@element.find( 'form' ).on 'submit', ( event ) => @runForm event
|
140
|
-
@element.on 'click', ( event ) => @runLink event if @element.is 'a'
|
141
|
-
@element.on 'submit', ( event ) => @runForm event if @element.is 'form'
|
143
|
+
@element.on 'click', ( event ) => @runLink event if @element.is 'a'
|
144
|
+
@element.on 'submit', ( event ) => @runForm event if @element.is 'form'
|
142
145
|
for [ selector, type, events, handlers ] in @eventsMap
|
143
146
|
for handler in handlers
|
144
147
|
do ( selector, type, events, handler ) =>
|
145
|
-
@element[ type ] events, selector, ( event ) => @[ handler ] event
|
146
|
-
@
|
148
|
+
@element[ type ] events, selector, ( event ) => @[ handler ] event
|
149
|
+
@_eventsBinded = true
|
147
150
|
@
|
148
|
-
|
151
|
+
|
149
152
|
prepareElement: ->
|
150
153
|
unless @element
|
151
154
|
@element = @_ @template
|
152
155
|
@element[0].view = @
|
153
156
|
@addAssistants()
|
154
157
|
@
|
155
|
-
|
158
|
+
|
156
159
|
getNode: ( path ) ->
|
157
160
|
node = @element[0]
|
158
161
|
node = node[ sub ] for sub in path
|
159
|
-
node
|
160
|
-
|
162
|
+
node
|
163
|
+
|
161
164
|
parseTemplate: ->
|
162
|
-
if container = document.querySelector '#' + @_name.underscore()
|
165
|
+
if container = document.querySelector '#' + @_name.underscore()
|
163
166
|
@template = container.innerHTML.trim().replace( /\s+/g, ' ' )
|
164
167
|
.replace( /({\s*\+.+?\s*})/g, ' <assist>$1</assist>' )
|
165
168
|
.replace( /{\s*yield\s*}/g, '<div class="yield"></div>' )
|
@@ -169,16 +172,16 @@ Nali.extend View:
|
|
169
172
|
container.parentNode.removeChild container
|
170
173
|
else console.warn 'Template %s not exists', @_name
|
171
174
|
@
|
172
|
-
|
175
|
+
|
173
176
|
parseRelations: ->
|
174
177
|
@relationsMap = []
|
175
|
-
@template = @template.replace /{\s*(\w+) of @(\w+)\s*}/g, ( match, view, relation ) =>
|
178
|
+
@template = @template.replace /{\s*(\w+) of @(\w+)\s*}/g, ( match, view, relation ) =>
|
176
179
|
className = relation.capitalize() + view.capitalize() + 'Relation'
|
177
180
|
@relationsMap.push selector: '.' + className, name: relation, view: view
|
178
181
|
"<div class=\"#{ className }\"></div>"
|
179
182
|
@parseAssistants()
|
180
183
|
@
|
181
|
-
|
184
|
+
|
182
185
|
parseAssistants: ->
|
183
186
|
@assistantsMap = []
|
184
187
|
if /{\s*.+?\s*}|bind=".+?"/.test @template
|
@@ -186,41 +189,41 @@ Nali.extend View:
|
|
186
189
|
tmp.innerHTML = @template
|
187
190
|
@scanAssistants tmp.children[0]
|
188
191
|
@
|
189
|
-
|
192
|
+
|
190
193
|
scanAssistants: ( node, path = [] ) ->
|
191
194
|
if node.nodeType is 3 and /{\s*.+?\s*}/.test node.textContent
|
192
195
|
@assistantsMap.push nodepath: path, type: 'Text'
|
193
196
|
else if node.nodeName is 'ASSIST'
|
194
197
|
@assistantsMap.push nodepath: path, type: 'Html'
|
195
|
-
else
|
198
|
+
else
|
196
199
|
if node.attributes
|
197
|
-
for attribute, index in node.attributes
|
200
|
+
for attribute, index in node.attributes
|
198
201
|
if attribute.name is 'bind'
|
199
202
|
@assistantsMap.push nodepath: path, type: 'Form'
|
200
203
|
else if /{\s*.+?\s*}/.test attribute.value
|
201
204
|
@assistantsMap.push nodepath: path.concat( 'attributes', index ), type: 'Attr'
|
202
205
|
@scanAssistants child, path.concat 'childNodes', index for child, index in node.childNodes
|
203
206
|
@
|
204
|
-
|
207
|
+
|
205
208
|
addAssistants: ->
|
206
209
|
@assistants = show: [], draw: [], hide: []
|
207
210
|
@[ "add#{ type }Assistant" ] @getNode nodepath for { nodepath, type } in @assistantsMap
|
208
211
|
@
|
209
|
-
|
212
|
+
|
210
213
|
runAssistants: ( type ) ->
|
211
214
|
assistant.call @ for assistant in @assistants[ type ]
|
212
215
|
@
|
213
|
-
|
216
|
+
|
214
217
|
addTextAssistant: ( node ) ->
|
215
218
|
initialValue = node.textContent
|
216
219
|
@assistants[ 'draw' ].push -> node.textContent = @analize initialValue
|
217
220
|
@
|
218
|
-
|
221
|
+
|
219
222
|
addAttrAssistant: ( node ) ->
|
220
223
|
initialValue = node.value
|
221
224
|
@assistants[ 'draw' ].push -> node.value = @analize initialValue
|
222
225
|
@
|
223
|
-
|
226
|
+
|
224
227
|
addHtmlAssistant: ( node ) ->
|
225
228
|
parent = node.parentNode
|
226
229
|
initialValue = node.innerHTML
|
@@ -233,17 +236,17 @@ Nali.extend View:
|
|
233
236
|
parent.removeChild node for node in Array::slice.call( parent.childNodes, start, end )
|
234
237
|
parent.insertBefore element, before for element in @_( @analize initialValue )
|
235
238
|
@
|
236
|
-
|
239
|
+
|
237
240
|
addFormAssistant: ( node ) ->
|
238
241
|
if bind = @analizeChain node.attributes.removeNamedItem( 'bind' ).value
|
239
242
|
[ source, property ] = bind
|
240
243
|
_node = @_ node
|
241
|
-
|
244
|
+
|
242
245
|
updateSource = ->
|
243
246
|
( params = {} )[ property ] = node.value
|
244
247
|
source.update params
|
245
248
|
source.save() unless node.form?
|
246
|
-
|
249
|
+
|
247
250
|
[ setValue, bindChange ] = switch
|
248
251
|
when node.type in [ 'text', 'textarea']
|
249
252
|
[
|
@@ -253,14 +256,14 @@ Nali.extend View:
|
|
253
256
|
when node.type in [ 'checkbox', 'radio' ]
|
254
257
|
[
|
255
258
|
-> node.checked = source[ property ] + '' is node.value
|
256
|
-
-> _node.on 'change', => updateSource.call @ if node.checked is true
|
259
|
+
-> _node.on 'change', => updateSource.call @ if node.checked is true
|
257
260
|
]
|
258
261
|
when node.type is 'select-one'
|
259
262
|
[
|
260
263
|
-> option.selected = true for option in node when source[ property ] + '' is option.value
|
261
264
|
-> _node.on 'change', => updateSource.call @
|
262
265
|
]
|
263
|
-
|
266
|
+
|
264
267
|
@assistants[ 'show' ].push ->
|
265
268
|
setValue.call @
|
266
269
|
bindChange.call @
|
@@ -269,17 +272,17 @@ Nali.extend View:
|
|
269
272
|
@assistants[ 'hide' ].push ->
|
270
273
|
_node.off 'change'
|
271
274
|
@
|
272
|
-
|
275
|
+
|
273
276
|
analize: ( value ) ->
|
274
277
|
value.replace /{\s*(.+?)\s*}/g, ( match, sub ) => @analizeMatch sub
|
275
|
-
|
276
|
-
analizeMatch: ( sub ) ->
|
278
|
+
|
279
|
+
analizeMatch: ( sub ) ->
|
277
280
|
if match = sub.match /^@([\w\.]+)(\?)?$/
|
278
281
|
if result = @analizeChain match[1]
|
279
282
|
[ source, property ] = result
|
280
283
|
source.subscribe? @, "update.#{ property }", @onSourceUpdated if source isnt @model
|
281
|
-
if match[2] is '?'
|
282
|
-
if source[ property ] then property else ''
|
284
|
+
if match[2] is '?'
|
285
|
+
if source[ property ] then property else ''
|
283
286
|
else source[ property ]
|
284
287
|
else ''
|
285
288
|
else if match = sub.match /^[=|\+](\w+)$/
|
@@ -296,4 +299,4 @@ Nali.extend View:
|
|
296
299
|
unless property of source
|
297
300
|
console.warn "%s: chain \"%s\" is invalid, \"%s\" is not Object", @_name, chain, segment
|
298
301
|
return null
|
299
|
-
[ source, property ]
|
302
|
+
[ source, property ]
|