Files
Umbraco-CMS/components/editorControls/macrocontainer/Editor.cs

302 lines
10 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.interfaces;
using umbraco.cms.businesslogic.macro;
using umbraco.presentation.webservices;
using System.Text.RegularExpressions;
using ClientDependency.Core;
using System.Web;
using ClientDependency.Core.Controls;
using umbraco.presentation;
using umbraco.IO;
namespace umbraco.editorControls.macrocontainer
{
[ClientDependency(ClientDependencyType.Javascript, "ui/jqueryui.js", "UmbracoClient")]
[ClientDependency(ClientDependencyType.Css, "macroContainer/macroContainer.css", "UmbracoClient")]
public class Editor : UpdatePanel, IDataEditor
{
2010-10-26 09:13:47 +00:00
private IData _data;
private List<string> _allowedMacros;
private int _maxNumber, _preferedHeight, _preferedWidth;
2010-10-26 09:13:47 +00:00
private LinkButton _addMacro;
private Literal _limit;
2010-10-26 09:13:47 +00:00
public Editor(IData data, List<string> allowedMacros, int maxNumber, int preferedHeight, int preferedWidth)
{
2010-10-26 09:13:47 +00:00
_data = data;
_allowedMacros = allowedMacros;
_maxNumber = maxNumber;
_preferedHeight = preferedHeight;
_preferedWidth = preferedWidth;
}
2010-10-26 09:13:47 +00:00
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
2010-10-26 09:13:47 +00:00
//SD: This is useless as it won't work in live editing anyways whilst using MS Ajax/ScriptManager for ajax calls
if (!UmbracoContext.Current.LiveEditingContext.Enabled)
{
presentation.webservices.ajaxHelpers.EnsureLegacyCalls(base.Page);
ScriptManager sm = ScriptManager.GetCurrent(base.Page);
ServiceReference webservicePath = new ServiceReference(umbraco.IO.IOHelper.ResolveUrl(umbraco.IO.SystemDirectories.Umbraco) + "/webservices/MacroContainerService.asmx");
2010-10-26 09:13:47 +00:00
if (!sm.Services.Contains(webservicePath))
sm.Services.Add(webservicePath);
}
else
{
ClientDependencyLoader.Instance.RegisterDependency("webservices/legacyAjaxCalls.asmx/js", "UmbracoRoot", ClientDependencyType.Javascript);
ClientDependencyLoader.Instance.RegisterDependency("webservices/MacroContainerService.asmx/js", "UmbracoRoot", ClientDependencyType.Javascript);
}
2010-10-26 09:13:47 +00:00
_addMacro = new LinkButton();
_addMacro.ID = ID + "_btnaddmacro";
2010-10-26 09:13:47 +00:00
_addMacro.Click += new EventHandler(_addMacro_Click);
_addMacro.Text = ui.Text("insertMacro");
_addMacro.CssClass = "macroContainerAdd";
2010-10-26 09:13:47 +00:00
this.ContentTemplateContainer.Controls.Add(_addMacro);
2010-10-26 09:13:47 +00:00
_limit = new Literal();
_limit.Text = string.Format(" Only {0} macros are allowed", _maxNumber);
_limit.ID = ID + "_litlimit";
_limit.Visible = false;
2010-10-26 09:13:47 +00:00
this.ContentTemplateContainer.Controls.Add(_limit);
2010-10-26 09:13:47 +00:00
string widthHeight = "";
if (_preferedHeight > 0 && _preferedWidth > 0)
{
widthHeight = String.Format(" style=\"min-width: {0}px; min-height: {1}px;\"", _preferedWidth, _preferedHeight);
}
2010-10-26 09:13:47 +00:00
this.ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"" + ID + "container\" class=\"macrocontainer\"{0}>", widthHeight)));
2010-10-26 09:13:47 +00:00
Regex tagregex = new Regex("<[^>]*(>|$)", RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled);
MatchCollection tags = tagregex.Matches(_data.Value.ToString());
2010-10-26 09:13:47 +00:00
List<int> editornumbers = new List<int>();
string sortorder = string.Empty;
2010-10-26 09:13:47 +00:00
for (int i = 0; i < _maxNumber; i++)
{
if (!editornumbers.Contains(i))
{
string data = string.Empty;
2010-10-26 09:13:47 +00:00
if (tags.Count > i)
data = tags[i].Value;
2010-10-26 09:13:47 +00:00
MacroEditor macroEditor = new MacroEditor(data, _allowedMacros);
macroEditor.ID = ID + "macroeditor_" + i;
2010-10-26 09:13:47 +00:00
this.ContentTemplateContainer.Controls.Add(macroEditor);
}
2010-10-26 09:13:47 +00:00
}
2010-10-26 09:13:47 +00:00
this.ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
2010-10-26 09:13:47 +00:00
if (tags.Count == _maxNumber)
{
_addMacro.Enabled = false;
_limit.Visible = true;
}
2010-10-26 09:13:47 +00:00
MacroContainerEvent.Execute += new MacroContainerEvent.ExecuteHandler(MacroContainerEvent_Execute);
2010-10-26 09:13:47 +00:00
}
private void CheckLimit()
{
bool allowadd = false;
for (int i = 0; i < _maxNumber; i++)
{
MacroEditor current = ((MacroEditor)this.ContentTemplateContainer.FindControl(ID + "macroeditor_" + i.ToString()));
2010-10-26 09:13:47 +00:00
if (!current.Visible)
{
allowadd = true;
break;
};
}
2010-10-26 09:13:47 +00:00
if (!allowadd)
{
_addMacro.Enabled = false;
_limit.Visible = true;
}
else
{
_addMacro.Enabled = true;
_limit.Visible = false;
}
}
2010-10-26 09:13:47 +00:00
private void MacroContainerEvent_Execute()
{
CheckLimit();
}
private void _addMacro_Click(object sender, EventArgs e)
{
for (int i = 0; i < _maxNumber; i++)
{
MacroEditor current = ((MacroEditor)this.ContentTemplateContainer.FindControl(ID + "macroeditor_" + i.ToString()));
if (!current.Visible)
{
current.Visible = true;
MacroContainerEvent.Add();
break;
};
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// And a reference to the macro container calls
//if (!UmbracoContext.Current.LiveEditingContext.Enabled)
//{
// ScriptManager sm = ScriptManager.GetCurrent(base.Page);
// ServiceReference webservicePath = new ServiceReference(SystemDirectories.Webservices + "/MacroContainerService.asmx");
// if (!sm.Services.Contains(webservicePath))
// sm.Services.Add(webservicePath);
//}
//else
//{
// ClientDependencyLoader.Instance.RegisterDependency("webservices/MacroContainerService.asmx/js", "UmbracoRoot", ClientDependencyType.Javascript);
//}
2010-10-26 09:13:47 +00:00
string script = "function " + ID + "makesortable(){ ";
script += " jQuery('#" + ID + "container').sortable({ update : function () { ";
script += " umbraco.presentation.webservices.MacroContainerService.SetSortOrder('" + ID + "', jQuery('#" + ID + "container').sortable('serialize'));";
script += " }}); ";
script += " ";
script += "}";
2010-10-26 09:13:47 +00:00
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), ID + "macrocontainersortable",
script, true);
2010-10-26 09:13:47 +00:00
if (!Page.IsPostBack)
HttpContext.Current.Session[ID + "sortorder"] = null;
2010-10-26 09:13:47 +00:00
ScriptManager.RegisterStartupScript(this, this.GetType(), ID + "initsort", ID + "makesortable();", true);
2010-10-26 09:13:47 +00:00
string sortscript = string.Empty;
2010-10-26 09:13:47 +00:00
string sortorder = string.Empty;
if (HttpContext.Current.Session[ID + "sortorder"] != null)
{
sortorder = HttpContext.Current.Session[ID + "sortorder"].ToString();
}
if (sortorder != string.Empty)
{
foreach (string temp in sortorder.Split('&'))
{
string number = temp.Substring(temp.LastIndexOf('=') + 1);
2010-10-26 09:13:47 +00:00
sortscript += "jQuery('#container" + ID + "macroeditor_" + number + "').appendTo('#" + ID + "container');";
}
}
if (sortscript != string.Empty)
ScriptManager.RegisterStartupScript(this, this.GetType(), ID + "resort", sortscript, true);
2010-10-26 09:13:47 +00:00
EnsureChildControls();
2010-10-26 09:13:47 +00:00
}
#region IDataEditor Members
public void Save()
{
2010-10-26 09:13:47 +00:00
string value = string.Empty;
if (HttpContext.Current.Session[ID + "sortorder"] != null)
{
string sortorder = HttpContext.Current.Session[ID + "sortorder"].ToString();
foreach (string temp in sortorder.Split('&'))
{
string number = temp.Substring(temp.LastIndexOf('=') + 1);
if (this.ContentTemplateContainer.FindControl(ID + "macroeditor_" + number) != null)
{
MacroEditor current = ((MacroEditor)this.ContentTemplateContainer.FindControl(ID + "macroeditor_" + number));
if (current.Visible)
value += current.MacroTag;
}
}
}
else
{
2010-10-26 09:13:47 +00:00
for (int i = 0; i < _maxNumber; i++)
{
if (this.ContentTemplateContainer.FindControl(ID + "macroeditor_" + i.ToString()) != null)
{
MacroEditor current = ((MacroEditor)this.ContentTemplateContainer.FindControl(ID + "macroeditor_" + i.ToString()));
if (current.Visible)
value += current.MacroTag;
}
}
}
_data.Value = value;
}
public bool ShowLabel
{
get { return true; }
}
public bool TreatAsRichTextEditor
{
get { return false; }
}
Control IDataEditor.Editor
{
get { return this; }
}
#endregion
}
}