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  *** Alfresco WebView Dashlet
 22  *
 23  * @namespace Alfresco.dashlet
 24  * @class Alfresco.dashlet.WebView
 25  *
 26  */
 27 (function()
 28 {
 29    /**
 30     * YUI Library aliases
 31     */
 32    var Dom = YAHOO.util.Dom,
 33       Event = YAHOO.util.Event;
 34 
 35    /**
 36     * Alfresco Slingshot aliases
 37     */
 38    var $html = Alfresco.util.encodeHTML;
 39    
 40    Alfresco.dashlet.WebView = function WebView_constructor(htmlId)
 41    {
 42       Alfresco.dashlet.WebView.superclass.constructor.call(this, "Alfresco.dashlet.WebView", htmlId);
 43 
 44       // Initialise prototype properties
 45       this.configDialog = null;
 46 
 47       /**
 48        * Decoupled event listeners
 49        */
 50       YAHOO.Bubbling.on("showPanel", this.onShowPanel, this);
 51       YAHOO.Bubbling.on("hidePanel", this.onHidePanel, this);
 52 
 53       return this;
 54    };
 55 
 56    YAHOO.extend(Alfresco.dashlet.WebView, Alfresco.component.Base,
 57    {
 58       /**
 59        * Object container for initialization options
 60        *
 61        * @property options
 62        * @type object
 63        */
 64       options:
 65       {
 66          /**
 67           * ComponentId used for saving configuration
 68           * @property componentId
 69           * @type string
 70           */
 71          componentId: "",
 72          
 73          /**
 74           * URI for the web page to view
 75           * @property webviewURI
 76           * @type string
 77           */
 78          webviewURI: "",
 79          
 80          /**
 81           * Dashlet title
 82           * @property webviewTitle
 83           * @type string
 84           */
 85          webviewTitle: "",
 86          
 87          /**
 88           * Default web page
 89           * @property isDefault
 90           * @type boolean
 91           * @default true
 92           */
 93          isDefault: true
 94       },
 95       
 96       /**
 97        * Configuration dialog instance
 98        *
 99        * @property configDialog
100        * @type object
101        */
102       configDialog: null,
103 
104       /**
105        * Fired by YUI when parent element is available for scripting.
106        * Initialises components, including YUI widgets.
107        *
108        * @method onReady
109        */
110       onReady: function WebView_onReady()
111       {
112          var configWebViewLink = Dom.get(this.id + "-configWebView-link");
113          Event.addListener(configWebViewLink, "click", this.onConfigWebViewClick, this, true);
114 
115          /**
116           * Save reference to iframe wrapper so we can hide and show it depending
117           * on how well the browser handles flash movies.
118           */
119          this.widgets.iframeWrapper = Dom.get(this.id + "-iframeWrapper");
120       },
121 
122       /**
123        * Event listener for configuration link click.
124        *
125        * @method onConfigWebViewClick
126        * @param e {object} HTML event
127        */
128       onConfigWebViewClick: function WebView_onConfigWebViewClick(e)
129       {
130          Event.stopEvent(e);
131          
132          var actionUrl = Alfresco.constants.URL_SERVICECONTEXT + "modules/webview/config/" + encodeURIComponent(this.options.componentId);
133 
134          if (!this.configDialog)
135          {
136             this.configDialog = new Alfresco.module.SimpleDialog(this.id + "-configDialog").setOptions(
137             {
138                width: "50em",
139                templateUrl: Alfresco.constants.URL_SERVICECONTEXT + "modules/webview/config",
140                onSuccess:
141                {
142                   fn: function WebView_onConfigWebView_callback(response)
143                   {
144                      // MSIE6 doesn't redraw the IFRAME correctly, so tell it to refresh the page
145                      if (YAHOO.env.ua.ie === 6)
146                      {
147                         window.location.reload(true);
148                      }
149                      else
150                      {
151                         var div = Dom.get(this.id + "-iframeWrapper");
152                         div.innerHTML = response.serverResponse.responseText + '<div class="resize-mask"></div>';
153                         var iframe = Dom.getFirstChildBy(div, function(node)
154                         {
155                            return (node.tagName.toUpperCase() == "IFRAME");
156                         });
157                         if (iframe)
158                         {
159                            if (iframe.attributes["name"])
160                            {
161                               var titleLink = Dom.get(this.id + "-title-link");
162                               // update iframe and internal config
163                               titleLink.href = this.options.webviewURI = iframe.attributes["src"].value;
164                               this.options.webviewTitle = iframe.attributes["name"].value;
165                               titleLink.innerHTML = $html(this.options.webviewTitle);
166                            }
167                            this.options.isDefault = false;
168                         }
169                      }
170                   },
171                   scope: this
172                },
173                doSetupFormsValidation:
174                {
175                   fn: function WebView_doSetupForm_callback(form)
176                   {
177                      form.addValidation(this.configDialog.id + "-url", Alfresco.forms.validation.mandatory, null, "blur");
178                      form.addValidation(this.configDialog.id + "-url", Alfresco.forms.validation.url, null, "keyup");
179                      
180                      // 511 characters is the maximum length of URL that IE appears to support without causing a page direct
181                      // and preventing the user from returning to their dashboard. To avoid this occurring a check on the length
182                      // is set. Rather than just adding this for IE it is added for all browsers because it is possible that
183                      // a user could edit the URL on one browser to something greater than 511 characters and then attempt
184                      // to view the page in another browser.
185                      form.addValidation(this.configDialog.id + "-url", function(field, args, event, form, silent, message)
186                         {
187                            return (field.value.length < 512);
188                         }, null, "keyup");
189                      form.setShowSubmitStateDynamically(true, false);
190 
191                      /* Get the link title */
192                      var elem = Dom.get(this.configDialog.id + "-webviewTitle");
193                      if (elem)
194                      {
195                         elem.value = this.options.webviewTitle;
196                      }
197 
198                      /* Get the url value */
199                      elem = Dom.get(this.configDialog.id + "-url");
200                      if (elem)
201                      {
202                         elem.value = this.options.isDefault ? "http://" : this.options.webviewURI;
203                      }
204                   },
205                   scope: this
206                }
207             });
208          }
209 
210          this.configDialog.setOptions(
211          {
212             actionUrl: actionUrl
213          }).show();
214       },
215 
216       /**
217        * Called when any Panel in share created with createYUIPanel is shown.
218        * Will hide the content for browsers that can't handle a flash movies properly,
219        * since the flash movie could hide parts of the the panel.
220        *
221        * @method onShowPanel
222        * @param p_layer {object} Event fired (unused)
223        * @param p_args {array} Event parameters (unused)
224        */
225       onShowPanel: function WW_onShowPanel(p_layer, p_args)
226       {
227          if (this._browserDestroysPanel())
228          {
229             Dom.setStyle(this.widgets.iframeWrapper, "visibility", "hidden");
230          }
231       },
232 
233       /**
234        * Called when any Panel in share created with createYUIPanel is hidden.
235        * Will display the content again if it was hidden before.
236        *
237        * @method onHidePanel
238        * @param p_layer {object} Event fired (unused)
239        * @param p_args {array} Event parameters (unused)
240        */
241       onHidePanel: function WW_onHidePanel(p_layer, p_args)
242       {
243          if (this._browserDestroysPanel())
244          {
245             Dom.setStyle(this.widgets.iframeWrapper, "visibility", "visible");
246          }
247       },
248 
249       /**
250        * Returns true if browser will make flash movie hide parts of a panel
251        *
252        * @method _browserDestroysPanel
253        * @return {boolean} True if browser will let flash movie mess up panel
254        */
255       _browserDestroysPanel: function WW__browserDestroysPanel()
256       {
257          // All browsers on Windows (tested w FP 10) and FF2 and below on Mac
258          return (navigator.userAgent.indexOf("Windows") !== -1 ||
259                  (navigator.userAgent.indexOf("Macintosh") !== -1 && YAHOO.env.ua.gecko > 0 && YAHOO.env.ua.gecko < 1.9));
260       }
261    });
262 })();