1 define(function(require) {
6 var _ = 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: gettext('Submit'),
31 cancelButtonText: gettext('Cancel')
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));
41 _.template(template)(_.extend({description: '', initialValue: ''}, field))
45 if(this.options.text) {
46 body.append('<p>' + this.options.text + '</p>');
49 this.$el.modal({backdrop: 'static'});
50 this.$el.modal('show');
51 this.$('textarea, input').first().focus();
53 onExecute: function(e) {
58 (this.options.fields || []).forEach(function(field) {
59 var widget = view.$('[name=' + field.name +']');
60 formData[field.name] = widget.val();
63 this.trigger('execute', {
65 success: function() { view.actionsDisabled = false; view.close(); },
66 error: function() { view.actionsDisabled = false; view.close(); },
69 onCancel: function() {
70 this.trigger('cancel');
77 if(!this.actionsDisabled) {
78 this.$el.modal('hide');
81 this.trigger('close');
83 toggleButtons: function(toggle) {
84 this.$('.btn, button').toggleClass('disabled', !toggle);
85 this.$('textarea').attr('disabled', !toggle);
86 this.actionsDisabled = !toggle;
91 create: function(config) {
92 return new DialogView(config);