Refactoring of modal api.

This commit is contained in:
Martin Edenhofer 2014-09-16 20:38:13 +02:00
parent e07f703667
commit c043b8bcf8
23 changed files with 243 additions and 323 deletions

View file

@ -441,125 +441,91 @@ class App.ControllerModal extends App.Controller
className: 'modal fade'
events:
'submit form': 'submit'
'click .js-submit': 'submit'
'click .js-cancel': 'modalHide'
'click .js-close': 'modalHide'
'submit form': 'onSubmit'
'click .js-submit': 'onSubmit'
'click .js-cancel': 'hide'
'click .js-close': 'hide'
constructor: (options) ->
constructor: (options = {}) ->
defaults =
backdrop: true
keyboard: true
close: true
title: '?'
options = _.extend( options, defaults )
# do not use @el, because it's inserted by js
if options
delete options.el
# callbacks
# @callback = {}
# if options.success
# @callback.success = options.success
# if options.error
# @callback.error = options.error
delete options.el
super(options)
if options.show
@render()
render: ->
@show()
show: (content) ->
console.log('M', @message)
if @button is true
@button = 'Submit'
@html App.view('modal')(
title: @title
head: @head
message: @message
detail: @detail
close: @close
cancel: @cancel
button: @button
)
@modalShow(
backdrop: @backdrop
keyboard: @keyboard
)
modalShow: (params) ->
defaults = {
backdrop: true
keyboard: true
show: true
}
data = $.extend({}, defaults, params)
@el.modal(data)
@el.bind('hidden.bs.modal', =>
# navigate back to home page
# if @pageData && @pageData.home
# @navigate @pageData.home
# navigate back
if params && params.navigateBack
window.history.back()
# remove modal from dom
$('.modal').remove();
)
if content
@el.find('.modal-body').html content
@el.modal('show')
modalHide: (e) ->
if e
e.preventDefault()
@el.modal('hide')
submit: (e) ->
hide: (e) ->
if e
e.preventDefault()
@el.modal('hide')
onShow: ->
console.log('no nothing')
# do nothing
onHide: ->
console.log('no nothing')
# do nothing
onSubmit: (e) ->
e.preventDefault()
@log 'error', 'You need to implement your own "submit" method!'
@log 'error', 'You need to implement your own "onSubmit" method!'
class App.ErrorModal extends App.ControllerModal
constructor: ->
super
@render()
render: ->
@html App.view('modal')(
title: 'Error',
message: @message
detail: @detail
close: @close
)
@modalShow(
backdrop: false,
keyboard: false,
)
@show()
class App.SessionMessage extends App.ControllerModal
constructor: ->
super
@render()
render: ->
@html App.view('modal')(
title: @title || '?'
message: @message || '?'
detail: @detail
close: @close
button: @button
)
@modalShow(
backdrop: @backdrop,
keyboard: @keyboard,
)
console.log('SM', @)
# reload page on modal hidden
if @forceReload
@el.on('hidden', =>
@reload()
)
@show()
modalHide: (e) =>
# reload page on modal hidden
onHide: (e) =>
if @forceReload
@reload(e)
@el.modal('hide')
submit: (e) =>
onSubmit: (e) =>
if @forceReload
@reload(e)
reload: (e) ->
if e
e.preventDefault()
$('#app').hide().attr('style', 'display: none!important')
if window.location.reload
window.location.reload()
return true

View file

@ -19,6 +19,9 @@ class App.ControllerForm extends App.Controller
@form.find('textarea').trigger('change')
@form.find('select').trigger('change')
@finishForm = true
@form
html: =>
@form.html()
@ -1594,6 +1597,9 @@ class App.ControllerForm extends App.Controller
else if form.parents('form')[0]
form = $( form.parents('form')[0] )
# use current content as form if form isn't already finished
else if !@finishForm
from = form
else
App.Log.error 'ControllerForm', 'no form found!', form

View file

