e1f49b4641a1b400e86c1588475ac0ab08f72965
[fnpeditor.git] / src / editor / modules / data / saveDialog.js
1 define([
2 'libs/text!./saveDialog.html',
3 'libs/underscore',
4 'libs/backbone'
5 ], function(saveDialogTemplate, _, Backbone) {
6
7     'use strict';
8
9     var DialogView = Backbone.View.extend({
10         template: _.template(saveDialogTemplate),
11         events: {
12             'click .save-btn': 'onSave',
13             'click .cancel-btn': 'close',
14             'click .close': 'close'
15         },
16         initialize: function() {
17             _.bindAll(this);
18             this.actionsDisabled = false;
19         },
20         show: function() {
21             this.setElement(this.template());
22             this.$el.modal({backdrop: 'static'});
23             this.$el.modal('show');
24             this.$('textarea').focus();
25
26         },
27         onSave: function(e) {
28             e.preventDefault();
29             var view = this;
30             this.trigger('save', {
31                 data: {description: view.$el.find('textarea').val()},
32                 success: function() { view.actionsDisabled = false; view.close(); },
33                 error: function() { view.actionsDisabled = false; view.close(); },
34             });
35         },
36         close: function(e) {
37             if(e) {
38                 e.preventDefault();
39             }
40             if(!this.actionsDisabled) {
41                 this.$el.modal('hide');
42                 this.$el.remove();
43             }
44         },
45         toggleButtons: function(toggle) {
46             this.$('.btn, button').toggleClass('disabled', !toggle);
47             this.$('textarea').attr('disabled', !toggle);
48             this.actionsDisabled = !toggle;
49         }
50     });
51
52     return {
53         create: function() {
54             return new DialogView();
55         }
56     };
57
58 });