cowtech-js-rails 1.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ Cowtech JS Rails
2
+ =======
3
+
4
+ A set of Javascript libraries.
5
+
6
+ Copyright (c) 2012 Shogun <shogun_panda@me.com>, released under the MIT license.
data/lib/cowtech_js.rb ADDED
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module Rails
9
+ module JS
10
+ class Engine < Rails::Engine
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module Rails
9
+ module JS
10
+ module Version
11
+ MAJOR = 1
12
+ MINOR = 0
13
+ PATCH = 0
14
+ BUILD = 0
15
+
16
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,92 @@
1
+ ###
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+ ###
7
+
8
+ (($) ->
9
+ $.cowtech = $.cowtech || {}
10
+ $.cowtech.accesskeys =
11
+ registered: null
12
+ mod: (if navigator.appVersion.indexOf("Mac") != -1 then "⌃⌥" else "Alt + ")
13
+
14
+ add: (key, description, trigger) ->
15
+ $.cowtech.accesskeys.registered = {} if !$.cowtech.accesskeys.registered?
16
+ $.cowtech.accesskeys.registered[key.toUpperCase()] = description
17
+ $("<a href=\"#\" accesskey=\"#{key}\"></a>").appendTo($("body")).on "click", (ev) ->
18
+ $(":focus").trigger("blur").trigger("change")
19
+ trigger($(this), ev)
20
+ false
21
+
22
+ add_trigger: (element, key, description, the_event, no_label) ->
23
+ return if $(element).size() == 0
24
+ if no_label == true
25
+ el = $(element)
26
+
27
+ if el.size() > 0
28
+ lel = el
29
+ lel = lel.find("span.label") if lel.find("span.label").size() > 0
30
+ label = (if lel.is("input") || lel.is("submit") then lel.val() else lel.html())
31
+ regex = $.ui.autocomplete.escapeRegex(" (#{$.cowtech.accesskeys.mod}@)").replace("@", "[A-Za-z0-9]+")
32
+ label = label.replace(new RegExp(regex), "")
33
+ label += " (#{$.cowtech.accesskeys.mod + key.toUpperCase()})"
34
+ (if lel.is("input") || lel.is("submit") then lel.val(label) else lel.html(label))
35
+
36
+ $.cowtech.accesskeys.add(key, description, (sender, ev) ->
37
+ selector = sender.data("accesskeys-element")
38
+ action_event = sender.data("accesskeys-event")
39
+ el = $(selector)
40
+
41
+ if $.cowtech.utils.is_blank(action_event)
42
+ el.click()
43
+ else
44
+ el.trigger(action_event)
45
+ ).data(
46
+ "accesskeys-element": element
47
+ "accesskeys-event": the_event || ""
48
+ )
49
+
50
+ autoload: ->
51
+ if $("body").is(":not(.embedded)")
52
+ setTimeout((->
53
+ if $.cowtech.accesskeys.registered?
54
+ summary = $("[data-accesskeys-role=summary]")
55
+ $.each($.cowtech.accesskeys.registered, (key, desc) ->
56
+ $("<tr><td>" + $.cowtech.accesskeys.mod + key + "</td><td>" + desc + "</td></tr>").appendTo(summary)
57
+ )
58
+
59
+ $.cowtech.modal.setup($("[data-accesskeys-role=\"help\"]"),
60
+ autoScale: true
61
+ autoDimensions: true
62
+ iframe: false
63
+ html: $("[data-accesskeys-role=\"template\"]").html()
64
+ width: 800
65
+ height: 800
66
+ )
67
+ ), 10)
68
+
69
+ link = $("<a data-accesskeys-role=\"help\" href=\"#\"></a>").appendTo($("[data-accesskeys-role=placeholder]"))
70
+ $("<span class=\"c-icon accesskeys\"></span><span class=\"c-icon-label\">Scorciatoie da tastiera (#{$.cowtech.accesskeys.mod}H)</span>").appendTo(link)
71
+
72
+ $.cowtech.accesskeys.add_trigger(link, "h", "Visualizza questo aiuto")
73
+ $.cowtech.accesskeys.add_trigger("[data-ui-role=add]", "a", "Aggiungi", null, true)
74
+
75
+ if $("[data-ui-role=save]").size() > 0
76
+ $.cowtech.accesskeys.add("s", "Salva modifiche", ->
77
+ submit = $("[data-ui-role=save]")
78
+ form = submit.closest("form")
79
+ if form.is(".to-validate") && !form.data("validator").checkValidity()
80
+ return
81
+ else
82
+ submit.click()
83
+ )
84
+
85
+ $("[data-ui-role=save]").each(->
86
+ $(this).val $(this).val() + " (" + $.cowtech.accesskeys.mod + "S)"
87
+ )
88
+
89
+ $.cowtech.accesskeys.add_trigger("[data-search-role=search]", "c", "Cerca", null, true)
90
+ $.cowtech.accesskeys.add_trigger("[data-search-role=clear]", "v", "Visualizza tutti", null, true)
91
+ $.cowtech.accesskeys.add_trigger("#select-action-button", "x", "Esegui azione sulle righe selezionate", null, true)
92
+ )(jQuery)
@@ -0,0 +1,49 @@
1
+ ###
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+ ###
7
+
8
+ (($) ->
9
+ $.cowtech = $.cowtech ||{}
10
+
11
+ $.cowtech.autocomplete =
12
+ sources: {}
13
+
14
+ autoload: (->
15
+ $("[type=\"autocomplete\"]").each(->
16
+ input = $(this)
17
+ id = input.attr("id")
18
+
19
+ $.cowtech.autocomplete.sources[id] = input.attr("data-autocomplete-source") if !input.attr("data-autocomplete-source")?
20
+
21
+ wrapper = $("<div></div>").attr("style", "display: inline-block; position: relative").attr("data-autocomplete-role", "wrapper")
22
+ input.wrap(wrapper)
23
+
24
+ input.autocomplete(
25
+ minLength: 1
26
+ source: ((request, response) ->
27
+ regex = "#{(if input.attr("data-autocomplete-match-beginning") == "true" then "^(" else "(")}#{$.ui.autocomplete.escapeRegex(request.term)})"
28
+ matcher = new RegExp(regex, "i")
29
+
30
+ response($.map($.cowtech.autocomplete.sources[id], (text) ->
31
+ if text && (!request.term || matcher.test(text))
32
+ {
33
+ label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(#{$.ui.autocomplete.escapeRegex(request.term)})(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>")
34
+ value: text
35
+ }
36
+ else
37
+ null
38
+ ))
39
+ )
40
+ open: (->
41
+ $("ul.ui-autocomplete.ui-menu").css(s)
42
+ )
43
+ )
44
+ input.data("autocomplete")._renderItem = ((ul, item) ->
45
+ $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul)
46
+ )
47
+ )
48
+ )
49
+ )(jQuery)
@@ -0,0 +1,71 @@
1
+ ###
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+ ###
7
+
8
+ (($) ->
9
+ $.cowtech = $.cowtech || {}
10
+ $.cowtech.bulk =
11
+ action: null
12
+ button: null
13
+ label: null
14
+
15
+ update: ->
16
+ count = $("[data-bulk-role=single]:checked").size()
17
+ if $.cowtech.bulk.label
18
+ text = $.cowtech.bulk.label.text().replace(/(\(\d+\))?:$/, "").trim()
19
+ if count > 0
20
+ text += " <b>(#{count})</b>:"
21
+ else
22
+ text += ":"
23
+ $.cowtech.bulk.label.html(text)
24
+
25
+ if count > 0
26
+ $.cowtech.bulk.button.show()
27
+ else
28
+ $.cowtech.bulk.button.hide()
29
+
30
+ autoload: ->
31
+ $.cowtech.bulk.action = $("[data-bulk-role=action]")
32
+ $.cowtech.bulk.label = $.cowtech.bulk.action.prev("label")
33
+ $.cowtech.bulk.button = $("[data-bulk-role=execute]")
34
+ $("[data-bulk-role=all]").on("change", ->
35
+ if $(this).is(":checked")
36
+ $(this).closest("table").find("[data-bulk-role=single]").attr("checked", "checked")
37
+ else
38
+ $(this).closest("table").find("[data-bulk-role=single]").removeAttr("checked")
39
+ $.cowtech.bulk.update()
40
+ )
41
+
42
+ $("[data-bulk-role=single]").on("change", ->
43
+ $.cowtech.bulk.update()
44
+ false
45
+ )
46
+
47
+ $.cowtech.bulk.action.on("change", ->
48
+ $("[date-bulk-role=date]").css "display", (if $(this).val() == "change-" then "inline" else "none")
49
+ false
50
+ ).change()
51
+
52
+ $.cowtech.bulk.button.on("click", (ev) ->
53
+ action = $.cowtech.bulk.action.val()
54
+
55
+ if !$.cowtech.utils.is_blank(action)
56
+ ids = []
57
+ $("[data-bulk-role=single]:checked").each ->
58
+ ids.push $(this).attr("id").replace("select-", "")
59
+
60
+ if ids.length > 0
61
+ action += "#{$("select#change-year-field").val()}-#{$("select#change-month-field").val()}" if action == "change-"
62
+ uri = $.param(
63
+ bulk_action: action
64
+ ids: ids.join(",")
65
+ )
66
+ location.href = "#{$.cowtech.data.params.bulk_url}?#{uri}"
67
+ false
68
+ )
69
+
70
+ $.cowtech.bulk.update()
71
+ )(jQuery)
@@ -0,0 +1,110 @@
1
+ ###
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+ ###
7
+
8
+ (($) ->
9
+ $.cowtech = $.cowtech || {}
10
+
11
+ $.cowtech.calculator =
12
+ root: null
13
+ field: null
14
+ last: null
15
+ result: null
16
+ copy: null
17
+ redo: null
18
+
19
+ autoload: ->
20
+ if $("body").is(":not(.embedded)")
21
+ link = $("<a data-calculator-role=\"help\" href=\"#\"></a>").appendTo($("[data-calculator-role=placeholder]"))
22
+ $("<span class=\"c-icon calcolatrice\"></span><span class=\"c-icon-label\">Calcolatrice (#{$.cowtech.accesskeys.mod}Q)</span>").appendTo(link)
23
+
24
+ $.cowtech.modal.callbacks.completed.push( ->
25
+ $.cowtech.calculator.setup()
26
+ )
27
+
28
+ $.cowtech.modal.setup(link, {
29
+ autoScale: true
30
+ autoDimensions: true
31
+ content: $("[data-calculator-role=template]").html()
32
+ iframe: false
33
+ width: 700
34
+ height: 200
35
+ scrolling: false
36
+ })
37
+
38
+ $.cowtech.accesskeys.add_trigger("#calculator-opener", "q", "Apri la calcolatrice", null, true)
39
+
40
+ string_to_expr: (num) ->
41
+ num.trim().replace(/,/g, ".").replace(/\s+/g, "")
42
+
43
+ format_history: (num) ->
44
+ num.replace(/\./g, ",").replace(/(\+|\*|-|\/)/g, " $1 ")
45
+
46
+ compute: ->
47
+ input = $.cowtech.calculator.string_to_expr($.cowtech.calculator.field.val())
48
+
49
+ $.cowtech.calculator.field.val("").removeClass("unset error success")
50
+ $.cowtech.calculator.last.removeClass("unset error success")
51
+ $.cowtech.calculator.result.removeClass("unset error success")
52
+
53
+ if !$.cowtech.utils.is_blank(input)
54
+ $.cowtech.calculator.last.val($.cowtech.calculator.format_history(input))
55
+ result = null
56
+
57
+ if /^([0-9\+\-\*\/\%\^\(\).]+)$/.test(input)
58
+ internal_result = 0.0
59
+
60
+ try
61
+ eval("internal_result = 0.0 + (" + input + ")")
62
+ result = internal_result if $.cowtech.utils.is_of_type(internal_result, "number")
63
+ catch e
64
+ result = null
65
+
66
+ if !result?
67
+ $.cowtech.calculator.last.addClass("error")
68
+ $.cowtech.calculator.result.addClass("error").val("Calcolo non valido")
69
+ else
70
+ $.cowtech.calculator.result.addClass("success").val(result)
71
+ else
72
+ $.cowtech.calculator.last.val("Nessun calcolo effettuato")
73
+ $.cowtech.calculator.result.val("Nessun calcolo effettuato")
74
+
75
+ redo: ->
76
+ $.cowtech.calculator.field.val($.cowtech.calculator.last.val()) if !$.cowtech.calculator.last.is(".unset")
77
+ $.cowtech.calculator.field.focus()
78
+
79
+ setup: ->
80
+ $.cowtech.calculator.root = $.cowtech.modal.root.find("[data-calculator-role=root]")
81
+ return if $.cowtech.calculator.root.size() == 0
82
+
83
+ $.cowtech.calculator.field = $.cowtech.calculator.root.find("[data-calculator-role=expression]")
84
+ $.cowtech.calculator.last = $.cowtech.calculator.root.find("[data-calculator-role=last]")
85
+ $.cowtech.calculator.result = $.cowtech.calculator.root.find("[data-calculator-role=result]")
86
+ $.cowtech.calculator.copy = $.cowtech.calculator.root.find("[data-calculator-role=copy]")
87
+ $.cowtech.calculator.repeat = $.cowtech.calculator.root.find("[data-calculator-role=repeat]")
88
+
89
+ $.cowtech.calculator.field.on("change", (ev) ->
90
+ ev.preventDefault()
91
+ $.cowtech.calculator.compute()
92
+ ).on("keypress", (ev) ->
93
+ if ev.keyCode == 13
94
+ ev.preventDefault()
95
+ $.cowtech.calculator.compute()
96
+ )
97
+
98
+ $.cowtech.calculator.repeat.on("click", (ev) ->
99
+ $.cowtech.calculator.redo()
100
+ false
101
+ )
102
+
103
+ $.cowtech.clipboard.setup($.cowtech.calculator.copy,
104
+ set_text: (client) ->
105
+ text = (if $.cowtech.calculator.result.is(".success") then $.cowtech.calculator.result.val() else " ")
106
+ client.setText(text)
107
+ copied: (client, text) ->
108
+ $.cowtech.messages.alert("success", "Risultato copiato negli appunti.") if !$.cowtech.utils.is_blank(text)
109
+ )
110
+ )(jQuery)
@@ -0,0 +1,46 @@
1
+ ###
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+ ###
7
+
8
+ (($) ->
9
+ $.cowtech = $.cowtech || {}
10
+ $.cowtech.clipboard =
11
+ swf: "",
12
+
13
+ setup: ((el, options) ->
14
+ return if el.size() == 0
15
+
16
+ clipboard = new ZeroClipboard.Client()
17
+ text = el.attr("data-clipboard-data")
18
+ msg = el.attr("data-clipboard-msg")
19
+ options = $.cowtech.utils.initialize(options, {})
20
+
21
+ text = el.attr("href") if $.cowtech.utils.is_null(text)
22
+ msg = "Testo copiato negli appunti." if $.cowtech.utils.is_null(msg)
23
+
24
+ copier = clipboard.getHTML(el.outerWidth(), el.outerHeight())
25
+ el.after(copier)
26
+ el.parent().attr("data-clipboard-role", "wrapper")
27
+
28
+ if !$.cowtech.utils.is_null(options.set_text)
29
+ clipboard.addEventListener("onMouseDown", options.set_text)
30
+ else
31
+ clipboard.setText(text)
32
+
33
+ clipboard.glue(el.get(0))
34
+
35
+ clipboard.addEventListener("onComplete", (if !$.cowtech.utils.is_null(options.copied) then options.copied else ((client, text) ->
36
+ $.cowtech.messages.alert("success", msg)
37
+ )))
38
+ )
39
+
40
+ autoload: (->
41
+ ZeroClipboard.setMoviePath($.cowtech.clipboard.swf)
42
+ $("[data-clipboard=true]").each(->
43
+ $.cowtech.clipboard.setup($(this))
44
+ )
45
+ )
46
+ )(jQuery)
@@ -0,0 +1,101 @@
1
+ ###
2
+ #
3
+ # This file is part of the cowtech-js-rails gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+ ###
7
+
8
+ (($) ->
9
+ $.widget("ui.combobox", {
10
+ _create: ->
11
+ self = this
12
+ select = @element.hide()
13
+ selected = select.children(":selected")
14
+ value = (if selected.val() then selected.text() else "")
15
+
16
+ # Modification #1: Wrap in a div
17
+ wrapper = $("<div class=\"input-append ui-combo\"></div>").attr("style", "display: inline-block; position: relative").attr("data-combobox-role", "wrapper")
18
+ select.wrap(wrapper)
19
+
20
+ input = $("<input/>").insertAfter(select).val(value).autocomplete(
21
+ delay: 0
22
+ minLength: 0
23
+ source: ((request, response) ->
24
+ regex = "#{if select.attr("data-combobox-match-beginning" == "true") then "^(" else "("}#{$.ui.autocomplete.escapeRegex(request.term)})"
25
+ matcher = new RegExp(regex, "i")
26
+
27
+ response select.children("option").map(->
28
+ text = $(this).text()
29
+ if @value && (!request.term || matcher.test(text))
30
+ {
31
+ label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(#{$.ui.autocomplete.escapeRegex(request.term)})(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>")
32
+ value: text
33
+ option: this
34
+ }
35
+ )
36
+ )
37
+ open: (-> # Modification #2: Positioning fix and style fix
38
+ ul = $("ul.ui-autocomplete.ui-menu")
39
+ ul.css(
40
+ marginTop: "3px"
41
+ width: "#{input.closest("div.ui-combo").width() - 2}px"
42
+ zIndex: ""
43
+ )
44
+ )
45
+ select: ((event, ui) ->
46
+ ui.item.option.selected = true
47
+ self._trigger("selected", event, {
48
+ item: ui.item.option
49
+ })
50
+ )
51
+ change: ((event, ui) ->
52
+ if !ui.item
53
+ matcher = new RegExp("^#{$.ui.autocomplete.escapeRegex($(this).val())}$", "i")
54
+ valid = false
55
+
56
+ select.children("option").each(->
57
+ if @value.match(matcher)
58
+ @selected = valid = true
59
+ false
60
+ )
61
+
62
+ if !valid
63
+ $(this).val("")
64
+ select.val("")
65
+ return false
66
+ )
67
+ ).addClass("ui-widget ui-widget-content ui-corner-left ui-combo")
68
+
69
+ # Modification #3: Copy original classes
70
+ input.addClass(select.attr("class"))
71
+
72
+ # Modification #4: Add validation tip-field
73
+ input.attr("id", "#{select.attr("id")}_ce")
74
+ select.attr("data-form-validation-msg-target", "##{input.attr("id")}")
75
+
76
+ input.data("autocomplete")._renderItem = ((ul, item) ->
77
+ $("<li></li>").data("item.autocomplete", item).append("<a>#{item.label}</a>").appendTo(ul)
78
+ )
79
+
80
+ addon = $("<label class=\"add-on\"></label").insertAfter(input)
81
+
82
+ $("<button>&nbsp;</button>").attr("tabIndex", -1).attr("title", "Show All Items").appendTo(addon).button(
83
+ icons:
84
+ primary: "c-icon c-icon-32 down"
85
+ text: false
86
+ ).removeClass("ui-corner-all").addClass("ui-corner-right ui-button-icon ui-combo").on("click", ->
87
+ if input.autocomplete("widget").is(":visible")
88
+ input.autocomplete("close")
89
+ else
90
+ input.autocomplete("search", "")
91
+ input.focus()
92
+ false
93
+ )
94
+ })
95
+
96
+ $.cowtech = $.cowtech || {}
97
+ $.cowtech.combobox =
98
+ autoload: (->
99
+ $("[type=\"combobox\"]").combobox()
100
+ )
101
+ )(jQuery)