0.1.1 • Published 8 years ago

wwl-js-vm-modals v0.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

WWL VM Modals

View module (wwl-js-vm) implementation to maintain a stack of modals. The implementation is based on backbone and backbone.marionette.

Each modal requires to get a view object (Backbone.View api compatible) to passed in for each modal. The model will render that view as content.

Example

context = new (require('wwl-js-app-context'))({ root: true })
vm      = new (require('wwl-js-vm-modals'))({ context: context })

vm.getView().render()
$('body').append(vm.getView().$el)

# Create a dummy content view
View = Backbone.View.extend({ template: '<div><h1>Test</h1></div>' })
view = new View()

# Create a modal
modal = vm.addModal({
  view:   view
  title:  'My first Modal'
  closeButton:      true
  backTopButton:    true
  backBottomButton: true
  destroyOnClose:   true
  cancelButton:     'Cancel'
  successButton:    'Save'
})

# Listen to your modal

modal.on 'view:render', ->
  # The modal view itself has been rendered
  null

modal.on 'view:action:click:success', ->
  # The modals success button ("Save") has been pressed
  null

Installation

wwl-js-vm-modals requires sass, coffeescript, browserify, hamlify.

Make sure, that for sass and for browserify, the load paths are set up correctly.

// In your applications stylesheet:

// For cmi version:
@import 'wwl-js-vm-modals/base_cmi'

// For marionette-only version:
@import 'wwl-js-vm-modals/base_no_cmi'
# In your js application:

# For cmi version:
VmClass = require('wwl-js-vm-modals')

# For marionette-only version:
VmClass = require('wwl-js-vm-modals/lib/vm')

Cmi Version

Important: To run the cmi version, you need to install webcomponents and link at least:

%link{ rel: "import", href: "../paper-spinner/paper-spinner.html" }
%link{ rel: "import", href: "../cmi-button/cmi-button.html" }
%link{ rel: "import", href: "../cmi-button/cmi-button-link.html" }
%link{ rel: "import", href: "../cmi-dialog/cmi-dialog-scrollable.html" }
%link{ rel: "import", href: "../cmi-dialog/cmi-dialog.html" }
%link{ rel: "import", href: "../cmi-dialog/cmi-dialog-extended.html" }

Unfortunatly, it's not easy to automate this.

General concept

The modals stack is maintained by a Backbone.Collection with Backbone.Model instances. Each model represents a modal you can see. A modal model requires at least the attribute view on initialization.

Every action happening on your modals view, you can listen to via the model.

The modal won't close or save automatically. You need to maintain this.

The view you're passing in and the modal view itself don't have any knowledge about each other. E.g. if you need to persist form data from your inside view on 'view:action:click:success', pass that information to your inner view by hand!

Modal Model Attributes

attributetypedefaultdescription
viewBackbone.View or same apinullrequired - The view that will be rendered as content.
visiblebooleantrueYou can add modals to the collection without displaying them. Internally, we're using a Backbone.VirtualCollection to only display modals that got visible true.
closeButtonbooleantrueIf the x close button is visible.
backTopButtonbooleanfalseIf the back button on top is visible.
backBottomButtonbooleanfalseIf the back button on bottom is visible.
headlinestring''The headline of the modal.
cancelButtonstring''The text of the cancel button. If null, undefined or empty string, it will hide the button.
doneButtonstring''The text of the done button. If null, undefined or empty string, it will hide the button.
successButtonstring''The text of the success button (colored). If null, undefined or empty string, it will hide the button.
noCancelOnEscKeybooleantrueIf true, you won't get a cancel event when pressing esc.
disableBackdropClickClosebooleanfalseFire close event when clicking the backdrop.
viewIdstring''Set an id for the modal view (not your view you're passing in).
viewClassstring''Set classes for the modal view (not your view you're passing in).
destroyOnCancelbooleanfalseIf to destroy the modal model (close modal), if the cancel event is fired.
destroyOnBackdropbooleanfalseIf to destroy the modal model (close modal), if the backdrop has been clicked.
destroyOnClosebooleanfalseIf to destroy the modal model (close modal), if the close event has been fired.
cmi modal only:
entryAnimationstring'scale-up-animation'See https://elements.polymer-project.org/elements/neon-animation?active=scale-up-animation 
exitAnimationstring'fade-in-animation'See https://elements.polymer-project.org/elements/neon-animation?active=scale-up-animation

Modal Model Events

eventdescription
view:beforeRenderMarionette event on the modal item view
view:renderMarionette event on the modal item view
view:beforeAttachMarionette event on the modal item view
view:attachMarionette event on the modal item view
view:beforeShowMarionette event on the modal item view
view:showMarionette event on the modal item view
view:domRefreshMarionette event on the modal item view
view:beforeDestroyMarionette event on the modal item view
view:destroyMarionette event on the modal item view
view:action:click:backdropWhen clicked the backdrop.
view:action:click:cancelWhen clicked the cancel button.
view:action:click:doneWhen clicked the done button.
view:action:click:successWhen clicked the success button.
view:action:click:backWhen clicked on of the back buttons.
view:action:click:closeWhen clicked the close button (x).

With or without cmi webcomponents

By default the modal is build on base of curo-material-interface.

So you'll get the webcomponents implementation if you call:

require('wwl-js-vm-modals')

Or explicit:

require('wwl-js-vm-modals/lib/vm_cmi')

If you want to have a marionette-only implementation, you would require the following:

require('wwl-js-vm-modals/lib/vm')