ultimate-base 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ultimate-base (0.2.1)
4
+ ultimate-base (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1,4 +1,4 @@
1
- (@Ultimate ||= {}).Backbone ||= {}
1
+ #= require ./base
2
2
 
3
3
  class Ultimate.Backbone.App
4
4
  @App: null
@@ -13,6 +13,7 @@ class Ultimate.Backbone.App
13
13
  scopes: ["Models", "Collections", "Routers", "Views"]
14
14
 
15
15
  constructor: (name = null) ->
16
+ Ultimate.Backbone.debug ".App.constructor()", @
16
17
  if @constructor.App
17
18
  throw new Error("Can't create new Ultimate.Backbone.App because the single instance has already been created");
18
19
  else
@@ -0,0 +1,15 @@
1
+ #= require ultimate/helpers
2
+
3
+ (@Ultimate ||= {}).Backbone =
4
+
5
+ debugMode: false
6
+
7
+ debug: ->
8
+ if @debugMode
9
+ a = ["info", "Ultimate.Backbone"]
10
+ Array::push.apply a, arguments if arguments.length > 0
11
+ cout.apply @, a
12
+
13
+ isView: (viewClass) -> viewClass instanceof Backbone.View
14
+
15
+ isViewClass: (viewClass) -> (viewClass::) instanceof Backbone.View
@@ -1,7 +1,11 @@
1
- (@Ultimate ||= {}).Backbone ||= {}
1
+ #= require ./base
2
2
 
3
3
  class Ultimate.Backbone.Collection extends Backbone.Collection
4
4
 
5
+ constructor: ->
6
+ Ultimate.Backbone.debug ".Collection.constructor()", @
7
+ super
8
+
5
9
  ready: (callback, context = @) ->
6
10
  unless @length
7
11
  @readyDeferred ||= $.Deferred()
@@ -0,0 +1,94 @@
1
+ #= require ../base
2
+
3
+ do ($ = jQuery) ->
4
+
5
+ # TODO arguments
6
+ $.fn.getViews = (viewClass = null, inheritance = false, domDeep = false) ->
7
+ if not @length
8
+ return []
9
+ else if @length is 1
10
+ views = @data("views") or []
11
+ else
12
+ # TODO check work and perf tests
13
+ views = _.flatten($(o).data("views") or [] for o in @, true)
14
+ #2: @map -> $(@).data("views") or []
15
+ #3: _.flatten ( @map -> $(@).data("views") or [] ), true
16
+ if viewClass?
17
+ _.filter views, if inheritance then (w) -> w instanceof viewClass else (w) -> w.costructor is viewClass
18
+ else
19
+ views
20
+
21
+ $.fn.getView = (viewClass, inheritance = false) ->
22
+ if _.isString(viewClass)
23
+ for o in @
24
+ views = $(o).data("views") or []
25
+ for view in views
26
+ return view if view.constructor.className is viewClass
27
+ else if Ultimate.Backbone.isViewClass(viewClass)
28
+ for o in @
29
+ views = $(o).data("views") or []
30
+ if inheritance
31
+ for view in views
32
+ return view if view instanceof viewClass
33
+ else
34
+ for view in views
35
+ return view if view.constructor is viewClass
36
+ false
37
+
38
+ $.fn.hasView = (viewClass) ->
39
+ if _.isString(viewClass)
40
+ for o in @
41
+ return true if _.any $(o).data("views") or [], (w) -> w.constructor.className is viewClass
42
+ else if Ultimate.Backbone.isViewClass(viewClass)
43
+ for o in @
44
+ return true if _.any $(o).data("views") or [], (w) -> w.constructor is viewClass
45
+ false
46
+
47
+ # $.fn.bindOneView = (viewClass, options = {}) ->
48
+ # Ultimate.gcViews()
49
+ # bindedView = null
50
+ # if @length
51
+ # selector = "#{viewClass.selector}:not(.prevent-binding)"
52
+ # jContainer = if @is(selector) then @filter(selector) else @find(selector)
53
+ # if (l = jContainer.length)
54
+ # if l is 1
55
+ # bindedView = Ultimate.createView viewClass, jContainer, options
56
+ # else
57
+ # warning "$.fn.bindOneView() found #{l} elements by #{selector}, when need only 1"
58
+ # else
59
+ # warning "$.fn.bindOneView() not found elements by #{selector}"
60
+ # else
61
+ # warning "$.fn.bindOneView() call from empty jQuery()"
62
+ # bindedView
63
+ #
64
+ # $.fn.bindView = (viewClass, options = {}) ->
65
+ # Ultimate.gcViews()
66
+ # bindedViews = []
67
+ # if @length
68
+ # ( if @is(viewClass.selector) then @ else @find(viewClass.selector) ).filter(":not(.prevent-binding)").each ->
69
+ # if viewClass.canCreateInstance()
70
+ # jContainer = $ @
71
+ # if view = Ultimate.createView viewClass, jContainer, options
72
+ # bindedViews.push view
73
+ # else
74
+ # false
75
+ # bindedViews
76
+ #
77
+ # $.fn.bindViews = (viewClasses = Ultimate.LazyViews, options = {}) ->
78
+ # cout "info", "bindViews begin"
79
+ # bindedViews = []
80
+ # if @length
81
+ # bindedViews = for viewName, viewClass of viewClasses when viewClass.canCreateInstance()
82
+ # viewClass.constructor.className ||= viewName
83
+ # @bindView viewClass, options
84
+ # _.flatten bindedViews, true
85
+
86
+
87
+
88
+ # # TODO filtering by viewClass
89
+ # # TODO enother algorithm with enother projection
90
+ # $.fn.closestViews = (viewClass = null) ->
91
+ # views = []
92
+ # jTry = @
93
+ # jTry = jTry.parent() while jTry.length and not (views = jTry.getViews()).length
94
+ # views
@@ -0,0 +1,66 @@
1
+ #= require ./jquery-ext
2
+
3
+ do ($ = jQuery) ->
4
+ $.fn.ultimateBackboneViewPluginAdapter = (pluginName, viewClass, pluginArgs) ->
5
+ a = []
6
+ Array::push.apply a, pluginArgs if pluginArgs.length > 0
7
+ argsLength = a.length
8
+ # Shall return the View, if have arguments and last argument of the call is a Boolean true.
9
+ _returnView =
10
+ if argsLength and _.isBoolean(_.last(a))
11
+ argsLength--
12
+ a.pop()
13
+ else
14
+ false
15
+ unless @length
16
+ return if _returnView then undefined else @
17
+ # get the first TODO analize, maybe each?
18
+ jContainer = @first()
19
+ # try to get the View-object, controlling everything that happens in our magical container
20
+ view = jContainer.getView(viewClass)
21
+ if view and view.$el[0] is jContainer[0]
22
+ command = null
23
+ if argsLength and _.isString(a[0])
24
+ command = a.shift()
25
+ else
26
+ return view if _returnView
27
+ return jContainer unless argsLength
28
+ command = "updateSettings"
29
+ if view[command]
30
+ return getValue(view, command)
31
+ else
32
+ $.error "Command [#{command}] does not exist on jQuery.#{pluginName}()"
33
+ else
34
+ options = if argsLength then a[0] else {}
35
+ if $.isPlainObject(options)
36
+ view = new viewClass(_.extend({}, options, el: jContainer[0]))
37
+ else
38
+ $.error "First argument of jQuery.#{pluginName}() must be plain object"
39
+ if _returnView then view else jContainer
40
+
41
+
42
+ # TODO rewrite docs
43
+ ###
44
+ * Invoke Ultimate Flash functionality for the first element in the set of matched elements.
45
+ * If last argument {Boolean} true, then returns {View}.
46
+ * @usage
47
+ *** standart actions ***
48
+ * construction .ultimateFlash([Object options = {}]) ~ {jQuery} jContainer
49
+ * show .ultimateFlash("show", String type, String text) ~ {jQuery} jFlash | {Boolean} false
50
+ * notice .ultimateFlash("notice", String text) ~ {jQuery} jFlash | {Boolean} false
51
+ * alert .ultimateFlash("alert", String text) ~ {jQuery} jFlash | {Boolean} false
52
+ *** extended actions ***
53
+ * getSettings .ultimateFlash("getSettings") ~ {Object} settings
54
+ * setSettings .ultimateFlash("setSettings", {Object} settings) ~ {jQuery} jContainer
55
+ * updateSettings .ultimateFlash({Object} options) ~ {Object} settings
56
+ * auto .ultimateFlash("auto", {ArrayOrObject} obj) ~ {Array} ajFlashes | {Boolean} false
57
+ * ajaxSuccess .ultimateFlash("ajaxSuccess"[, Arguments successArgs = []])
58
+ * ajaxError .ultimateFlash("ajaxError"[, String text = settings.translations.defaultErrorText][, Arguments errorArgs = []])
59
+ ###
60
+
61
+ Ultimate.Backbone.createJQueryPlugin = (pluginName, viewClass) ->
62
+ cout "info", "Ultimate.Backbone.createJQueryPlugin", pluginName, viewClass
63
+ if Ultimate.Backbone.isViewClass(viewClass)
64
+ jQuery.fn[pluginName] = -> @ultimateBackboneViewPluginAdapter pluginName, viewClass, arguments
65
+ else
66
+ jQuery.error "Second argument of Ultimate.Backbone.createJQueryPlugin() must be View class inherited from Backbone.View"
@@ -1,7 +1,11 @@
1
- (@Ultimate ||= {}).Backbone ||= {}
1
+ #= require ./base
2
2
 
3
3
  class Ultimate.Backbone.Model extends Backbone.Model
4
4
 
5
+ constructor: ->
6
+ Ultimate.Backbone.debug ".Model.constructor()", @
7
+ super
8
+
5
9
  ready: (callback, context = @) ->
6
10
  if _.isEmpty(@attributes)
7
11
  @readyDeferred ||= $.Deferred()
@@ -1,7 +1,11 @@
1
- (@Ultimate ||= {}).Backbone ||= {}
1
+ #= require ./base
2
2
 
3
3
  class Ultimate.Backbone.Router extends Backbone.Router
4
4
 
5
+ constructor: ->
6
+ Ultimate.Backbone.debug ".Router.constructor()", @
7
+ super
8
+
5
9
  namedParam = /:\w+/g
6
10
  splatParam = /\*\w+/g
7
11
  escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g
@@ -1,4 +1,4 @@
1
- (@Ultimate ||= {}).Backbone ||= {}
1
+ #= require ./base
2
2
 
3
3
  class Ultimate.Backbone.View extends Backbone.View
4
4
 
@@ -11,13 +11,24 @@ class Ultimate.Backbone.View extends Backbone.View
11
11
  loadingOverlayClass: "loading-overlay"
12
12
 
13
13
  constructor: ->
14
+ Ultimate.Backbone.debug ".View.constructor()", @
14
15
  super
15
16
 
16
17
 
17
18
 
18
- # Overload parent method Backbone.View.setElement() as hook for findNodes().
19
- setElement: (element, delegate) ->
19
+ # Overload parent method Backbone.View.setElement() as hook for findNodes() and trick for data("views").
20
+ setElement: (element, delegate, sideCall = false) ->
21
+ if @$el?.length and not sideCall
22
+ views = @$el.data("views")
23
+ for view in views when view isnt @
24
+ view.setElement element, delegate, true
25
+ @$el.data("views", [])
20
26
  super
27
+ if @$el.length
28
+ if views = @$el.data("views")
29
+ views.push @
30
+ else
31
+ @$el.data "views", [@]
21
32
  @findNodes()
22
33
  @
23
34
 
@@ -53,9 +64,10 @@ class Ultimate.Backbone.View extends Backbone.View
53
64
  normalizedEvents = {}
54
65
  for key, method of events
55
66
  [[], eventName, selector] = key.match(delegateEventSplitter)
56
- jObj = @[selector]
57
- if jObj instanceof jQuery and _.isString(jObj.selector)
58
- key = "#{eventName} #{jObj.selector}"
67
+ selector = getValue(@, selector)
68
+ selector = selector.selector if selector instanceof jQuery
69
+ if _.isString(selector)
70
+ key = "#{eventName} #{selector}"
59
71
  normalizedEvents[key] = method
60
72
  events = normalizedEvents
61
73
  events
@@ -67,7 +79,8 @@ class Ultimate.Backbone.View extends Backbone.View
67
79
  super
68
80
  @reflectOptions()
69
81
 
70
- reflectOptions: (viewOptions = @viewOptions, options = @options) ->
82
+ reflectOptions: (viewOptions, options = @options) ->
83
+ viewOptions = getValue(@, "viewOptions") unless viewOptions
71
84
  @[attr] = options[attr] for attr in viewOptions when options[attr]
72
85
 
73
86
 
@@ -0,0 +1,40 @@
1
+ #= require ultimate/improves/typed-fields
2
+
3
+ Ultimate.Backbone.Views ||= {}
4
+
5
+ class Ultimate.Backbone.Views.TypedFields extends Ultimate.Backbone.View
6
+
7
+ selector: "input:text[data-data-type], input:text[data-regexp-mask]"
8
+
9
+ events: ->
10
+ "keypress selector": "onKeyPress"
11
+ "change selector": "onChange"
12
+ "paste selector": "onPasteOrDrop"
13
+ "drop selector": "onPasteOrDrop"
14
+
15
+ viewOptions: ["selector"]
16
+
17
+ onKeyPress: (event) ->
18
+ if event.metaKey
19
+ @onPasteOrDrop event
20
+ return true
21
+ return true unless event.charCode
22
+ jField = $(event.currentTarget)
23
+ oldValue = jField.val()
24
+ newValue = oldValue.substring(0, @selectionStart) + String.fromCharCode(event.charCode) + oldValue.substring(@selectionEnd)
25
+ return false unless jField.getRegExpMask().test(newValue)
26
+ jField.data "value", newValue
27
+
28
+ onChange: (event) ->
29
+ jField = $(event.currentTarget)
30
+ if jField.getRegExpMask().test(jField.val())
31
+ jField.data "value", jField.val()
32
+ else
33
+ jField.val jField.data("value")
34
+
35
+ onPasteOrDrop: (event) ->
36
+ setTimeout ( -> $(event.currentTarget).trigger "change", arguments ), 100
37
+
38
+
39
+
40
+ Ultimate.Backbone.createJQueryPlugin? "typedFields", Ultimate.Backbone.Views.TypedFields
@@ -70,33 +70,4 @@
70
70
  .change()
71
71
  @
72
72
 
73
- #class Backbone.Ultimate.
74
-
75
- $.fn.observeTypedFields = (selector = 'input:text[data-data-type], input:text[data-regexp-mask]') ->
76
-
77
-
78
- return @ unless @length
79
-
80
- @on 'keypress', selector, (event) ->
81
- if event.metaKey
82
- $(@).trigger 'paste', arguments
83
- return true
84
- return true unless event.charCode
85
- jThis = $ @
86
- oldValue = jThis.val()
87
- newValue = oldValue.substring(0, @selectionStart) + String.fromCharCode(event.charCode) + oldValue.substring(@selectionEnd)
88
- return false unless jThis.getRegExpMask().test(newValue)
89
- jThis.data 'value', newValue
90
-
91
- @on 'change', selector, ->
92
- jThis = $ @
93
- if jThis.getRegExpMask().test(jThis.val())
94
- jThis.data 'value', jThis.val()
95
- else
96
- jThis.val jThis.data('value')
97
-
98
- @on 'paste drop', selector, ->
99
- jThis = $ @
100
- setTimeout ( -> jThis.trigger 'change', arguments ), 100
101
-
102
73
  )( jQuery )
