1 define(function(require) {
 
   5     var $ = require('libs/jquery'),
 
   6         _ = require('libs/underscore'),
 
   7         Backbone = require('libs/backbone'),
 
   8         dialogTemplate = require('libs/text!./dialog.html'),
 
  10         fieldTemplates.checkbox = require('libs/text!./templates/checkbox.html');
 
  11         fieldTemplates.select = require('libs/text!./templates/select.html');
 
  12         fieldTemplates.textarea = require('libs/text!./templates/textarea.html');
 
  13         fieldTemplates.input = require('libs/text!./templates/input.html');
 
  17     var DialogView = Backbone.View.extend({
 
  18         template: _.template(dialogTemplate),
 
  20             'click .execute-btn': 'onExecute',
 
  21             'click .cancel-btn': 'onCancel',
 
  22             'click .close': 'close'
 
  24         initialize: function() {
 
  26             this.actionsDisabled = false;
 
  29             this.setElement(this.template(_.extend({
 
  30                 executeButtonText: null,
 
  31                 cancelButtonText: null
 
  34             var body = this.$('.modal-body');
 
  35             (this.options.fields || []).forEach(function(field) {
 
  36                 var template = fieldTemplates[field.type];
 
  38                     throw new Error('Field type {type} not recognized.'.replace('{type}', field.type));
 
  40                 var widget = $(_.template(template)(_.extend({description: '', initialValue: ''}, field)));
 
  44                 if(_.isFunction(field.prePasteHandler) && field.type === 'input') { // TODO: extract this out to widget specific impl.
 
  45                     widget.find('input').on('paste', function(e) {
 
  46                         var clipboardData = e.originalEvent.clipboardData;
 
  47                         if(!clipboardData || !clipboardData.getData) {
 
  51                         var text = clipboardData.getData('text/plain').replace(/\r?\n|\r/g, ' ');
 
  52                         $(e.target).val(field.prePasteHandler(text));
 
  57             if(this.options.text) {
 
  58                 body.append('<p>' + this.options.text + '</p>');
 
  61             this.$el.modal({backdrop: 'static'});
 
  62             this.$el.modal('show');
 
  63             this.$('textarea, input').first().focus();
 
  65         onExecute: function(e) {
 
  70             (this.options.fields || []).forEach(function(field) {
 
  71                 var widget = view.$('[name=' + field.name +']');
 
  72                 formData[field.name] = widget.val();
 
  75             this.trigger('execute', {
 
  77                 success: function() { view.actionsDisabled = false; view.close(); },
 
  78                 error: function() { view.actionsDisabled = false; view.close(); },
 
  81         onCancel: function() {
 
  82             this.trigger('cancel');
 
  89             if(!this.actionsDisabled) {
 
  90                 this.$el.modal('hide');
 
  93             this.trigger('close');
 
  95         toggleButtons: function(toggle) {
 
  96             this.$('.btn, button').toggleClass('disabled', !toggle);
 
  97             this.$('textarea').attr('disabled', !toggle);
 
  98             this.actionsDisabled = !toggle;
 
 103         create: function(config) {
 
 104             return new DialogView(config);