allow switch from any to image/video
[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                 cssClass: '',
33                 closeButton: true
34             }, this.options)));
35
36             var body = this.$('.modal-body');
37             (this.options.fields || []).forEach(function(field) {
38                 var template = fieldTemplates[field.type];
39                 if(!template) {
40                     throw new Error('Field type {type} not recognized.'.replace('{type}', field.type));
41                 }
42                 var widget = $(_.template(template)(_.extend({description: '', initialValue: ''}, field)));
43
44                 body.append(widget);
45
46                 if(_.isFunction(field.prePasteHandler) && field.type === 'input') { // TODO: extract this out to widget specific impl.
47                     widget.find('input').on('paste', function(e) {
48                         var clipboardData = e.originalEvent.clipboardData;
49                         if(!clipboardData || !clipboardData.getData) {
50                             return;
51                         }
52                         e.preventDefault();
53                         var text = clipboardData.getData('text/plain').replace(/\r?\n|\r/g, ' ');
54                         $(e.target).val(field.prePasteHandler(text));
55                     });
56                 }
57             });
58
59             if(this.options.text) {
60                 body.append('<p>' + this.options.text + '</p>');
61             }
62
63             this.$el.modal({backdrop: 'static'});
64             this.$el.modal('show');
65             this.$('textarea, input').first().focus();
66         },
67         onExecute: function(e) {
68             e.preventDefault();
69             var view = this,
70                 formData = {};
71
72             (this.options.fields || []).forEach(function(field) {
73                 var widget = view.$('[name=' + field.name +']');
74                 formData[field.name] = widget.val();
75             });
76
77             this.trigger('execute', {
78                 formData: formData,
79                 success: function() { view.actionsDisabled = false; view.close(); },
80                 error: function() { view.actionsDisabled = false; view.close(); },
81             });
82         },
83         onCancel: function() {
84             this.trigger('cancel');
85             this.close();
86         },
87         close: function(e) {
88             if(e) {
89                 e.preventDefault();
90             }
91             if(!this.actionsDisabled) {
92                 this.$el.modal('hide');
93                 this.$el.remove();
94             }
95             this.trigger('close');
96         },
97         toggleButtons: function(toggle) {
98             this.$('.btn, button').toggleClass('disabled', !toggle);
99             this.$('textarea').attr('disabled', !toggle);
100             this.actionsDisabled = !toggle;
101         },
102         setContentView: function(view) {
103             var body = this.$('.modal-body');
104             body.append(view);
105         }
106     });
107
108     return {
109         create: function(config) {
110             return new DialogView(config);
111         }
112     };
113
114 });