Tuesday, April 20, 2010

Javascript tooling

So I've been working with Javascript for a week or two now. I still loathe it. However I would not probably touch it at all were it not for


  • JQuery - Feels like a .net framework for javascript
  • JQueryUi - Ah a (ightweight custom control library
  • JQuerify - Inject jQuery onto pages that don't have it for making bookmarklets.
I've used Visual Studio 2010 Ultimate primarily for the script writing because of the code/syntax coloring and  built in (poor but still built in) {} () [] matching capabilities. Those aren't specific to Ultimate, or 2010 for all I know. 

For debugging scripts I've used
  • FireBug
  • Google Chrome's built-in developer tool bar (alot)
  • JsBin - online javascript collaborative debugger (seemed slightly buggy, but still worth it)
  • Javascript Lint - nice online static analyzer for syntax and other problems.
On top of those as base scripts to help with life
  • qTip - Nice jQuery plugin  tool tip script for web pages
  • Google's lovely api hosting content delivery network for jQuery, and jQueryUi.
    • Sidenote: apparently resizable, and who knows what else required I also reference "http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css"

Saturday, April 10, 2010

ClickOnce Bookmarklets...maybe click twice

I have a set of bookmarklets that go to my website, and actually use the current version of the bookmarklet. So the bookmarklet on the bar is just a pointer to a script out on my site. So I can update the bookmarklets to my heart's content without the end user having to do anything at all. Unless I move where the bookmarklets live on my site. So if your bookmarklet living space can be more permanent here's the cross domain bookmarklet injection code:

var e=document.createElement('script');

e.setAttribute(
'language', 'javascript');

e.setAttribute(
'token', '@testToken');

e.setAttribute(
'src', 'http://imaginarydevelopment.com/Sfc/Scripts/ClientScripts/AjaxXmlHttp.js');

document.body.appendChild(e);
void(0);

Notice the token code. That makes it so that in the child script I can pull in which user it is, and make posts to that user's account directly.

Friday, April 2, 2010

Macro Resources

So for any that live primarily never paying any attention to the fact that Visual Studio supports macros like me.  I thought in the spirit of my last post it would help to have some resources around other useful macros.

Additionally apparently macros can be auto triggered by 'Environment' Events such as solution open or close
Here's a nice list from the Stackoverflow community
  • Awesome Visual Studio Macros
    • Selected Text browser macros (whatever you have highlighted/selected in your code file)
      • google it
      • spellcheck the word
      • MSDN search
    • InsertNewGuidLiteral
    • AutoClose the start page when you open a solution
    • AutoOpen the start page when you close a solution
    • Automatic Build timer with every build to the output window
    • Dual monitor / Single monitor macro for switching back and forth on the fly
    • Outlining: Collapse to definitions but expand regions

Wednesday, March 31, 2010

VS2008 Macro problem

Maybe I'm slipping in my VB skills since I've been using C# exclusively for around 2 years now, but this seems like a major user unfriendly 'feature' in Visual Studio.

Macros do not work, nor expand into macro explorer if the module in the file doesn't have the same name as the file it is in.

So when I pasted Jeff Atwood's wonderful FormatCodeToHtml Macro into a macro file in the macro editor called FormatToHtml, nothing happened. The macro file was added to the MyMacros project, but none of the actual macros expanded under it. Google was not helpful since the terms "Visual Studio 2008 macro not expanding" didn't turn anything helpful up. Apparently my code snippet on code keep is the 2nd google result for "jeff atwood rtf to html" sweet!

And the real kicker is... Visual studio doesn't complain, it doesn't show an error. It does not show any output when you click build on the macro project. It gives you no hints whatsoever.

Wednesday, February 17, 2010

Enum Extensions

If you're a fan of Enums for readability in an application here are some resources for making them more usable.
The first item is used for places where you'd like some enum values to have a space in the name when displayed somewhere. You can use System.ComponentModel.DescriptionAttribute like so:

       [Flags]
       public enum PermissionType
       {

           Select = 1,
           Insert = 2,
           Update = 4,
           Delete = 8,
           Alter = 16,
           Execute = 32,
           [
Description("View Definition")]
           ViewDefinition = 64
       }
 
and here's the extension method to make it easily usable:
 
    using System.ComponentModel;
 
    /// from: http://www.moggoly.me.uk/blog/post/Enum-description-values.asp
    public static string DescriptionOrToString(this T value)
    {

        
var da = (DescriptionAttribute[])(typeof(T).GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false));

        
return da.Length > 0 ? da[0].Description : value.ToString();

    }

if you don't like the possible clutter of having every object have a DescriptionOrToString in your intellisense, change the signature to
 
public static string DescriptionOrToString(this T value) where T:struct

This method is for enumerating enum values that are used as [Flags] (where a single value can contain many different values in the same enum)

    
///
    /// Gets all combined items from an enum value.
    /// from: http://stackoverflow.com/questions/105372/c-how-to-enumerate-an-enum
    ///
    ///
    /// The value.
    ///
    public static IEnumerable GetAllSelectedItems(this Enum value)
    {
        
int valueAsInt = Convert.ToInt32(value, CultureInfo.InvariantCulture);

        
foreach (object item in Enum.GetValues(typeof(T)))
        {
            
int itemAsInt = Convert.ToInt32(item, CultureInfo.InvariantCulture);

            
if (itemAsInt == (valueAsInt & itemAsInt))
            {
                
yield return (T)item;
            }
        }
    }

The next post will contain a usage example for GetAllSelectedItems.