From 872c1639999f6b2f1fc2c4d62bb07069cd9844ef Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 3 Jan 2013 06:21:55 +0300 Subject: [PATCH] Updated EditMacro.js to support inserting different syntax depending on rendering engine. Just need to get it to insert the params for MVC now. Added HttpRequestExtenions with a method GetItemAsString which obsoletes/supercedes the old 'umbraco.helper.Request' method. --- .../umbraco/dialogs/editMacro.aspx | 4 +- .../umbraco/settings/editTemplate.aspx | 2 +- .../umbraco_client/Dialogs/EditMacro.js | 79 +++-- .../umbraco_client/Editors/EditView.js | 4 +- src/Umbraco.Web/HttpRequestExtensions.cs | 39 +++ src/Umbraco.Web/Umbraco.Web.csproj | 2 + .../AttributeCollectionAdapter.cs | 312 +++++++++++++++++ .../umbraco.presentation/helper.cs | 314 +----------------- 8 files changed, 414 insertions(+), 342 deletions(-) create mode 100644 src/Umbraco.Web/HttpRequestExtensions.cs create mode 100644 src/Umbraco.Web/umbraco.presentation/AttributeCollectionAdapter.cs diff --git a/src/Umbraco.Web.UI/umbraco/dialogs/editMacro.aspx b/src/Umbraco.Web.UI/umbraco/dialogs/editMacro.aspx index 63357070fa..879df9829c 100644 --- a/src/Umbraco.Web.UI/umbraco/dialogs/editMacro.aspx +++ b/src/Umbraco.Web.UI/umbraco/dialogs/editMacro.aspx @@ -2,6 +2,7 @@ <%@ Register TagPrefix="umb" Namespace="ClientDependency.Core.Controls" Assembly="ClientDependency.Core" %> +<%@ Import Namespace="Umbraco.Web" %> <%@ Register TagPrefix="cc2" Namespace="umbraco.uicontrols" Assembly="controls" %> @@ -13,7 +14,8 @@ $(document).ready(function () { Umbraco.Dialogs.EditMacro.getInstance().init({ useAspNetMasterPages: <%=umbraco.UmbracoSettings.UseAspNetMasterPages.ToString().ToLower() %>, - codeEditorElementId: "<%=umbraco.helper.Request("objectId")%>", + codeEditorElementId: "<%=Request.GetItemAsString("objectId")%>", + renderingEngine: "<%=Request.GetItemAsString("renderingEngine")%>", macroAlias: '<%= _macroAlias %>' }); }); diff --git a/src/Umbraco.Web.UI/umbraco/settings/editTemplate.aspx b/src/Umbraco.Web.UI/umbraco/settings/editTemplate.aspx index c981daa489..b2178496aa 100644 --- a/src/Umbraco.Web.UI/umbraco/settings/editTemplate.aspx +++ b/src/Umbraco.Web.UI/umbraco/settings/editTemplate.aspx @@ -163,7 +163,7 @@ if(alias != null && alias != ""){ t = "&alias="+alias; } - UmbClientMgr.openModalWindow('<%= IOHelper.ResolveUrl(SystemDirectories.Umbraco) %>/dialogs/editMacro.aspx?objectId=<%= editorSource.ClientID %>' + t, 'Insert Macro', true, 470, 530, 0, 0, '', ''); + UmbClientMgr.openModalWindow('<%= IOHelper.ResolveUrl(SystemDirectories.Umbraco) %>/dialogs/editMacro.aspx?renderingEngine=Webforms&objectId=<%= editorSource.ClientID %>' + t, 'Insert Macro', true, 470, 530, 0, 0, '', ''); } diff --git a/src/Umbraco.Web.UI/umbraco_client/Dialogs/EditMacro.js b/src/Umbraco.Web.UI/umbraco_client/Dialogs/EditMacro.js index f3a448fdb8..37a60a7f5c 100644 --- a/src/Umbraco.Web.UI/umbraco_client/Dialogs/EditMacro.js +++ b/src/Umbraco.Web.UI/umbraco_client/Dialogs/EditMacro.js @@ -22,36 +22,15 @@ document.getElementById("label" + macroAlias).innerHTML = "updated with id: " + treePicker + "
"; }, - // Constructor - constructor: function () { + _getMacroSyntaxMvc: function() { + /// Return the macro syntax to insert for MVC + + return "@Umbraco.RenderMacro(\"" + this._opts.macroAlias + "\")"; }, - //public methods - - init: function (opts) { - /// Initializes the class and any UI bindings - - // Merge options with default - this._opts = $.extend({ - // Default options go here - }, opts); - - var self = this; - - //The knockout js view model for the selected item - var koViewModel = { - cancelModal: function () { - UmbClientMgr.closeModalWindow(); - }, - updateMacro: function () { - self.updateMacro(); - } - }; - - ko.applyBindings(koViewModel); - }, - - updateMacro: function () { + _getMacroSyntaxWebForms: function () { + /// Return the macro syntax to insert for webforms + var macroElement; if (this._opts.useAspNetMasterPages) { macroElement = "umbraco:Macro"; @@ -120,9 +99,51 @@ else { macroString += ">"; } + return macroString; + }, + // Constructor + constructor: function () { + }, + + //public methods + + init: function (opts) { + /// Initializes the class and any UI bindings + + // Merge options with default + this._opts = $.extend({ + // Default options go here + }, opts); + + var self = this; + + //The knockout js view model for the selected item + var koViewModel = { + cancelModal: function () { + UmbClientMgr.closeModalWindow(); + }, + updateMacro: function () { + self.updateMacro(); + } + }; + + ko.applyBindings(koViewModel); + }, + + updateMacro: function () { + + var macroSyntax = null; + //if it is Mvc or empty, then use Mvc + if (this._opts.renderingEngine == "Mvc" || this._opts.renderingEngine == "") { + macroSyntax = this._getMacroSyntaxMvc(); + } + else { + macroSyntax = this._getMacroSyntaxWebForms(); + } + UmbClientMgr.contentFrame().focus(); - UmbClientMgr.contentFrame().UmbEditor.Insert(macroString, '', this._opts.codeEditorElementId); + UmbClientMgr.contentFrame().UmbEditor.Insert(macroSyntax, '', this._opts.codeEditorElementId); UmbClientMgr.closeModalWindow(); }, diff --git a/src/Umbraco.Web.UI/umbraco_client/Editors/EditView.js b/src/Umbraco.Web.UI/umbraco_client/Editors/EditView.js index 9bfa35ab5c..382b70b53a 100644 --- a/src/Umbraco.Web.UI/umbraco_client/Editors/EditView.js +++ b/src/Umbraco.Web.UI/umbraco_client/Editors/EditView.js @@ -42,7 +42,9 @@ if (alias != null && alias != "") { t = "&alias=" + alias; } - UmbClientMgr.openModalWindow(this._opts.modalUrl + '?objectId=' + this._opts.codeEditorElementId + t, 'Insert Macro', true, 470, 530, 0, 0, '', ''); + UmbClientMgr.openModalWindow( + this._opts.modalUrl + '?renderingEngine=Mvc&objectId=' + this._opts.codeEditorElementId + t, + 'Insert Macro', true, 470, 530, 0, 0, '', ''); }, doSubmit: function () { diff --git a/src/Umbraco.Web/HttpRequestExtensions.cs b/src/Umbraco.Web/HttpRequestExtensions.cs new file mode 100644 index 0000000000..b2746c563b --- /dev/null +++ b/src/Umbraco.Web/HttpRequestExtensions.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; +using Umbraco.Core; + +namespace Umbraco.Web +{ + /// + /// Extension methods for the HttpRequest and HttpRequestBase objects + /// + public static class HttpRequestExtensions + { + /// + /// Safely get a request item as string, if the item does not exist, an empty string is returned. + /// + /// + /// + /// + public static string GetItemAsString(this HttpRequest request, string key) + { + return new HttpRequestWrapper(request).GetItemAsString(key); + } + + /// + /// Safely get a request item as string, if the item does not exist, an empty string is returned. + /// + /// + /// + /// + public static string GetItemAsString(this HttpRequestBase request, string key) + { + var val = HttpContext.Current.Request[key]; + return !val.IsNullOrWhiteSpace() ? val : string.Empty; + } + + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 702c15529e..a635180419 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -255,6 +255,7 @@ + @@ -338,6 +339,7 @@ + ASPXCodeBehind diff --git a/src/Umbraco.Web/umbraco.presentation/AttributeCollectionAdapter.cs b/src/Umbraco.Web/umbraco.presentation/AttributeCollectionAdapter.cs new file mode 100644 index 0000000000..b7c2f7907a --- /dev/null +++ b/src/Umbraco.Web/umbraco.presentation/AttributeCollectionAdapter.cs @@ -0,0 +1,312 @@ +using System; +using System.Collections; +using System.Web.UI; + +namespace umbraco +{ + /// + /// Class that adapts an to the interface. + /// + public class AttributeCollectionAdapter : IDictionary + { + private readonly AttributeCollection _collection; + + /// + /// Initializes a new instance of the class. + /// + /// The collection. + public AttributeCollectionAdapter(AttributeCollection collection) + { + _collection = collection; + } + + #region IDictionary Members + + /// + /// Adds an element with the provided key and value to the object. + /// + /// The to use as the key of the element to add. + /// The to use as the value of the element to add. + public void Add(object key, object value) + { + _collection.Add(key.ToString(), value.ToString()); + } + + /// + /// Removes all elements from the object. + /// + /// + /// The object is read-only. + /// + public void Clear() + { + _collection.Clear(); + } + + /// + /// Determines whether the object contains an element with the specified key. + /// + /// The key to locate in the object. + /// + /// true if the contains an element with the key; otherwise, false. + /// + public bool Contains(object key) + { + return _collection[key.ToString()] != null; + } + + /// + /// Returns an object for the object. + /// + /// + /// An object for the object. + /// + public IDictionaryEnumerator GetEnumerator() + { + return new AttributeCollectionAdapterEnumerator(this); + } + + /// + /// Gets a value indicating whether the object has a fixed size. + /// + /// + /// true if the object has a fixed size; otherwise, false. + /// + public bool IsFixedSize + { + get { return false; } + } + + /// + /// Gets a value indicating whether the object is read-only. + /// + /// + /// true if the object is read-only; otherwise, false. + /// + public bool IsReadOnly + { + get { return false; } + } + + /// + /// Gets an object containing the keys of the object. + /// + /// + /// + /// An object containing the keys of the object. + /// + public ICollection Keys + { + get { return _collection.Keys; } + } + + /// + /// Removes the element with the specified key from the object. + /// + /// The key of the element to remove. + public void Remove(object key) + { + _collection.Remove(key.ToString()); + } + + /// + /// Gets an object containing the values in the object. + /// + /// + /// + /// An object containing the values in the object. + /// + public ICollection Values + { + get { throw new NotImplementedException(); } + } + + /// + /// Gets or sets the with the specified key. + /// + /// + public object this[object key] + { + get { return _collection[key.ToString()]; } + set { _collection[key.ToString()] = value.ToString(); } + } + + #endregion + + #region ICollection Members + + /// Not implemented. + /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. + /// The zero-based index in at which copying begins. + public void CopyTo(Array array, int index) + { + throw new NotImplementedException(); + } + + /// + /// Gets the number of elements contained in the . + /// + /// + /// + /// The number of elements contained in the . + /// + public int Count + { + get { return _collection.Count; } + } + + /// + /// Gets a value indicating whether access to the is synchronized (thread safe). + /// + /// + /// true if access to the is synchronized (thread safe); otherwise, false. + /// + public bool IsSynchronized + { + get { return false; } + } + + /// + /// Gets an object that can be used to synchronize access to the . + /// + /// + /// + /// An object that can be used to synchronize access to the . + /// + public object SyncRoot + { + get { return _collection; } + } + + #endregion + + #region IEnumerable Members + + /// + /// Returns an enumerator that iterates through a collection. + /// + /// + /// An object that can be used to iterate through the collection. + /// + IEnumerator IEnumerable.GetEnumerator() + { + foreach (object key in _collection.Keys) + yield return _collection[(string)key]; + } + + #endregion + + + /// + /// for the class. + /// + private class AttributeCollectionAdapterEnumerator : IDictionaryEnumerator + { + private readonly AttributeCollectionAdapter _adapter; + private readonly IEnumerator _enumerator; + + /// + /// Initializes a new instance of the class. + /// + /// The adapter. + public AttributeCollectionAdapterEnumerator(AttributeCollectionAdapter adapter) + { + _adapter = adapter; + _enumerator = ((IEnumerable)adapter).GetEnumerator(); + } + + #region IDictionaryEnumerator Members + + /// + /// Gets both the key and the value of the current dictionary entry. + /// + /// + /// + /// A containing both the key and the value of the current dictionary entry. + /// + /// + /// The is positioned before the first entry of the dictionary or after the last entry. + /// + public DictionaryEntry Entry + { + get { return new DictionaryEntry(Key, Value); } + } + + /// + /// Gets the key of the current dictionary entry. + /// + /// + /// + /// The key of the current element of the enumeration. + /// + /// + /// The is positioned before the first entry of the dictionary or after the last entry. + /// + public object Key + { + get { return _enumerator.Current; } + } + + /// + /// Gets the value of the current dictionary entry. + /// + /// + /// + /// The value of the current element of the enumeration. + /// + /// + /// The is positioned before the first entry of the dictionary or after the last entry. + /// + public object Value + { + get { return _adapter[_enumerator.Current]; } + } + + #endregion + + #region IEnumerator Members + + /// + /// Gets the current element in the collection. + /// + /// + /// + /// The current element in the collection. + /// + /// + /// The enumerator is positioned before the first element of the collection or after the last element. + /// + public object Current + { + get { return _enumerator.Current; } + } + + /// + /// Advances the enumerator to the next element of the collection. + /// + /// + /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. + /// + /// + /// The collection was modified after the enumerator was created. + /// + public bool MoveNext() + { + return _enumerator.MoveNext(); + } + + /// + /// Sets the enumerator to its initial position, which is before the first element in the collection. + /// + /// + /// The collection was modified after the enumerator was created. + /// + public void Reset() + { + _enumerator.Reset(); + } + + #endregion + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/umbraco.presentation/helper.cs b/src/Umbraco.Web/umbraco.presentation/helper.cs index 869169ccd2..3caf4ec5c0 100644 --- a/src/Umbraco.Web/umbraco.presentation/helper.cs +++ b/src/Umbraco.Web/umbraco.presentation/helper.cs @@ -5,7 +5,6 @@ using System.Web; using umbraco.BusinessLogic; using System.Xml; -using System.Web.UI; using umbraco.presentation; namespace umbraco @@ -26,12 +25,13 @@ namespace umbraco return umbraco.BasePages.UmbracoEnsuredPage.CurrentUser; } + [Obsolete("This method has been superceded. Use the extension method for HttpRequest or HttpRequestBase method: GetItemAsString instead.")] public static string Request(string text) { - if (UmbracoContext.Current.Request[text.ToLower()] != null) - if (UmbracoContext.Current.Request[text] != string.Empty) - return UmbracoContext.Current.Request[text]; + if (HttpContext.Current.Request[text.ToLower()] != null) + if (HttpContext.Current.Request[text] != string.Empty) + return HttpContext.Current.Request[text]; return String.Empty; } @@ -158,310 +158,4 @@ namespace umbraco return Context.Request.Url.GetLeftPart(UriPartial.Authority); } } - - /// - /// Class that adapts an to the interface. - /// - public class AttributeCollectionAdapter : IDictionary - { - private AttributeCollection m_Collection; - - /// - /// Initializes a new instance of the class. - /// - /// The collection. - public AttributeCollectionAdapter(AttributeCollection collection) - { - m_Collection = collection; - } - - #region IDictionary Members - - /// - /// Adds an element with the provided key and value to the object. - /// - /// The to use as the key of the element to add. - /// The to use as the value of the element to add. - public void Add(object key, object value) - { - m_Collection.Add(key.ToString(), value.ToString()); - } - - /// - /// Removes all elements from the object. - /// - /// - /// The object is read-only. - /// - public void Clear() - { - m_Collection.Clear(); - } - - /// - /// Determines whether the object contains an element with the specified key. - /// - /// The key to locate in the object. - /// - /// true if the contains an element with the key; otherwise, false. - /// - public bool Contains(object key) - { - return m_Collection[key.ToString()] != null; - } - - /// - /// Returns an object for the object. - /// - /// - /// An object for the object. - /// - public IDictionaryEnumerator GetEnumerator() - { - return new AttributeCollectionAdapterEnumerator(this); - } - - /// - /// Gets a value indicating whether the object has a fixed size. - /// - /// - /// true if the object has a fixed size; otherwise, false. - /// - public bool IsFixedSize - { - get { return false; } - } - - /// - /// Gets a value indicating whether the object is read-only. - /// - /// - /// true if the object is read-only; otherwise, false. - /// - public bool IsReadOnly - { - get { return false; } - } - - /// - /// Gets an object containing the keys of the object. - /// - /// - /// - /// An object containing the keys of the object. - /// - public ICollection Keys - { - get { return m_Collection.Keys; } - } - - /// - /// Removes the element with the specified key from the object. - /// - /// The key of the element to remove. - public void Remove(object key) - { - m_Collection.Remove(key.ToString()); - } - - /// - /// Gets an object containing the values in the object. - /// - /// - /// - /// An object containing the values in the object. - /// - public ICollection Values - { - get { throw new NotImplementedException(); } - } - - /// - /// Gets or sets the with the specified key. - /// - /// - public object this[object key] - { - get { return m_Collection[key.ToString()]; } - set { m_Collection[key.ToString()] = value.ToString(); } - } - - #endregion - - #region ICollection Members - - /// Not implemented. - /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. - /// The zero-based index in at which copying begins. - public void CopyTo(Array array, int index) - { - throw new NotImplementedException(); - } - - /// - /// Gets the number of elements contained in the . - /// - /// - /// - /// The number of elements contained in the . - /// - public int Count - { - get { return m_Collection.Count; } - } - - /// - /// Gets a value indicating whether access to the is synchronized (thread safe). - /// - /// - /// true if access to the is synchronized (thread safe); otherwise, false. - /// - public bool IsSynchronized - { - get { return false; } - } - - /// - /// Gets an object that can be used to synchronize access to the . - /// - /// - /// - /// An object that can be used to synchronize access to the . - /// - public object SyncRoot - { - get { return m_Collection; } - } - - #endregion - - #region IEnumerable Members - - /// - /// Returns an enumerator that iterates through a collection. - /// - /// - /// An object that can be used to iterate through the collection. - /// - IEnumerator IEnumerable.GetEnumerator() - { - foreach (object key in m_Collection.Keys) - yield return m_Collection[(string)key]; - } - - #endregion - - - /// - /// for the class. - /// - private class AttributeCollectionAdapterEnumerator : IDictionaryEnumerator - { - private AttributeCollectionAdapter m_Adapter; - private IEnumerator m_Enumerator; - - /// - /// Initializes a new instance of the class. - /// - /// The adapter. - public AttributeCollectionAdapterEnumerator(AttributeCollectionAdapter adapter) - { - m_Adapter = adapter; - m_Enumerator = ((IEnumerable)adapter).GetEnumerator(); - } - - #region IDictionaryEnumerator Members - - /// - /// Gets both the key and the value of the current dictionary entry. - /// - /// - /// - /// A containing both the key and the value of the current dictionary entry. - /// - /// - /// The is positioned before the first entry of the dictionary or after the last entry. - /// - public DictionaryEntry Entry - { - get { return new DictionaryEntry(Key, Value); } - } - - /// - /// Gets the key of the current dictionary entry. - /// - /// - /// - /// The key of the current element of the enumeration. - /// - /// - /// The is positioned before the first entry of the dictionary or after the last entry. - /// - public object Key - { - get { return m_Enumerator.Current; } - } - - /// - /// Gets the value of the current dictionary entry. - /// - /// - /// - /// The value of the current element of the enumeration. - /// - /// - /// The is positioned before the first entry of the dictionary or after the last entry. - /// - public object Value - { - get { return m_Adapter[m_Enumerator.Current]; } - } - - #endregion - - #region IEnumerator Members - - /// - /// Gets the current element in the collection. - /// - /// - /// - /// The current element in the collection. - /// - /// - /// The enumerator is positioned before the first element of the collection or after the last element. - /// - public object Current - { - get { return m_Enumerator.Current; } - } - - /// - /// Advances the enumerator to the next element of the collection. - /// - /// - /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. - /// - /// - /// The collection was modified after the enumerator was created. - /// - public bool MoveNext() - { - return m_Enumerator.MoveNext(); - } - - /// - /// Sets the enumerator to its initial position, which is before the first element in the collection. - /// - /// - /// The collection was modified after the enumerator was created. - /// - public void Reset() - { - m_Enumerator.Reset(); - } - - #endregion - } - } } \ No newline at end of file