cowtech-js-rails 1.0.0.0

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.
@@ -0,0 +1,22 @@
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
+ _gaq = []
9
+
10
+ (($) ->
11
+ $.cowtech = $.cowtech || {}
12
+ $.cowtech.google_analytics =
13
+ setup: ((account, domain) ->
14
+ _gaq = [["_setAccount", account], ["_setDomainName", domain], ["_trackPageview" ]]
15
+ ga = document.createElement("script")
16
+ ga.type = "text/javascript"
17
+ ga.async = true
18
+ ga.src = (if "https:" is document.location.protocol then "https://ssl" else "http://www") + ".google-analytics.com/ga.js"
19
+ s = document.getElementsByTagName("script")[0]
20
+ s.parentNode.insertBefore(ga, s)
21
+ )
22
+ )(jQuery)
@@ -0,0 +1,57 @@
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.keepalive =
11
+ autostart: true
12
+ last_update: 0
13
+ interval: null
14
+ check_timeout: 300
15
+ expire: 300
16
+ uri: null
17
+ callbacks:
18
+ updated: []
19
+ skipped: []
20
+
21
+ timestamp: (->
22
+ Math.round((new Date()).getTime() / 1000)
23
+ )
24
+
25
+ update: (->
26
+ $.cowtech.keepalive.last = $.cowtech.keepalive.timestamp()
27
+ $.getJSON($.cowtech.keepalive.uri) if !$.cowtech.utils.is_blank($.cowtech.keepalive.uri)
28
+ )
29
+
30
+ check: (->
31
+ now = $.cowtech.keepalive.timestamp()
32
+ if now - $.cowtech.keepalive.last >= $.cowtech.keepalive.expire
33
+ $.cowtech.keepalive.update()
34
+
35
+ $.each($.cowtech.keepalive.callbacks.updated, (index, callback) ->
36
+ callback()
37
+ )
38
+ else
39
+ $.each($.cowtech.keepalive.callbacks.skipped, (index, callback) ->
40
+ callback()
41
+ )
42
+ )
43
+
44
+ start: (->
45
+ $.cowtech.keepalive.uri = $.cowtech.data.params.keepalive_url
46
+ $.cowtech.keepalive.last = $.cowtech.keepalive.timestamp()
47
+ $.cowtech.keepalive.interval = setInterval($.cowtech.keepalive.check, $.cowtech.keepalive.check_timeout * 1000)
48
+ )
49
+
50
+ stop: (->
51
+ clearInterval($.cowtech.keepalive.interval) if !$.cowtech.utils.is_blank($.cowtech.keepalive.interval)
52
+ )
53
+
54
+ autoload: (->
55
+ $.cowtech.keepalive.start() if $.cowtech.keepalive.autostart == true
56
+ )
57
+ )(jQuery)
@@ -0,0 +1,258 @@
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.messages =
11
+ root: null
12
+ config:
13
+ msg:
14
+ element: "div"
15
+ classes: ""
16
+ title:
17
+ element: "h4"
18
+ classes: ""
19
+ closer:
20
+ element: "a"
21
+ classes: "close"
22
+ html: "×"
23
+ timeouts:
24
+ success: 3000
25
+ info: 5000
26
+ loader:
27
+ message: "Loading ..."
28
+ image: "/images/loading.gif"
29
+ labels:
30
+ close: "Close"
31
+ http_error: "HTTP Error"
32
+ no_reply: "No reply from server."
33
+ check_connection: "Check network connection."
34
+
35
+ autoload: (->
36
+ $.cowtech.messages.root = $("[data-messages-role=root]")
37
+ )
38
+
39
+ clear: (->
40
+ $.cowtech.messages.root.html("")
41
+ )
42
+
43
+ create_msg: ((type, options) ->
44
+ options = $.cowtech.utils.initialize(options, {})
45
+ rv = $("<#{$.cowtech.messages.config.msg.element}/>").addClass($.cowtech.messages.config.msg.classes).addClass("alert-" + type)
46
+ rv.attr("data-messages-role", "message").prependTo($.cowtech.messages.root).hide()
47
+ rv.attr("data-messages-id", options["id"]) if !$.cowtech.utils.is_null(options["id"])
48
+ rv
49
+ )
50
+
51
+ create_title: ((type, msg, options) ->
52
+ msg = $.cowtech.utils.initialize(msg, $.cowtech.messages.loader.message)
53
+ icon = ""
54
+
55
+ if type == "loading"
56
+ icon = "<img class=\"message-icon\" alt=\"#{msg}\" src=\"#{$.cowtech.messages.loader.image}\"/><span class=\"c-icon-label\">#{msg}</span>"
57
+ else
58
+ icon = "<span class=\"message-icon #{type}\"></span>#{msg}"
59
+ rv = $("<#{$.cowtech.messages.config.title.element}/>").addClass($.cowtech.messages.config.title.classes)
60
+ rv.attr("data-messages-role", "title").html(icon)
61
+ rv
62
+ )
63
+
64
+ create_closer: ((options) ->
65
+ rv = $("<#{$.cowtech.messages.config.closer.element}/>").addClass($.cowtech.messages.config.closer.classes)
66
+ rv.attr("href", "#") if $.cowtech.messages.config.closer.element == "a"
67
+ rv.attr("data-messages-role", "close").html($.cowtech.messages.config.closer.html)
68
+ rv
69
+ )
70
+
71
+ loading: ((msg, options) ->
72
+ options = $.extend($.cowtech.utils.initialize(options, {}), {
73
+ id: "loading"
74
+ no_animations: true
75
+ })
76
+
77
+ msg = $.cowtech.utils.initialize(msg, "")
78
+ $.cowtech.messages.show("loading", msg, options)
79
+ )
80
+
81
+ show: ((type, msg, options) ->
82
+ options = $.cowtech.utils.initialize(options, {})
83
+ msg = $.cowtech.utils.initialize(msg, "")
84
+
85
+ $.cowtech.messages.clear() if options["clear"] == true
86
+
87
+ container = $.cowtech.messages.create_msg(type, options)
88
+ title = null
89
+
90
+ if $.cowtech.utils.is_object(msg)
91
+ title = $.cowtech.messages.create_title(type, msg.title, options).appendTo(container)
92
+ $("<p data-messages-role=\"details\"></p>").appendTo(container).html(msg.message || msg.msg)
93
+ else
94
+ title = $.cowtech.messages.create_title(type, msg, options).appendTo(container)
95
+
96
+ if type != "loading"
97
+ closer = $.cowtech.messages.create_closer(options).prependTo(title).on("click", (ev) ->
98
+ ev.preventDefault()
99
+
100
+ if options.no_animations == true
101
+ container.remove()
102
+ else
103
+ container.slideUp("fast", ->
104
+ $(this).remove()
105
+ )
106
+ )
107
+
108
+ $("<p data-message-role=\"error\"></p>").appendTo(container).html("#{options.http.message} (#{$.cowtech.messages.labels.http_error} #{options.http.status})") if type == "error" && !$.cowtech.utils.is_null(options.http)
109
+ timeout = $.cowtech.messages.timeouts[type]
110
+
111
+ if options.no_autohide != true && !$.cowtech.utils.is_blank(timeout)
112
+ setTimeout((->
113
+ container.find("[data-messages-role=close]").click()
114
+ ), timeout)
115
+ if options.no_animations == true
116
+ container.show()
117
+ else
118
+ container.slideDown("fast")
119
+
120
+ options.showed() if !$.cowtech.utils.is_null(options.showed)
121
+ )
122
+
123
+ has_message: ((id) ->
124
+ $.cowtech.messages.root.find("[data-messages-id=\"#{id}\"]").size() > 0
125
+ )
126
+
127
+ alert: ((type, msg) ->
128
+ if $.cowtech.utils.module_active("modal")
129
+ root = $("<div></div>")
130
+ container = $("<div data-messages-role=\"alert\"></div>").appendTo(root)
131
+ h2 = $("<h2><span class=\"message-icon #{type}\"></span><span></span></h2>").appendTo(container)
132
+
133
+ if $.cowtech.utils.is_object(msg)
134
+ h2.find("span:not(.message-icon)").html(msg.title)
135
+ $("<p data-message-role=\"details\"></p>").appendTo(container).html(msg.message || msg.msg)
136
+ else
137
+ h2.find("span:not(.message-icon)").html(msg)
138
+
139
+ buttons = $("<div class=\"buttons\"></div>").appendTo(container)
140
+ $("<button class=\"button btn btn-primary\" data-messages-role=\"alert-close\">#{$.cowtech.messages.labels.close}</button>").appendTo(buttons)
141
+
142
+ $.cowtech.modal.callbacks.completed.push(->
143
+ $("[data-messages-role=\"alert-close\"]").on("click", (ev) ->
144
+ $.cowtech.modal.close()
145
+ false
146
+ )
147
+ )
148
+
149
+ $.cowtech.modal.open("#", {
150
+ autoScale: false
151
+ autoDimensions: false
152
+ content: root.html()
153
+ iframe: false
154
+ width: 700
155
+ height: 700
156
+ scrolling: false
157
+ })
158
+ else
159
+ alert(msg)
160
+ )
161
+
162
+ pause: ((amount) ->
163
+ start = new Date()
164
+ current = null
165
+ loop
166
+ current = new Date()
167
+ break if current - start >= amount
168
+ )
169
+
170
+ confirm: ((type, msg) ->
171
+ confirm(msg)
172
+ )
173
+
174
+ $.cowtech.ajax =
175
+ pending_requests: []
176
+
177
+ set_custom_events: ((value) ->
178
+ if value == true
179
+ $.cowtech.messages.root.addClass("custom-ajax-events")
180
+ else
181
+ $.cowtech.messages.root.removeClass("custom-ajax-events")
182
+ )
183
+
184
+ custom_events: (->
185
+ $.cowtech.messages.root.is(".custom-ajax-events")
186
+ )
187
+
188
+ start: ((force) ->
189
+ return if force != true && $.cowtech.ajax.custom_events()
190
+ $.cowtech.ajax.pending_requests.push(force) if $.cowtech.utils.is_object(force)
191
+ $.cowtech.messages.loading($.cowtech.messages.loader.message) if !$.cowtech.messages.has_message("loading")
192
+ )
193
+
194
+ end: ((force) ->
195
+ return if force != true && $.cowtech.ajax.custom_events()
196
+ $.cowtech.ajax.pending_requests.pop() if $.cowtech.utils.is_object(force)
197
+
198
+ if $.cowtech.ajax.pending_requests.length == 0
199
+ $.cowtech.messages.root.find("[data-messages-id=loading]").slideUp("fast", ->
200
+ $(this).remove()
201
+ )
202
+ )
203
+
204
+ autoload: (->
205
+ $.ajaxSetup(
206
+ cache: false
207
+ timeout: 60000
208
+ )
209
+
210
+ $(document).ajaxSend($.cowtech.ajax.start)
211
+ $(document).ajaxComplete($.cowtech.ajax.end)
212
+ $(document).ajaxError((e, xhr, settings, error) ->
213
+ return if $.cowtech.ajax.custom_events() || xhr.status != 0
214
+
215
+ $.cowtech.messages.show("error", $.cowtech.messages.labels.no_reply, {
216
+ http:
217
+ status: 400
218
+ message: $.cowtech.messages.labels.check_connection
219
+ }
220
+ )
221
+ e.stopPropagation()
222
+ )
223
+
224
+ $(window).on("unload", ->
225
+ # TODO: Avoid error on page change
226
+ $.each($.cowtech.ajax.pending_requests, (index, request) ->
227
+ request.abort()
228
+ )
229
+
230
+ $.cowtech.ajax.pending_requests = []
231
+ $.cowtech.ajax.end()
232
+ )
233
+
234
+ $("tr").attr("data-ajax-role", "container")
235
+
236
+ $("body").on("ajax:success", "a[data-remote=true]", (ev, data, status, xhr) ->
237
+ el = $(this)
238
+ if el.attr("data-type") == "html"
239
+ old_row = el.closest("[data-ajax-role=container]")
240
+
241
+ if $.cowtech.utils.is_blank(data)
242
+ old_row.slideUp("fast", ->
243
+ old_row.remove()
244
+ )
245
+ else
246
+ new_row = $(data).attr("data-ajax-role", "container").hide()
247
+
248
+ old_row.fadeOut("fast", ->
249
+ old_row.replaceWith(new_row)
250
+ new_row.fadeIn("fast")
251
+ )
252
+
253
+ $.cowtech.modal.setup new_row.find("[data-modal=true]")
254
+ else
255
+ $.cowtech.messages.show((if data.success then "success" else "error"), data.reply)
256
+ )
257
+ )
258
+ )(jQuery)
@@ -0,0 +1,175 @@
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.modal =
11
+ active: "colorbox"
12
+ root: null
13
+ callbacks:
14
+ loading: []
15
+ completed: []
16
+ closed: []
17
+
18
+ autoload: (->
19
+ $("[data-modal=true]").each(->
20
+ $.cowtech.modal.setup $(this)
21
+ )
22
+ )
23
+
24
+ close: (->
25
+ if $.cowtech.modal.active == "fancybox"
26
+ $.fancybox.close()
27
+ else if $.cowtech.modal.active == "colorbox"
28
+ $.colorbox.close()
29
+ )
30
+
31
+ parent_close: (->
32
+ if $.cowtech.modal.active == "fancybox"
33
+ window.top.window.$.fancybox.close()
34
+ else if $.cowtech.modal.active == "colorbox"
35
+ window.top.window.$.colorbox.close()
36
+ )
37
+
38
+ toggle_reload_parent: (->
39
+ $(window).data("modal-reload", "yes")
40
+ )
41
+
42
+ on_loading: (->
43
+ $.each($.cowtech.modal.callbacks.loading, (index, callback) ->
44
+ callback()
45
+ )
46
+ )
47
+
48
+ on_complete: (->
49
+ $.cowtech.modal.root = $("#cboxContent") if $.cowtech.modal.active == "colorbox"
50
+
51
+ $.each($.cowtech.modal.callbacks.completed, (index, callback) ->
52
+ callback()
53
+ )
54
+ )
55
+
56
+ on_closed: (->
57
+ $.each($.cowtech.modal.callbacks.closed, (index, callback) ->
58
+ callback()
59
+ )
60
+
61
+ if $(window).data("modal-reload") == "yes"
62
+ $(window).data("modal-reload", "no")
63
+ window.location.reload()
64
+ )
65
+
66
+ finalize_config: ((rv) ->
67
+ if $.cowtech.modal.active == "fancybox"
68
+ rv["type"] = null if !$.cowtech.utils.is_blank(rv["content"])
69
+ else if $.cowtech.modal.active == "colorbox"
70
+ if !$.cowtech.utils.is_blank(rv["content"])
71
+ rv["html"] = rv["content"]
72
+ rv["content"] = null
73
+ rv["width"] = null
74
+ rv["height"] = null
75
+ rv["type"] = null
76
+
77
+ rv
78
+ )
79
+
80
+ get_config: ((override, raw) ->
81
+ raw_defaults =
82
+ colorbox:
83
+ onLoad: ->
84
+ $("#cboxClose").hide()
85
+ $.cowtech.modal.on_loading()
86
+ onComplete: ->
87
+ $("#cboxClose").show().addClass("c-icon c-icon-24 close")
88
+ $.cowtech.modal.on_complete()
89
+ onClosed: $.cowtech.modal.on_closed
90
+
91
+ defaults =
92
+ fancybox:
93
+ autoScale: false
94
+ autoDimensions: false
95
+ width: 1400
96
+ height: 850
97
+ scrolling: "no"
98
+ titleShow: false
99
+ type: "iframe"
100
+ overlayOpacity: 0.8
101
+ overlayColor: "#404040"
102
+ speedIn: 0
103
+ speedOut: 0
104
+ transitionIn: "none"
105
+ transitionOut: "none"
106
+ easingIn: "none"
107
+ easingOut: "none"
108
+ onClosed: $.cowtech.on_closed
109
+ colorbox:
110
+ autoScale: false
111
+ autoDimensions: false
112
+ width: 1080
113
+ height: 950
114
+ transition: "none"
115
+ iframe: true
116
+ fixed: true
117
+ fastIframe: true
118
+ title: false
119
+ speed: 0
120
+ scrolling: false
121
+ onLoad: ->
122
+ $("#cboxClose").hide()
123
+ $.cowtech.modal.on_loading()
124
+ onComplete: ->
125
+ $("#cboxClose").show().addClass("c-icon c-icon-24 close")
126
+ $.cowtech.modal.on_complete()
127
+ onClosed: $.cowtech.modal.on_closed
128
+
129
+ dict = (if raw is true then raw_defaults else defaults)
130
+ $.cowtech.modal.finalize_config($.extend(dict[$.cowtech.modal.active], override))
131
+ )
132
+
133
+ add: ((el, config) ->
134
+ if $.cowtech.modal.active == "fancybox"
135
+ if el.is(".file")
136
+ config.type = "image"
137
+ delete config["width"]
138
+ delete config["height"]
139
+ el.fancybox(config)
140
+ else if $.cowtech.modal.active == "colorbox"
141
+ is_file = el.is("file")
142
+ delete config["width"] if is_file || config["width"] == "auto"
143
+ delete config["height"] if is_file || config["height"] == "auto"
144
+ el.colorbox(config)
145
+ )
146
+
147
+ open: ((url, config) ->
148
+ url = $.cowtech.utils.initialize(url, "#")
149
+ config = $.cowtech.utils.initialize(config, {})
150
+ link = $("<a href=\"#{url}\" data-modal=\"true\"></a>").appendTo($("body"))
151
+ $.cowtech.modal.setup(link, config)
152
+ link.click().remove()
153
+ )
154
+
155
+ setup: ((el, config) ->
156
+ override = {}
157
+ eld = el.get(0)
158
+ use_raw = false
159
+ return if el.size() == 0
160
+ config = $.cowtech.utils.initialize(config, {})
161
+
162
+ $.each(eld.attributes, (index) ->
163
+ name = eld.attributes[index].name
164
+ if name.match(/^data-modal-(.+)/)
165
+ if name != "data-modal-defaults"
166
+ override[name.replace("data-modal-", "")] = eld.attributes[index].value
167
+ else
168
+ override = {}
169
+ use_raw = true
170
+ false
171
+ )
172
+
173
+ $.cowtech.modal.add(el, $.cowtech.modal.get_config($.extend(override, config), use_raw))
174
+ )
175
+ )(jQuery)