@ -1,21 +1,22 @@
class App.ControllerGenericNew extends App.ControllerModal
constructor: (params) ->
super
@render()
render: ->
@head = App.i18n.translateContent( 'New' ) + ': ' + App.i18n.translateContent( @pageData.object )
@cancel = true
@button = true
@html App.view('generic/admin/new')( head: @pageData.object )
new App.ControllerForm(
controller = new App.ControllerForm(
el: @el.find('#object_new')
model: App[ @genericObject ]
params: @item
screen: @screen || 'edit'
autofocus: true
)
@modalShow()
submit: (e) ->
@show(controller.form)
onSubmit: (e) ->
e.preventDefault()
params = @formParam( e.target )
@ -39,32 +40,33 @@ class App.ControllerGenericNew extends App.ControllerModal
if ui.callback
item = App[ ui.genericObject ].fullLocal(@id)
ui.callback( item )
ui.modalHide()
ui.hide()
fail: ->
ui.log 'errors'
ui.modalHide()
ui.hide()
)
class App.ControllerGenericEdit extends App.ControllerModal
constructor: (params) ->
super
@item = App[ @genericObject ].find( params.id )
@render()
render: ->
@head = App.i18n.translateContent( 'Edit' ) + ': ' + App.i18n.translateContent( @pageData.object )
@cancel = true
@button = true
@html App.view('generic/admin/edit')( head: @pageData.object )
new App.ControllerForm(
el: @el.find('#object_edit')
controller = new App.ControllerForm(
el: @el.find('#object_new')
model: App[ @genericObject ]
params: @item
screen: @screen || 'edit'
autofocus: true
)
@modalShow()
submit: (e) ->
@show(controller.form)
onSubmit: (e) ->
e.preventDefault()
params = @formParam(e.target)
@item.load(params)
@ -86,11 +88,11 @@ class App.ControllerGenericEdit extends App.ControllerModal
if ui.callback
item = App[ ui.genericObject ].fullLocal(@id)
ui.callback( item )
ui.modalHide()
ui.hide()
fail: =>
ui.log 'errors'
ui.modalHide()
ui.hide()
)
class App.ControllerGenericIndex extends App.Controller
@ -186,22 +188,15 @@ class App.ControllerGenericIndex extends App.Controller
class App.ControllerGenericDestroyConfirm extends App.ControllerModal
constructor: ->
super
@render()
@head = 'Confirm'
@cancel = true
@button = 'Yes'
@message = 'Sure to delete this object?'
@show(@message)
render: ->
@html App.view('modal')(
title: 'Confirm'
message: 'Sure to delete this object?'
cancel: true
button: 'Yes'
)
@modalShow(
backdrop: true
keyboard: true
)
submit: (e) =>
@modalHide()
onSubmit: (e) ->
e.preventDefault()
@hide()
@item.destroy()
class App.ControllerDrox extends App.Controller
@ -398,7 +393,7 @@ class App.GenericHistory extends App.ControllerModal
@historyListCache
)
@modalShow()
@hide()
# enable user popups
@userPopups()

View file

