Revert "Merge branch 'm28_MDL-46431' of https://github.com/totara/moodle"
[moodle.git] / lib / yui / src / notification / js / confirm.js
1 /**
2  * A dialogue type designed to display a confirmation to the user.
3  *
4  * @module moodle-core-notification
5  * @submodule moodle-core-notification-confirm
6  */
8 var CONFIRM_NAME = 'Moodle confirmation dialogue',
9     CONFIRM;
11 /**
12  * Extends core Dialogue to show the confirmation dialogue.
13  *
14  * @param {Object} config Object literal specifying the dialogue configuration properties.
15  * @constructor
16  * @class M.core.confirm
17  * @extends M.core.dialogue
18  */
19 CONFIRM = function(config) {
20     CONFIRM.superclass.constructor.apply(this, [config]);
21 };
22 Y.extend(CONFIRM, M.core.notification.info, {
23     /**
24      * The list of events to detach when destroying this dialogue.
25      *
26      * @property _closeEvents
27      * @type EventHandle[]
28      * @private
29      */
30     _closeEvents: null,
31     initializer: function() {
32         this._closeEvents = [];
33         this.publish('complete');
34         this.publish('complete-yes');
35         this.publish('complete-no');
36         var yes = Y.Node.create('<input type="button" id="id_yuiconfirmyes-' + this.get('COUNT') + '" value="'+this.get(CONFIRMYES)+'" />'),
37             no = Y.Node.create('<input type="button" id="id_yuiconfirmno-' + this.get('COUNT') + '" value="'+this.get(CONFIRMNO)+'" />'),
38             content = Y.Node.create('<div class="confirmation-dialogue"></div>')
39                         .append(Y.Node.create('<div class="confirmation-message">'+this.get(QUESTION)+'</div>'))
40                         .append(Y.Node.create('<div class="confirmation-buttons"></div>')
41                             .append(yes)
42                             .append(no));
43         this.get(BASE).addClass('moodle-dialogue-confirm');
44         this.setStdModContent(Y.WidgetStdMod.BODY, content, Y.WidgetStdMod.REPLACE);
45         this.setStdModContent(Y.WidgetStdMod.HEADER,
46                 '<h1 id="moodle-dialogue-'+this.get('COUNT')+'-header-text">' + this.get(TITLE) + '</h1>', Y.WidgetStdMod.REPLACE);
48         this._closeEvents.push(
49             Y.on('key', this.submit, window, 'down:27', this, false),
50             yes.on('click', this.submit, this, true),
51             no.on('click', this.submit, this, false)
52         );
54         var closeButton = this.get('boundingBox').one('.closebutton');
55         if (closeButton) {
56             // The close button should act exactly like the 'No' button.
57             this._closeEvents.push(
58                 closeButton.on('click', this.submit, this)
59             );
60         }
61     },
62     submit: function(e, outcome) {
63         new Y.EventHandle(this._closeEvents).detach();
64         this.fire('complete', outcome);
65         if (outcome) {
66             this.fire('complete-yes');
67         } else {
68             this.fire('complete-no');
69         }
70         this.hide();
71         this.destroy();
72     }
73 }, {
74     NAME: CONFIRM_NAME,
75     CSS_PREFIX: DIALOGUE_PREFIX,
76     ATTRS: {
78         /**
79          * The button text to use to accept the confirmation.
80          *
81          * @attribute yesLabel
82          * @type String
83          * @default 'Yes'
84          */
85         yesLabel: {
86             validator: Y.Lang.isString,
87             value: M.util.get_string('yes', 'moodle')
88         },
90         /**
91          * The button text to use to reject the confirmation.
92          *
93          * @attribute noLabel
94          * @type String
95          * @default 'No'
96          */
97         noLabel: {
98             validator: Y.Lang.isString,
99             value: M.util.get_string('no', 'moodle')
100         },
102         /**
103          * The title of the dialogue.
104          *
105          * @attribute title
106          * @type String
107          * @default 'Confirm'
108          */
109         title: {
110             validator: Y.Lang.isString,
111             value: M.util.get_string('confirm', 'moodle')
112         },
114         /**
115          * The question posed by the dialogue.
116          *
117          * @attribute question
118          * @type String
119          * @default 'Are you sure?'
120          */
121         question: {
122             validator: Y.Lang.isString,
123             value: M.util.get_string('areyousure', 'moodle')
124         }
125     }
126 });
127 Y.augment(CONFIRM, Y.EventTarget);
129 M.core.confirm = CONFIRM;