pin.js for IE9

Site pinning in IE9 presents a way to interoperate with the Windows 7 desktop and make a website feel more like a native app. This library adds missng wraps some of the sitemode methods in window.external and adds..

Get Started

<script src="pin.js"></script>
<script>
	pin.jumplist.title("Recently visited");
	pin.jumplist.add("Test Link " + (new Date), "#" + (new Date));
</script>

...which is an alternative to...

<script>
	if( ( "external" in window ) && ( "msIsSiteMode" in window.external ) && window.external.msIsSiteMode() ){
		window.external.msSiteModeCreateJumpList("My List Label");
		window.external.msSiteModeAddJumpListItem("Test Link " + (new Date), "#" + (new Date));
		window.external.msSiteModeShowJumpList(); 
	}
</script>

Which both give you something like this...

Before pinning - set some custom pin parameters

pin.name( string ) - Define the name of the application

pin.name("Development with IE9 pin.js");

pin.starturl( url ) - Define the URL which will be pinned

pin.starturl("./?start");

pin.tasks( [ { name:string, url:url, icon:url }, ... ] ) - Replace all the task list items with this list

pin.tasks([ 
	{title: "Tasks", url : window.location.href, icon : "favicon.ico" }, 
	{title: "Jumplist", url : window.location.href, icon : "favicon.ico" } 
]);

pin.jumplist

First of all we initiate a new list. We typically only want to when a pinned application is loaded, so wrap it in the condition if( pin.first ){...}

pin.jumplist.title( [title] ) - Get/Set the title of the jumplist

pin.jumplist.title( "My Jumplist" );
alert(pin.jumplist.title());

pin.jumplist.add( title, url [, icon][, pos] ) - Add item to the list

position is optional, e.g. setting the position to 0 the new item will be appended to the top of the list. Without it items are placed at the end of the list.

pin.jumplist.add("Test Link " + (new Date), "#" + (new Date));

pin.jumplist.add( object[] [, pos] ) - Add many items to the list at once

position is optional

object[] - is an array of objects containing the attributes title, url, icon(optional). E.g...

pin.jumplist.add([
	{title : "Jumplists", url : "#jumplist"},
	{title : "Tasks", url : "#tasks"},
	{title : "Buttons", url : "#buttons"},
]);

pin.jumplist.items() - Return an array of items in the jumplist

var s = '',
    a = pin.jumplist.items();

for( var i in a ){
	s += [a[i].title, a[i].url, (a[i].icon||'Image not defined')].join(', ') + '<br />';
}
document.getElementById('list').innerHTML = s;

pin.jumplist.remove( pos [, len] ) - Delete an items using position

This the secondparameter specifies an optional length to delete more than one item.

E.g delete the first item

pin.jumplist.remove(0);

pin.jumplist.clear() - Clear all items from the jumplist

pin.jumplist.clear();

pin.jumplist.replace( pos [, len], object[] ) - Replace items in the list

The second parameter specifies an optional length to delete more than one item before new items are inserted into their place. The number of items do not have to match.

E.g. replace first item from the list, pos=0

pin.jumplist.replace(0,{title:"Replaced at "+ (new Date), url: '#'+(new Date)})

pin.button (Thumbnail Preview Buttons)

pin.button.add( icon [, label], callback ) - Add buttons.

btnId = pin.button.add("tick.ico", "Tick", function(){alert("Hello");});

pin.button.update( btnId [, icon [, label]] [, callback] ) - Update buttons

pin.button.update(btnId, "banned.ico", "Banned", function(){alert("Goodbye");});

pin.button.hide( btnId ) - Hide button

pin.button.hide( btnId );

Now you try...

if( pin.pinned ){
	// Add a toggle button
	(function(){
		var toggle = true;
		pin.button.add('/winconnect/toobify/images/toob_play.ico','Toggle Play', function(btnid){
			// Change style
			toggle = !toggle;
			var s = (toggle?"play":"pause");
			pin.button.update(btnid,'/winconnect/toobify/images/toob_'+s+'.ico','Toggle '+s);
		});
	})();

	// Add a self-disappearing button
	pin.button.add('/winconnect/toobify/images/toob_next.ico','Hide Me',function(btnid){
		pin.button.hide(btnid);
	});
}

If the button is linked to a visual property in the page then you might like bring the page into focus, maximised and infront of other windows if it not already.

pin.button.add("tick.ico","Bring to forefront", function(){
	window.blur();
	window.focus();
});

pin.overlay (Overlay's)

pin.overlay( icon, descr [, ttl]) - Add an overlay

E.g. add an icon overlay for 3 seconds

pin.overlay("tick.ico","Tick", 3000);

E.g. switch periodically between two overlays

pin.overlay("bug.ico","Bug");
pin.overlay("banned.ico","Banned");

pin.overlay( icon, descr, 0) - Remove an overlay

pin.overlay("tick.ico","Tick", 0);

Add a bug and then remove after 3 seconds

var o = pin.overlay("bug.ico","Bug");
setTimeout(function(){
   pin.overlay(o, 0);
},3000)

pin.overlay(0) - Remove all

Remove all current overlays

pin.overlay(0);

pin.activate (Flash the taskbar icon)

pin.activate()

When testing this please ALT+tab to remove focus from your browser window

setTimeout(function(){
	pin.activate();
}, 3000);

pin.master (prioritise actions to one of possibly many open tabs)

pin.master() - Is this the master tab?

Use this as a condition to run code which would of otherwise been executed as many times as the user has tabs and windows open to the site. Very useful for updating shared features i.e. like the jumplist.

alert( pin.master() ? "master window" : "slave window" );

Master is a title that can be held by only one of the pinned window or tabs. Using this property will help reduce unnessecary duplicate actions. e.g. each page calling a server script to populate the Jumplist.

pin.master(true) - Make this window the master.

alert( pin.master(true) ? "master window" : "slave window" );

pin.master(false) - Abdicate, remove Master status.

If another window doesn't take over as master before calling pin.master() in this window then the window will be reassigned as master.

alert( pin.master(false) ? "master window" : "slave window" );

pin.masterReign - Define the period of master's reign in the absence of a call to pin.master().

// 120 seconds
pin.masterRegin = 120e3;

Properties

pin.pinable - Is the site pinable?

alert( pin.pinable ? "Yes this browser supports pinning" : "This browser does not support pinning" );

pin.pinned - Is the browser in sitemode?

alert( pin.pinned ? "Site is Pinned" : "Please pin this site");

pin.first - Is this the page loaded from pinned button.

alert( pin.first ? "First" : "Nope");

pin.pinnedUrl - Returns the absolute path of the page which was pinned.

alert( pin.pinnedUrl );

Pitfalls with the library and site pinning

  1. Non-UI Event If a button triggers a window.open it is caught in IE9 security

Resources

  1. http://msdn.microsoft.com/en-us/ie/dd797411.aspx