@ -73,31 +73,24 @@ class App.ChannelEmailFilter extends App.Controller
class App.ChannelEmailFilterEdit extends App.ControllerModal
constructor: ->
super
@render(@object)
render: (data = {}) ->
if @object
@html App.view('generic/admin/edit')(
head: 'Postmaster Filter'
)
@form = new App.ControllerForm(
el: @el.find('#object_edit'),
model: App.PostmasterFilter,
params: @object,
autofocus: true,
)
else
@html App.view('generic/admin/new')(
head: 'Postmaster Filter'
)
@form = new App.ControllerForm(
el: @el.find('#object_new'),
model: App.PostmasterFilter,
autofocus: true,
)
@modalShow()
@head = 'Postmaster Filter'
@button = true
@close = true
@cancel = true
@show( @form.form )
submit: (e) =>
onSubmit: (e) =>
e.preventDefault()
# get params
@ -122,9 +115,9 @@ class App.ChannelEmailFilterEdit extends App.ControllerModal
# save object
object.save(
done: =>
@modalHide()
@hide()
fail: =>
@modalHide()
@hide()
)
@ -165,31 +158,24 @@ class App.ChannelEmailAddress extends App.Controller
class App.ChannelEmailAddressEdit extends App.ControllerModal
constructor: ->
super
@render(@object)
render: (data = {}) ->
if @object
@html App.view('generic/admin/edit')(
head: 'Email-Address'
)
@form = new App.ControllerForm(
el: @el.find('#object_edit')
model: App.EmailAddress
params: @object
autofocus: true
)
else
@html App.view('generic/admin/new')(
head: 'Email-Address'
)
@form = new App.ControllerForm(
el: @el.find('#object_new'),
model: App.EmailAddress,
autofocus: true,
)
@modalShow()
@head = 'Email-Address'
@button = true
@close = true
@cancel = true
@show( @form.form )
submit: (e) =>
onSubmit: (e) =>
e.preventDefault()
# get params
@ -213,9 +199,9 @@ class App.ChannelEmailAddressEdit extends App.ControllerModal
# save object
object.save(
done: =>
@modalHide()
@hide()
fail: =>
@modalHide()
@hide()
)
class App.ChannelEmailSignature extends App.Controller
@ -253,31 +239,24 @@ class App.ChannelEmailSignature extends App.Controller
class App.ChannelEmailSignatureEdit extends App.ControllerModal
constructor: ->
super
@render(@object)
render: (data = {}) ->
if @object
@html App.view('generic/admin/edit')(
head: 'Signature'
)
@form = new App.ControllerForm(
el: @el.find('#object_edit')
model: App.Signature
params: @object
autofocus: true
)
else
@html App.view('generic/admin/new')(
head: 'Signature'
)
@form = new App.ControllerForm(
el: @el.find('#object_new')
model: App.Signature
autofocus: true
)
@modalShow()
@head = 'Signature'
@button = true
@close = true
@cancel = true
@show( @form.form )
submit: (e) =>
onSubmit: (e) =>
e.preventDefault()
# get params
@ -301,9 +280,9 @@ class App.ChannelEmailSignatureEdit extends App.ControllerModal
# save object
object.save(
done: =>
@modalHide()
@hide()
fail: =>
@modalHide()
@hide()
)
class App.ChannelEmailInbound extends App.Controller
@ -342,30 +321,24 @@ class App.ChannelEmailInbound extends App.Controller
class App.ChannelEmailInboundEdit extends App.ControllerModal
constructor: ->
super
@render(@object)
render: (data = {}) ->
if @object
@html App.view('generic/admin/edit')(
head: 'Email Channel'
)
@form = new App.ControllerForm(
el: @el.find('#object_edit')
model: App.Channel
model: App.Channel
params: @object
autofocus: true
)
else
@html App.view('generic/admin/new')(
head: 'Email Channel'
)
@form = new App.ControllerForm(
el: @el.find('#object_new')
model: App.Channel
model: App.Channel
autofocus: true
)
@modalShow()
@head = 'Email Channel'
@button = true
@close = true
@cancel = true
@show( @form.form )
submit: (e) =>
onSubmit: (e) =>
e.preventDefault()
# get params
@ -390,9 +363,9 @@ class App.ChannelEmailInboundEdit extends App.ControllerModal
# save object
object.save(
done: =>
@modalHide()
@hide()
fail: =>
@modalHide()
@hide()
)
class App.ChannelEmailOutbound extends App.Controller

View file

@ -479,22 +479,20 @@ class Sidebar extends App.Controller
class UserNew extends App.ControllerModal
constructor: ->
super
@render()
@head = 'New User'
@cancel = true
@button = true
render: ->
@html App.view('agent_user_create')( head: 'New User' )
new App.ControllerForm(
controller = new App.ControllerForm(
el: @el.find('#form-user')
model: App.User
screen: 'edit'
autofocus: true
)
@modalShow()
@show( controller.form )
submit: (e) ->
onSubmit: (e) ->
e.preventDefault()
params = @formParam(e.target)
@ -503,12 +501,13 @@ class UserNew extends App.ControllerModal
if !params.login && params.email
params.login = params.email
user = new App.User
# find role_id
role = App.Role.findByAttribute( 'name', 'Customer' )
params.role_ids = role.id
if !params.role_ids || _.isEmpty( params.role_ids )
role = App.Role.findByAttribute( 'name', 'Customer' )
params.role_ids = role.id
@log 'notice', 'updateAttributes', params
user = new App.User
user.load(params)
errors = user.validate()
@ -532,11 +531,11 @@ class UserNew extends App.ControllerModal
# start customer info controller
ui.userInfo( user_id: user.id )
ui.modalHide()
ui.hide()
App.User.full( @id, callbackReload , true )
fail: ->
ui.modalHide()
ui.hide()
)
class Router extends App.ControllerPermanent

