/**
* emojiarea - A rich textarea control that supports emojis, WYSIWYG-style.
* Copyright (c) 2012 DIY Co
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/**
* This file also contains some modifications by Igor Zhukov in order to add
* custom scrollbars to EmojiMenu See keyword `MODIFICATION` in source code.
*/
(function($, window, document) {
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
var TAGS_BLOCK = [ 'p', 'div', 'pre', 'form' ];
var KEY_ESC = 27;
var KEY_TAB = 9;
/* Keys that are not intercepted and canceled when the textbox has reached its max length:
Backspace, Tab, Ctrl, Alt, Left Arrow, Up Arrow, Right Arrow, Down Arrow, Cmd Key, Delete
*/
var MAX_LENGTH_ALLOWED_KEYS = [8, 9, 17, 18, 37, 38, 39, 40, 91, 46];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*
* ! MODIFICATION START Options 'spritesheetPath', 'spritesheetDimens',
* 'iconSize' added by Andre Staltz.
*/
$.emojiarea = {
assetsPath : '',
spriteSheetPath: '',
blankGifPath: '',
iconSize : 25,
icons : {},
};
var defaultRecentEmojis = ':joy:,:kissing_heart:,:heart:,:heart_eyes:,:blush:,:grin:,:+1:,:relaxed:,:pensive:,:smile:,:sob:,:kiss:,:unamused:,:flushed:,:stuck_out_tongue_winking_eye:,:see_no_evil:,:wink:,:smiley:,:cry:,:stuck_out_tongue_closed_eyes:,:scream:,:rage:,:smirk:,:disappointed:,:sweat_smile:,:kissing_closed_eyes:,:speak_no_evil:,:relieved:,:grinning:,:yum:,:laughing:,:ok_hand:,:neutral_face:,:confused:'
.split(',');
/* ! MODIFICATION END */
$.fn.emojiarea = function(options) {
options = $.extend({}, options);
return this
.each(function () {
var originalInput = $(this);
if ('contentEditable' in document.body
&& options.wysiwyg !== false) {
var id = getGuid();
new EmojiArea_WYSIWYG(originalInput, id, $.extend({}, options));
} else {
var id = getGuid();
new EmojiArea_Plain(originalInput, id, options);
}
originalInput.attr(
{
'data-emojiable': 'converted',
'data-id': id,
'data-type': 'original-input'
});
});
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
var util = {};
util.restoreSelection = (function() {
if (window.getSelection) {
return function(savedSelection) {
var sel = window.getSelection();
sel.removeAllRanges();
for (var i = 0, len = savedSelection.length; i < len; ++i) {
sel.addRange(savedSelection[i]);
}
};
} else if (document.selection && document.selection.createRange) {
return function(savedSelection) {
if (savedSelection) {
savedSelection.select();
}
};
}
})();
util.saveSelection = (function() {
if (window.getSelection) {
return function() {
var sel = window.getSelection(), ranges = [];
if (sel.rangeCount) {
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
ranges.push(sel.getRangeAt(i));
}
}
return ranges;
};
} else if (document.selection && document.selection.createRange) {
return function() {
var sel = document.selection;
return (sel.type.toLowerCase() !== 'none') ? sel.createRange()
: null;
};
}
})();
util.replaceSelection = (function() {
if (window.getSelection) {
return function(content) {
var range, sel = window.getSelection();
var node = typeof content === 'string' ? document
.createTextNode(content) : content;
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
//range.insertNode(document.createTextNode(''));
range.insertNode(node);
range.setStart(node, 0);
window.setTimeout(function() {
range = document.createRange();
range.setStartAfter(node);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}, 0);
}
}
} else if (document.selection && document.selection.createRange) {
return function(content) {
var range = document.selection.createRange();
if (typeof content === 'string') {
range.text = content;
} else {
range.pasteHTML(content.outerHTML);
}
}
}
})();
util.insertAtCursor = function(text, el) {
text = ' ' + text;
var val = el.value, endIndex, startIndex, range;
if (typeof el.selectionStart != 'undefined'
&& typeof el.selectionEnd != 'undefined') {
startIndex = el.selectionStart;
endIndex = el.selectionEnd;
el.value = val.substring(0, startIndex) + text
+ val.substring(el.selectionEnd);
el.selectionStart = el.selectionEnd = startIndex + text.length;
} else if (typeof document.selection != 'undefined'
&& typeof document.selection.createRange != 'undefined') {
el.focus();
range = document.selection.createRange();
range.text = text;
range.select();
}
};
util.extend = function(a, b) {
if (typeof a === 'undefined' || !a) {
a = {};
}
if (typeof b === 'object') {
for ( var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
}
return a;
};
util.escapeRegex = function(str) {
return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
};
util.htmlEntities = function(str) {
return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
};
/*
* ! MODIFICATION START This function was added by Igor Zhukov to save
* recent used emojis.
*/
util.emojiInserted = function(emojiKey, menu) {
ConfigStorage.get('emojis_recent', function(curEmojis) {
curEmojis = curEmojis || defaultRecentEmojis || [];
var pos = curEmojis.indexOf(emojiKey);
if (!pos) {
return false;
}
if (pos != -1) {
curEmojis.splice(pos, 1);
}
curEmojis.unshift(emojiKey);
if (curEmojis.length > 42) {
curEmojis = curEmojis.slice(42);
}
ConfigStorage.set({
emojis_recent : curEmojis
});
})
};
/* ! MODIFICATION END */
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
var EmojiArea = function() {
};
EmojiArea.prototype.setup = function() {
var self = this;
this.$editor.on('focus', function() {
self.hasFocus = true;
});
this.$editor.on('blur', function() {
self.hasFocus = false;
});
// Assign a unique instance of an emojiMenu to
self.emojiMenu = new EmojiMenu(self);
this.setupButton();
};
EmojiArea.prototype.setupButton = function() {
var self = this;
var $button = $('[data-id=' + this.id + '][data-type=picker]');
$button.on('click', function(e) {
self.emojiMenu.show(self);
});
this.$button = $button;
this.$dontHideOnClick = 'emoji-picker';
};
/*
* ! MODIFICATION START This function was modified by Andre Staltz so that
* the icon is created from a spritesheet.
*/
EmojiArea.createIcon = function(emoji, menu) {
var category = emoji[0];
var row = emoji[1];
var column = emoji[2];
var name = emoji[3];
var filename = $.emojiarea.spriteSheetPath ? $.emojiarea.spriteSheetPath : $.emojiarea.assetsPath + '/emoji_spritesheet_!.png';
var blankGifPath = $.emojiarea.blankGifPath ? $.emojiarea.blankGifPath : $.emojiarea.assetsPath + '/blank.gif';
var iconSize = menu && Config.Mobile ? 26 : $.emojiarea.iconSize
var xoffset = -(iconSize * column);
var yoffset = -(iconSize * row);
var scaledWidth = (Config.EmojiCategorySpritesheetDimens[category][1] * iconSize);
var scaledHeight = (Config.EmojiCategorySpritesheetDimens[category][0] * iconSize);
var style = 'display:inline-block;';
style += 'width:' + iconSize + 'px;';
style += 'height:' + iconSize + 'px;';
style += 'background:url(\'' + filename.replace('!', category) + '\') '
+ xoffset + 'px ' + yoffset + 'px no-repeat;';
style += 'background-size:' + scaledWidth + 'px ' + scaledHeight
+ 'px;';
return '';
};
$.emojiarea.createIcon = EmojiArea.createIcon;
/* ! MODIFICATION END */
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Editor (plain-text)
*
* @constructor
* @param {object}
* $textarea
* @param {object}
* options
*/
var EmojiArea_Plain = function($textarea, id, options) {
this.options = options;
this.$textarea = $textarea;
this.$editor = $textarea;
this.id = id;
this.setup();
};
EmojiArea_Plain.prototype.insert = function(emoji) {
if (!$.emojiarea.icons.hasOwnProperty(emoji))
return;
util.insertAtCursor(emoji, this.$textarea[0]);
/*
* MODIFICATION: Following line was added by Igor Zhukov, in order to
* save recent emojis
*/
util.emojiInserted(emoji, this.menu);
this.$textarea.trigger('change');
};
EmojiArea_Plain.prototype.val = function() {
if (this.$textarea == '\n')
return '';
return this.$textarea.val();
};
util.extend(EmojiArea_Plain.prototype, EmojiArea.prototype);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Editor (rich)
*
* @constructor
* @param {object}
* $textarea
* @param {object}
* options
*/
var EmojiArea_WYSIWYG = function($textarea, id, options) {
var self = this;
this.options = options || {};
if ($($textarea).attr('data-emoji-input') === 'unicode')
this.options.inputMethod = 'unicode';
else
this.options.inputMethod = 'image';
this.id = id;
this.$textarea = $textarea;
this.emojiPopup = options.emojiPopup;
this.$editor = $('
| ' + ' | ' + ' | ' + ' | ' + ' | ' + ' | ' + ' |