cowtech-js-rails 1.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,252 @@
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.dynamic_table.add_on_empty = false
12
+
13
+ $.cowtech.document =
14
+ search_uri: ""
15
+ advanced_search_uri: ""
16
+ pending_row: null
17
+ adding: false
18
+ callbacks:
19
+ searched: []
20
+ added: []
21
+ updated: []
22
+
23
+ fields:
24
+ label: "label"
25
+ price: ""
26
+ amount: ""
27
+ off: ""
28
+ decimal: ""
29
+ precision: ""
30
+ symbol: ""
31
+
32
+ on_updated: ((root) ->
33
+ return if $.cowtech.document.adding
34
+
35
+ $.each($.cowtech.document.callbacks.updated, (index, callback) ->
36
+ callback(root)
37
+ )
38
+ )
39
+
40
+ on_added: ((root, row) ->
41
+ $.each($.cowtech.document.callbacks.added, (index, callback) ->
42
+ callback(root, row)
43
+ )
44
+ )
45
+
46
+ on_searched: ((root, input, result) ->
47
+ $.each($.cowtech.document.callbacks.searched, (index, callback) ->
48
+ callback(root, input, result)
49
+ )
50
+ )
51
+
52
+ autoload: (->
53
+ $.cowtech.dynamic_table.reverse_index = true
54
+ $.cowtech.document.get_root().cowtech_document()
55
+ )
56
+
57
+ get_root: (->
58
+ $("[data-document-role=root]")
59
+ )
60
+
61
+ get_rows: ((root) ->
62
+ root.find("[data-dynamic-table-role=row]")
63
+ )
64
+
65
+ row_subtotal: ((row) ->
66
+ data = row.data("article")
67
+ $.cowtech.document.subtotal(data.price, data.amount, data.off)
68
+ )
69
+
70
+ subtotal: ((price, amount, off_) ->
71
+ $.cowtech.utils.round_number((amount * price) * (1 - (off_ / 100)), 2)
72
+ )
73
+
74
+ is_row_valid: ((row) ->
75
+ !$.cowtech.utils.is_null(row.data("article"))
76
+ )
77
+
78
+ reset_search: ((root, row) ->
79
+ row.find("[data-document-role=label]").addClass("unset").text("Nessuno")
80
+ row.find("[data-document-role=insert] span.label").text("Cerca")
81
+ row.find("[data-document-role=price]").val("0.00").numberize()
82
+ row.find("[data-document-role=amount]").val("0.00").attr(
83
+ "data-numeric-float": $.cowtech.data.params.unit.decimal
84
+ "data-precision": $.cowtech.data.params.unit.precision
85
+ ).numberize()
86
+ row.find("[data-document-role=amount]").next().html($.cowtech.data.params.unit.symbol)
87
+ row.find("[data-document-role=off]").val("0.00").numberize()
88
+ row.find("[data-document-role=code]").focus()
89
+ row.find("[data-document-role=code]").focus()
90
+ )
91
+
92
+ start_external_search: ((root, row, query) ->
93
+ $.cowtech.document.pending_row = row
94
+ link = root.find("[data-document-role=search]").attr("href").replace("?", "?query=#{encodeURIComponent(query)}&")
95
+ $.cowtech.modal.open(link, {
96
+ scrolling: "no"
97
+ })
98
+ )
99
+
100
+ end_external_search: ((code) ->
101
+ row = $.cowtech.document.pending_row
102
+ $.cowtech.document.pending_row = null
103
+ row = $.cowtech.document.get_root().find("[data-dynamic-table-role=base]") if !row?
104
+ row.find("[data-document-role=code]").val(code).focus().change()
105
+ )
106
+
107
+ search: ((root, input) ->
108
+ value = input.val()
109
+ row = input.closest("tr")
110
+
111
+ if !$.cowtech.utils.is_blank(value)
112
+ row.find("[data-document-role=label]").addClass("unset").text("Ricerca in corso ...")
113
+ row.removeData("article")
114
+
115
+ $.ajax(
116
+ url: $.cowtech.document.search_uri
117
+ data:
118
+ codice: value
119
+ success: ((data, text, xhr) ->
120
+ if data.valid
121
+ article = data.data
122
+ fields = $.cowtech.document.fields
123
+
124
+ # Set data
125
+ row.data("article", article)
126
+ row.find("[data-document-role=code]").val(article[fields.code])
127
+ row.find("[data-document-role=label]").removeClass("unset").text(article[fields.label])
128
+
129
+ # Set unit
130
+ row.find("[data-document-role=amount]").attr(
131
+ "data-numeric-float": (if !$.cowtech.utils.is_blank(article[fields.decimal]) then article[fields.decimal] else $.cowtech.data.params.unit.decimal)
132
+ "data-numeric-precision": (if !$.cowtech.utils.is_blank(article[fields.precision]) then article[fields.precision] else $.cowtech.data.params.unit.precision)
133
+ ).numberize()
134
+ row.find("[data-document-role=amount]").next().html(if !$.cowtech.utils.is_blank(article[fields.symbol]) then article[fields.symbol] else $.cowtech.data.params.unit.symbol)
135
+
136
+ # Set button, if present
137
+ if row.is("[data-dynamic-table-role=base]")
138
+ row.find("[data-document-role=insert] span.label").text("Aggiungi")
139
+ row.find("[data-document-role=price]").val(article[fields.price])
140
+ row.find("[data-document-role=amount]").val(article[fields.amount])
141
+ row.find("[data-document-role=off]").val(article[fields.off])
142
+ setTimeout((->
143
+ row.find("[data-document-role=amount]").focus()
144
+ ), 300)
145
+ else
146
+ row.find("[data-document-role=label]").text("Nessuno")
147
+ query = row.find("[data-document-role=code]").val()
148
+ $.cowtech.document.reset_search(root, row) if row.is("[data-dynamic-table-role=base]")
149
+ $.cowtech.document.start_external_search(root, row, query)
150
+
151
+ $.cowtech.document.on_searched(root, input, data)
152
+ )
153
+ error: ((xhr, text, error) ->
154
+ $.cowtech.messages.alert("error", "Impossibile cercare l'articolo.")
155
+ )
156
+ )
157
+ )
158
+
159
+ add: ((root) ->
160
+ base = root.find("[data-dynamic-table-role=base]")
161
+
162
+ base.find("[data-dynamic-table-attr-type=numeric]").each(->
163
+ $(this).numberize()
164
+ )
165
+
166
+ $.cowtech.document.adding = true
167
+ base.find("[data-document-role=amount]").val(1).change().numberize() if base.find("[data-document-role=amount]").data("numeric-value") == 0
168
+ row = $.cowtech.dynamic_table.add(root.find("[data-dynamic-table-role=root]"), true)
169
+
170
+ # Copy data
171
+ row.data("article", base.data("article"))
172
+ base.removeData("article")
173
+
174
+ # Reset search
175
+ base.find("[data-document-role=insert] span.label").text("Verifica")
176
+ base.find("[data-document-role=code]").val("")
177
+ base.find("[data-document-role=label]").text("")
178
+
179
+ # Modify button
180
+ row.find("[data-document-role=insert]").remove()
181
+ row.find("[data-dynamic-table-role=clear]").attr("data-dynamic-table-role", "delete").text("Rimuovi")
182
+
183
+ $.cowtech.document.on_added(root, row)
184
+ $.cowtech.document.adding = false
185
+ $.cowtech.document.on_updated(root)
186
+ $.cowtech.document.reset_search(root, base)
187
+ )
188
+
189
+ update: ((root) ->
190
+ $("[data-dynamic-table-role=table]").show()
191
+ $.cowtech.document.on_updated(root)
192
+ )
193
+
194
+ $.fn.cowtech_document = (->
195
+ $.cowtech.dynamic_table.callbacks.updated.push((root) ->
196
+ $.cowtech.document.update(root)
197
+ )
198
+
199
+ $.tools.validator.fn("[validate=article]", (el, value) ->
200
+ (if !$.cowtech.utils.is_null(el.closest("tr").data("article")) then true else
201
+ it: "Articolo non valido"
202
+ en: "Invalid article"
203
+ )
204
+ )
205
+
206
+ @each(->
207
+ root = $(this)
208
+ root.find("[data-dynamic-table-role=base]").show()
209
+ $.cowtech.dynamic_table.update(root.find("[data-dynamic-table-role=root]"))
210
+
211
+ $("body").on("change", "[data-document-role=root] input", (ev) ->
212
+ return if $.cowtech.document.adding
213
+
214
+ el = $(this)
215
+ if el.is("[data-document-role=code]")
216
+ $.cowtech.document.search(root, $(this))
217
+ else
218
+ $.cowtech.document.update(root)
219
+ )
220
+
221
+ $("body").on("keypress", "[data-document-role=root] input", (ev) ->
222
+ el = $(this)
223
+ row = $(this).closest("tr")
224
+
225
+ if ev.keyCode == 13
226
+ if el.is("[data-document-role=code]")
227
+ if row.is("[data-dynamic-table-role=base]") && $.cowtech.document.is_row_valid(row)
228
+ $.cowtech.document.add(root)
229
+ else
230
+ $.cowtech.document.search(root, $(this))
231
+ else
232
+ if row.is("[data-dynamic-table-role=base]") && $.cowtech.document.is_row_valid(row)
233
+ $.cowtech.document.add(root)
234
+ else
235
+ el.parent().next("td").find("input").focus()
236
+
237
+ false
238
+ )
239
+
240
+ $("[data-document-role=insert]").on("click", (ev) ->
241
+ row = $(this).closest("tr")
242
+
243
+ if $.cowtech.document.is_row_valid(row)
244
+ $.cowtech.document.add(root)
245
+ else
246
+ $.cowtech.document.search(root, row)
247
+
248
+ false
249
+ )
250
+ )
251
+ )
252
+ )(jQuery)
@@ -0,0 +1,184 @@
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.dynamic_table =
11
+ reverse_index: false
12
+ add_on_empty: true
13
+ callbacks:
14
+ added: []
15
+ removed: []
16
+ cleared: []
17
+ updated: []
18
+
19
+ autoload: (->
20
+ $("[data-dynamic-table-role=root]").cowtech_dynamic_table()
21
+ )
22
+
23
+ on_added: ((root, row) ->
24
+ $.each($.cowtech.dynamic_table.callbacks.added, (index, callback) ->
25
+ callback(root, row)
26
+ )
27
+ )
28
+
29
+ on_removed: ((root, row) ->
30
+ $.each($.cowtech.dynamic_table.callbacks.removed, (index, callback) ->
31
+ callback(root, row)
32
+ )
33
+ )
34
+
35
+ on_cleared: ((root) ->
36
+ $.each($.cowtech.dynamic_table.callbacks.cleared, (index, callback) ->
37
+ callback(root)
38
+ )
39
+ )
40
+
41
+ on_updated: ((root) ->
42
+ $.each($.cowtech.dynamic_table.callbacks.updated, (index, callback) ->
43
+ callback(root)
44
+ )
45
+ )
46
+
47
+ get_table: ((root) ->
48
+ root.find("[data-dynamic-table-role=body]")
49
+ )
50
+
51
+ get_rows: ((root) ->
52
+ root.find("[data-dynamic-table-role=row]")
53
+ )
54
+
55
+ size: ((root) ->
56
+ $.cowtech.dynamic_table.get_table(root).find("[data-dynamic-table-role=row]").size()
57
+ )
58
+
59
+ update_index: ((root) ->
60
+ index = $.cowtech.utils.initialize(root.data("dynamic-table-row-index"), 0)
61
+ index++
62
+ root.data("dynamic-table-row-index", index)
63
+ index
64
+ )
65
+
66
+ update: ((root) ->
67
+ size = $.cowtech.dynamic_table.size(root)
68
+ table = $.cowtech.dynamic_table.get_table(root)
69
+ rows = table.find("[data-dynamic-table-role=row]")
70
+ root.find("[data-dynamic-table-role=summary]").text(size)
71
+
72
+ rows.each(->
73
+ new_index = rows.index($(this)) + 1
74
+ new_index = size - new_index + 1 if $.cowtech.dynamic_table.reverse_index
75
+ $(this).find("[data-dynamic-table-role=index]").text(new_index)
76
+ )
77
+
78
+ if size > 0
79
+ table.closest("[data-dynamic-table-role=table]").show()
80
+ else
81
+ table.closest("[data-dynamic-table-role=table]").hide()
82
+
83
+ $.cowtech.numeric.autoload()
84
+ table.find("[data-dynamic-table-attr-type=numeric]").cowtech_numeric()
85
+ table.find("[data-dynamic-table-attr-type=date]").cowtech_date()
86
+ table.find("[data-dynamic-table-attr-type=combobox]").combobox()
87
+ root.closest("form").cowtech_form()
88
+
89
+ $.cowtech.dynamic_table.on_updated(root)
90
+ )
91
+
92
+ add: ((root, prepend) ->
93
+ table = $.cowtech.dynamic_table.get_table(root)
94
+ tr = root.find("[data-dynamic-table-role=base]").clone()
95
+ index = $.cowtech.dynamic_table.update_index(root)
96
+ tr.attr("data-dynamic-table-role", "row")
97
+
98
+ if prepend is true
99
+ tr.prependTo(table)
100
+ else
101
+ tr.appendTo(table)
102
+
103
+ tr.collapsible() if $.cowtech.utils.is_mobile()
104
+ tr.find("[data-dynamic-table-role=index]").text(index)
105
+
106
+ # Setup field id and name
107
+ tr.find("[data-dynamic-table-field=true]").each(->
108
+ field = $(this)
109
+ name = field.attr("name").replace("_INDEX", "[" + index + "]")
110
+ id = name.replace(/\[|\]/g, "_").replace(/__/, "_").replace(/_$/, "")
111
+ field.attr(
112
+ id: id
113
+ name: name
114
+ )
115
+ )
116
+
117
+ # Required fields
118
+ tr.find("[data-dynamic-table-required=true]").each(->
119
+ $(this).attr "required", "true"
120
+ )
121
+
122
+ tr.find("[data-dynamic-table-validate]").each(->
123
+ $(this).attr "validate", $(this).attr("data-dynamic-table-validate")
124
+ )
125
+
126
+ # Callback
127
+ $.cowtech.dynamic_table.on_added(root, tr)
128
+ $.cowtech.dynamic_table.update(root)
129
+ tr
130
+ )
131
+
132
+ remove: ((root, row, skip_events) ->
133
+ row.remove()
134
+
135
+ if skip_events ! true
136
+ $.cowtech.dynamic_table.on_removed(root, row)
137
+ $.cowtech.dynamic_table.update(root)
138
+ )
139
+
140
+ clear: ((root) ->
141
+ $.cowtech.dynamic_table.get_rows(root).each(->
142
+ $.cowtech.dynamic_table.remove(root, $(this), true)
143
+ )
144
+
145
+ $.cowtech.dynamic_table.on_cleared(root)
146
+ $.cowtech.dynamic_table.update(root)
147
+ )
148
+
149
+ $.fn.cowtech_dynamic_table = (->
150
+ the_event = (if $.cowtech.utils.is_mobile() then "tap" else "click")
151
+
152
+ $("[data-dynamic-table-role=root]").on(the_event, "[data-dynamic-table-role=delete]", (ev) ->
153
+ $.cowtech.dynamic_table.remove $(this).closest("[data-dynamic-table-role=root]"), $(this).closest("[data-dynamic-table-role=row]")
154
+ false
155
+ )
156
+
157
+ @each(->
158
+ root = $(this)
159
+ root.find("[data-dynamic-table-role=add]").on(the_event, (ev) ->
160
+ $.cowtech.dynamic_table.add(root)
161
+ false
162
+ )
163
+
164
+ root.find("[data-dynamic-table-role=clear]").on(the_event, (ev) ->
165
+ $.cowtech.dynamic_table.clear(root)
166
+ false
167
+ )
168
+
169
+ root.data("dynamic-table-row-index", $.cowtech.dynamic_table.size(root) + 1)
170
+ root.find("[data-dynamic-table-role=row]").collapsible() if $.cowtech.utils.is_mobile()
171
+
172
+ if $.cowtech.dynamic_table.size(root) == 0 && $.cowtech.dynamic_table.add_on_empty
173
+ $.cowtech.dynamic_table.add(root)
174
+ else
175
+ $.cowtech.numeric.autoload()
176
+
177
+ root.find("[data-dynamic-table-role=row]").each(->
178
+ $.cowtech.dynamic_table.on_added root, $(this)
179
+ )
180
+
181
+ $.cowtech.dynamic_table.update(root)
182
+ )
183
+ )
184
+ )(jQuery)
@@ -0,0 +1,383 @@
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.form =
11
+ effect: "cowtech"
12
+ date_plugin: "jquery-ui"
13
+ highlight_method: "twitter-bootstrap"
14
+ locale: "en"
15
+ date_format: "mm/dd/yy"
16
+ password_minimum_length: 8
17
+ labels:
18
+ close: "Close"
19
+ today: "Todat"
20
+ validation_messages:
21
+ phone: "Insert a valid phone number (only number, spaces and \"+\")."
22
+ letter: "Insert a single letter."
23
+ number: "Invalid number."
24
+ date: "Invalid date."
25
+ time: "Invalid time"
26
+ vat: "Invalid VAT number."
27
+ cf: "Invalid CF."
28
+ cap: "Invalid CAP."
29
+ password_length: "Password must be at least 8 characters length."
30
+ password_mismatch: "Password don't match."
31
+
32
+ highlight_methods:
33
+ qtip:
34
+ show: ((form, errors, event) ->
35
+ $.cowtech.form.highlight_methods["qtip"].hide(form, form.getInputs())
36
+
37
+ conf = form.getConf()
38
+ if errors.length > 0
39
+ $("[data-remote]").each(->
40
+ el = $(this)
41
+ original = el.attr("data-remote")
42
+ el.removeAttr("data-remote")
43
+ el.attr("data-remote-disabled", original)
44
+ )
45
+
46
+ $.each(errors, (index, error) ->
47
+ $.cowtech.form.highlight_methods["qtip"].show_single($(error.input), error.messages[0])
48
+ )
49
+ )
50
+
51
+ hide: ((form, inputs) ->
52
+ $("[data-remote-disabled]").each(->
53
+ el = $(this)
54
+ original = el.attr("data-remote-disabled")
55
+ el.removeAttr("data-remote-disabled")
56
+ el.attr("data-remote", original)
57
+ )
58
+
59
+ $(inputs).each(->
60
+ $.cowtech.form.highlight_methods["qtip"].hide_single($(this))
61
+ )
62
+ )
63
+
64
+ show_single: ((field, msg) ->
65
+ field = $(field.attr("data-form-validation-msg-target")) if !$.cowtech.utils.is_blank(field.attr("data-form-validation-msg-target"))
66
+ field.addClass("invalid")
67
+
68
+ if $.cowtech.utils.is_null(field.data("qtip"))
69
+ field.qtip(
70
+ content:
71
+ text: msg
72
+ position:
73
+ my: "left center"
74
+ at: "right center"
75
+ target: field
76
+ adjust:
77
+ x: 3
78
+ style:
79
+ classes: "ui-tooltip-validation-error"
80
+ tip: true
81
+ show:
82
+ ready: true
83
+ hide:
84
+ event: "change"
85
+ )
86
+ )
87
+
88
+ hide_single: ((field) ->
89
+ field = $(field.attr("data-form-validation-msg-target")) if !$.cowtech.utils.is_blank(field.attr("data-form-validation-msg-target"))
90
+ field.removeClass("invalid").qtip("destroy")
91
+ )
92
+
93
+ "twitter-bootstrap":
94
+ show: ((form, errors, event) ->
95
+ $.cowtech.form.highlight_methods["twitter-bootstrap"].hide(form, form.getInputs())
96
+
97
+ if errors.length > 0
98
+ $("[data-remote]").each(->
99
+ el = $(this)
100
+ original = el.attr("data-remote")
101
+ el.removeAttr("data-remote")
102
+ el.attr("data-remote-disabled", original)
103
+ )
104
+ $.each(errors, (index, error) ->
105
+ console.info(error)
106
+ $.cowtech.form.highlight_methods["twitter-bootstrap"].show_single($(error.input), error.messages[0])
107
+ )
108
+
109
+ $(window).scrollTop($(".invalid:first").offset().top - 100) if $(".invalid").size() > 0
110
+ )
111
+
112
+ hide: ((form, inputs) ->
113
+ $("[data-remote-disabled]").each(->
114
+ el = $(this)
115
+ original = el.attr("data-remote-disabled")
116
+ el.removeAttr("data-remote-disabled")
117
+ el.attr("data-remote", original)
118
+ )
119
+
120
+ $(inputs).each(->
121
+ $.cowtech.form.highlight_methods["twitter-bootstrap"].hide_single($(this))
122
+ )
123
+ )
124
+
125
+ show_single: ((field, msg) ->
126
+ field = $(field.attr("data-form-validation-msg-target")) if !$.cowtech.utils.is_blank(field.attr("data-form-validation-msg-target"))
127
+ container = field.closest(".control-group")
128
+ field.addClass("invalid error")
129
+ container.addClass("error")
130
+ container.find("span.help-block.error").remove()
131
+ $("<span class=\"hint help-block error\"><span class=\"c-icon c-icon-16 error\"></span>#{msg}</span>").appendTo(container.find("div.controls"))
132
+ )
133
+
134
+ hide_single: ((field) ->
135
+ console.info('h')
136
+ field = $(field.attr("data-form-validation-msg-target")) if !$.cowtech.utils.is_blank(field.attr("data-form-validation-msg-target"))
137
+ container = field.closest(".control-group")
138
+ field.removeClass("invalid error")
139
+ container.removeClass("error")
140
+ container.find("span.help-block.error").remove()
141
+ )
142
+
143
+ format_validation: ((type) ->
144
+ rv = {}
145
+ rv[$.cowtech.form.locale] = $.cowtech.form.validation_messages[type]
146
+ rv
147
+ )
148
+
149
+ autoload: (->
150
+ $("form[data-form-validate=true]").cowtech_form()
151
+ $("[data-form-role=editor], [data-wysiwyg-role=editor]").cowtech_editor()
152
+ )
153
+
154
+ initialize: (->
155
+ $.tools.validator.localize("en", {
156
+ ":email": "Insert a valid e-mail."
157
+ "[required]": "This field is required."
158
+ })
159
+
160
+ $.datepicker.regional[$.cowtech.form.locale] =
161
+ closeText: $.cowtech.form.close
162
+ prevText: "&larr;"
163
+ nextText: "&rarr;"
164
+ currentText: $.cowtech.form.today
165
+ monthNames: Date.localized_labels.months
166
+ monthNamesShort: Date.localized_labels.monthsShort
167
+ dayNames: Date.localized_labels.days
168
+ dayNamesShort: Date.localized_labels.daysShort
169
+ dayNamesMin: Date.localized_labels.daysMin
170
+ weekHeader: "Sm"
171
+ dateFormat: $.cowtech.form.date_format
172
+ firstDay: 1
173
+ isRTL: false
174
+ showMonthAfterYear: false
175
+ yearSuffix: ""
176
+ $.datepicker.setDefaults($.datepicker.regional[$.cowtech.form.locale])
177
+
178
+ $.tools.dateinput.localize($.cowtech.form.locale, {
179
+ months: Date.localized_labels.months.join(",")
180
+ shortMonths: Date.localized_labels.monthsShort.join(",")
181
+ days: Date.localized_labels.days.join(",")
182
+ shortDays: Date.localized_labels.daysShort.join(",")
183
+ })
184
+
185
+ $.tools.validator.fn("[type=phone]", (el, value) ->
186
+ (if ($.cowtech.utils.is_blank(value) || /^(((\+|00)\d{1,4}\s?)?(\d{0,4}\s?)?(\d{5,}))?$/i.test(value)) then true else $.cowtech.form.format_validation("phone"))
187
+ )
188
+
189
+ $.tools.validator.fn("[type=letter]", (el, value) ->
190
+ (if ($.cowtech.utils.is_blank(value) || /^([a-z])$/i.test(value)) then true else $.cowtech.form.format_validation("letter"))
191
+ )
192
+
193
+ $.tools.validator.fn("[type=numeric], .numeric", (el, value) ->
194
+ (if ($.cowtech.utils.is_blank(value) || $.cowtech.numeric.handle(el)) then true else $.cowtech.form.format_validation("number"))
195
+ )
196
+
197
+ $.tools.validator.fn("[validate=date]", (el, value) ->
198
+ (if ($.cowtech.utils.is_blank(value) || /^((\d{2})\/(\d{2})\/(\d{2}|\d{4}))$/i.test(value) or /^((\d{2}|\d{4})-(\d{2})-(\d{2}))$/i.test(value)) then true else $.cowtech.form.format_validation("date"))
199
+ )
200
+
201
+ $.tools.validator.fn("[validate=time]", (el, value) ->
202
+ if $.cowtech.utils.is_blank(value)
203
+ if /^((\d{2}):(\d{2}):(\d{2}))$/i.test(value)
204
+ if parseInt(RegExp.$2) > 23 || parseInt(RegExp.$3) > 59 || parseInt(RegExp.$4) > 59 then $.cowtech.form.format_validation("time") else true
205
+ else
206
+ $.cowtech.form.format_validation("time")
207
+ else
208
+ true
209
+ )
210
+
211
+ $.tools.validator.fn("[validate=partita-iva]", (el, value) ->
212
+ (if ($.cowtech.utils.is_blank(value) or /^([0-9A-Z]{11,17})$/i.test(value)) then true else $.cowtech.form.format_validation("vat"))
213
+ )
214
+
215
+ $.tools.validator.fn("[validate=codice-fiscale]", (el, value) ->
216
+ (if ($.cowtech.utils.is_blank(value) or /^([0-9A-Z]{16})$/i.test(value)) then true else $.cowtech.form.format_validation("cf"))
217
+ )
218
+
219
+ $.tools.validator.fn("[validate=cap]", (el, value) ->
220
+ (if ($.cowtech.utils.is_blank(value) or /^([0-9]{5})$/i.test(value)) then true else $.cowtech.form.format_validation("cap"))
221
+ )
222
+
223
+ $.tools.validator.fn("[type=password]", (el, value) ->
224
+ el = $(el)
225
+ confirm = el.attr("data-form-password-confirm")
226
+
227
+ if !$.cowtech.utils.is_blank(value)
228
+ if ! (new RegExp("/^.{#{$.cowtech.form.password_minimum_length},}$/i")).test(value) && !el.hasClass("free")
229
+ return $.cowtech.form.format_validation("password_length")
230
+ else if el.is("[data-form-password-confirmer]")
231
+ cel = $("#" + el.attr("data-form-password-confirmer"))
232
+ if cel.val() == el.val()
233
+ $.cowtech.form.highlight_methods[$.cowtech.form.highlight_method].hide_single(cel)
234
+ else
235
+ return $.cowtech.form.format_validation("password_mismatch")
236
+ true
237
+ )
238
+
239
+ $("#calprev").html("&laquo;")
240
+ $("#calnext").html("&raquo;")
241
+ $.tools.validator.addEffect("cowtech", $.cowtech.form.show_errors, $.cowtech.form.hide_errors)
242
+ $(window).data("cowtech-form-initialized", true)
243
+ )
244
+
245
+ setup: ((objs) ->
246
+ locale = $.cowtech.utils.initialize($("body").attr("data-locale"), $.cowtech.form.locale)
247
+
248
+ objs.validator(
249
+ effect: $.cowtech.form.effect
250
+ lang: locale
251
+ position: "center left"
252
+ )
253
+
254
+ objs.find("input, select, textarea").andSelf().attr("novalidate", "novalidate")
255
+ )
256
+
257
+ add_availability_validator: ((config) ->
258
+ jQuery(document).ready(($) ->
259
+ $.tools.validator.fn((if (config.selector) then config.selector else "[validate=" + config.field_type + "]"), (el, value) ->
260
+ rv = true
261
+ value = value.trim()
262
+
263
+ if value != ""
264
+ request = $.ajax(
265
+ url: config.url
266
+ async: false
267
+ data: $.extend({
268
+ query: value
269
+ id: el.attr("data-form-validation-availability-id")
270
+ }, config.data || {})
271
+ error: (xhr, text, error) ->
272
+ rv = it: config.error_msg
273
+ success: (data, text, xhr) ->
274
+ if data.success
275
+ rv = {it: config.unavailable_msg} if !data.valid
276
+ else
277
+ rv = {it: config.error_msg}
278
+ )
279
+ rv
280
+ )
281
+ )
282
+ )
283
+
284
+ show_errors: ((errors, event) ->
285
+ $.cowtech.form.highlight_methods[$.cowtech.form.highlight_method].show(this, errors, event)
286
+ )
287
+
288
+ hide_errors: ((inputs) ->
289
+ $.cowtech.form.highlight_methods[$.cowtech.form.highlight_method].hide(this, inputs)
290
+ )
291
+
292
+ $.fn.cowtech_date = (->
293
+ return this if $.cowtech.utils.is_mobile()
294
+
295
+ $.fn.date = ((value) ->
296
+ rv = null
297
+
298
+ switch $.cowtech.form.date_plugin
299
+ when "jquery-tools"
300
+ if $.cowtech.utils.is_blank(value)
301
+ rv = @data("dateinput").getValue()
302
+ else
303
+ @data("dateinput").setValue(value)
304
+ when "jquery-ui"
305
+ if $.cowtech.utils.is_blank(value)
306
+ rv = @datepicker("getDate")
307
+ else
308
+ @datepicker("setDate", value)
309
+ rv
310
+ )
311
+
312
+ @each(->
313
+ locale = $.cowtech.utils.initialize($("body").attr("data-locale"), "it")
314
+ el = $(this)
315
+ format = $.cowtech.utils.initialize(el.attr("data-date-format"), (if $.cowtech.form.date_plugin is "jquery-ui" then $.cowtech.form.date_format else "dd/mm/yyyy"))
316
+ root_id = el.attr("id") + "-popup"
317
+
318
+ if $.cowtech.form.highlight_method is "twitter-bootstrap" && !el.parent().is("div.input-append")
319
+ div = $("<div class=\"input-append\"></div>")
320
+ label = $("<label class=\"add-on date\"><button disabled=\"true\"><span class=\"c-icon c-icon-16 date\"></span></button></label>")
321
+ label.find("button, span").css("cursor", "auto")
322
+ el.wrap(div)
323
+ el.after(label)
324
+ el.css("width", el.width() - label.width() - 11)
325
+
326
+ switch $.cowtech.form.date_plugin
327
+ when "jquery-tools"
328
+ el.dateinput(
329
+ lang: locale
330
+ format: format
331
+ firstDay: 1
332
+ selectors: true
333
+ css:
334
+ root: root_id
335
+ offset: [5, -1]
336
+ yearRange: [-5, 5]
337
+ )
338
+ when "jquery-ui"
339
+ val = el.val()
340
+ val = val.replace(/^((\d{4})-(\d{2})-(\d{2}))(.*)$/, "$4/$3/$2")
341
+ el.val val
342
+ el.datepicker(
343
+ changeMonth: true
344
+ changeYear: true
345
+ dateFormat: format
346
+ showOtherMonths: true
347
+ selectOtherMonths: true
348
+ minDate: "-5y"
349
+ maxDate: "5y"
350
+ )
351
+ $("#" + root_id).addClass("date-popup")
352
+ )
353
+ )
354
+
355
+ $.fn.cowtech_form = (->
356
+ $.cowtech.form.initialize() if $(window).data("cowtech-form-initialized") != true
357
+ @each(->
358
+ form = $(this)
359
+ form.find("[type=date], [validate=date]:not([data-form-date-raw])").cowtech_date()
360
+ $.cowtech.form.setup(form)
361
+ )
362
+ )
363
+
364
+ $.fn.cowtech_editor = (->
365
+ @each(->
366
+ field = $(this)
367
+ field.on("keydown", (ev) ->
368
+ rv = true
369
+ el = field.get(0)
370
+
371
+ keyCode = ev.keyCode || ev.which
372
+ if keyCode == 9
373
+ rv = false
374
+ start = el.selectionStart
375
+ end = el.selectionEnd
376
+ indented = "#{$(this).val().substring(0, start)}\t#{$(this).val().substring(end)}"
377
+ field.val(indented)
378
+ el.selectionStart = el.selectionEnd = start + 1
379
+ rv
380
+ )
381
+ )
382
+ )
383
+ )(jQuery)