View file

@ -63,7 +63,7 @@ class App.TicketMerge extends App.ControllerModal
$(e.target).parents().find('[name="master_ticket_number"]').val( ticket.number )
)
@modalShow()
@hide()
submit: (e) =>
e.preventDefault()
@ -91,7 +91,7 @@ class App.TicketMerge extends App.ControllerModal
App.Collection.load( type: 'Ticket', data: [data.slave_ticket] )
# hide dialog
@modalHide()
@hide()
# view ticket
@log 'notice', 'nav...', App.Ticket.find( data.master_ticket['id'] )

View file

@ -130,7 +130,6 @@ class Index extends App.ControllerContent
msg: App.i18n.translateContent( 'Can\'t create user!' )
timeout: 2500
}
# @modalHide()
)
relogin: (data, status, xhr) =>

View file

@ -41,6 +41,48 @@ class ContentSidebarRightSidebarOptional extends App.ControllerContent
App.Config.set( 'layout_ref/content_sidebar_right_sidebar_optional', ContentSidebarRightSidebarOptional, 'Routes' )
class ModalForm extends App.ControllerModal
constructor: ->
super
@head = '123 some title'
@cancel = true
@button = true
controller = new App.ControllerForm(
model: App.User
autofocus: true
)
@show(controller.form)
onHide: =>
window.history.back()
onSubmit: (e) =>
e.preventDefault()
params = App.ControllerForm.params( $(e.target).closest('form') )
console.log('params', params)
App.Config.set( 'layout_ref/modal_form', ModalForm, 'Routes' )
class ModalText extends App.ControllerModal
constructor: ->
super
@head = '123 some title'
form = App.view('layout_ref/content')()
@show(form)
onHide: =>
window.history.back()
App.Config.set( 'layout_ref/modal_text', ModalText, 'Routes' )
class ContentSidebarTabsRight extends App.ControllerContent
elements:

View file

@ -70,8 +70,6 @@ class Index extends App.ControllerContent
success: @success
error: @error
)
# fail: =>
# @modalHide()
)
success: (data, status, xhr) =>

View file

@ -75,7 +75,7 @@ class App.TaskbarWidget extends App.Controller
remove: (e, key = false, force = false) =>
e.preventDefault()
if !key
key = $(e.target).parent().parent().data('key')
key = $(e.target).parents('a').data('key')
if !key
throw "No such key attributes found for task item"
@ -117,20 +117,14 @@ class App.TaskbarWidget extends App.Controller
class Remove extends App.ControllerModal
constructor: ->
super
@render()
@head = 'Confirm'
@message = 'Tab has changed, you really want to close it?'
@cancel = true
@close = true
@button = 'Close'
@show()
render: ->
@html App.view('modal')(
title: 'Confirm'
message: 'Tab has changed, you really want to close it?'
close: true
button: 'Close'
)
@modalShow(
backdrop: true,
keyboard: true,
)
submit: (e) =>
@modalHide()
onSubmit: (e) =>
e.preventDefault()
@hide()
@ui.remove(e, @key, true)

View file

