1 define(function(require) {
5 var _ = require('libs/underscore'),
6 Backbone = require('libs/backbone'),
7 dialogTemplate = require('libs/text!./dialog.html'),
9 fieldTemplates.checkbox = require('libs/text!./templates/checkbox.html');
10 fieldTemplates.select = require('libs/text!./templates/select.html');
11 fieldTemplates.textarea = require('libs/text!./templates/textarea.html');
12 fieldTemplates.input = require('libs/text!./templates/input.html');
16 var DialogView = Backbone.View.extend({
17 template: _.template(dialogTemplate),
19 'click .execute-btn': 'onExecute',
20 'click .cancel-btn': 'onCancel',
21 'click .close': 'close'
23 initialize: function() {
25 this.actionsDisabled = false;
28 this.setElement(this.template(_.extend({
29 executeButtonText: null,
30 cancelButtonText: null
33 var body = this.$('.modal-body');
34 (this.options.fields || []).forEach(function(field) {
35 var template = fieldTemplates[field.type];
37 throw new Error('Field type {type} not recognized.'.replace('{type}', field.type));
40 _.template(template)(_.extend({description: '', initialValue: ''}, field))
44 if(this.options.text) {
45 body.append('<p>' + this.options.text + '</p>');
48 this.$el.modal({backdrop: 'static'});
49 this.$el.modal('show');
50 this.$('textarea, input').first().focus();
52 onExecute: function(e) {
57 (this.options.fields || []).forEach(function(field) {
58 var widget = view.$('[name=' + field.name +']');
59 formData[field.name] = widget.val();
62 this.trigger('execute', {
64 success: function() { view.actionsDisabled = false; view.close(); },
65 error: function() { view.actionsDisabled = false; view.close(); },
68 onCancel: function() {
69 this.trigger('cancel');
76 if(!this.actionsDisabled) {
77 this.$el.modal('hide');
80 this.trigger('close');
82 toggleButtons: function(toggle) {
83 this.$('.btn, button').toggleClass('disabled', !toggle);
84 this.$('textarea').attr('disabled', !toggle);
85 this.actionsDisabled = !toggle;
90 create: function(config) {
91 return new DialogView(config);