123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- Spine = require('spine/core')
- # $ = Spine.$
- templates = require('duality/templates')
- FileUploadUI = require('controllers/ui/file-upload')
- Block = require('models/block')
- Site = require('models/site')
- class BlockForm extends Spine.Controller
- className: 'block form panel'
- elements:
- '.item-title': 'itemTitle'
- '.error-message': 'errorMessage'
- 'form': 'form'
- 'select[name=site]': 'formSite'
- 'input[name=enabled]': 'formEnabled'
- 'textarea[name=content]': 'formContent'
- '.upload-ui': 'fileUploadContainer'
- '.save-button': 'saveButton'
- '.cancel-button': 'cancelButton'
- events:
- 'submit form': 'preventSubmit'
- 'change *[name]': 'markAsDirty'
- 'keyup *[name]': 'markAsDirty'
- 'click .save-button': 'save'
- 'click .cancel-button': 'cancel'
- 'click .delete-button': 'destroy'
- 'change select[name=site]': 'siteChange'
- constructor: ->
- super
- @active @render
- render: (params) ->
- @dirtyForm = false
- @editing = params.id?
- if @editing
- @copying = params.id.split('-')[0] is 'copy'
- if @copying
- @title = 'Copy Block'
- @item = Block.find(params.id.split('-')[1]).dup()
- # Important to indicate that we are creating a new record
- @editing = false
- else
- @item = Block.find(params.id)
- @title = @item.name
-
- # Fetch missing data if need be
- if not @item.content?
- @item.ajax().reload {},
- success: =>
- @formContent.val(@item.content)
- else
- @title = 'New Block'
- @item = {}
- @item._attachments ?= {}
-
- @item.sites = Site.all().sort(Site.alphaSort)
- @html templates.render('block-form.html', {}, @item)
- @itemTitle.html @title
-
- # Set few initial form values
- if @editing or @copying
- @formSite.val(@item.site)
- @formEnabled.prop('checked', @item.enabled)
- else
- @formSite.val(@stack.stack.filterBox.siteId)
- @formEnabled.prop('checked', true)
- @siteChange()
- # Files upload area
- @fileUploadUI = new FileUploadUI
- docId: @item.id
- selectedFile: @item.photo
- attachments: @item._attachments
- changeCallback: @markAsDirty
- @fileUploadContainer.html @fileUploadUI.el
- return @
- siteChange: ->
- $siteSelected = @formSite.parents('.field').find('.site-selected')
- site = Site.exists(@formSite.val())
- if site
- $siteSelected.html "<div class=\"site-name theme-#{site.theme}\">#{site.name_html}</div>"
- else
- $siteSelected.html ""
- save: (e) ->
- e.preventDefault()
- if not navigator.onLine
- alert "Can not save. You are OFFLINE."
- return
-
- if @editing
- @item.fromForm(@form)
- else
- @item = new Block().fromForm(@form)
- @item._attachments = @fileUploadUI.attachments
- # Take care of some boolean checkboxes
- @item.enabled = @formEnabled.is(':checked')
-
- # Save the item and make sure it validates
- if @item.save()
- @back()
- else
- msg = @item.validate()
- @showError msg
- return @
- showError: (msg) ->
- @errorMessage.html(msg).show()
- @el.scrollTop(0)
-
- destroy: (e) ->
- e.preventDefault()
- if @item and confirm "Are you sure you want to delete this item?"
- @item.destroy()
- @back()
- markAsDirty: =>
- @dirtyForm = true
- @saveButton.addClass('glow')
- cancel: (e) ->
- e.preventDefault()
- if @dirtyForm
- if confirm "You may have some unsaved changes.\nAre you sure you want to proceed?"
- @back()
- else
- @back()
- back: ->
- @navigate('/blocks/list')
- preventSubmit: (e) ->
- e.preventDefault()
- return false
-
- deactivate: ->
- @el.scrollTop(0)
- super
- class BlockList extends Spine.Controller
- className: 'block list panel fixed-header'
- events:
- 'click h1 .count': 'reload'
- constructor: ->
- super
- # @active @render
- Block.bind 'change refresh', @render
- Spine.bind 'filterbox:change', @filter
- render: =>
- context =
- blocks: Block.filter(@filterObj).sort(Block.alphaSort)
- @html templates.render('blocks.html', {}, context)
- filter: (@filterObj) =>
- @render()
- @el.scrollTop(0)
- reload: ->
- Block.fetch()
- class Blocks extends Spine.Stack
- className: 'blocks panel'
- controllers:
- list: BlockList
- form: BlockForm
- default: 'list'
- routes:
- '/blocks/list': 'list'
- '/block/new': 'form'
- '/block/:id': 'form'
- constructor: ->
- super
- for k, v of @controllers
- @[k].active => @active()
- module.exports = Blocks
|