-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
181 lines (127 loc) · 5.68 KB
/
app.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
App = angular.module('contactbooster', ['ionic', 'restangular']);
if window.location.origin is "http://localhost:8080"
App.constant('BASE_URL', "http://localhost:3000/")
else if window.location.origin is "http://localhost:8033"
App.constant('BASE_URL', 'http://railscontactbooster.apiary-mock.com/')
else
App.constant('BASE_URL', 'http://contactbooster-rails.herokuapp.com/')
# Restangular is used as a RESTFul manager -> https://github.com/mgonto/restangular
App.factory('Contactbooster', (Restangular, BASE_URL) ->
Restangular.withConfig (RestangularConfigurer) ->
RestangularConfigurer.setBaseUrl(BASE_URL)
RestangularConfigurer.addResponseInterceptor (data, operation, what, url, response, deferred) ->
if operation == "getList"
extractedData = data.lists
extractedData.total = data.total
return extractedData
)
.controller('ContactsCtrl', ($scope,
Contactbooster,
$ionicModal,
$ionicSideMenuDelegate,
$ionicLoading,
$ionicActionSheet) ->
ContactView = new ContactsController $scope,
Contactbooster,
$ionicModal,
$ionicSideMenuDelegate,
$ionicLoading,
$ionicActionSheet
ContactView.fetch()
)
class ContactsController
constructor: ($scope, @Contactbooster, @ionicModal, @ionicSideMenuDelegate, @ionicLoading, @ionicActionSheet) ->
@scope = $scope
@scope.contact = {} # contact object, used as placeholder for new/editer contact
@scope.contactsInitialized = false # boolean used to know if a contact array is initialized
@scope.contactListInitialized = false # boolean used to know if a contactList array is initialized
@scope.contactLists = [ ]
@scope.activeContacts = [ ]
@scope.activeContactLists = [ ]
@initContactFormModal()
@delegateEvent()
initContactFormModal: ->
@ionicModal.fromTemplateUrl './partials/contact_form.html', (modal) =>
@scope.contactModal = modal
, { scope: @scope, animation: 'slide-in-up' }
delegateEvent: ->
@scope.selectList = @selectList
@scope.contactForm = @contactForm
@scope.submitContact = @submitContact
@scope.closeContactForm = @closeContactForm
@scope.createContact = @createContact
@scope.actionContact = @actionContact
actionContact: (contact) =>
contactId = contact.id
@ionicActionSheet.show({
buttons: [ { text: 'Edit' } ]
, destructiveText: 'Delete'
, destructiveButtonClicked: =>
@deleteContact(contactId) if confirm "Are you sure you want to delete contact #{contactId} ?"
, titleText: "Make an action for this contact"
, cancelText: "Cancel"
, buttonClicked: (index) =>
@contactForm(angular.copy(contact))
return true
, cancel: ->
})
deleteContact: (contactId) =>
@Contactbooster.one('lists', @scope.activeContacts.id).one('contacts', contactId).remove().then =>
$("#contact_#{contactId}").fadeOut('fast')
@removeContactFromMemory contactId
return true
removeContactFromMemory: (contactId) =>
@scope.activeContacts.contacts = _.without(@scope.activeContacts.contacts, _.findWhere(@scope.activeContacts.contacts, { id: contactId }))
contactForm: (contact = {}) =>
@scope.contact = contact
if @scope.contact.id
@scope.modal = { title: "Edit contact: ##{@scope.contact.id}", text_button: "Edit contact"}
else
@scope.modal = { title: "New contact", text_button: "Add"}
@scope.contactModal.show()
closeContactForm: =>
@scope.contactModal.hide()
submitContact: (contact) =>
# prep the request
return alert "Please fill the fields" if _.isEmpty contact
@ionicLoading.show({ template: 'Creating new contact...'})
contact_to_save = { contact: contact }
# branch depending on context
if contact.id
q = @editContact contact_to_save
else
q = @createContact contact_to_save
q.then @success_cleanup, @error_cleanup
success_cleanup: =>
@ionicLoading.hide()
@scope.contactModal.hide()
error_cleanup: (err) =>
@ionicLoading.hide()
error_string = "Validation error\n"
for field, error of err.data.error
error_string += "#{field} : #{error[0]}\n"
alert (error_string)
editContact: (contact) =>
q = @Contactbooster.one('lists', @scope.activeContacts.id).one('contacts', contact.contact.id).patch(contact)
q.then (modified) =>
old_contact = _.findWhere(@scope.activeContacts.contacts, {id: contact.contact.id})
for key in [ 'lastname', 'firstname', 'phone' ]
old_contact[key] = contact.contact[key]
return q
createContact: (contact) =>
q = @Contactbooster.one('lists', @scope.activeContacts.id).post('contacts', contact)
q.then (created) =>
@scope.activeContacts.contacts.push contact.contact
return q
fetch: ->
@ionicLoading.show({ template: 'Fetching lists.....' })
@Contactbooster.all('lists').getList().then (lists) =>
@ionicLoading.hide();
@scope.contactListInitialized = true
@scope.contactLists = lists
@scope.activeContactLists = _.map( lists, (list) -> list.name )
selectList: (index) =>
@scope.contactsInitialized = true
@scope.activeContacts = @scope.contactLists[index]
@scope.activeListName = @scope.activeContacts.name
@ionicSideMenuDelegate.toggleLeft()