pinkman 0.9.4.9 → 0.9.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 775f024c32a5518a396f6dd1dafe3dae39ab098e
4
- data.tar.gz: 92202f0cc00e5236651d7fb3244ff93060b87c42
3
+ metadata.gz: 392c59fb9a8c75861fe6db48458e3f3e90602f39
4
+ data.tar.gz: b3a4795dcb9b432ded4711a2c8cf0721c067d751
5
5
  SHA512:
6
- metadata.gz: 5d82d6c5ffa6ecdbc4dc34fb4737e7e78b06b01d331edbaaad4be0086e2d8cfbfb2d7327ed657c5aefb5df14a46dc8d453d1b7fa3de8b46c71972e4a1085ccd1
7
- data.tar.gz: 63d0369b3f639a98e0381d9e0c58474a32b4c907778fec1ac03b8bc219ce18a55c59df2a81134734229c126dadd0ee73b55a2e5d69158f2676eedab4c7c091b4
6
+ metadata.gz: 422b15670036ea2bcc3a1e91caca10f3fc6dad2f81ec5ae3170a70a6d50fdf8a3f94bb6b158e9c4cbc70a7b28cec7c7501ab192ddeb31690321c4dcefb118ae0
7
+ data.tar.gz: 6e19c387fc2268f263bb66ca946a1ccb3f09e8ffe4245dded1eb7b290b7b4af3d3764d0233e1e1d4dddc5afe9a8a215753d1f4d753a8501badd18f1fcfdd5140
@@ -6,5 +6,7 @@
6
6
  //= require pinkman_base/collection
7
7
  //= require pinkman_base/render
8
8
  //= require pinkman_base/controller
9
+ //= require pinkman_base/state
10
+ //= require pinkman_base/router
9
11
  //= require pinkman_base/css
10
- // = require pinkman_base/cable
12
+ //= require pinkman_base/cable
@@ -175,6 +175,20 @@ class window.PinkmanCollection extends window.PinkmanCommon
175
175
  else
176
176
  false
177
177
 
178
+
179
+ firstOrInitialize: (args...) ->
180
+ collection = @select(args...)
181
+ if collection.any()
182
+ collection.first()
183
+ else
184
+ @forceNew(args...)
185
+
186
+ forceNew: (args...)->
187
+ obj = new (@config.memberClass)
188
+ obj.initialize(args...)
189
+ @push(obj)
190
+ obj
191
+
178
192
  first: (n=1) ->
179
193
  if n==1
180
194
  return @collection[0]
@@ -193,6 +207,10 @@ class window.PinkmanCollection extends window.PinkmanCommon
193
207
  @select(criteria).count() > 0
194
208
  else
195
209
  @count() > 0
210
+
211
+ # Desc: exact opposite of any. Return trues if not a single members is found.
212
+ empty: (args...) ->
213
+ !@any(args...)
196
214
 
197
215
  # Desc: return the first object that matches
198
216
  getBy: (attribute, value,callback) ->
@@ -24,11 +24,25 @@ class window.PinkmanController extends window.PinkmanObject
24
24
  selector: ->
25
25
  '#' + @id
26
26
 
27
+ setParams: (params) ->
28
+ query = location.search.substring(1);
29
+ if query? and query!=''
30
+ @params = JSON.parse('{"' + decodeURI(query).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}')
31
+ else
32
+ @params = new Object
33
+
34
+ (@params[k] = v) for k,v of params if params? and typeof params == 'object'
35
+ @params
36
+
37
+ title: (title) ->
38
+ $('title').html(title) if Pinkman.isString(title)
39
+
27
40
  # build a controller from user definition
28
- build: ->
41
+ # main: boolean argument that forces main function be executed
42
+ build: (main) ->
29
43
  if @builder? and typeof @builder == 'function'
30
44
  @builder(this)
31
- @main() if @main? and $("##{@id}").length and typeof @main == 'function'
45
+ @main() if @main? and (main or ($("##{@id}").length and typeof @main == 'function'))
32
46
  return(true)
