This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.coffee
73 lines (61 loc) · 1.72 KB
/
application.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
$.mockjax
url: '/gen'
responseTime: 1
responseText:
'itsafaaaaake'
$ ->
$.widget "custom.generator",
container: {}
classNamespace: ''
history: []
_history: (self) ->
$hist = self.container[self.options.history]
update = ->
$hist.html ''
$hist.append "<li>#{data}</li>" for data in self.history
return {
push: (data)->
self.history.push data
update data
}
_create: ->
do @_builder
_builder: ->
@classNamespace = @element.attr 'id'
@_build("<input class='input'>")
@_build("<button class='button'>#{@options.buttonText}</button>")
@_build("<ul class='results'>")
do @_construct
_build: (el)->
$el = $ el
cl = $el.attr 'class'.toLowerCase()
$el.data 'action', cl
elClass = "#{@classNamespace}__#{cl}"
$el.attr 'class', elClass
@container[cl] = $el
return @
_construct: ->
el.appendTo(@element) for name, el of @container
do @_clicker
do @_updater
return @
_updater: ->
@element.on 'keyup', 'input', (e)=>
@element.data 'origin', $(e.target).val()
_clicker: ->
@element.on 'click', (e)=>
do @_sender if $(e.target).hasClass "#{@classNamespace}__#{@options.sender}"
action = $(e.target).data 'action'
@options[action]({value: @element.data 'origin'}) if @options[action]
_sender: ->
$.ajax
type: "POST"
url: "#{@options.url}"
data: {origin: @element.data 'origin'}
success: (r)=>
@_history(@).push r
$('#generator').generator
buttonText: 'GENERATE!'
url: '/gen'
sender: 'button'
history: 'results'