PageSourceSearch

https://sajbl.org.za/lib/pkp/js/controllers/linkAction/LinkActionHandler.js

js sajbl.org.za collected 2026-10-02 14:35:44 UTC 8,646 bytes, 305 lines download raw bytes

1/**
2 * @defgroup js_controllers_linkAction
3 */
4// Create the linkAction namespace.
5jQuery.pkp.controllers.linkAction = jQuery.pkp.controllers.linkAction || { };
6
7/**
8 * @file js/controllers/linkAction/LinkActionHandler.js
9 *
10 * Copyright (c) 2000-2013 John Willinsky
11 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
12 *
13 * @class LinkActionHandler
14 * @ingroup js_controllers_linkAction
15 *
16 * @brief Link action handler that executes the action's handler when activated
17 *  and delegates the action handler's response to the action's response
18 *  handler.
19 */
20(function($) {
21
22
23	/**
24	 * @constructor
25	 *
26	 * @extends $.pkp.classes.Handler
27	 *
28	 * @param {jQueryObject} $handledElement The clickable element
29	 *  the link action will be attached to.
30	 * @param {{actionRequest, actionRequestOptions, actionResponseOptions}} options
31	 *  Configuration of the link action handler. The object must contain the
32	 *  following elements:
33	 *  - actionRequest: The action to be executed when the link
34	 *                   action is being activated.
35	 *  - actionRequestOptions: Configuration of the action request.
36	 *  - actionResponse: The action's response listener.
37	 *  - actionResponseOptions: Options for the response listener.
38	 */
39	$.pkp.controllers.linkAction.LinkActionHandler =
40			function($handledElement, options) {
41		this.parent($handledElement, options);
42
43		// We need to know the static part of the element id
44		// (id attribute will change after refreshing,
45		// because it uses the uniqId function) for accessing
46		// the link action element in the DOM.
47		if (options.staticId) {
48			this.staticId_ = options.staticId;
49		} else {
50			// If none, the link action element id is
51			// not using the unique function, so we
52			// can consider it static.
53			this.staticId_ = /** @type {string} */ $handledElement.attr('id');
54		}
55
56		// Instantiate the link action request.
57		if (!options.actionRequest || !options.actionRequestOptions) {
58			throw new Error(['The "actionRequest" and "actionRequestOptions"',
59				'settings are required in a LinkActionHandler'].join(''));
60		}
61
62		// Bind the handler for image preview.
63		if ($handledElement.hasClass('image')) {
64			this.bind('mouseover', this.imagePreviewHandler_);
65		}
66
67		// Configure the callback called when the link
68		// action request finishes.
69		options.actionRequestOptions.finishCallback =
70				this.callbackWrapper(this.enableLink);
71
72		this.linkActionRequest_ =
73				/** @type {$.pkp.classes.linkAction.LinkActionRequest} */
74				($.pkp.classes.Helper.objectFactory(
75						options.actionRequest,
76						[$handledElement, options.actionRequestOptions]));
77
78		// Bind the link action request to the handled element.
79		this.bindActionRequest();
80
81		// Publish this event so we can handle it and grids still
82		// can listen to it to refresh themselves.
83		//
84		// This needs to happen before the dataChangedHandler_ bound,
85		// otherwise when the publish event handler try to bubble up the
86		// dataChanged event, this html element could be already removed
87		// by the notifyUser event handlers triggered by dataChangedHandler_
88		this.publishEvent('dataChanged');
89
90		// Bind the data changed event, so we know when trigger
91		// the notify user event.
92		this.bind('dataChanged', this.dataChangedHandler_);
93
94		// Bind the 'modalCanceled' event, so we can re-enable submit buttons
95		this.bind('modalCanceled', this.removeDisabledCssClass_);
96
97		if (options.selfActivate) {
98			this.trigger('click');
99		}
100	};
101	$.pkp.classes.Helper.inherits(
102			$.pkp.controllers.linkAction.LinkActionHandler,
103			$.pkp.classes.Handler);
104
105
106	//
107	// Private properties
108	//
109	/**
110	 * The link action request object.
111	 * @private
112	 * @type {$.pkp.classes.linkAction.LinkActionRequest}
113	 */
114	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
115			linkActionRequest_ = null;
116
117
118	/**
119	 * The part of this HTML element id that's static, not
120	 * changing after a refresh.
121	 * @private
122	 * @type {?string}
123	 */
124	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
125			staticId_ = null;
126
127
128	//
129	// Getter
130	//
131	/**
132	 * Get the static id part of the HTML element id.
133	 * @return {?string} Non-unique part of HTML element id.
134	 */
135	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
136			getStaticId = function() {
137		return this.staticId_;
138	};
139
140
141	/**
142	 * Get the link url.
143	 * @return {?string} Link url.
144	 */
145	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
146			getUrl = function() {
147		return this.linkActionRequest_.getUrl();
148	};
149
150
151	//
152	// Private methods
153	//
154	/**
155	 * Preview an image when hovering over its link in the grid.
156	 *
157	 * @private
158	 *
159	 * @param {HTMLElement} sourceElement The element that
160	 *  issued the event.
161	 * @param {Event} event The triggering event.
162	 */
163	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
164			imagePreviewHandler_ = function(sourceElement, event) {
165
166		// Use the jQuery imagepreview plug-in to show the image.
167		var $sourceElement = $(sourceElement);
168		$sourceElement.imgPreview({
169			preloadImages: false,
170			imgCSS: {
171				width: '300px'
172			}
173		});
174	};
175
176
177	//
178	// Public methods
179	//
180	/**
181	 * Activate the link action request.
182	 *
183	 * @param {HTMLElement} callingElement The element that triggered
184	 *  the link action activation event.
185	 * @param {Event} event The event that activated the link action.
186	 * @return {boolean} Should return false to stop event propagation.
187	 */
188	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
189			activateAction = function(callingElement, event) {
190
191		// Unbind our click handler to avoid double-execution
192		// while the link action is executing. We will not disable
193		// if this link action have a null action request. In that
194		// case, the action request is handled by some parent widget.
195		if (this.linkActionRequest_.getObjectName() !=
196				'$.pkp.classes.linkAction.NullAction') {
197			this.disableLink();
198		}
199
200		// Call the link request.
201		return this.linkActionRequest_.activate.call(this.linkActionRequest_,
202				callingElement, event);
203	};
204
205
206	/**
207	 * Bind the link action request.
208	 */
209	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
210			bindActionRequest = function() {
211
212		// (Re-)bind our click handler so that the action
213		// can be executed.
214		this.bind('click', this.activateAction);
215	};
216
217
218	/**
219	 * Enable link action.
220	 */
221	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
222			enableLink = function() {
223		var $linkActionElement, actionRequestUrl;
224
225		$linkActionElement = $(this.getHtmlElement());
226
227		// only remove the disabled state if it is not a submit button.
228		// we let FormHandler remove that after a form is submitted.
229		if (!this.getHtmlElement().is(':submit')) {
230			this.removeDisabledCssClass_();
231		}
232
233		actionRequestUrl = this.getUrl();
234		if (this.getHtmlElement().is('a') && actionRequestUrl) {
235			$linkActionElement.attr('href', actionRequestUrl);
236		}
237		this.unbind('click', this.noAction_);
238		this.bindActionRequest();
239	};
240
241
242	/**
243	 * Disable link action.
244	 */
245	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
246			disableLink = function() {
247		var $linkActionElement = $(this.getHtmlElement());
248		$linkActionElement.addClass('ui-state-disabled');
249		if (this.getHtmlElement().is('a')) {
250			$linkActionElement.attr('href', '#');
251		}
252		this.unbind('click', this.activateAction);
253		this.bind('click', this.noAction_);
254	};
255
256
257	//
258	// Private methods.
259	//
260	/**
261	 * Remove the 'disabled' CSS class for the linkActionElement.
262	 * @private
263	 */
264	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
265			removeDisabledCssClass_ = function() {
266
267		var $linkActionElement = $(this.getHtmlElement());
268		$linkActionElement.removeClass('ui-state-disabled');
269	};
270
271
272	/**
273	 * Handle the changed data event.
274	 * @private
275	 *
276	 * @param {jQueryObject} callingElement The calling html element.
277	 * @param {Event} event The event object (dataChanged).
278	 * @param {Object} eventData Event data.
279	 */
280	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
281			dataChangedHandler_ = function(callingElement, event, eventData) {
282
283		if (this.getHtmlElement().parents('.pkp_controllers_grid').length === 0) {
284			// We might want to redirect this data changed event to a grid.
285			// Trigger another event so parent widgets can handle this
286			// redirection.
287			this.trigger('redirectDataChangedToGrid', [eventData]);
288		}
289		this.trigger('notifyUser', [this.getHtmlElement()]);
290	};
291
292
293	/**
294	 * Click event handler used to avoid any action request.
295	 * @return {boolean} Always returns false.
296	 * @private
297	 */
298	$.pkp.controllers.linkAction.LinkActionHandler.prototype.
299			noAction_ = function() {
300		return false;
301	};
302
303
304/** @param {jQuery} $ jQuery closure. */
305}(jQuery));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.