33
47
  else
34
48
  return(false)
@@ -123,8 +123,11 @@ class window.Pinkman
123
123
  AppCollection.scope
124
124
  else if typeof obj == 'object' and obj.pinkmanType == 'object' and AppObject? and AppObject.scope?
125
125
  AppObject.scope
126
-
127
-
126
+
127
+ # ready: same as jquery document ready
128
+ @ready: (callback) ->
129
+ $(document).ready(callback)
130
+
128
131
  # --- Ajax
129
132
 
130
133
  @ajax:
@@ -203,7 +206,7 @@ class window.Pinkman
203
206
  upload: (options...) ->
204
207
  @file(options...)
205
208
 
206
- $(document).ready ->
209
+ Pinkman.ready ->
207
210
  unless Pinkman.pathname?
208
211
  Pinkman.pathname = window.location.pathname
209
212
  Pinkman.pathname = Pinkman.pathname + '/' if Pinkman.pathname.charAt([Pinkman.pathname.length] - 1) != "/"
@@ -0,0 +1,224 @@
1
+ class window.PinkmanPath extends Pinkman.object
2
+
3
+ constructor: (url) ->
4
+ @levels = new Pinkman.collection
5
+ @static = new Pinkman.collection
6
+ @dynamic = new Pinkman.collection
7
+ @params = new Object
8
+ super()
9
+ if Pinkman.isString(url)
10
+ url = url.replace(window.location.origin,'') if PinkmanPath.isInternal(url)
11
+ a = url.split('/')
12
+ a.shift() if a[0] == ''
13
+ a.pop() if a[a.length-1] == ''
14
+ i = 0
15
+ for l in a
16
+ i = i + 1
17
+ obj = new Pinkman.object({entry: l, index: i})
18
+ if /:/.test(l[0])
19
+ obj.set('dynamic',yes)
20
+ obj.set('static',no)
21
+ @dynamic.push(obj)
22
+ else
23
+ obj.set('dynamic',no)
24
+ obj.set('static',yes)
25
+ @static.push(obj)
26
+ @levels.push(obj)
27
+
28
+ @set('depth',i)
29
+
30
+
31
+ @isExternal: (url) ->
32
+ urlRegex = new RegExp('^(?:[a-z]+:)?//', 'i')
33
+ hostRegex = new RegExp(window.location.origin,'i')
34
+ urlRegex.test(url) and not hostRegex.test(url)
35
+
36
+ @isInternal: (url) ->
37
+ not @isExternal(url)
38
+
39
+ level: (index) ->
40
+ @levels.getBy('index',index)
41
+
42
+ match: (path) ->
43
+ path = new PinkmanPath(path) if Pinkman.isString(path)
44
+ if PinkmanPath.isInstance(path) and path.depth == @depth
45
+ match = true
46
+ @static.each (level) ->
47
+ match = false if level.entry != path.level(level.index).entry
48
+ if match
49
+ @dynamic.each (level) ->
50
+ path.params[level.entry.replace(/:/g,"")] = path.level(level.index).entry
51
+ return(match)
52
+ else
53
+ false
54
+
55
+
56
+ # receives a string and matches it through the defined routes
57
+ class window.PinkmanRouteMatcher extends Pinkman.object
58
+
59
+ initialize: ->
60
+ if @controllers.any()?
61
+ @controllers.each (c) =>
62
+ c.setParams(@params())
63
+ c.build(yes)
64
+ else
65
+ false
66
+
67
+ match: (url) ->
68
+ path = new PinkmanPath(url)
69
+ if path?
70
+ candidates = Pinkman.routes.select(depth: path.depth)
71
+ routes = candidates.select (candidate) ->
72
+ candidate.path.match(path)
73
+ route = routes.first()
74
+
75
+ if route?
76
+ @set('url',url)
77
+ @set('path',path)
78
+ @set 'route', route
79
+ @set 'controller', @route.controller
80
+ @set('controllers', Pinkman.controllers.select(id: @controller))
81
+ if @controllers.any()
82
+ return(this)
83
+ else
84
+ throw "(Pinkman Route) Controller '#{@route.controller}' not found."
85
+ else
86
+ return(false)
87
+
88
+ params: ->
89
+ # console.log @path
90
+ # console.log @path.params
91
+ @path.params if @path and @path.params?
92
+
93
+
94
+ # every route defined turn into a object of this class: PinkmanRoute
95
+ class window.PinkmanRoute extends Pinkman.object
96
+ yieldIn: ->
97
+ @yield || @container
98
+
99
+
100
+ # Routes collection. Have the capability to search a string through routes and storage all defined routes. Used once in Pinkman.routes
101
+ class window.PinkmanRoutes extends Pinkman.collection
102
+ config:
103
+ className: 'PinkmanRoutes'
104
+ memberClass: ->
105
+ return (new PinkmanRoute)
106
+
107
+ match: (url) ->
108
+ matcher = new PinkmanRouteMatcher
109
+ return(matcher.match(url))
110
+ # global variable to store routes
111
+ Pinkman.routes = new PinkmanRoutes
112
+
113
+ # Main class: define routes throughout the application
114
+ class window.PinkmanRouter
115
+
116
+ # Sets the global container of the application.
117
+ # Everything will be yield inside this container. This container is just simple valid(present) selector that will serve as a wrapper for things to be dynamically rendered in.
118
+ @config: (value) ->
119
+ $(document).ready =>
120
+ if value? and typeof value == 'object' and value.yield?
121
+ @_config = value
122
+ throw 'PinkmanRouter Config: yield must be a valid(present) selector' if $(@_config.yield).length == 0
123
+ else
124
+ throw '(Pinkman Router) Config argument must be a object and must have yield attribute.'
125
+
126
+ # This function is responsible for defining our app routes
127
+ # It is manipulated by the final user
128
+ @define: (routes) ->
129
+ router = new PinkmanRouter
130
+ routes(router) if typeof routes == 'function'
131
+ return(router)
132
+
133
+
134
+ # Search path throughout routes. On match, activate respective controllers: clears template and execute main(s) function(s)
135
+ @activate: (path,callback) ->
136
+ r = Pinkman.routes.match(path)
137
+ if r? and r
138
+ Pinkman.state.initialize()
139
+ yieldIn = r.route.yieldIn() || @_config.yield
140
+ $(yieldIn).html("<div class='pink-yield' id='#{r.controller}'></div>") if r.route.blank
141
+ r.initialize()
142
+ callback() if typeof callback == 'function'
143
+ true
144
+ else
145
+ false
146
+
147
+ # Goes to a path
148
+ @visit: (path) ->
149
+ @activate path, ->
150
+ Pinkman.state.push(path)
151
+
152
+ @force: (path) ->
153
+ (window.location=path) unless @visit(path)
154
+
155
+ @restore: (path) ->
156
+ (window.location=path) unless @activate(path)
157
+
158
+ @redirect: (args...) ->
159
+ @force(args...)
160
+
161
+ @redirectTo: (args...) ->
162
+ @force(args...)
163
+
164
+ @go: (args...) ->
165
+ @force(args...)
166
+
167
+ @start: ->
168
+ Pinkman.ready =>
169
+ Pinkman.router = this
170
+ @activate(window.location.pathname)
171
+ $('body').on 'click', 'a', (ev) =>
172
+ ev.preventDefault()
173
+ path = ev.currentTarget.href
174
+ (window.location = path) unless path? and @visit(path)
175
+
176
+ # namespace: (path, rules) ->
177
+
178
+ get: (path, object) ->
179
+ if Pinkman.isString(path)
180
+ p = new PinkmanPath(path)
181
+ route = new PinkmanRoute
182
+ route.set('id',path)
183
+ route.set('url',path)
184
+ route.set('path',p).set('depth',p.depth)
185
+ route.set('blank',yes)
186
+ route.set('controller', if object? and object.controller? then object.controller else p.level(1).entry)
187
+ if object? and typeof object == 'object'
188
+ route.set('blank',no) if (object.keep? and object.keep) or (object.blank? and not object.blank)
189
+ route.set('yield',object.container|| object.yield )
190
+ Pinkman.routes.push(route)
191
+ return(route)
192
+
193
+ root: (controller) ->
194
+ @get('/',controller: controller)
195
+
196
+ # motivation
197
+ # class window.AppRouter extends PinkmanRouter
198
+ #
199
+ # AppRouter.config yield: 'body'
200
+ #
201
+ # AppRouter.define (r) ->
202
+ #
203
+ # # @namespace 'lol', =>
204
+ # # @get 'test'
205
+ # # # define /lol/test
206
+ # #
207
+ # #
208
+ # # @get 'produto/:id',
209
+ # # controller: 'produto'
210
+ # # #define /produto/qualquer-coisa
211
+ # # # controler.param.id = 'qualquer-coisa'
212
+ # #
213
+ #
214
+ #
215
+ # @get 'test',
216
+ # controller: 'test'
217
+ #
218
+ # ->
219
+ # header
220
+ # title Lol
221
+ # body#test
222
+ # execute 'controller.test.main'
223
+ #
224
+ # AppRouter.visit 'test'
@@ -0,0 +1,44 @@
1
+ class window.PinkmanStates extends Pinkman.collection
2
+ config:
3
+ className: 'PinkmanStates'
4
+ memberClass: ->
5
+ return (new PinkmanState)
6
+
7
+ Pinkman.states = new PinkmanStates
8
+
9
+ class window.PinkmanState extends Pinkman.object
10
+ config:
11
+ className: 'PinkmanState'
12
+
13
+
14
+ @initialize: ->
15
+ if Pinkman.states.empty() and window? and history? and history.replaceState?
16
+ state = Pinkman.states.forceNew(path: window.location.pathname)
17
+ history.replaceState({pinkey: state.pinkey}, "", state.path)
18
+
19
+ @push: (path) ->
20
+ if Pinkman.isString(path)
21
+ path = @normalizePath(path)
22
+ state = Pinkman.states.forceNew(path: path)
23
+ history.pushState({ pinkey: state.pinkey }, "", path) if window? and history? and history.pushState?
24
+
25
+ @normalizePath: (path) ->
26
+ path = path.replace(window.location.origin, '')
27
+ if path[0] == '/' then (window.location.origin + path) else (window.location.origin + '/' + path)
28
+
29
+ @restore: (event) ->
30
+ if typeof event == 'object' and event.state? and event.state.pinkey?
31
+ state = Pinkman.states.firstOrInitialize(pinkey: event.state.pinkey)
32
+ unless state.path?
33
+ state.set('path',window.location.pathname)
34
+ state.restore()
35
+
36
+ restore: ->
37
+ Pinkman.router.restore(@path)
38
+
39
+ Pinkman.state = PinkmanState
40
+ #
41
+ $(document).ready ->
42
+ if window? and history?
43
+ window.onpopstate = (event) ->
44
+ Pinkman.state.restore(event)
@@ -1,3 +1,3 @@
1
1
  module Pinkman
2
- VERSION = "0.9.4.9"
2
+ VERSION = "0.9.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pinkman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4.9
4
+ version: 0.9.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Agilso Oliveira
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-12 00:00:00.000000000 Z
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -192,6 +192,8 @@ files:
192
192
  - app/assets/javascripts/pinkman_base/object.js.coffee.erb
193
193
  - app/assets/javascripts/pinkman_base/pinkman.coffee
194
194
  - app/assets/javascripts/pinkman_base/render.coffee.erb
195
+ - app/assets/javascripts/pinkman_base/router.coffee
196
+ - app/assets/javascripts/pinkman_base/state.coffee
195
197
  - app/assets/javascripts/pinkman_base/tools.coffee
196
198
  - app/assets/javascripts/pinkman_with_handlebars.js
197
199
  - app/assets/javascripts/pinkman_with_hogan.js