diff options
Diffstat (limited to 'addons/website/static/lib/summernote/src/js/module/Button.js')
| -rw-r--r-- | addons/website/static/lib/summernote/src/js/module/Button.js | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/addons/website/static/lib/summernote/src/js/module/Button.js b/addons/website/static/lib/summernote/src/js/module/Button.js new file mode 100644 index 00000000000..805ebbce75e --- /dev/null +++ b/addons/website/static/lib/summernote/src/js/module/Button.js @@ -0,0 +1,117 @@ +define([ + 'summernote/core/list' +], function (list) { + /** + * Button + */ + var Button = function () { + /** + * update button status + * + * @param {jQuery} $container + * @param {Object} styleInfo + */ + this.update = function ($container, styleInfo) { + /** + * handle dropdown's check mark (for fontname, fontsize, lineHeight). + * @param {jQuery} $btn + * @param {Number} value + */ + var checkDropdownMenu = function ($btn, value) { + $btn.find('.dropdown-menu li a').each(function () { + // always compare string to avoid creating another func. + var isChecked = ($(this).data('value') + '') === (value + ''); + this.className = isChecked ? 'checked' : ''; + }); + }; + + /** + * update button state(active or not). + * + * @param {String} selector + * @param {Function} pred + */ + var btnState = function (selector, pred) { + var $btn = $container.find(selector); + $btn.toggleClass('active', pred()); + }; + + // fontname + var $fontname = $container.find('.note-fontname'); + if ($fontname.length) { + var selectedFont = styleInfo['font-family']; + if (!!selectedFont) { + selectedFont = list.head(selectedFont.split(',')); + selectedFont = selectedFont.replace(/\'/g, ''); + $fontname.find('.note-current-fontname').text(selectedFont); + checkDropdownMenu($fontname, selectedFont); + } + } + + // fontsize + var $fontsize = $container.find('.note-fontsize'); + $fontsize.find('.note-current-fontsize').text(styleInfo['font-size']); + checkDropdownMenu($fontsize, parseFloat(styleInfo['font-size'])); + + // lineheight + var $lineHeight = $container.find('.note-height'); + checkDropdownMenu($lineHeight, parseFloat(styleInfo['line-height'])); + + btnState('button[data-event="bold"]', function () { + return styleInfo['font-bold'] === 'bold'; + }); + btnState('button[data-event="italic"]', function () { + return styleInfo['font-italic'] === 'italic'; + }); + btnState('button[data-event="underline"]', function () { + return styleInfo['font-underline'] === 'underline'; + }); + btnState('button[data-event="strikethrough"]', function () { + return styleInfo['font-strikethrough'] === 'strikethrough'; + }); + btnState('button[data-event="superscript"]', function () { + return styleInfo['font-superscript'] === 'superscript'; + }); + btnState('button[data-event="subscript"]', function () { + return styleInfo['font-subscript'] === 'subscript'; + }); + btnState('button[data-event="justifyLeft"]', function () { + return styleInfo['text-align'] === 'left' || styleInfo['text-align'] === 'start'; + }); + btnState('button[data-event="justifyCenter"]', function () { + return styleInfo['text-align'] === 'center'; + }); + btnState('button[data-event="justifyRight"]', function () { + return styleInfo['text-align'] === 'right'; + }); + btnState('button[data-event="justifyFull"]', function () { + return styleInfo['text-align'] === 'justify'; + }); + btnState('button[data-event="insertUnorderedList"]', function () { + return styleInfo['list-style'] === 'unordered'; + }); + btnState('button[data-event="insertOrderedList"]', function () { + return styleInfo['list-style'] === 'ordered'; + }); + }; + + /** + * update recent color + * + * @param {Node} button + * @param {String} eventName + * @param {value} value + */ + this.updateRecentColor = function (button, eventName, value) { + var $color = $(button).closest('.note-color'); + var $recentColor = $color.find('.note-recent-color'); + var colorInfo = JSON.parse($recentColor.attr('data-value')); + colorInfo[eventName] = value; + $recentColor.attr('data-value', JSON.stringify(colorInfo)); + var sKey = eventName === 'backColor' ? 'background-color' : 'color'; + $recentColor.find('i').css(sKey, value); + }; + }; + + return Button; +}); |
