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