Only show media clipboard if media manager is open
authorMatthias Schmidt <gravatronics@live.com>
Sat, 16 Jul 2016 12:32:51 +0000 (14:32 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 16 Jul 2016 12:32:58 +0000 (14:32 +0200)
Clipboard now allows stacking multiple page class names if
`Clipboard.setup()` is called multiple times.

wcfsetup/install/files/js/WoltLab/WCF/Controller/Clipboard.js
wcfsetup/install/files/js/WoltLab/WCF/Media/Manager/Base.js
wcfsetup/install/files/js/WoltLab/WCF/Ui/Dialog.js
wcfsetup/install/files/lib/data/clipboard/item/ClipboardItemAction.class.php
wcfsetup/install/files/lib/system/clipboard/ClipboardHandler.class.php

index c0e8cbde9d9adeae76df27471c3a18f9002c4e2d..3713d23c6a91fa931390d7336f38b8de959bacb9 100644 (file)
@@ -34,6 +34,8 @@ define(
        var _callbackItem = null;
        var _callbackUnmarkAll = null;
        
+       var _addPageOverlayActiveClass = false;
+       
        /**
         * Clipboard API
         * 
@@ -46,19 +48,31 @@ define(
                 * @param       {object<string, *>}     options         initialization options
                 */
                setup: function(options) {
-                       _callbackCheckbox = this._mark.bind(this);
-                       _callbackItem = this._executeAction.bind(this);
-                       _callbackUnmarkAll = this._unmarkAll.bind(this);
-                       _options = Core.extend({
-                               hasMarkedItems: false,
-                               pageClassName: '',
-                               pageObjectId: 0
-                       }, options);
-                       
-                       if (!_options.pageClassName) {
+                       if (!options.pageClassName) {
                                throw new Error("Expected a non-empty string for parameter 'pageClassName'.");
                        }
                        
+                       if (_callbackCheckbox === null) {
+                               _callbackCheckbox = this._mark.bind(this);
+                               _callbackItem = this._executeAction.bind(this);
+                               _callbackUnmarkAll = this._unmarkAll.bind(this);
+                               
+                               _options = Core.extend({
+                                       hasMarkedItems: false,
+                                       pageClassNames: [options.pageClassName],
+                                       pageObjectId: 0
+                               }, options);
+                               
+                               delete _options.pageClassName;
+                       }
+                       else {
+                               if (options.pageObjectId) {
+                                       throw new Error("Cannot load secondary clipboard with page object id set.");
+                               }
+                               
+                               _options.pageClassNames.push(options.pageClassName);
+                       }
+                       
                        this._initContainers();
                        
                        if (_options.hasMarkedItems && _elements.length) {
@@ -122,7 +136,7 @@ define(
                        Ajax.api(this, {
                                actionName: 'getMarkedItems',
                                parameters: {
-                                       pageClassName: _options.pageClassName,
+                                       pageClassNames: _options.pageClassNames,
                                        pageObjectID: _options.pageObjectId
                                }
                        });
@@ -216,7 +230,7 @@ define(
                        Ajax.api(this, {
                                actionName: (isMarked ? 'mark' : 'unmark'),
                                parameters: {
-                                       pageClassName: _options.pageClassName,
+                                       pageClassNames: _options.pageClassNames,
                                        pageObjectID: _options.pageObjectId,
                                        objectIDs: objectIds,
                                        objectType: type
@@ -550,6 +564,36 @@ define(
                                        parent.classList[(markAll ? 'add' : 'remove')]('jsMarked');
                                }
                        }
+               },
+               
+               /**
+                * Hides the clipboard editor for the given object type.
+                * 
+                * @param       {string}        objectType
+                */
+               hideEditor: function(objectType) {
+                       UiPageAction.remove('wcfClipboard-' + objectType);
+                       
+                       if (_addPageOverlayActiveClass) {
+                               _addPageOverlayActiveClass = false;
+                               
+                               document.documentElement.classList.add('pageOverlayActive');
+                       }
+               },
+               
+               /**
+                * Shows the clipboard editor for the given object type.
+                * 
+                * @param       {string}        objectType
+                */
+               showEditor: function(objectType) {
+                       this._loadMarkedItems();
+                       
+                       if (document.documentElement.classList.contains('pageOverlayActive')) {
+                               document.documentElement.classList.remove('pageOverlayActive');
+                               
+                               _addPageOverlayActiveClass = true;
+                       }
                }
        };
 });
index 77a3dc18732ca725b90ff6d4b41fd68e870f1764..d9d013dcb575b51cfb8f3cc07dba22a69042bcdf 100644 (file)
@@ -92,6 +92,14 @@ define(
                        }
                },
                
+               /**
+                * Is called if the media manager dialog is closed.
+                */
+               _dialogClose: function() {
+                       // only show media clipboard if editor is open
+                       Clipboard.hideEditor('com.woltlab.wcf.media');
+               },
+               
                /**
                 * Returns all data to setup the media manager dialog.
                 * 
@@ -101,6 +109,8 @@ define(
                        return {
                                id: 'mediaManager',
                                options: {
+                                       onClose: this._dialogClose.bind(this),
+                                       onShow: this._dialogShow.bind(this),
                                        title: this._options.dialogTitle
                                },
                                source: {
@@ -117,6 +127,14 @@ define(
                        };
                },
                
+               /**
+                * Is called if the media manager dialog is shown.
+                */
+               _dialogShow: function() {
+                       // only show media clipboard if editor is open
+                       Clipboard.showEditor('com.woltlab.wcf.media');
+               },
+               
                /**
                 * Opens the media editor for a media file.
                 * 
index f209cd3f80628c7552755f6a65d0845e2113b75a..e2e077592d6d3eaa998e65fc25d477d57d835a3c 100644 (file)
@@ -189,6 +189,8 @@ define(
                 * @return      {object<string, *>}             dialog data
                 */
                openStatic: function(id, html, options, createOnly) {
+                       document.documentElement.classList.add('pageOverlayActive');
+                       
                        if (_dialogs.has(id)) {
                                this._updateDialog(id, html);
                        }
@@ -220,8 +222,6 @@ define(
                                this._createDialog(id, html, options);
                        }
                        
-                       document.documentElement.classList.add('pageOverlayActive');
-                       
                        return _dialogs.get(id);
                },
                
index 42c5e322093d733e8610452de8b1fc787f952afb..565bcd83da3949e52f594f38103ce99e10959d0a 100644 (file)
@@ -116,7 +116,8 @@ class ClipboardItemAction extends AbstractDatabaseObjectAction {
         * Validates generic parameters used for most clipboard actions.
         */
        protected function validateDefaultParameters() {
-               $this->readString('pageClassName');
+               $this->readStringArray('pageClassNames');
+               
                $this->readInteger('pageObjectID', true);
        }
        
@@ -140,7 +141,7 @@ class ClipboardItemAction extends AbstractDatabaseObjectAction {
         * @return      mixed[]
         */
        protected function getEditorItems() {
-               $data = ClipboardHandler::getInstance()->getEditorItems($this->parameters['pageClassName'], $this->parameters['pageObjectID']);
+               $data = ClipboardHandler::getInstance()->getEditorItems($this->parameters['pageClassNames'], $this->parameters['pageObjectID']);
                
                if ($data === null) {
                        return [];
index 4d2f88a673348a0445171c69754e9f1d5536079c..dea634ab49eafa5ecf4c0ebed2d235c97628ccf1 100644 (file)
@@ -292,16 +292,18 @@ class ClipboardHandler extends SingletonFactory {
        /**
         * Returns items for clipboard editor.
         * 
-        * @param       string          $page
-        * @param       integer         $pageObjectID
+        * @param       string|string[]         $page
+        * @param       integer                 $pageObjectID
         * @return      mixed[][]
         * @throws      SystemException
         */
        public function getEditorItems($page, $pageObjectID) {
-               $this->pageObjectID = 0;
+               $pages = $page;
+               if (!is_array($pages)) {
+                       $pages = [$page];
+               }
                
-               // ignore unknown pages
-               if (!isset($this->pageCache[$page])) return null;
+               $this->pageObjectID = 0;
                
                // get objects
                $this->loadMarkedItems();
@@ -312,9 +314,11 @@ class ClipboardHandler extends SingletonFactory {
                // fetch action ids
                $this->loadActionCache();
                $actionIDs = [];
-               foreach ($this->pageCache[$page] as $actionID) {
-                       if (isset($this->actionCache[$actionID])) {
-                               $actionIDs[] = $actionID;
+               foreach ($pages as $page) {
+                       foreach ($this->pageCache[$page] as $actionID) {
+                               if (isset($this->actionCache[$actionID])) {
+                                       $actionIDs[] = $actionID;
+                               }
                        }
                }
                $actionIDs = array_unique($actionIDs);