smartxml: fix - getIndex returns undefined for a node not attached to a document...
[fnpeditor.git] / src / editor / views / dialog / dialog.js
1 define(function(require) {
2
3     'use strict';
4
5     var $ = require('libs/jquery'),
6         _ = require('libs/underscore'),
7         Backbone = require('libs/backbone'),
8         dialogTemplate = require('libs/text!./dialog.html'),
9         fieldTemplates = {};
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');
14
15
16
17     var DialogView = Backbone.View.extend({
18         template: _.template(dialogTemplate),
19         events: {
20             'click .execute-btn': 'onExecute',
21             'click .cancel-btn': 'onCancel',
22             'click .close': 'close'
23         },
24         initialize: function() {
25             _.bindAll(this);
26             this.actionsDisabled = false;
27         },
28         show: function() {
29             this.setElement(this.template(_.extend({
30                 executeButtonText: null,
31                 cancelButtonText: null
32             }, this.options)));
33
34             var body = this.$('.modal-body');
35             (this.options.fields || []).forEach(function(field) {
36                 var template = fieldTemplates[field.type];
37                 if(!template) {
38                     throw new Error('Field type {type} not recognized.'.replace('{type}', field.type));
39                 }
40                 var widget = $(_.template(template)(_.extend({description: '', initialValue: ''}, field)));
41
42                 body.append(widget);
43
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) {
48                             return;
49                         }
50                         e.preventDefault();
51                         var text = clipboardData.getData('text/plain').replace(/\r?\n|\r/g, ' ');
52                         $(e.target).val(field.prePasteHandler(text));
53                     });
54                 }
55             });
56
57             if(this.options.text) {
58                 body.append('<p>' + this.options.text + '</p>');
59             }
60
61             this.$el.modal({backdrop: 'static'});
62             this.$el.modal('show');
63             this.$('textarea, input').first().focus();
64         },
65         onExecute: function(e) {
66             e.preventDefault();
67             var view = this,
68                 formData = {};
69
70             (this.options.fields || []).forEach(function(field) {
71                 var widget = view.$('[name=' + field.name +']');
72                 formData[field.name] = widget.val();
73             });
74
75             this.trigger('execute', {
76                 formData: formData,
77                 success: function() { view.actionsDisabled = false; view.close(); },
78                 error: function() { view.actionsDisabled = false; view.close(); },
79             });
80         },
81         onCancel: function() {
82             this.trigger('cancel');
83             this.close();
84         },
85         close: function(e) {
86             if(e) {
87                 e.preventDefault();
88             }
89             if(!this.actionsDisabled) {
90                 this.$el.modal('hide');
91                 this.$el.remove();
92             }
93             this.trigger('close');
94         },
95         toggleButtons: function(toggle) {
96             this.$('.btn, button').toggleClass('disabled', !toggle);
97             this.$('textarea').attr('disabled', !toggle);
98             this.actionsDisabled = !toggle;
99         }
100     });
101
102     return {
103         create: function(config) {
104             return new DialogView(config);
105         }
106     };
107
108 });