| 1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/*
|
|
|
17 |
* @package atto_emojipicker
|
|
|
18 |
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
|
|
|
19 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* @module moodle-atto_emojipicker-button
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
var COMPONENTNAME = 'atto_emojipicker',
|
|
|
27 |
EMOJI_PICKER_SEARCH_INPUT = '[data-region="search-input"]';
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Atto text editor emoji picker plugin.
|
|
|
31 |
*
|
|
|
32 |
* @namespace M.atto_emojipicker
|
|
|
33 |
* @class button
|
|
|
34 |
* @extends M.editor_atto.EditorPlugin
|
|
|
35 |
*/
|
|
|
36 |
|
|
|
37 |
Y.namespace('M.atto_emojipicker').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* A reference to the current selection at the time that the dialogue
|
|
|
41 |
* was opened.
|
|
|
42 |
*
|
|
|
43 |
* @property _currentSelection
|
|
|
44 |
* @type Range
|
|
|
45 |
* @private
|
|
|
46 |
*/
|
|
|
47 |
_currentSelection: null,
|
|
|
48 |
|
|
|
49 |
initializer: function() {
|
|
|
50 |
if (this.get('disabled')) {
|
|
|
51 |
return;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
this.addButton({
|
|
|
55 |
icon: 'e/emoticons',
|
|
|
56 |
callback: this._displayDialogue
|
|
|
57 |
});
|
|
|
58 |
},
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Display the emoji picker.
|
|
|
62 |
*
|
|
|
63 |
* @method _displayDialogue
|
|
|
64 |
* @private
|
|
|
65 |
*/
|
|
|
66 |
_displayDialogue: function() {
|
|
|
67 |
// Store the current selection.
|
|
|
68 |
this._currentSelection = this.get('host').getSelection();
|
|
|
69 |
if (this._currentSelection === false) {
|
|
|
70 |
return;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
var dialogue = this.getDialogue({
|
|
|
74 |
headerContent: M.util.get_string('emojipicker', COMPONENTNAME),
|
|
|
75 |
width: 'auto',
|
|
|
76 |
focusAfterHide: true,
|
|
|
77 |
additionalBaseClass: 'emoji-picker-dialogue'
|
|
|
78 |
}, true);
|
|
|
79 |
|
|
|
80 |
// Set the dialogue content, and then show the dialogue.
|
|
|
81 |
dialogue.set('bodyContent', this._getDialogueContent())
|
|
|
82 |
.show();
|
|
|
83 |
},
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Insert the emoticon.
|
|
|
87 |
*
|
|
|
88 |
* @method _insertEmote
|
|
|
89 |
* @param {String} emoji
|
|
|
90 |
* @private
|
|
|
91 |
*/
|
|
|
92 |
_insertEmoji: function(emoji) {
|
|
|
93 |
var host = this.get('host');
|
|
|
94 |
|
|
|
95 |
// Hide the dialogue.
|
|
|
96 |
this.getDialogue({
|
|
|
97 |
focusAfterHide: null
|
|
|
98 |
}).hide();
|
|
|
99 |
|
|
|
100 |
// Focus on the previous selection.
|
|
|
101 |
host.setSelection(this._currentSelection);
|
|
|
102 |
|
|
|
103 |
// And add the character.
|
|
|
104 |
host.insertContentAtFocusPoint(emoji);
|
|
|
105 |
|
|
|
106 |
this.markUpdated();
|
|
|
107 |
},
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Generates the content of the dialogue, attaching event listeners to
|
|
|
111 |
* the content.
|
|
|
112 |
*
|
|
|
113 |
* @method _getDialogueContent
|
|
|
114 |
* @return {Node} Node containing the dialogue content
|
|
|
115 |
* @private
|
|
|
116 |
*/
|
|
|
117 |
_getDialogueContent: function() {
|
|
|
118 |
var wrapper = Y.Node.create('<div></div>');
|
|
|
119 |
|
|
|
120 |
require(['core/templates', 'core/emoji/picker'], function(Templates, initialiseEmojiPicker) {
|
|
|
121 |
Templates.render('core/emoji/picker', {}).then(function(html) {
|
|
|
122 |
var domNode = wrapper.getDOMNode();
|
|
|
123 |
domNode.innerHTML = html;
|
|
|
124 |
initialiseEmojiPicker(domNode, this._insertEmoji.bind(this));
|
|
|
125 |
this.getDialogue().centerDialogue();
|
|
|
126 |
domNode.querySelector(EMOJI_PICKER_SEARCH_INPUT).focus();
|
|
|
127 |
}.bind(this));
|
|
|
128 |
}.bind(this));
|
|
|
129 |
|
|
|
130 |
return wrapper;
|
|
|
131 |
}
|
|
|
132 |
}, {
|
|
|
133 |
ATTRS: {
|
|
|
134 |
disabled: {
|
|
|
135 |
value: true
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
});
|