Click here to Skip to main content
15,885,244 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.3K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Collections.Generic;
using Signum.Entities;
using Signum.Entities.Basics;
using Signum.Utilities;
using System.Web.Mvc;
using Signum.Web.Properties;
using Signum.Web.Controllers;
using Signum.Entities.DynamicQuery;

namespace Signum.Web
{
    public static class NoteWidgetHelper
    {
        public static Type NoteType { get; set; }
        public static Func<IdentifiableEntity, INoteDN> CreateNote { get; set; }
        public static string NotesQueryColumn { get; set; }

        static object notesQuery; 
        public static object NotesQuery 
        {
            get { return notesQuery ?? NoteType; }
            set { notesQuery = value; }
        }

        public static int CountNotes(IdentifiableEntity identifiable)
        { 
            return Navigator.QueryCount(new CountOptions(NotesQuery)
            {
                FilterOptions = { new FilterOption(NotesQueryColumn, identifiable) }
            });
        }

        public static string JsOnNoteCreated(string prefix, string successMessage)
        {
            return "SF.Widgets.onNoteCreated('{0}','{1}','{2}')".Formato(
                RouteHelper.New().Action<WidgetsController>(wc => wc.NotesCount()), 
                prefix,
                successMessage);
        }

        public static WidgetItem CreateWidget(IdentifiableEntity identifiable, string partialViewName, string prefix)
        {
            if (identifiable == null || identifiable.IsNew || identifiable is INoteDN)
                return null;

            JsFindOptions foptions = new JsFindOptions
            {
                Prefix = TypeContextUtilities.Compose(prefix, "New"),
                FindOptions = new FindOptions
                {
                    QueryName = NotesQuery,
                    Create = false,
                    SearchOnLoad = true,
                    FilterMode = FilterMode.AlwaysHidden,
                    FilterOptions = { new FilterOption( NotesQueryColumn, identifiable.ToLite()) },
                    ColumnOptions = { new ColumnOption(NotesQueryColumn) },
                    ColumnOptionsMode = ColumnOptionsMode.Remove,
                }
            };

            JsViewOptions voptions = new JsViewOptions
            {
                Type = NoteType.Name,
                Prefix = prefix,
                ControllerUrl = RouteHelper.New().Action("CreateNote", "Widgets"),
                RequestExtraJsonData = "function(){{ return {{ {0}: new SF.RuntimeInfo('{1}').find().val() }}; }}".Formato(EntityBaseKeys.RuntimeInfo, prefix),
                OnOkClosed = new JsFunction() { JsOnNoteCreated(prefix, Resources.NoteCreated) }
            };

            HtmlStringBuilder content = new HtmlStringBuilder();
            using (content.Surround(new HtmlTag("ul").Class("sf-menu-button sf-widget-content sf-notes")))
            {
                using (content.Surround(new HtmlTag("li").Class("sf-note")))
                {
                    content.AddLine(new HtmlTag("a")
                        .Class("sf-note-view")
                        .Attr("onclick", new JsFindNavigator(foptions).openFinder().ToJS())
                        .InnerHtml(Resources.ViewNotes.EncodeHtml())
                        .ToHtml());
                }

                using (content.Surround(new HtmlTag("li").Class("sf-note")))
                {
                    content.AddLine(new HtmlTag("a")
                       .Class("sf-note-create")
                       .Attr("onclick", new JsViewNavigator(voptions).createSave(RouteHelper.New().SignumAction("TrySavePartial")).ToJS())
                       .InnerHtml(Resources.CreateNote.EncodeHtml())
                       .ToHtml());
                }
            }

            int count = CountNotes(identifiable);

            HtmlStringBuilder label = new HtmlStringBuilder();
            
            var toggler = new HtmlTag("a")
                .Class("sf-widget-toggler sf-notes-toggler")
                .Attr("title", Resources.Notes);
            
            using (label.Surround(toggler))
            {
                label.Add(new HtmlTag("span")
                    .Class("ui-icon ui-icon-pin-w")
                    .InnerHtml(Resources.Notes.EncodeHtml())
                    .ToHtml());
                                
                label.Add(new HtmlTag("span")
                    .Class("sf-widget-count")
                    .SetInnerText(count.ToString())
                    .ToHtml());
            }

            return new WidgetItem
            {
                Id = TypeContextUtilities.Compose(prefix, "notesWidget"),
                Label = label.ToHtml(),
                Content = content.ToHtml()
            };
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions