[asp.net] Fix for bug #658278. Panels render children only when parent is present...
[mono.git] / mcs / class / System.Web.Extensions / System.Web.UI / ScriptReference.cs
1 //
2 // ScriptReference.cs
3 //
4 // Author:
5 //   Igor Zelmanovich <igorz@mainsoft.com>
6 //
7 // (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.ComponentModel;
33 using System.Reflection;
34 using System.Text;
35 using System.Threading;
36 using System.Web.Handlers;
37 using System.Web.UI.WebControls;
38
39 namespace System.Web.UI
40 {
41         [DefaultProperty ("Path")]
42         public class ScriptReference : ScriptReferenceBase
43         {
44                 string _name;
45                 string _assembly;
46                 bool _ignoreScriptPath;
47
48                 public ScriptReference ()
49                 {
50                 }
51
52                 public ScriptReference (string path)
53                 {
54                         this.Path = path;
55                 }
56
57                 public ScriptReference (string name, string assembly)
58                 {
59                         _name = name;
60                         _assembly = assembly;
61                 }
62
63                 public string Assembly {
64                         get {
65                                 return _assembly;
66                         }
67                         set {
68                                 _assembly = value;
69                         }
70                 }
71
72                 public bool IgnoreScriptPath {
73                         get {
74                                 return _ignoreScriptPath;
75                         }
76                         set {
77                                 _ignoreScriptPath = value;
78                         }
79                 }
80
81                 public string Name {
82                         get {
83                                 return _name != null ? _name : String.Empty;
84                         }
85                         set {
86                                 _name = value;
87                         }
88                 }
89
90                 internal ScriptMode ScriptModeInternal {
91                         get {
92                                 if (ScriptMode == ScriptMode.Auto) {
93                                         if (!String.IsNullOrEmpty (Name))
94                                                 return ScriptMode.Inherit;
95                                         else
96                                                 return ScriptMode.Release;
97                                 }
98                                 else
99                                         return ScriptMode;
100                         }
101                 }
102
103                 [MonoTODO ("Compression not supported yet.")]
104                 protected internal override string GetUrl (ScriptManager scriptManager, bool zip)
105                 {
106                         bool isDebugMode = scriptManager.IsDeploymentRetail ? false :
107                                 (ScriptModeInternal == ScriptMode.Inherit ? scriptManager.IsDebuggingEnabled : (ScriptModeInternal == ScriptMode.Debug));
108                         string path = Path;
109                         string url = String.Empty;
110                         
111                         if (!String.IsNullOrEmpty (path)) {
112                                 url = GetScriptName (path, isDebugMode, scriptManager.EnableScriptLocalization ? ResourceUICultures : null);
113                         } else if (!String.IsNullOrEmpty (Name)) {
114                                 Assembly assembly;
115                                 string assemblyName = this.Assembly;
116                                 
117                                 if (String.IsNullOrEmpty (assemblyName))
118                                         assembly = typeof (ScriptManager).Assembly;
119                                 else
120                                         assembly = global::System.Reflection.Assembly.Load (assemblyName);
121                                 string name = GetScriptName (Name, isDebugMode, null);
122                                 string scriptPath = scriptManager.ScriptPath;
123                                 if (IgnoreScriptPath || String.IsNullOrEmpty (scriptPath))
124                                         url = ScriptResourceHandler.GetResourceUrl (assembly, name, NotifyScriptLoaded);
125                                 else {
126                                         AssemblyName an = assembly.GetName ();
127                                         url = scriptManager.ResolveClientUrl (String.Concat (VirtualPathUtility.AppendTrailingSlash (scriptPath), an.Name, '/', an.Version, '/', name));
128                                 }
129                         } else {
130                                 throw new InvalidOperationException ("Name and Path cannot both be empty.");
131                         }
132
133                         return url;
134                 }
135
136                 static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures) {
137                         if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0))
138                                 return releaseName;
139
140                         if (releaseName.Length < 3 || !releaseName.EndsWith (".js", StringComparison.OrdinalIgnoreCase))
141                                 throw new InvalidOperationException (String.Format ("'{0}' is not a valid script path.  The path must end in '.js'.", releaseName));
142
143                         StringBuilder sb = new StringBuilder (releaseName);
144                         sb.Length -= 3;
145                         if (isDebugMode)
146                                 sb.Append (".debug");
147                         string culture = Thread.CurrentThread.CurrentUICulture.Name;
148                         if (supportedUICultures != null && Array.IndexOf<string> (supportedUICultures, culture) >= 0)
149                                 sb.AppendFormat (".{0}", culture);
150                         sb.Append (".js");
151
152                         return sb.ToString ();
153                 }
154                 
155                 protected internal override bool IsFromSystemWebExtensions ()
156                 {
157                         return false;
158                 }
159                 
160                 public override string ToString ()
161                 {
162                         return Name.Length > 0 ? Name : Path;
163                 }
164         }
165 }