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