@ -18,7 +18,7 @@ class App.TicketCustomer extends App.ControllerModal
},
autofocus: true,
)
@modalShow()
@hide()
submit: (e) =>
e.preventDefault()
@ -30,7 +30,7 @@ class App.TicketCustomer extends App.ControllerModal
callback = =>
# close modal
@modalHide()
@hide()
# update ticket
@ticket.updateAttributes(

View file

@ -437,13 +437,7 @@ class App.OverviewSettings extends App.ControllerModal
constructor: ->
super
@overview = App.Overview.find(@overview_id)
@render()
render: ->
@html App.view('dashboard/ticket_settings')(
overview: @overview,
)
@configure_attributes_article = []
if @view_mode is 'd'
@configure_attributes_article.push({
@ -593,15 +587,20 @@ class App.OverviewSettings extends App.ControllerModal
class: 'medium'
})
new App.ControllerForm(
el: @el.find('#form-setting')
form = App.view('dashboard/ticket_settings')(
overview: @overview,
)
@head = App.i18n.translateContent( 'Edit' ) + ': ' + App.i18n.translateContent( @overview.name )
@close = true
@cancel = true
@button = true
controller = new App.ControllerForm(
model: { configure_attributes: @configure_attributes_article }
autofocus: false
)
@show( controller.form )
@modalShow()
submit: (e) =>
onSubmit: (e) =>
e.preventDefault()
params = @formParam(e.target)
@ -627,7 +626,7 @@ class App.OverviewSettings extends App.ControllerModal
else
@overview.trigger('local:rerender')
)
@modalHide()
@hide()
class Navbar extends App.Controller
constructor: ->

View file

@ -83,17 +83,18 @@ class App.WidgetLink extends App.Controller
class App.LinkAdd extends App.ControllerModal
constructor: ->
super
@render()
@head = 'Links'
@button = true
@cancel = true
render: =>
@html App.view('link/add')(
form = App.view('link/add')(
link_object: @link_object,
link_object_id: @link_object_id,
object: @object,
)
@modalShow()
@show( form )
submit: (e) =>
onSubmit: (e) =>
e.preventDefault()
params = @formParam(e.target)
@ -111,6 +112,6 @@ class App.LinkAdd extends App.ControllerModal
}
processData: true,
success: (data, status, xhr) =>
@modalHide()
@hide()
@parent.fetch()
)

View file

