06 May 2013

Nested Navigation With Umbraco

No Comments ASP.NET

Navigation can always be fiddly with Umbraco with XSLT. This template take two parameters, startNode and maxLevel. It recursively prints a hyperlinked list of nodes so you end up with a nested list of <ul>‘s going maxLevel levels deep from startNode.

Read more

05 Apr 2013

Generic Comparing and Merging of Lists

No Comments Programming

While writing the database synchronization code for a number of apps, I’ve found myself writing method after method of boilerplate which does more or less the same thing. This generic method uses a sorted collection comparer, which I can’t claim credit for. I believe it was originally from StackOverflow.

The SortedCollectionComparer compares two lists using an IComparer, performing one action when an item only exists in one list (eg: adding it to a combined list), and another action when an object is in both lists (eg: comparing last modified time).

This lets you write (relatively) little boilerplate to merge two lists:
This is done using an intermediate method to call SortedCollectionComparer.CompareSortedCollections.

public List<locationbase> SynchronizeLocations(string otherDbPath) {
    SqLiteDal otherDb = new SqLiteDal(otherDbPath);
    var merged = new List</locationbase><locationbase>();
    this.SynchronizeItems</locationbase><locationbase>(() => this.GetLocations(), () => otherDb.GetLocations(), merged.Add, (loc1, loc2) =>
        {
            if (loc1.LastModified > loc2.LastModified) {
                merged.Add(loc1); // first item newer
            } else {
                // either they're equal or the one in the second database is newer, either way we want the one from the second Db.
                merged.Add(loc2);
            }
    }, new LocationComparer());
    return merged;
}
</locationbase>

Read more

17 Mar 2013

Vanilla Forums to YAF.NET Conversion

No Comments Programming and WPF

Migrating data between applications is always a fiddly process. Ideally someone has already written a converter which allows you to import from a generic format (eg: XML) but in the case of my transition from Vanilla forums to YAF.NET, nothing existed already and I had to start from scratch.

First I tried using SSIS to take a flat file (exported from Vanilla) into SQL Server. This took the best part of a day due to all of the conversions involved and the difficult to parse format used by Vanilla. After a day struggling I eventually decided to give up and do it in .NET.

This WPF utility needs a little modification before it will work for you, it should at least give you a huge headstart in the process. I used SSIS to import the users so I’ll skip that step.

  1. Export the gdn_users (Users), gdn_discussion (Topics), gdn_category (Forums) and gdn_comment (Posts) tables using phpMyAdmin to XML.
  2. Open the Constants.cs in the solution and update the paths to point to these files.
  3. Compile and run the app and hit the ‘Go’ button
  4. Disco

This will import your forums, topics and posts from Vanilla. The only thing it won’t do is correctly arrange subforums, so you’ll still need to re-arrange the forums afterwards to re-nest forums. The main thing for me was getting the post data across, but the app could easily be extended to import any other important data from Vanilla.

10 Mar 2013

jQuery Toolbar Placeholder Plugin

No Comments JavaScript

In creating a templating system for people to insert placeholder text, I wanted a toolbar with buttons which would insert text at the position of the cursor when clicked.
jQuery toolbar plugin
This plugin takes an array of button IDs and the text to be inserted when each button is clicked. EG:L

var buttons = [
	{ 'buttonId':'btnName', 'insertionText':'%%NAME%%' },
	{ 'buttonId':'btnAddress', 'insertionText':'%%ADDRESS%%' },
];
$('#toolbar').hypotoolbar(buttons, 'txtBody');

When the btnName and btnAddress buttons are clicked, %%NAME%% and %%ADDRESS%% will be inserted into the textarea with ID txtBody at the cursor position.
Read more

22 Feb 2013

jQuery Text Progress Meter

No Comments JavaScript

This small plugin will display a progress meter which fills up as text is entered into an input or textarea. Useful for indicating to a user how much remaining text they have in a field with a maximum length.

jQuery.textprogress Demo

Options

  • messageFormat – The format for the count label. Available placeholders are {current}, {total} and {percent}
  • showCounttrue/false – Whether or not to display a total of the current length/maximum allowed length.
  • max – The maximum allowed length
  • allowOverLengthtrue/false – Whether or not to prevent more text being entered than allowed.
  • classes An object containing two keys:
    • standard – The progress bar class if less than the allowed length of text is entered.
    • over – The class if equal to or more than the allowed text length is entered
  • textbox – The textbox/input element to measure.

Demo
Source on GitHub

17 Feb 2013

Bootstrap CSS Image Buttons

No Comments JavaScript and Programming

One of the repetitive things I found myself doing with Twitter’s Bootstrap framework was creating buttons which had a background gradient and an image. (Doesn’t work in < IE9, but does in Fx, Chrome and Safari).

This makes use of the multiple backgrounds CSS3 property. The CSS I was typing every time was this (eg: for an ‘add’ button):
Read more

17 Feb 2013

Working with IsolatedStorage

No Comments Windows Phone 7

IsolatedStorage lacks some of the convenience methods which FileInfo and DirectoryInfo provide. These are some methods which simplify the process.

  • GetAllFilePaths – Recursively gets a list of all files in IsolatedStorage.
  • GetSafePath – Gets the usable safe path for a file/directory in IsolatedStorage.
  • GetLastWriteTime – Gets the last write time for a file/directory in IsolatedStorage.
  • GetFileSize – Gets the size of a file in IsolatedStorage.

Code after the jump.
Read more

05 Feb 2013

Custom logging with Log4Net

No Comments Programming

Using log4net, it’s possible to log to a number of different sources such as ADO.NET, the console or a text file. This works fine if the parameters for logging (eg: username, password) are static and don’t change, but you run in to difficulties when trying to use dependency injection. One way to get around this is to add a custom appender which picks up your configuration before logging.

Read more

24 Jan 2013

Synchonising a Textbox and Checkbox List

No Comments ASP.NET and JavaScript

Choosing a selection from a list of items is fine when there are only a few choices, but if there are ~100 or more options, can be unsightly on a page.

It’s better to give the user the option to type a code if they know it or browse the list if they don’t. For this, I recently implemented a jQuery Dialog which keeps the contents of a TextBox in sync with a list of checkboxes. The open and close events of the dialog are used to keep the textbox and checkbox list in sync.

Read more

28 Dec 2012

Templating with Spark and WPF

No Comments ASP.NET and WPF

Generating text from a template can be a powerful tool to make markup cleaner and simplify creation of emails or other text.

I needed to format a template from WPF, and after trying in vain with ASP.NET’s Razor engine I eventually found Spark, a templating engine which, with very little effort, enables formatting a template to produce a string.

Read more