(function ($) { 'use strict'; window.RedactorPlugins = window.RedactorPlugins || {} var Figure = function (redactor) { this.redactor = redactor this.toolbar = {} this.init() } Figure.prototype = { control: { up : { classSuffix: 'arrow-up' }, down : { classSuffix: 'arrow-down' }, '|' : { classSuffix: 'divider' }, remove: { classSuffix: 'delete' } }, controlGroup: ['up', 'down', 'remove'], init: function () { this.observeToolbars() this.observeKeyboard() }, showToolbar: function (event) { var $figure = $(event.currentTarget), type = $figure.data('type') || 'default', $toolbar = this.getToolbar(type).data('figure', $figure).prependTo($figure).show() if (this.redactor[type] && this.redactor[type].onShow) { this.redactor[type].onShow($figure, $toolbar) } }, hideToolbar: function (event) { $(event.currentTarget).find('.oc-figure-controls').appendTo(this.redactor.$box).hide() }, observeToolbars: function () { /* * Before clicking a command, make sure we save the current node within the editor */ this.redactor.$editor.on('mousedown.figure', '.oc-figure-controls', $.proxy(function (event) { event.preventDefault() this.current = this.redactor.selection.getCurrent() }, this)) this.redactor.$editor.on('click.figure', '.oc-figure-controls span, .oc-figure-controls a', $.proxy(function (event) { event.stopPropagation() var $target = $(event.currentTarget), command = $target.data('command'), $figure = $target.closest('figure'), plugin = this.redactor[$figure.data('type')] this.command(command, $figure, plugin) }, this)) this.redactor.$editor.on('keydown.figure', function () { $(this).find('figure').triggerHandler('mouseleave') }) /* * Mobile */ if (this.redactor.utils.isMobile()) { /* * If $editor is focused, click doesn't seem to fire */ this.redactor.$editor.on('touchstart.figure', 'figure', function (event) { if (event.target.nodeName !== 'FIGCAPTION' && $(event.target).parents('.oc-figure-controls').length) { $(this).trigger('click', event) } }) this.redactor.$editor.on('click.figure', 'figure', $.proxy(function (event) { if (event.target.nodeName !== 'FIGCAPTION') { this.redactor.$editor.trigger('blur') } this.showToolbar(event) }, this)) } /* * Desktop */ else { /* * Move toolbar into figure on mouseenter */ this.redactor.$editor.on('mouseenter.figure', 'figure', $.proxy(this.showToolbar, this)) /* * Remove toolbar from figure on mouseleave */ this.redactor.$editor.on('mouseleave.figure', 'figure', $.proxy(this.hideToolbar, this)) } }, getToolbar: function (type) { if (this.toolbar[type]) return this.toolbar[type] var controlGroup = (this.redactor[type] && this.redactor[type].controlGroup) || this.controlGroup, controls = $.extend({}, this.control, (this.redactor[type] && this.redactor[type].control) || {}), $controls = this.buildControls(controlGroup, controls), $toolbar = $('