-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRemoteButton.coffee
108 lines (94 loc) · 2.36 KB
/
RemoteButton.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
goog.require 'goog.dom.dataset'
goog.require 'goog.dom.forms'
goog.require 'goog.Uri'
###*
A button that allows an Ajax call.
###
class wzk.ui.form.RemoteButton extends wzk.ui.Button
@EVENTS:
SUCCESS: 'remote-button-success'
ERROR: 'remote-button-error'
###*
@enum {string}
###
@DATA:
FIELDS: 'fields'
###*
@param {Object} params
###
constructor: (params) ->
super params
@fields = {}
###*
Sends a request on a given model with a given method
@param {wzk.resource.Client} client
@param {string} url
@param {string} method
@param {Object|null|string=} content
@param {function(Object)|null=} onSuccess
@param {boolean=} responseByModel
###
call: (client, url, method, content, onSuccess = null, responseByModel = false) ->
@setEnabled false
url = @composePath url
handleSuccess = (response) =>
@setEnabled true
onSuccess(response) if onSuccess?
@dispatchSuccess response
client.request url, method, content, handleSuccess, @handleError, responseByModel
###*
@protected
@param {Object} response
###
handleError: (response) =>
@setEnabled true
@dispatchError response
###*
@protected
@param {Object} response
###
dispatchSuccess: (response) ->
@dispatchEvent new goog.events.Event(wzk.ui.form.RemoteButton.EVENTS.SUCCESS, response)
###*
@protected
@param {Object} response
###
dispatchError: (response) ->
@dispatchEvent new goog.events.Event(wzk.ui.form.RemoteButton.EVENTS.ERROR, response)
###*
@override
###
createDom: ->
@addClassName 'remote-button'
super()
###*
@override
###
decorate: (el) ->
el.setAttribute 'type', 'button'
super el
@parseFields el
###*
@protected
@param {string} url
@return {string}
###
composePath: (url) ->
uri = new goog.Uri url
for param, field of @fields
uri.setParameterValue param, goog.dom.forms.getValue field
parts = [uri.getPath()]
parts.push uri.getQuery() if uri.getQuery()
parts.join '?'
###*
@protected
@param {Element} el
###
parseFields: (el) ->
els = goog.dom.dataset.get el, wzk.ui.form.RemoteButton.DATA.FIELDS
if els
selectors = String(els).split ','
for s in selectors
field = @dom.cls('remote-button-param-' + s)
if field?
@fields[s] = field