@ -6,23 +6,18 @@ class Widget extends App.Controller
App.Event.bind(
'session:maintenance'
(data) =>
new Message( message: data )
console.log('session:maintenance', data)
@showMessage( data )
'maintenance'
)
class Message extends App.ControllerModal
constructor: ->
super
@render(@message)
render: (message = {}) ->
showMessage: (message = {}) =>
if message.reload
@disconnectClient()
button = 'Reload application'
new App.SessionMessage(
title: message.title
head: message.head
message: message.message
keyboard: false
backdrop: true

View file

@ -39,7 +39,7 @@ class Widget extends App.Controller
# only if new client id isnt own client id
if data.taskbar_id isnt App.TaskManager.TaskbarId()
@error = new App.SessionMessage(
title: 'Session'
head: 'Session'
message: 'Session taken over... please reload page or work with other browser window.'
keyboard: false
backdrop: true

View file

@ -42,9 +42,9 @@ class App.Browser
@message: (data) ->
new App.ControllerModal(
title: 'Browser too old!'
head: 'Browser too old!'
message: "Your Browser is not supported (#{data.browser} #{data.version} #{data.OS}). Please use a newer one."
show: true
close: false
backdrop: false
keyboard: false
)

View file

@ -194,7 +194,7 @@ class _webSocketSingleton extends App.Controller
# close error message show up (because try so connect again) if exists
App.Delay.clear('websocket-no-connection-try-reconnect-message', 'ws')
if @error
@error.modalHide()
@error.hide()
@error = false
@tryToConnect = false
@ -242,7 +242,7 @@ class _webSocketSingleton extends App.Controller
message: 'Lost network connection to system, trying to reconnect...'
backdrop: false
keyboard: false
show: true
close: false
)
if !@tryToConnect
App.Delay.set message, 7000, 'websocket-no-connection-try-reconnect-message', 'ws'

View file

@ -1,17 +0,0 @@
<form>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="modal-close js-close">
<div class="close icon"></div>
</div>
<h1 class="modal-title"><%- @T( 'Edit' ) %>: <%- @T( @head ) %></h1>
</div>
<div class="modal-body" id="object_edit"></div>
<div class="modal-footer horizontal">
<a class="subtle-link standalone js-cancel" href="#/"><%- @T( 'Cancel & Go Back' ) %></a>
<button type="submit" class="btn btn-create js-submit align-right"><%- @T( 'Submit' ) %></button>
</div>
</div>
</div>
</form>

View file

@ -1,17 +0,0 @@
<form>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="modal-close js-close">
<div class="close icon"></div>
</div>
<h1 class="modal-title"><%- @T( 'Neu' ) %>: <%- @T( @head ) %></h1>
</div>
<div class="modal-body" id="object_new"></div>
<div class="modal-footer horizontal">
<a class="subtle-link standalone js-cancel" href="#/"><%- @T( 'Cancel & Go Back' ) %></a>
<button type="submit" class="btn btn-create js-submit align-right"><%- @T('Submit') %></button>
</div>
</div>
</div>
</form>

View file

@ -9,6 +9,7 @@
<li>Content + Sidebar Right not shown if screen resulution is lower then 1280px <a href="#layout_ref/content_sidebar_right_sidebar_optional">Example</a></li>
<li>Content + Sidebar Left <a href="#layout_ref/content_sidebar_left">Example</a></li>
<li>Content + Sidebar Tabs Right <a href="#layout_ref/content_sidebar_tabs_right">Example</a></li>
<li>Modal Dialog <a href="#layout_ref/modal_form">Example with From</a> / <a href="#layout_ref/modal_text">Example with Text</a></li>
</ul>
</div>

View file

@ -1,13 +1,4 @@
<form>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="modal-close js-close">
<div class="close icon"></div>
</div>
<h1 class="modal-title"><%- @T( 'Link' ) %></h1>
</div>
<div class="modal-body">
<fieldset>
<%- @T( 'Link' ) %>
<%- @T( @link_object ) %>
<input type="text" name="ticket_number" value="" class="span2" required/>
@ -28,11 +19,4 @@
<li>Recent Customer Tickets</li>
</ul>
-->
</div>
<div class="modal-footer horizontal">
<a class="subtle-link standalone js-cancel" href="#/"><%- @T( 'Cancel & Go Back' ) %></a>
<button type="submit" class="btn btn-create js-submit align-right"><%- @T('Submit') %></button>
</div>
</div>
</div>
</form>
</fieldset>

View file

@ -6,7 +6,7 @@
<div class="form-group">
<label for="title"><%- @T('Title') %></label>
<div class="controls">
<input type="text" name="title" class="form-control" placeholder="<%- @Ti('some title') %>" required>
<input type="text" name="head" class="form-control" placeholder="<%- @Ti('some title') %>" required>
</div>
</div>
<div class="form-group">

View file

@ -1,28 +1,30 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<% if @close: %>
<div class="modal-close js-close">
<div class="close icon"></div>
<form>
<div class="modal-content">
<div class="modal-header">
<% if @close: %>
<div class="modal-close js-close">
<div class="close icon"></div>
</div>
<% end %>
<h1 class="modal-title"><%- @T( @head ) %></h1>
</div>
<div class="modal-body">
<% if @message: %>
<p><%= @message %></p>
<% end %>
<% if @detail: %>
<pre><%= @detail %></p>
<% end %>
</div>
<div class="modal-footer horizontal">
<% if @cancel: %>
<a class="subtle-link standalone js-cancel" href="#/"><%- @T( 'Cancel & Go Back' ) %></a>
<% end %>
<% if @button: %>
<button type="submit" class="btn btn-create js-submit align-right"><%- @T( @button ) %></button>
<% end %>
</div>
<% end %>
<h1 class="modal-title"><%- @T( @title ) %></h1>
</div>
<div class="modal-body">
<form>
<p><%= @message %></p>
<% if @detail: %>
<pre><%= @detail %></p>
<% end %>
</form>
</div>
<div class="modal-footer horizontal">
<% if @cancel: %>
<a class="subtle-link standalone js-cancel" href="#/"><%- @T( 'Cancel & Go Back' ) %></a>
<% end %>
<% if @button: %>
<button type="submit" class="btn btn-create js-submit align-right"><%- @T( @button ) %></button>
<% end %>
</div>
</div>
</div>
</form>
</div>