[TASK] Drop faulty getIcons() method from "Icons" module
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / Resources / Public / JavaScript / Toolbar / OpendocsMenu.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * main JS part taking care of
16 * - navigating to the documents
17 * - updating the menu
18 */
19 define(['jquery', 'TYPO3/CMS/Backend/Icons'], function($, Icons) {
20 'use strict';
21
22 var OpendocsMenu = {
23 options: {
24 containerSelector: '#typo3-cms-opendocs-backend-toolbaritems-opendocstoolbaritem',
25 hashDataAttributeName: 'opendocsidentifier',
26 closeSelector: '.dropdown-list-link-close',
27 menuContainerSelector: '.dropdown-menu',
28 menuItemSelector: '.dropdown-menu li a',
29 toolbarIconSelector: '.dropdown-toggle span.icon',
30 openDocumentsItemsSelector: 'li.opendoc',
31 counterSelector: '#tx-opendocs-counter'
32 }
33 };
34
35 /**
36 * register event handlers
37 */
38 OpendocsMenu.initializeEvents = function() {
39 // send a request when removing an opendoc
40 $(OpendocsMenu.options.containerSelector).on('click', OpendocsMenu.options.closeSelector, function(evt) {
41 evt.preventDefault();
42 var md5 = $(this).data(OpendocsMenu.options.hashDataAttributeName);
43 if (md5) {
44 OpendocsMenu.closeDocument(md5);
45 }
46 });
47 };
48
49 /**
50 * Displays the menu and does the AJAX call to the TYPO3 backend
51 */
52 OpendocsMenu.updateMenu = function() {
53 var $toolbarItemIcon = $(OpendocsMenu.options.toolbarIconSelector, OpendocsMenu.options.containerSelector),
54 $existingIcon = $toolbarItemIcon.clone();
55
56 Icons.getIcon('spinner-circle-light', Icons.sizes.small).done(function(spinner) {
57 $toolbarItemIcon.replaceWith(spinner);
58 });
59
60 $.ajax({
61 url: TYPO3.settings.ajaxUrls['opendocs_menu'],
62 type: 'post',
63 cache: false,
64 success: function(data) {
65 $(OpendocsMenu.options.containerSelector).find(OpendocsMenu.options.menuContainerSelector).html(data);
66 OpendocsMenu.updateNumberOfDocs();
67 $(OpendocsMenu.options.toolbarIconSelector, OpendocsMenu.options.containerSelector).replaceWith($existingIcon);
68 }
69 });
70 };
71
72 /**
73 * Updates the number of open documents in the toolbar according to the
74 * number of items in the menu bar.
75 */
76 OpendocsMenu.updateNumberOfDocs = function() {
77 var num = $(OpendocsMenu.options.containerSelector).find(OpendocsMenu.options.openDocumentsItemsSelector).length;
78 $(OpendocsMenu.options.counterSelector).text(num).toggle(num > 0);
79 };
80
81 /**
82 * Closes an open document
83 *
84 * @param {string} md5sum
85 */
86 OpendocsMenu.closeDocument = function(md5sum) {
87 $.ajax({
88 url: TYPO3.settings.ajaxUrls['opendocs_closedoc'],
89 type: 'post',
90 cache: false,
91 data: {
92 md5sum: md5sum
93 },
94 success: function(data) {
95 $(OpendocsMenu.options.menuContainerSelector, OpendocsMenu.options.containerSelector).html(data);
96 OpendocsMenu.updateNumberOfDocs();
97 // Re-open the menu after closing a document
98 $(OpendocsMenu.options.containerSelector).toggleClass('open');
99 }
100 });
101 };
102
103 /**
104 * closes the menu (e.g. when clicked on an item)
105 */
106 OpendocsMenu.toggleMenu = function() {
107 $(OpendocsMenu.options.containerSelector).toggleClass('open');
108 };
109
110 $(function() {
111 OpendocsMenu.initializeEvents();
112 OpendocsMenu.updateMenu();
113 });
114
115 // expose to global
116 TYPO3.OpendocsMenu = OpendocsMenu;
117
118 return OpendocsMenu;
119 });