aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/popup/confirm.reel/confirm.js
blob: 8e1660c5c7dc8c6a6ad20d87138f0827d22debf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */

/**
    @module "montage/ui/popup/confirm.reel"
    @requires montage/core/core
    @requires montage/ui/component
    @requires "montage/ui/popup/popup.reel"
*/

var Montage = require("montage").Montage;
var Component = require("ui/component").Component;
var Popup = require("ui/popup/popup.reel").Popup;

/**
 @class module:"montage/ui/popup/confirm.reel".Confirm
 @extends module:montage/ui/component.Component
 */

var Confirm = exports.Confirm = Montage.create(Component, /** @lends module:"montage/ui/popup/confirm.reel".Confirm# */ {
    hasTemplate: {value: true},

    title: {
        value: 'Confirm'
    },
    /**
        Text of message to display on the confirm popup
        @type {Property}
        @default {String} 'Are you sure?'
    */
    msg: {
        value: 'Are you sure?'
    },
    
    /**
        Text to display on the OK button
        @type {Property}
        @default {String} 'OK'
    */
    okLabel: {
        value: 'OK'
    },
    
    /**
        Text to display on the Cancel button
        @type {Property}
        @default {String} 'Cancel'
    */
    cancelLabel: {
        value: 'Cancel'
    },
    
/**
  Description TODO
  @private
*/
    _popup: {
        value: null
    },
/**
        Description TODO
        @type {Function}
        @default null
    */
    popup: {
        set: function(value) {
            this._popup = value;
        },
        get: function() {
            return this._popup;
        }
    },

    okCallback: {value: null},
    cancelCallback: {value: null},

    prepareForDraw: {
        value: function() {
            this.element.addEventListener("keyup", this, false);
        }
    },
/**
    Description TODO
    @function
    */
    draw: {
        value: function() {
        }
    },
/**
    Description TODO
    @function
    @param {Event} evt The event keyCode.
    */
    handleKeyup: {
        value: function(evt) {
            if (evt.keyCode == 13 /*Enter*/) {
                this.handleOkAction(evt);
            } else if (evt.keyCode == 27 /*Escape*/) {
                this.handleCancelAction(evt);
            }
        }
    },
/**
    Description TODO
    @function
    @param {Event} evt The event keyCode.
    */
    handleOkAction: {
        value: function(evt) {
            if(this.okCallback) {
                this.okCallback.call(this, evt);
            }
            var anEvent = document.createEvent("CustomEvent");
            anEvent.initCustomEvent("montage_confirm_ok", true, true);
            this.dispatchEvent(anEvent);

            this.popup.hide();
        }
    },
 /**
    Description TODO
    @function
    @param {Event} evt The event keyCode.
    */
    handleCancelAction: {
        value: function(evt) {
            if(this.cancelCallback) {
                this.cancelCallback.call(this, evt);
            }
            var anEvent = document.createEvent("CustomEvent");
            anEvent.initCustomEvent("montage_confirm_cancel", true, true);
            this.dispatchEvent(anEvent);

            this.popup.hide();
        }
    },

    // Static method to show a Confirmation dialog
    /**
     Displays a confirm dialog with OK and Cancel buttons.
     @function
     @param {String} msg A message to display in the dialog.
     @param {Function} okCallback Function that's invoked when the user clicks OK
     @param {Function} cancelCallback Function that's invoked if the user clicks Cancel.
     @example
     ...
     */
    show: {
        value: function(options, okCallback, cancelCallback) {
            var popup = this.application._confirmPopup, confirm;
            if(!popup) {
                popup = Popup.create();
                this.popup = popup;

                popup.type = 'confirm';
                popup.title = 'Confirmation';
                popup.modal = true;
                this.application._confirmPopup = popup;

                confirm = Confirm.create();
                popup.content = confirm;
            }
            
            confirm = popup.content;
            
            if (typeof(options) === "string") {
                confirm.msg = options;
                confirm.okLabel = "OK";
                confirm.cancelLabel = "Cancel";
            } else {
                confirm.msg = options.message;
                confirm.okLabel = options.okLabel || "OK";
                confirm.cancelLabel = options.cancelLabel || "Cancel";
            }
            
            confirm.okCallback = okCallback || null;
            confirm.cancelCallback = cancelCallback || null;

            popup.show();
        }
    }
});