[TASK] Drop faulty getIcons() method from "Icons" module
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Icons.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 DocumentHeader source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Uses the icon API of the core to fetch icons via AJAX.
16 */
17 define(['jquery'], function($) {
18 'use strict';
19
20 var Icons = {
21 cache: {},
22 sizes: {
23 small: 'small',
24 default: 'default',
25 large: 'large',
26 overlay: 'overlay'
27 },
28 states: {
29 default: 'default',
30 disabled: 'disabled'
31 }
32 };
33
34 /**
35 * Get the icon by its identifier.
36 *
37 * @param {string} identifier
38 * @param {string} size
39 * @param {string} overlayIdentifier
40 * @param {string} state
41 * @return {Promise<Array>}
42 */
43 Icons.getIcon = function(identifier, size, overlayIdentifier, state) {
44 return $.when(Icons.fetch(identifier, size, overlayIdentifier, state));
45 };
46
47 /**
48 * Performs the AJAX request to fetch the icon.
49 *
50 * @param {string} identifier
51 * @param {string} size
52 * @param {string} overlayIdentifier
53 * @param {string} state
54 * @return {String|Promise}
55 * @private
56 */
57 Icons.fetch = function(identifier, size, overlayIdentifier, state) {
58 /**
59 * Icon keys:
60 *
61 * 0: identifier
62 * 1: size
63 * 2: overlayIdentifier
64 * 3: state
65 */
66 size = size || Icons.sizes.default;
67 state = state || Icons.states.default;
68
69 var icon = [identifier, size, overlayIdentifier, state],
70 cacheIdentifier = icon.join('_');
71
72 if (Icons.isCached(cacheIdentifier)) {
73 return Icons.getFromCache(cacheIdentifier);
74 }
75
76 return $.ajax({
77 url: TYPO3.settings.ajaxUrls['icons'],
78 dataType: 'html',
79 data: {
80 icon: JSON.stringify(icon)
81 },
82 success: function(markup) {
83 Icons.putInCache(cacheIdentifier, markup);
84 }
85 });
86 };
87
88 /**
89 * Check whether icon was fetched already
90 *
91 * @param {String} cacheIdentifier
92 * @returns {Boolean}
93 * @private
94 */
95 Icons.isCached = function(cacheIdentifier) {
96 return typeof Icons.cache[cacheIdentifier] !== 'undefined';
97 };
98
99 /**
100 * Get icon from cache
101 *
102 * @param {String} cacheIdentifier
103 * @returns {String}
104 * @private
105 */
106 Icons.getFromCache = function(cacheIdentifier) {
107 return Icons.cache[cacheIdentifier];
108 };
109
110 /**
111 * Put icon into cache
112 *
113 * @param {String} cacheIdentifier
114 * @param {Object} markup
115 * @private
116 */
117 Icons.putInCache = function(cacheIdentifier, markup) {
118 Icons.cache[cacheIdentifier] = markup;
119 };
120
121 return Icons;
122 });