@@ -1,5 +1,5 @@
1
1
  module Ultimate
2
2
  module Base
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ultimate-base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-14 00:00:00.000000000 Z
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ultimate UI core, base helpers and improves for Ruby on Rails Front-end
15
15
  email:
@@ -27,12 +27,16 @@ files:
27
27
  - README.md
28
28
  - Rakefile
29
29
  - app/assets/javascripts/ultimate/backbone/app.js.coffee
30
+ - app/assets/javascripts/ultimate/backbone/base.js.coffee
30
31
  - app/assets/javascripts/ultimate/backbone/collection.js.coffee
32
+ - app/assets/javascripts/ultimate/backbone/extra/jquery-ext.js.coffee
33
+ - app/assets/javascripts/ultimate/backbone/extra/jquery-plugin-adapter.js.coffee
31
34
  - app/assets/javascripts/ultimate/backbone/lib/backbone.js
32
35
  - app/assets/javascripts/ultimate/backbone/model.js.coffee
33
36
  - app/assets/javascripts/ultimate/backbone/router.js.coffee
34
37
  - app/assets/javascripts/ultimate/backbone/view.js.coffee
35
38
  - app/assets/javascripts/ultimate/backbone/views/slider.js.coffee
39
+ - app/assets/javascripts/ultimate/backbone/views/typed-fields.js.coffee
36
40
  - app/assets/javascripts/ultimate/base.js.coffee
37
41
  - app/assets/javascripts/ultimate/bus.js.coffee
38
42
  - app/assets/javascripts/ultimate/devise.js.coffee
@@ -52,7 +56,7 @@ files:
52
56
  - app/assets/javascripts/ultimate/improves/form.js.coffee
53
57
  - app/assets/javascripts/ultimate/improves/magic-radios.js.coffee
54
58
  - app/assets/javascripts/ultimate/improves/tablesorter.js
55
- - app/assets/javascripts/ultimate/improves/typed-field.js.coffee
59
+ - app/assets/javascripts/ultimate/improves/typed-fields.js.coffee
56
60
  - app/assets/javascripts/ultimate/underscore/underscore.js
57
61
  - app/assets/javascripts/ultimate/underscore/underscore.string.js
58
62
  - app/assets/javascripts/ultimate/widgets/dock.js.coffee