edf32e0cddd15ab69b4626b039c494819a2a8f1f
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / ContextHelp.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 * Module: TYPO3/CMS/Backend/ContextHelp
16 * API for context help.
17 */
18 define(['jquery', 'TYPO3/CMS/Backend/Popover', 'bootstrap'], function($, Popover) {
19
20 /**
21 * The main ContextHelp object
22 *
23 * @type {{ajaxUrl: *, localCache: {}, helpModuleUrl: string, trigger: string, placement: string, selector: string}}
24 * @exports TYPO3/CMS/Backend/ContextHelp
25 */
26 var ContextHelp = {
27 ajaxUrl: TYPO3.settings.ajaxUrls['context_help'],
28 localCache: {},
29 helpModuleUrl: '',
30 trigger: 'click',
31 placement: 'auto',
32 selector: '.t3-help-link'
33 };
34
35 /**
36 * Initialize context help trigger
37 */
38 ContextHelp.initialize = function() {
39 var backendWindow = ContextHelp.resolveBackend();
40 ContextHelp.helpModuleUrl = null;
41 if (typeof backendWindow.TYPO3.settings.ContextHelp !== 'undefined') {
42 ContextHelp.helpModuleUrl = backendWindow.TYPO3.settings.ContextHelp.moduleUrl;
43 }
44
45 if (TYPO3.ShortcutMenu === undefined && backendWindow.TYPO3.ShortcutMenu === undefined) {
46 // @FIXME: if we are in the popup... remove the bookmark / shortcut button
47 // @TODO: make it possible to use the bookmark button also in popup mode
48 $('.icon-actions-system-shortcut-new').closest('.btn').hide();
49 }
50 var title = ' ';
51 if (typeof backendWindow.TYPO3.LLL !== 'undefined') {
52 title = backendWindow.TYPO3.LLL.core.csh_tooltip_loading;
53 }
54 var $element = $(this.selector);
55 $element
56 .attr('data-loaded', 'false')
57 .attr('data-html', true)
58 .attr('data-original-title', title)
59 .attr('data-placement', this.placement)
60 .attr('data-trigger', this.trigger);
61 Popover.popover($element);
62
63 $(document).on('show.bs.popover', ContextHelp.selector, function(evt) {
64 var $me = $(this),
65 description = $me.data('description');
66 if (typeof description !== 'undefined' && description !== '') {
67 Popover.setOptions($me, {
68 title: $me.data('title'),
69 content: description
70 });
71 } else if ($me.attr('data-loaded') === 'false' && $me.data('table')) {
72 ContextHelp.loadHelp($me);
73 }
74
75 // if help icon is in DocHeader, force open to bottom
76 if ($me.closest('.t3js-module-docheader').length) {
77 Popover.setOption($me, 'placement', 'bottom');
78 }
79 });
80 $(document).on('shown.bs.popover', ContextHelp.selector, function(evt) {
81 var $popover = $(evt.target).data('bs.popover').$tip;
82 if (!$popover.find('.popover-title').is(':visible')) {
83 $popover.addClass('no-title');
84 }
85 });
86 $(document).on('click', '.tipIsLinked', function(e) {
87 $('.popover').each(function() {
88 var $popover = $(this);
89 if ($popover.has(e.target).length) {
90 ContextHelp.showHelpPopup($popover.data('bs.popover').$element);
91 }
92 });
93 });
94 $(document).on('click', 'body', function (e) {
95 $(ContextHelp.selector).each(function () {
96 var $triggerElement = $(this);
97 // the 'is' for buttons that trigger popups
98 // the 'has' for icons within a button that triggers a popup
99 if (!$triggerElement.is(e.target) && $triggerElement.has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
100 Popover.hide($triggerElement);
101 }
102 });
103 });
104 };
105
106 /**
107 * Open the help popup
108 *
109 * @param {Object} $trigger
110 */
111 ContextHelp.showHelpPopup = function($trigger) {
112 try {
113 var cshWindow = window.open(
114 ContextHelp.helpModuleUrl +
115 '&tx_documentation_help_documentationcshmanual[table]=' + $trigger.data('table') +
116 '&tx_documentation_help_documentationcshmanual[field]=' + $trigger.data('field'),
117 'ContextHelpWindow',
118 'height=400,width=600,status=0,menubar=0,scrollbars=1'
119 );
120 cshWindow.focus();
121 Popover.hide($trigger);
122 return cshWindow;
123 } catch(e) {
124 // do nothing
125 }
126 };
127
128 /**
129 * Load help data
130 *
131 * @param {Object} $trigger
132 */
133 ContextHelp.loadHelp = function($trigger) {
134 var table = $trigger.data('table');
135 var field = $trigger.data('field');
136 // If a table is defined, use ajax call to get the tooltip's content
137 if (table) {
138 // Load content
139 $.getJSON(ContextHelp.ajaxUrl, {
140 params: {
141 action: 'getContextHelp',
142 table: table,
143 field: field
144 }
145 }).done(function(data) {
146 var title = data.title || '';
147 var content = data.content || '<p></p>';
148 Popover.setOptions($trigger, {
149 title: title,
150 content: content
151 });
152 $trigger
153 .attr('data-loaded', 'true')
154 .one('hidden.bs.popover', function() {
155 Popover.show($trigger);
156 });
157 Popover.hide($trigger);
158 });
159 }
160 };
161
162 /**
163 * @return {Window}
164 */
165 ContextHelp.resolveBackend = function () {
166 var windowReference;
167 if (typeof window.opener !== 'undefined' && window.opener !== null) {
168 windowReference = window.opener.top;
169 } else {
170 windowReference = top;
171 }
172 return windowReference;
173 };
174
175 ContextHelp.initialize();
176 TYPO3.ContextHelp = ContextHelp;
177 return ContextHelp;
178 });