contacts.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Spine = require('spine/core')
  2. # $ = Spine.$
  3. templates = require('duality/templates')
  4. Contact = require('models/contact')
  5. class ContactForm extends Spine.Controller
  6. className: 'contact form panel'
  7. elements:
  8. '.item-title': 'itemTitle'
  9. '.error-message': 'errorMessage'
  10. 'form': 'form'
  11. '.save-button': 'saveButton'
  12. '.cancel-button': 'cancelButton'
  13. events:
  14. 'submit form': 'preventSubmit'
  15. 'change *[name]': 'markAsDirty'
  16. 'keyup *[name]': 'markAsDirty'
  17. 'click .save-button': 'save'
  18. 'click .cancel-button': 'cancel'
  19. 'click .delete-button': 'destroy'
  20. constructor: ->
  21. super
  22. @active @render
  23. render: (params) ->
  24. @dirtyForm = false
  25. @editing = params.id?
  26. if @editing
  27. @copying = params.id.split('-')[0] is 'copy'
  28. if @copying
  29. @title = 'Copy Contact'
  30. @item = Contact.find(params.id.split('-')[1]).dup()
  31. # Important to indicate that we are creating a new record
  32. @editing = false
  33. else
  34. @item = Contact.find(params.id)
  35. @title = @item.name
  36. else
  37. @title = 'New Contact'
  38. @item = {}
  39. @html templates.render('contact-form.html', {}, @item)
  40. @itemTitle.html @title
  41. return @
  42. save: (e) ->
  43. e.preventDefault()
  44. if not navigator.onLine
  45. alert "Can not save. You are OFFLINE."
  46. return
  47. if @editing
  48. @item.fromForm(@form)
  49. else
  50. @item = new Contact().fromForm(@form)
  51. # Save the item and make sure it validates
  52. if @item.save()
  53. @back()
  54. else
  55. msg = @item.validate()
  56. @showError msg
  57. return @
  58. showError: (msg) ->
  59. @errorMessage.html(msg).show()
  60. @el.scrollTop(0)
  61. destroy: (e) ->
  62. e.preventDefault()
  63. if @item and confirm "Are you sure you want to delete this item?"
  64. @item.destroy()
  65. @back()
  66. markAsDirty: =>
  67. @dirtyForm = true
  68. @saveButton.addClass('glow')
  69. cancel: (e) ->
  70. e.preventDefault()
  71. if @dirtyForm
  72. if confirm "You may have some unsaved changes.\nAre you sure you want to proceed?"
  73. @back()
  74. else
  75. @back()
  76. back: ->
  77. @navigate('/contacts/list')
  78. preventSubmit: (e) ->
  79. e.preventDefault()
  80. return false
  81. deactivate: ->
  82. @el.scrollTop(0)
  83. super
  84. class ContactList extends Spine.Controller
  85. className: 'contact list panel fixed-header'
  86. events:
  87. 'click h1 .count': 'reload'
  88. constructor: ->
  89. super
  90. # @active @render
  91. Contact.bind 'change refresh', @render
  92. Spine.bind 'filterbox:change', @filter
  93. render: =>
  94. context =
  95. contacts: Contact.filter(@filterObj).sort(Contact.alphaSort)
  96. @html templates.render('contacts.html', {}, context)
  97. filter: (@filterObj) =>
  98. @render()
  99. @el.scrollTop(0)
  100. reload: ->
  101. Contact.fetch()
  102. class Contacts extends Spine.Stack
  103. className: 'contacts panel'
  104. controllers:
  105. list: ContactList
  106. form: ContactForm
  107. default: 'list'
  108. routes:
  109. '/contacts/list': 'list'
  110. '/contact/new': 'form'
  111. '/contact/:id': 'form'
  112. constructor: ->
  113. super
  114. for k, v of @controllers
  115. @[k].active => @active()
  116. module.exports = Contacts