1 /**
  2  * Copyright (C) 2005-2010 Alfresco Software Limited.
  3  *
  4  * This file is part of Alfresco
  5  *
  6  * Alfresco is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, either version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * Alfresco is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public License
 17  * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 18  */
 19 
 20 /**
 21  * Rules "Checkin" Action module.
 22  *
 23  * @namespace Alfresco.module
 24  * @class Alfresco.module.RulesCheckinAction
 25  */
 26 (function()
 27 {
 28    /**
 29    * YUI Library aliases
 30    */
 31    var Dom = YAHOO.util.Dom,
 32       KeyListener = YAHOO.util.KeyListener;
 33 
 34    /**
 35     * Alfresco Slingshot aliases
 36     */
 37     var $html = Alfresco.util.encodeHTML;
 38 
 39    Alfresco.module.RulesCheckinAction = function(htmlId)
 40    {
 41       Alfresco.module.RulesCheckinAction.superclass.constructor.call(this, "Alfresco.module.RulesCheckinAction", htmlId, ["button", "container", "connection"]);
 42       return this;
 43    };
 44 
 45    YAHOO.extend(Alfresco.module.RulesCheckinAction, Alfresco.component.Base,
 46    {
 47       /**
 48        * Object container for initialization options
 49        */
 50       options:
 51       {
 52          /**
 53           * Template URL
 54           *
 55           * @property templateUrl
 56           * @type string
 57           * @default Alfresco.constants.URL_SERVICECONTEXT + "modules/rules/actions/checkin"
 58           */
 59          templateUrl: Alfresco.constants.URL_SERVICECONTEXT + "modules/rules/actions/checkin"
 60       },
 61 
 62 
 63       /**
 64        * Container element for template in DOM.
 65        *
 66        * @property containerDiv
 67        * @type HTMLElement
 68        */
 69       containerDiv: null,
 70 
 71       /**
 72        * Main entry point
 73        * @method showDialog
 74        * @param checkinConfig {object} Data to fill the form with
 75        *        checkinConfig.version {string} ["minor"|"version"|null]
 76        *        checkinConfig.comments {string}
 77        */
 78       showDialog: function RCIA_showDialog(checkinConfig)
 79       {
 80          if (!this.containerDiv)
 81          {
 82             // Load the UI template from the server
 83             Alfresco.util.Ajax.request(
 84             {
 85                url: this.options.templateUrl,
 86                dataObj:
 87                {
 88                   htmlid: this.id
 89                },
 90                successCallback:
 91                {
 92                   fn: this.onTemplateLoaded,
 93                   obj: checkinConfig,
 94                   scope: this
 95                },
 96                failureMessage: "Could not load template:" + this.options.templateUrl,
 97                execScripts: true
 98             });
 99          }
100          else
101          {
102             // Show the dialog
103             this._showDialog(checkinConfig);
104          }
105       },
106 
107       /**
108        * Event callback when dialog template has been loaded
109        *
110        * @method onTemplateLoaded
111        * @param response {object} Server response from load template XHR request
112        * @param checkinConfig {object} Data to fill the form with
113        */
114       onTemplateLoaded: function RCIA_onTemplateLoaded(response, checkinConfig)
115       {
116          // Inject the template from the XHR request into a new DIV element
117          this.containerDiv = document.createElement("div");
118          this.containerDiv.setAttribute("style", "display:none");
119          this.containerDiv.innerHTML = response.serverResponse.responseText;
120 
121          // The panel is created from the HTML returned in the XHR request, not the container
122          var dialogDiv = Dom.getFirstChild(this.containerDiv);
123 
124          // Create and render the YUI dialog
125          this.widgets.dialog = Alfresco.util.createYUIPanel(dialogDiv);
126 
127          // Buttons (note: ok buttons click will be handled in forms onBeforeAjaxSubmit)
128          this.widgets.okButton = Alfresco.util.createYUIButton(this, "ok-button", null,
129          {
130             type: "submit"
131          });
132          this.widgets.cancelButton = Alfresco.util.createYUIButton(this, "cancel-button", this.onCancelClick);
133 
134          // Configure the forms runtime
135          var form = new Alfresco.forms.Form(this.id + "-form");
136          this.widgets.form = form;
137 
138          // ...and has a maximum length
139          form.addValidation(this.id + "-comments", Alfresco.forms.validation.length,
140          {
141             max: 256,
142             crop: true
143          }, "keyup");
144 
145          // The ok button is the submit button, and it should be enabled when the form is ready
146          form.setShowSubmitStateDynamically(true, false);
147          form.setSubmitElements(this.widgets.okButton);
148 
149          // Stop the form from being submitted and fire and event from the collected information
150          form.doBeforeAjaxRequest =
151          {
152             fn: function(p_config, p_obj)
153             {
154                // Fire event so other component know
155                YAHOO.Bubbling.fire("checkinConfigCompleted",
156                {
157                   options:
158                   {
159                      version: p_config.dataObj.version,
160                      comments: p_config.dataObj.comments
161                   },
162                   eventGroup: this
163                });
164 
165                this.widgets.dialog.hide();
166 
167                // Return false so the form isn't submitted
168                return false;
169             },
170             obj: null,
171             scope: this
172          };
173 
174          // We're in a popup, so need the tabbing fix
175          form.applyTabFix();
176          form.init();
177 
178          // Register the ESC key to close the dialog
179          var escapeListener = new KeyListener(document,
180          {
181             keys: KeyListener.KEY.ESCAPE
182          },
183          {
184             fn: function(id, keyEvent)
185             {
186                this.onCancelClick();
187             },
188             scope: this,
189             correctScope: true
190          });
191          escapeListener.enable();
192 
193          // Show the dialog
194          this._showDialog(checkinConfig);
195       },
196 
197       /**
198        * Internal show dialog function
199        *
200        * @method _showDialog
201        * @param checkinConfig {object} Data to fill the form with
202        */
203       _showDialog: function RCIA__showDialog(checkinConfig)
204       {
205          // Display form data from config
206          checkinConfig = checkinConfig ? checkinConfig : {};
207          var majorEl = Dom.get(this.id + "-version-major"),
208             minorEl = Dom.get(this.id + "-version-minor"),
209             focusEl;
210 
211          if (checkinConfig.version == "minor" || checkinConfig.version == null || checkinConfig.version == "")
212          {
213             minorEl.checked = true;
214             focusEl = minorEl;
215             majorEl.checked = false;
216          }
217          else if (checkinConfig.version == "major")
218          {
219             majorEl.checked = true;
220             focusEl = majorEl;
221             minorEl.checked = false;
222          }         
223          Dom.get(this.id + "-comments").value = checkinConfig.comments ? checkinConfig.comments : "";
224          this.widgets.form.updateSubmitElements();
225 
226          // Show the dialog
227          this.widgets.dialog.show();
228 
229          // Focus when element is visible so IE is happy
230          focusEl.focus();
231       },
232 
233       /**
234        * YUI WIDGET EVENT HANDLERS
235        * Handlers for standard events fired from YUI widgets, e.g. "click"
236        */
237 
238       /**
239        * Dialog Cancel button event handler
240        *
241        * @method onCancelClick
242        * @param e {object} DomEvent
243        * @param p_obj {object} Object passed back from addListener method
244        */
245       onCancelClick: function RCIA_onCancelClick(e, p_obj)
246       {
247          this.widgets.dialog.hide();
248       }
249 
250    });
251 
252    /* Dummy instance to load optional YUI components early */
253    var dummyInstance = new Alfresco.module.RulesCheckinAction("null");
254 })();
255