1 /** 2 * LinkEdit component. 3 * 4 * Component provides link creation/edit functionality. 5 * 6 * @namespace Alfresco 7 * @class Alfresco.LinkEdit 8 */ 9 (function() 10 { 11 /** 12 * YUI Library aliases 13 */ 14 var Dom = YAHOO.util.Dom, 15 Event = YAHOO.util.Event, 16 Element = YAHOO.util.Element; 17 18 /** 19 * Alfresco Slingshot aliases 20 */ 21 var $html = Alfresco.util.encodeHTML; 22 23 /** 24 * LinkEdit constructor. 25 * 26 * @param {String} htmlId The HTML id of the parent element 27 * @return {Alfresco.LinkEdit} The new Link instance 28 * @constructor 29 */ 30 Alfresco.LinkEdit = function(htmlId) 31 { 32 return Alfresco.LinkEdit.superclass.constructor.call(this, "Alfresco.LinkEdit", htmlId, ["json", "button", "menu"]); 33 }; 34 35 YAHOO.extend(Alfresco.LinkEdit, Alfresco.component.Base, 36 { 37 /** 38 * Object container for initialization options 39 * 40 * @property options 41 * @type object 42 */ 43 options: 44 { 45 /** 46 * Current siteId. 47 * 48 * @property siteId 49 * @type string 50 */ 51 siteId: "", 52 53 /** 54 * ContainerId representing root container 55 * 56 * @property containerId 57 * @type string 58 * @default "links" 59 */ 60 containerId: "links", 61 62 /** 63 * True if the component should be in edit mode. 64 */ 65 editMode: false, 66 67 /** 68 * Id of the link to edit. Only relevant if editMode is true 69 */ 70 linkId: "" 71 }, 72 73 /** 74 * Stores the data of the currently edited link 75 */ 76 linkData: null, 77 78 /** 79 * Fired by YUI when parent element is available for scripting. 80 * Component initialisation, including instantiation of YUI widgets and event listener binding. 81 * 82 * @method onReady 83 */ 84 onReady: function LinkEdit_onReady() 85 { 86 if (this.options.editMode) 87 { 88 // load the link data prior to initializing the form 89 this._loadLinkData(); 90 } 91 else 92 { 93 // directly initialize the form 94 this._initializeLinkForm(); 95 } 96 }, 97 98 /** 99 * Loads the comments for the provided nodeRef and refreshes the ui 100 */ 101 _loadLinkData: function LinkEdit__loadLinkData() 102 { 103 // ajax request success handler 104 var me = this; 105 var loadLinkDataSuccess = function CommentsList_loadCommentsSuccess(response) 106 { 107 // set the link data 108 var data = response.json.item; 109 me.linkData = data; 110 111 // now initialize the form, which will use the data we just loaded 112 me._initializeLinkForm(); 113 }; 114 115 // construct the request url 116 var url = YAHOO.lang.substitute(Alfresco.constants.PROXY_URI + "/api/links/link/site/{site}/{container}/{linkId}", 117 { 118 site : this.options.siteId, 119 container: this.options.containerId, 120 linkId: this.options.linkId 121 }); 122 123 // execute ajax request 124 Alfresco.util.Ajax.request( 125 { 126 url: url, 127 method: "GET", 128 responseContentType : "application/json", 129 successCallback: 130 { 131 fn: loadLinkDataSuccess, 132 scope: this 133 }, 134 failureMessage: this.msg("message.loadlinkdata.failure") 135 }); 136 }, 137 138 /** 139 * Initializes the link form with create/edit dependent data. 140 */ 141 _initializeLinkForm: function LinkEdit__initializeLinkForm() 142 { 143 // construct the actionUrl, which is different for creating/updating a link 144 var actionUrl = ""; 145 if(this.options.editMode){ 146 actionUrl = YAHOO.lang.substitute(Alfresco.constants.PROXY_URI + "api/links/site/{site}/{container}/{linkId}", 147 { 148 site: this.options.siteId, 149 container : this.options.containerId, 150 linkId : this.options.linkId 151 }); 152 } 153 else 154 { 155 actionUrl = YAHOO.lang.substitute(Alfresco.constants.PROXY_URI + "api/links/site/{site}/{container}/posts", 156 { 157 site: this.options.siteId, 158 container : this.options.containerId 159 }); 160 } 161 var form = Dom.get(this.id + '-form'); 162 form.setAttribute("action", actionUrl); 163 164 // title 165 var title = ''; 166 if (this.options.editMode) 167 { 168 title = this.linkData.title; 169 } 170 Dom.get(this.id + '-title').setAttribute("value", title); 171 172 // description 173 var description = ''; 174 if (this.options.editMode) 175 { 176 description = this.linkData.description; 177 } 178 Dom.get(this.id + '-description').value = description; 179 180 // url 181 var url = ''; 182 if (this.options.editMode) 183 { 184 url = $html(this.linkData.url); 185 } 186 Dom.get(this.id + '-url').value = url; 187 188 // internal link 189 var internal = false; 190 if (this.options.editMode) 191 { 192 internal = this.linkData.internal; 193 } 194 Dom.get(this.id + '-internal').checked = internal; 195 Dom.get(this.id + '-tag-input-field').tabIndex = 5; 196 Dom.get(this.id + "-add-tag-button").tabIndex = 6; 197 198 // register the behaviour with the form and display the form 199 this._registerLinkForm(); 200 }, 201 202 /** 203 * Registers the form logic 204 */ 205 _registerLinkForm: function LinkEdit__registerLinkForm() 206 { 207 // initialize the tag library 208 this.modules.tagLibrary = new Alfresco.module.TagLibrary(this.id); 209 this.modules.tagLibrary.setOptions( 210 { 211 siteId: this.options.siteId 212 }); 213 214 // add the tags that are already set on the link 215 if (this.options.editMode && this.linkData.tags.length > 0) 216 { 217 this.modules.tagLibrary.setTags(this.linkData.tags); 218 } 219 220 // create the Button 221 var okButtonLabel = ''; 222 if (this.options.editMode) 223 { 224 okButtonLabel = this.msg('button.update'); 225 } 226 else 227 { 228 okButtonLabel = this.msg('button.save'); 229 } 230 this.widgets.okButton = new YAHOO.widget.Button(this.id + "-ok", 231 { 232 type: "submit", 233 label: okButtonLabel 234 }); 235 236 // cancel button 237 this.widgets.cancelButton = Alfresco.util.createYUIButton(this, "cancel", this.onFormCancelButtonClick); 238 239 // create the form that does the validation/submit 240 this.widgets.linkForm = new Alfresco.forms.Form(this.id + "-form"); 241 // Title 242 this.widgets.linkForm.addValidation(this.id + "-title", Alfresco.forms.validation.mandatory, null, "keyup"); 243 this.widgets.linkForm.addValidation(this.id + "-title", Alfresco.forms.validation.length, 244 { 245 max: 256, 246 crop: true 247 }, "keyup"); 248 // Description 249 this.widgets.linkForm.addValidation(this.id + "-description", Alfresco.forms.validation.length, 250 { 251 max: 256, 252 crop: true 253 }, "keyup"); 254 // URL 255 this.widgets.linkForm.addValidation(this.id + "-url", Alfresco.forms.validation.mandatory, null, "blur"); 256 this.widgets.linkForm.addValidation(this.id + "-url", Alfresco.forms.validation.regexMatch, 257 { 258 pattern: /^(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/ 259 }, "keyup"); 260 this.widgets.linkForm.addValidation(this.id + "-url", Alfresco.forms.validation.length, 261 { 262 max: 2048, 263 crop: true 264 }, "blur"); 265 266 this.widgets.linkForm.setShowSubmitStateDynamically(true, false); 267 this.widgets.linkForm.setSubmitElements(this.widgets.okButton); 268 this.widgets.linkForm.setAJAXSubmit(true, 269 { 270 successCallback: 271 { 272 fn: this.onFormSubmitSuccess, 273 scope: this 274 }, 275 failureMessage: this.msg("message.savelink.failure"), 276 failureCallback: 277 { 278 fn: this.onFormSubmitFailure, 279 scope: this 280 } 281 }); 282 if (this.options.editMode) 283 { 284 this.widgets.linkForm.setAjaxSubmitMethod(Alfresco.util.Ajax.PUT); 285 } 286 this.widgets.linkForm.setSubmitAsJSON(true); 287 this.widgets.linkForm.doBeforeFormSubmit = 288 { 289 fn: function(form, obj) 290 { 291 // disable ui elements 292 this.widgets.okButton.set("disabled", true); 293 294 this.widgets.cancelButton.set("disabled", true); 295 296 // update the tags set in the form 297 this.modules.tagLibrary.updateForm(this.id + "-form", "tags"); 298 299 // show a wait message 300 this.widgets.feedbackMessage = Alfresco.util.PopupManager.displayMessage( 301 { 302 text: Alfresco.util.message(this.msg("message.submitting")), 303 spanClass: "wait", 304 displayTime: 0 305 }); 306 }, 307 scope: this 308 }; 309 this.modules.tagLibrary.initialize(this.widgets.linkForm); 310 this.widgets.linkForm.init(); 311 312 // finally display the form 313 var containerElem = YAHOO.util.Dom.get(this.id + "-div"); 314 Dom.removeClass(containerElem, "hidden"); 315 Dom.get(this.id + "-title").focus(); 316 }, 317 318 /** 319 * Cancel button click handler 320 */ 321 onFormCancelButtonClick: function LinkEdit_onFormCancelButtonClick(type, args) 322 { 323 // redirect to the page we came from 324 history.go(-1); 325 }, 326 327 /** 328 * Form submit success handler 329 */ 330 onFormSubmitSuccess: function LinkEdit_onFormSubmitSuccess(response) 331 { 332 // hide the wait message 333 this.widgets.feedbackMessage.destroy(); 334 335 var linkId = this.options.linkId; 336 if (!this.options.editMode) 337 { 338 linkId = response.json.name; 339 } 340 341 Alfresco.util.PopupManager.displayMessage({text: this.msg("message.savelink.success")}); 342 this._loadLinkViewPage(linkId); 343 }, 344 345 /** 346 * Reenables the inputs which got disabled as part of a comment submit 347 */ 348 onFormSubmitFailure: function LinkEdit_onFormSubmitFailure(response) 349 { 350 response.config.failureMessage = this.msg("message.savelink.failure"); 351 // enable the buttons 352 this.widgets.okButton.set("disabled", false); 353 354 this.widgets.cancelButton.set("disabled", false); 355 356 // hide the wait message 357 this.widgets.feedbackMessage.destroy(); 358 }, 359 360 /** 361 * PRIVATE FUNCTIONS 362 */ 363 364 /** 365 * Loads the link view page 366 */ 367 _loadLinkViewPage: function LinkEdit__loadLinkViewPage(linkId) 368 { 369 var url = YAHOO.lang.substitute(Alfresco.constants.URL_PAGECONTEXT + "site/{site}/links-view?linkId={linkId}", 370 { 371 site: this.options.siteId, 372 linkId: linkId 373 }); 374 window.location = url; 375 } 376 }); 377 })(); 378