PDA

View Full Version : [CLOSED] Starting up with Neptune theme programatically



ingbabic
Oct 12, 2015, 4:11 PM
Hello,
Please can you give code sample how to start up application with Neptune theme programmatically. Let's say I have default Web.config (just as you get when adding nuget Ext.NET)


<extnet theme="Gray" licenseKey="** Ext.NET LICENSE KEY HERE **" />


and I want to change it not in config, but from code (for example user chooses Neptune theme and I remembered it in Cookie).

Daniil
Oct 13, 2015, 12:10 PM
Hi @ingbabic,

That might be set up in a Global.asax by

Session["Ext.Net.Theme"] = Ext.Net.Theme.Neptune;

There is also a way to set it globally for an entire application:

Application["Ext.Net.Theme"] = Ext.Net.Theme.Neptune;

And also just to mention - a ResourceManager's Theme might be set in a page's Page_Load.

ingbabic
Oct 13, 2015, 1:34 PM
Ok, I'll show working sample, but actually I don't like it. This sample on click of the Save button saves the chosen theme in cookie and in the same time changes the theme. Next time you start sample it will be opened in saved theme. What I don't like about this sample is that it actually look clumsy (especially because of the way I apply Neptune theme on startup) and I am sure that there is a better way to do this, but I don't know how to do it. This is made out of parts of the code from examples project with simplifications. So here it is.
Controller:


[DirectController]
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
HttpCookie temp = Request.Cookies["Temp"];
if (temp != null)
{
Response.Cookies["Temp"].Expires = DateTime.Now.AddDays(-1);
}
else
{
HttpCookie settings = Request.Cookies["myapp"];
if (settings != null)
{
ViewBag.themename = settings["theme"];
Theme theme = (Theme)Enum.Parse(typeof(Theme), ViewBag.themename);
ViewBag.themepath = MvcResourceManager.GetThemeUrl(theme);
}
}
return View();
}

public ActionResult SaveSettings(string theme)
{
Response.Cookies["myapp"]["theme"] = theme;
Response.Cookies["myapp"].Expires = DateTime.Now.AddDays(1);
X.Js.Call("themeChange", theme);
return this.Direct();
}

[DirectMethod]
public DirectResult GetThemeUrl(string theme, bool reload)
{
Theme temp = (Theme)Enum.Parse(typeof(Theme), theme);
this.Session["Ext.Net.Theme"] = temp;
if (reload)
{
X.Reload();
Response.Cookies["Temp"]["Reload"] = true.ToString();
return this.Direct();
}
return this.Direct(MvcResourceManager.GetThemeUrl(temp));
}

}


View:


<script>
var themeChange = function (theme) {
var reload = theme == "Neptune" || Ext.net.ResourceMgr.theme == "neptune";

App.direct.GetThemeUrl(theme, reload, {
success: function (result) {
themeSet(result, theme.toLowerCase());
}
});
};

function themeSet(themePath, themeName) {
var v = App.MyApp;
Ext.net.ResourceMgr.setTheme(themePath, themeName.toLowerCase());
Ext.defer(v.doLayout, 500, v);
}

Ext.onReady(function () {
var themepath = '@ViewBag.themepath';
var themename = '@ViewBag.themename';
if (themename == "Neptune") {
themeChange("Neptune");
}
else if (themepath.length > 0)
themeSet(themepath, themename);
});

</script>

@(
Html.X().Viewport().ID("MyApp").Layout(LayoutType.Border).Items
(
Html.X().Window().ID("AppSettings").Title("Application settings").Icon(Icon.Cog).Width(350).Closable(true).Resizab le(false)
.Items(
Html.X().FormPanel().ID("SettingsForm").Width(340).Frame(true).PaddingSpec("15 0 10 15").FieldDefaults(fd => { fd.LabelWidth = 75; fd.LabelSeparator = ":"; fd.InputWidth = 235; })
.Items
(
Html.X().ComboBox().ID("theme").Name("theme").FieldLabel("Theme")
.Items(new ListItem("Default", "Default"), new ListItem("Gray", "Gray"), new ListItem("Neptune", "Neptune"), new ListItem("Access", "Access"))
.SelectedItems
(
items =>
{
HttpCookie settings = Request.Cookies["myapp"];
string theme = null;
if (settings != null)
{
theme = settings["theme"];
}
else
{
theme = this.Session["Ext.Net.Theme"] != null ? this.Session["Ext.Net.Theme"].ToString() : "Gray";
}
items.Add(new ListItem() { Value = theme });
}
)
.Editable(false)
)
.Buttons
(
Html.X().Button().ID("SaveSettings").Text("Save").Icon(Icon.Disk)
.DirectEvents(de => {
de.Click.Action = "SaveSettings";
de.Click.FormID = "SettingsForm";
})
,
Html.X().Button().Icon(Icon.Cross).Text("Cancel").OnClientClick("#{AppSettings}.close();")
)
)
)
)


Please help me to make this better :)

Daniil
Oct 14, 2015, 9:44 AM
Agree, the current way is not ideal, because, at least, it causes an overhead page reload when it applies the theme initially.

I see, at least, two possible solutions.

1. Setting up a ResourceManager's Theme.

Controller

public ActionResult Index()
{
HttpCookie temp = Request.Cookies["Temp"];

if (temp != null)
{
Response.Cookies["Temp"].Expires = DateTime.Now.AddDays(-1);
}
else
{
HttpCookie settings = Request.Cookies["myapp"];

if (settings != null)
{
ViewBag.theme = (Theme)Enum.Parse(typeof(Theme), settings["theme"]);
}
}

return View();
}
View

@Html.X().ResourceManager().Theme(ViewBag.theme)

2. Setting Session["Ext.Net.Theme"]

Controller

public ActionResult Index()
{
HttpCookie temp = Request.Cookies["Temp"];

if (temp != null)
{
Response.Cookies["Temp"].Expires = DateTime.Now.AddDays(-1);
}
else
{
HttpCookie settings = Request.Cookies["myapp"];
if (settings != null)
{
Session["Ext.Net.Theme"] = (Theme)Enum.Parse(typeof(Theme), settings["theme"]);
}
}

return View();
}

ingbabic
Oct 14, 2015, 11:09 AM
Thank you very much.