Wednesday, 30 December 2009

Google App Engine: paging with inequality filters

I recently implemented paging for my Google App Engine app, Braineos, ordering results using a unique inequality filter. My implementation is based on Joe Gregorio's Paging through large datasets tutorial, but tweaked in order to overcome apparent flaws in Gregorio's implementation and to meet the search result ordering requirements of my application.

Paginating results has important performance implications. While GAE limits results sets to 1000 and it's unlikely that a user will ever require more than a 1000 results, fetching up to the maximum number of results is less optimal that fetching a smaller defined limit with each query: pagination is included in Google's Best practices for writing scalable applications.

Gregorio describes several means of ordering paginated results, and the paging on a unique property approach was most appropriate for the needs of my application: I wished to order user generated content first by the number of positive 'votes' the content received and second by the most recently created content.  This approach uses shard counters to enable the creation of a unique key as well as quick updates to the datastore. Gregorio's Contributor class and unique 'when' property creation were added to my code base (the when property added to the model object representing the user generated content)[1]:

class Contributor (db.Model):
count = db.IntegerProperty(default=0)

def _unique_user(user):
email = user.email()

def txn():
contributor = Contributor.get_by_key_name(email)
if contributor == None:
contributor = Contributor(key_name=email) 
contributor.count += 1
contributor.put()
return contributor.count

count = db.run_in_transaction(txn)

return hashlib.md5(email + '|' + str(count)).hexdigest()

def whenfromcreated(created):
return created.isoformat()[0:19] + '|' + _unique_user(users.get_current_user())

My search module then used the 'when' property when ordering results sets:

def find(self, searchTerm, pagesize=configuration.Configuration.PAGESIZE):
searchTerm = searchTerm.split(',')
searchTerm = [term.strip().lower() for term in searchTerm]       
query = UGC.all()
for term in searchTerm:
query.filter('searchTerms =', term)
ugcs = query.order('-when').fetch(pagesize + 1)
  return ugcs

def findNext(self, searchTerm, next, pagesize=configuration.Configuration.PAGESIZE):
searchTerm = searchTerm.split(',')
searchTerm = [term.strip().lower() for term in searchTerm]    
query = UGC.all()
for term in searchTerm:
query.filter('searchTerms =', term)
query.filter('when <= ', next).order('-when')
ugcs = query.fetch(pagesize + 1)
return ugcs

This implementation fitted with the app's existing shard counting implementation and provided an excellent starting point. I came up against two issues. First, appending the count to the user's email before digesting with MD5 resulted in unreliable desc ordering. Second, because the 'when' property is used as an inequality filter, ordering must first be on that property (so I couldn't first order my results on 'votes' and then 'when').

I worked around the first issue by appending the count value after the user's email was digested:

return hashlib.md5(email).hexdigest() + '|' + str(count)


I worked around the second issue by prepending the number of votes to the 'when' property:

def whenfromcreated(created, votes=0):
return votes + '|' + created.isoformat()[0:19] + '|' + _unique_user(users.get_current_user())

This approach appeared to behave as expected, however my initial unit test used a sample of less than 10 UGC objects and it became apparent that once a user's contributions exceeded nine, desc ordering would be inaccurate: a unique user string ending in '|9' is deemed greater than a string ending in '|10'. Happily a fellow GAE app developer encountered a similar problem when attempting to use int values in a str comparison and suggested converting the int values to hex to allow alphabetical comparison.[2] I altered the code to wrap votes and count in the hex() factory function and my unit test, which seeks to order 10 items into two pages, is now passing.

There is significant discussion around pagination in GAE and while my implementation seems to stick pretty close to Google's reference implementation, if others have devised more elegant solutions, I'd be interested to know more - comment below!

[1] Gregorio's full code sample is available here.
[2] See Paging - ordered by a signed integer

Sunday, 4 October 2009

FOWA 2009: warrior principles of start-ups

Life is fighting. I have stronger belief in this maxim everyday of life's journey. I am a 'fighter' of sorts, having worked in a few BJJ competitions between knee injuries, and certainly martial arts, since I started training in '98 and increasingly over the past 4 to 5 years especially the art of Brazilian Jiu Jitsu, has been the prism through which I understand the world. 'Fighting' is, as I've touched on previously, a many splintered thing and the principles I understand as fundamental for tearing it up on the mats, for me, underpinned FOWA's second day of talks.

It is difficult to separate out three distinct principles, as in practice, they are organic to one another and represent articulations of different means of overcoming the limitations of ego. With that in mind, I propose three Warrior Principles for Start-ups/Cheerleaders/Zoo Keepers and Your Mom, because really they are grand notions that run through everything. Remember, life *is* fighting.

1 Be in the now

2 Be fearless

3 Be committed

I'll take the third principle first. Be committed. In tech community parlance this translates as 'passion'. This really came through for me during Alex Hunter's discussion of brand savvy. His message in a nutshell: there's no room to half-ass it, believe everyday that your project is awesome and let that standard of excellence and personal investment drive your business activities. Or, as Coach says, 'if you believe in your sweep, more than he believes in his base, you'll get it'. This isn't some sort of Jedi mind trick that magically teleports you to the top of Google's page rankings or mashes your opponent's side into the mats, rather absolute trust in your technique allows you to deliver it with powerful conviction and if your competitor is that much less confident, well then, you just might take their back and sink in an RNC (oh the joy!). Total commitment could be considered an expression of an unselfconscious state of being, where fears of failure or timidity associated with looking like a douche don't come into play. This state can best be achieved when fearlessly existing in 'the now'. Only with girded loins and a solid heart can we move forward to embrace change with positivity.

Lynne D Johnson's consideration of the 'future of print' and Gary Vaynerchuck's thoughts on, well, 'crushing it', for me, particularly stressed the importance of adapting to change and rolling with the punches. What does this mean, in practice? I turn to the rubric of BJJ for understanding. When starting my BJJ journey, I, like white belts the world over, found safety and comfort in holding on to positions that were long past their expiry. This is a manifestation of fear and the misguided desire to impose our will on a fluid situation rather than grappling with the opportunities as they are, there and then: see Gary's advice to 'stop your cryin' and exploit the opportunities that *are* there, rather than fixating on those that aren't. As I've metamorphosed into blue belt over the past year, fear remains an issue for my game, but chasing long dead positions is far less prominent in my rolling than in the early days. Fear of change leads to an inability to adapt and, well, you adapt or die. The more I am fully invested in the now, and the more I capitalise on immediate opportunities the more I am able to fight well and this allows me to win. Winning isn't just tapping someone out, though sometimes that is an effect, but it is coming away from the experience with the most personal growth. Okay, a bit 'new age' perhaps, but as Vaynerchuck noted he is, 'always about the process not the results'. It is the means, not the ends, that make us stronger and impart the confidence and skills to deal with change as it presents itself.

So, how did I enact these Warrior Principles at FOWA? I reverse stage-dived the 'Kevin and Gary Show' (a Q&A session with Kevin Rose and Gary Vaynerchuck). Gary asked for a show of hands, 'who has a start-up', and then invited us on stage, or at least the first one that could get there. I looked to my left and apart from my business partner there was one dude at the end, but I was calculating this as I jumped the dude's legs and laptop bag. I'm turning and running at a good clip towards the stage and glimpse movement out of the corner of my eye and only then realised; the steps are to the left, there's someone headed for them and stairs are for chumps. I'm taking the most direct route. A parallel process, also spawned, is determining if there are stairs at the front of the stage, without my glasses I can only make out what appear to be tiers, no, not tiers just detailing; gonna have to vault. My bod picks up pace and I'm putting my hands above me onto the stage and jumping to get my hips over, *knowing* that if I get my hips over the ledge I'm in. Swing my leg up and over, and boom, first on stage. Recognise!

Yes, I flashed my ass to the audience as my dress was not built for scaling stages. Yes, I was totally unprepared to pitch Braineos. Yes, my domain wasn't even configured properly and we're still frantically trying to tighten up our core features and implement our beautiful Safetygoat design in order to push to private alpha in 6 to 8 weeks. But, 'big whoop', these things meant nothing; I was completely committed, I was in the now and fearless. I saw the position and I went for it without *thinking* without the limitations of ego, just was and did in unison. This is what it is all about. Man oh man, I rejoice that I am a grappling-geek.

See ya next year!

Friday, 2 October 2009

Sausage Fest, strike that, FOWA 2009

The 'future of the web' a grand vision that is being considered and debated at this week's FOWA. You could be forgiven for assuming the 'future of the web' is both white and male – the demography of Internet engineers/web developers/IT professionals remains resilient to change. Wider, unchallenged social gender biases were especially prominent in Osama Bedler's (PayPal) presentation. While Bedler's talk on embracing change and moving forward with positivity into a cashless future was well delivered, inspired and fun, the promotional clip from PayPal depicted an intransigent world where women retained greatest responsibility for getting the shopping in and 'mom still made all the meals'. Yeah, way to challenge 'the now' and envision a liberated future. Yet, the conference has included some exciting technology announcements in a convivial atmosphere in West London.

Day 1 included some fantastic talks. Highlights for me? I took a lot from Mike McDermot's (FreshBooks) perspective on priority marketing systems required for even the most scope-trimmed start-ups; this is not my area of expertise and conferences, especially Think Visibility, have been a great source of Internet-focused marketing information. Bruce Lawson's (Opera) discussion and demonstration of HTML5 was thrilling and has me itching to implement new rules in my app's mark-up. Chris Thorpe's (The Guardian) talk on how The Guardian is seeking to adapt to a changing world through 'mutualisation' included a lot of lessons for industries/organisations clinging to outmoded business models and pointed to the prudence of embracing change and innovation in telecommunications. Aza Raskin (Mozilla) finished the evening with pizazz, with a thrilling discussion and demonstration of the future of browsers as Mozilla understands. Mozilla, again, is pushing a sound agenda of personal ownership and individual liberty by innovating a 'you-centric' browser. Inspiring, cogent stuff; not to mention the semi-gratuitous bondage pic included in the presentation appealed to my sense of fun.

With highlights come lowlights. FOWA has many corporate sponsers – a pro and a con – and the panel talk on experiments with building in ASP dot net MVC left me cold; while the tech sounded decent, though the panel's developer (Matt Lee, Redgate) noted it represented more of a 'catching up', it all seemed a bit staged and infomercial. Similarly, the breathless, chirpy presentation for Facebook (Cat Lee) regarding FB Connect was not up to par. The rep delivered what seemed in many ways to be a 'canned talk'; the announcement of a Connect API was exciting and the tech looks interesting, but the delivery did not seem at all appropriate to the audience. While I absolutely agree with Addison Berry's (Lullabot) message that employers benefit from facilitating passion through transparent communication, I was a bit disappointed by her talk which vaguely alluded to all the 'cool shit' and 'crazy shit' she was able to do working in Open Source. What is this 'amazing shit' – no idea – specific anecdotes/examples would have left me with less of an impression that she was 'talkin' shit'.

Day 2 is off with a bang! Britt Selvitelle (Twitter) threw out two points of note: 1) an upcoming release of Twitter Labs (currently in development); 2) the notion of whacking in an API with start-up alpha releases (we're on the same page, dude!). Simon Wardley's talk on cloud computing was hilarious - yes, really - informative, radicalised and astute. Following Wardley's discussion of uBuntu's drive for standardisation in provision of clouds (to drive innovation and retain Internet freedoms, versus constraint through lack of interoperability or a vendor's ownership of a reference model), Joel Moss (Codaset) and Sanj Matharu (Vodafone) demonstrated the ease of mobile widget (mobile apps to you and me) creation using Vodafone's API; these babies are made in HTML, CSS and JavaScript. There's a one million Euro prize fund for great apps, so get on it.

My morning break is coming to a close. All I can say is am looking forward to Vaynerchuck this evening – fuck yeah!

Wednesday, 17 June 2009

Pretty URLs: Routes in Google App Engine

Pretty URLs are an essential element of web applications. Pretty URLs are helpful to users, influential for SEO and implementations may enhance the beauty (and so the extensibility and maintainability) of a code base. This is superbly achieved in Google App Engine using Rodrigo Moraes' App Engine recipe for URL mapping using Routes.

I discussed in a previous post how I used a basic implementation of GAE's WSGIApplication for Pretty URL mapping. While this approach served my initial needs, it required a lot of extra logic to read params from incoming URLs and did not elegantly handle URLs invoked in an AJAX call (i.e. logic was need in the handler to interpret the URL and delegate to the appropriate action). My goal was to implement a Pretty URL mapping scheme that allowed me to:

-configure URL mappings in one place only
-gracefully pass params on URLs
-invoke actions contained in my handlers

Happily, I stumbled upon Moraes' recipe; it was a snap to follow the very clear directions to import Routes and override the WSGIApplication. Further support from the Routes documentation allowed me to quickly and easily meet all my Pretty URL goals.

All URL mappings are configured in routing.py. Params passed on URLs are discretely mapped and retrieved. For instance this mapping accepts a param named 'id' and hits a handler in views.py called 'MyHandler':

map.connect('handler/:id', controller = 'views:MyHandler')

In views.py, 'id' is picked up as an argument of the get() method:


class MyHandler(ParentHandler):
def get(self, id): ...


How cool is that?! Crisp and clean and no caffeine. It gets better.

I can also map to a handler and an action. This is particularly handy for my api.py, which accepts AJAX requests. With this mapping the correct handler's action is invoked without any messy delegation at the top of my api.py:

map.connect('api/handler/action', controller = 'api:MyHandler', action = 'action')

How great is this? Pretty URLs and cleaner code. Yes, I've added the Routes resources and Moraes' overridden WSGIApplication, but the separation of mapping logic from other aspects of the application pleases me and I believe makes for a more robust code base. Not to mention, I learned a new thing! Thanks Mr Moraes, my URLs got pulchriTUDE!

Tuesday, 24 March 2009

Ada Lovelace Day: my tech heroines

My own career in technology has been inspired by several women in technology and engineering; my tech heroines include my mother, Linda Ziegler, my mother-in-law, Julia Russell, and two former work colleagues, the designer Kat Neville and the Java developer Magz McCarthy.

In some key ways, my own career path reflects my mother's. Linda started her undergraduate degree with the aim of becoming an historian, but as the limited opportunities in this area became more apparent to her, she determined to train as a chemist and worked as a manufacturing chemist in the plating industry. Similarly, I attained a PhD in British History and Economic and Social History and pursued an academic career for some years; at the end of my ESRC Post-Doctoral Fellowship, at which stage I hadn't successfully interviewed for one of the three positions in the nation in my field, I determined that I had to make a change and decided to do a 'conversion course', or a one year Masters in Information Technology. My mother's personal history of moving between a humanities and science discipline has always been part of my understanding of her, and was an important part of my inspiration to 'do what I had to do' in terms of getting the sort of training that would give me access to decent labour opportunities. I should, however, note here that my partner's example - he has a Bachelors in English Language Linguistics and a Masters in Cognitive Science and learned Java 'on the job' during the turn of the century Dot Com Boom - as well as his practical assistance as tutor was further integral to my attaining my MSc in IT with Distinction from Queen Mary, University of London.

Upon completing my MSc, I worked firstly as a Research Assistant in the Computer Science Department of Queen Mary and then as a Java web application developer at Xbridge/Simply Business and then its child company, BView, but it is only over the last year, working for myself building my own application that I've started to feel confident and passionate about working in Internet tech. It had been a difficult transition from a career path I had invested in so heavily and had been deeply passionate for to one I'd entered out of pragmatism.

My flourishing passion and joy for my work has been encouraged in no small way by Julia, Kat and Magz. Jules has a great love of gadgetry and Internet technology and stands out as a 'woman of a certain age' with such an appetite and ability to learn to use the latest tools and gizmos. She's been building web sites since the early days of the web and has recently been recognised as setting a gold standard of best practice for school websites (she works part time at her local primary school). Jules used Google Sites to create a user-friendly and professional site to communicate the school's happenings to the community. Indeed, Jules represents a quirky blend of 'good life hippy' and technologist and her home, nestled in the Scottish Highlands, is aptly called 'the electronic cottage'; much of her work is home-based and includes web administration for an international development organisation and desk top publishing for local authors. Her willingness to investigate new technologies and to understand and advocate the appropriate uses of different Internet medium such as the different uses of blogs versus forums for the benefit of less informed colleagues, is impressive and unique even without considering her demographic. The tenacity and fearlessness with which Julia approaches technology is entirely admirable and never ceases to amaze (and sometimes to shame) me.

At a different end of the women in IT spectrum are Kat and Magz, both of whom are in their twenties and so are my 'juniors'. Working with Kat and Magz helped to push me out of my 'funk'. I'd been indulging, without being fully aware of it, in a rather self-pitying attitude towards working as a technologist after splitting from my first love, history. These two women were full of fire, creativity, enthusiasm and tremendous smarts and helped me wake up to the fact that I was working in an exciting field, full of imaginative, clever and fun people. Kat's amazing adaptability - she was hired as a pure designer but has since developed an impressive set of UI coding skillz to complement her artistry - inspired and impressed the whole development team and taught me to enjoy the challenges of problem-solving, rather than getting so easily down and frustrated. My first memory of Magz was a story of a previous job where she'd worked some impressive Tomcat-clustering magic. I was immensely impressed by this tale and by the confidence, competency and intensity with which she approached every day programming problems. These two women were my first female-developer role models; while I was notoriously 'the first female developer' hired at Xbridge (this is how the CEO once introduced me to a potential hire, much to my horror) and though I worked with an absolutely fantastic group of guys, I was a bit alone in the wilderness until Kat and Magz arrived; they taught me to revel in my work and that, actually, Internet tech is a pretty kick ass place to be!

Monday, 23 March 2009

Pretty URLs in Google App Engine

Today I implemented so-called 'pretty urls' for my Google App Engine application. While my URL structure was reasonably clean before this work, I was inspired to make improvements after attending ThinkVisibility and particularly after talks by Rob Lee and Tom Smith. While it wasn't too painful to track down information on how I might achieve Pretty URLs in GAE, it did take a little trial and error to get running, so I thought it worth a post. Indeed, if readers have suggestions for a more elegant solution, I welcome comments.

What is a Pretty URL? A Pretty URL is a URL designed to be 'hackable', or human-readable. Ideally, users should be able to intuit a Pretty URL address, and likewise a Pretty URL might serve as a 'bread crumb' or 'sign post' indicating to the user where s/he is in the application. Compare, for example, a less than fetching URL (a) with a more pleasing example (b):

a) mysite.com/mycategory?myparam=an_entity_id
b) mysite.com/mycategory/an_entity_id

Example 'b' is just the sort of URL I desired; purdy, ain't she?

I chose to use my WSGIApplication's constructor to point Pretty URLs at appropriate Handlers:

application = webapp.WSGIApplication([('/category/.*', CategoryHandler)], debug=True)

The 'category' is associated with a particular Handler, while the '.*' regex matches the UID of the entity to be retrieved, which is passed on the URL. In the Handler's get function I retrieve the path instance variable include on the Request object, which returns a substring of the URL between the host name and query parameters. I then substring the path string in order to retrieve the key of the entity requested by the user:

path = str(self.request.path)
key = path[6:len(path)]
if key:
#Retrieve the entity and shoot back a response


This appears to be working well and as expected; I'm certainly blushing every time I lay eyes on one of my app's sweet little URLs.

(Readers may wish to view Google's doc on URL config; note if you try copying and pasting the syntax in their example you'll get exceptions, hence the trial and error aspect of my implementation.)

Friday, 20 March 2009

Dynamically load YUI dependencies

I've been using YUI Grids and LayoutManager for the backbone of my app's interface since the end of last year. It's been a steep learning curve and I still consider myself very much novice and, indeed, only noticed this week the 'dynamic loading' tab on the YUI Configurator. Rather than statically including the required YUI CSS and JavaScript resources, it is possible to use YUILoader to dynamically import them on load. While I appreciate that YUI-experts will not be impressed by my YUILoader-epiphany, this approach has helped me to slim down my app's JS files while decreasing maintenance concerns and so I feel is worth mentioning for the benefit of other noobs.

As described on the Configurator page, it is possible to define the required resources in YUILoader's constructor and to tweak YUILoader's behaviour to suit your application. I found two constructor arguments particularly useful, 'base' and 'onSuccess':


var loader = new YAHOO.util.YUILoader({
base: "//YUI build dir",
require: ["dom","element","event","fonts","grids","layout","reset","selector"],
loadOptional: false,
combine: true,
filter: "MIN",
allowRollup: true,
onSuccess: function() {
//Stuff that needs doing on load, e.g. running LayoutManager
}

});


YUILoader API documentation claims that the String value of base is the directory used by YUILoader when loading dependencies. However, I have noticed that no matter what directory location I pass as base, YUILoader fetches resources using the directory used to import YUILoader. That is, if I've imported src="http://yui.yahooapis.com/2.7.0/build/yuiloader/yuiloader-min.js" and set base to "http://yui.yahooapis.com/2.6.0/build/", YUILoader sucks down 2.7.0 versions of my required resources, whereas if I change the directory of the YUILoader import to 2.6.0 I get resources corresponding to this version. Perhaps I am missing something in how this all works, but I would expect to be able to override the YUILoader's build version by passing a value to base. On the upside, it is possible to control the resources gathered by YUILoader so, once you've gone to production, your app doesn't spontaneously upgrade with potentially dire consequences.

In contrast, I've found the onSuccess argument reliable for running the JavaScript used to render my app's display, i.e. to instantiate and run a LayoutManager. Previously I had included a script on every page to run LayoutManager, and have now been able to reduce the page-weight of my app by one file (before I've even started really polishing my frontend optimisation).

Finally, I've used YUILoader's insert() function to demand that only the JS, and not the CSS, dependencies are fetched:

var options = [];
loader.insert(options, "js");


I've overridden some of YUI's skin-sam CSS in my own CSS file which is imported after my third party resources. However, because the YUILoader inserts dependencies at the end of the page, my customisation of YUI's CSS was negated by a second and freshly imported batch of CSS dependencies. So, I've told YUILoader to just bring me the JS and I'll have to update the CSS version manually (unless of course some wise reader can suggest a more optimal solution).

Happy ui-coding.

Thursday, 12 February 2009

Dav Glass to the rescue or the curious case of the hidden border-right

The YUI Grids and LayoutManager issue described in a previous post has been fully resolved with the kind assistance of Dav Glass, LayoutManager developer and Twitter buddy. Thanks brother!

As suspected, the issue was rooted in my noob-i-licious implementation rather than in the YUI tools themselves. Early-stage work-arounds came back to haunt me, and Dav kindly made the necessary adjustments to my code. There were two main flaws with my implementation: 1) manually added wrapper divs and classes (these are applied automagically onload); 2) missing override to YUI class. So, the mark-up ends up as:

div id="custom-doc" class="yui-layout"

div id="hd" class="myHd"

div id="bd"
div id="yui-main"
div class="yui-b"
div class="yui-gc"
div class="yui-u first"Lorem ipsum dolor lacus feugiat.
div class="yui-u"Lorem ipsum dolor sit amet, acus feugiat.

div id="ft"


How tidy, clean and sexy is that?! Marvellous. Now, just to add a bit of CSS to customise the YUI resources to my app's look and feel:

.yui-layout-unit-center .yui-layout-wrap {background-color: #ffffff; border: 3px solid yellow;}

Score! Is great to get the code tighter and, of course, behaving as expected. Feeling evermore pumped for London Twestival this evening, as once again, in spite of what the nay-sayers and sceptics may say about social networking on the inter-web, the gloriousness of the enlightened telecomms age we live in has saved my proverbial bacon.

Wednesday, 11 February 2009

YUI Grids and LayoutManager: border-right of .yui-layout-unit-center doesn't render

In the process of developing my app I realised that my layout needed a slight overhaul. My goal is to increase the width of my main content area and to dispense with the right-hand nav bar in favour of tabs above the main content area (to be implemented using jQuery UI). I've created a template UI using YUI Grids with LayoutManager and I'd like to define a custom width for my content pane and give the main content area a white background and yellow border. I believe that I've faithfully followed the directions given with the YUI Grids + LayoutManager example, but I am unable to get the border-right to render in either FF3 or IE7. I've stripped the page right back to first principles, hoping to resolve the error with a simplified page, but have only succeeded in reproducing the error. Here she is:

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"

html

head
title YUI Base Page /title

link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.6.0/build/reset-fonts-grids/reset-fonts-grids.css
&2.6.0/build/layout/assets/skins/sam/layout.css"

style type="text/css"

body {margin: 0; padding: 0; font-family: Arial, sans-serif; font-size: small; color: #33333; background-color: #6699cc;}

#custom-doc {margin: auto; text-align: left; width: 88.46em;}

#hd, .yui-layout-unit-top {background-color: #6699cc;}

.myHd {background-color: #6699cc; margin-left: 1.92em;}

#bd, .yui-layout-unit-center {background-color: #ffffff;}

.yui-layout-unit-center {border: 3px solid yellow;}
/style

/head

body

div id="custom-doc" class="yui-layout"
div class="yui-layout-doc"

div class="yui-layout-unit yui-layout-unit-top yui-layout-noscroll"
div class="yui-layout-wrap"
div class="yui-layout-bd yui-layout-bd-nohd yui-layout-bd-noft"
div id="hd" class="myHd" /div
/div
/div
/div

//This the main content area
div class="yui-layout-unit yui-layout-unit-center"
div class="yui-layout-wrap"
div class="yui-layout-bd yui-layout-bd-nohd yui-layout-bd-noft"
div id="bd"
div id="yui-main"
div class="yui-b"
div class="yui-gc"
div class="yui-u first" Lorem ipsum dolor lacus feugiat./div
div class="yui-u" Lorem ipsum dolor sit amet, acus feugiat./div
/div
/div
/div
/div
/div
/div
/div

div class="yui-layout-unit yui-layout-unit-bottom yui-layout-noscroll"
div class="yui-layout-wrap"
div class="yui-layout-bd yui-layout-bd-nohd yui-layout-bd-noft"
div id="ft" /div
/div
/div
/div

/div
/div

type="text/javascript" src="http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js
&2.6.0/build/element/element-beta-min.js
&2.6.0/build/selector/selector-beta-min.js
&2.6.0/build/layout/layout-min.js"

(function(){
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event;

Event.onDOMReady(function() {
var layout = new YAHOO.widget.Layout('custom-doc', {
height: Dom.getClientHeight(), //Height of the viewport
width: Dom.get('custom-doc').offsetWidth, //Width of the outer element
minHeight: 150, //So it doesn't get too small
units: [
{ position: 'top', height: 70, body: 'hd' },
{ position: 'bottom', height: 25, body: 'ft' },
{ position: 'center', body: 'bd', grids: true }
]
});
layout.on('beforeResize', function() {
Dom.setStyle('custom-doc', 'height', Dom.getClientHeight() + 'px');
});

layout.render();

//Handle the resizing of the window
Event.on(window, 'resize', layout.resize, layout, true);
});
})();

/body
/html

I suppose I am missing some very fundamental point, but I've nearly exhausted the tricks up my sleeve to get this baby rendering as I'd expect and would be grateful for any insight from more expert UI developers. If and when, dear reader, I crack this nut I'll be sure to post my solution.

Friday, 23 January 2009

jQuery min-height problem in IE7

I mentioned in my previous post that I was having trouble with jQuery Dialog's height in IE7, which curiously shrank or enlarged with alternate open/close events. Further investigation using the IE and FF web developer tools revealed that an in-line style, dynamically generated by jQuery, was setting the Dialog's min-height property to either 0px or 56px with every other opening. The presence of this in-line style certainly explains why every attempt to override the height and min-height attributes in the CSS or with jQuery's CSS functions failed to produce the desired display behaviour. My solution, which the developer tools in both browsers indicate is keeping minimum height at zero for every opening thus allowing the height default 'auto' to do its good work, is to use jQuery to set the min-height after the Dialog has been instantiated and opened (remember, dear reader, IE7 freaks out if you attempt to set the height or minHeight options in the Dialog constructor as per the API documentation).

So, first instantiate your Dialog, passing in those constructor arguments acceptable to IE7:


if($.browser.msie){
$("#confDialog").dialog({autoOpen: false, bgiframe: true, buttons: buttons, modal: true});
}


Next open the Dialog and reset the min-height attribute to zero:


$("#confDialog").dialog("open");
$(".ui-dialog-content").css("min-height", "0");


Because, as it seems, it is the open process that dynamically adds the in-line min-height to the Dialog's content area mark-up, it is necessary to reset this property after invoking Dialog's open function.

Thursday, 22 January 2009

jQuery Dialog: sometimes it feels like a scroll, sometimes it don't

I've made further refinements to the jQuery Dialog implementation discussed here. I've refactored my JavaScript to use vars for the buttons so I don't have to maintain seperate button instantiations for different browsers:

buttons = {"Cancel": function() {$(this).dialog("close");}, "Go":function() {add(id);}}

if($.browser.msie){
$("#addDialog").dialog({
autoOpen: false,
bgiframe: true,
buttons: buttons,
modal: true
}).dialog("open");
} else {
$("#addDialog").dialog({
autoOpen: false,
bgiframe: true,
buttons: buttons,
modal: true,
overlay: {backgroundColor: '#000000', opacity: 0.5}}).dialog("open");
}

I've also reduced the amount of mark-up being generated in the JavaScript by setting .ui-helper-hidden on the element in the HTML, e.g.:


div id="addDialog" title="Make new thing" class="ui-helper-clearfix"
form id="addForm" class="ui-helper-hidden"
fieldset
div class="yui-g"
div class="yui-u first"
label for="thingy"Thingy/label
label for="whatsit"Whatsit/label
/div
div class="yui-u"
input type="text" name="thingy" id="thingy"
input type="text" name="whatsit" id="whatsit"
/div
/div
/fieldset
/form
/div


Then I use jQuery to remove this class when the Dialog is opened:


$("#addForm").removeClass("ui-helper-hidden");


So, making progress getting tidied up and playing nice, and so far it is cooperating across the browsers I am supporting (FF and IE7). I did, however, run into a problem where the Dialog's height and scrolling behaviour changed with each open and close. Two issues: 1) the height of the content area was shrinking or expanding with each open/close; 2) a scroll bar was present even when no visible content was available further down the content area. These issues were isolated to FF3, as IE7 was rendering the Dialog with the same height and no unnecessary scroll bar every time.

I resolved these issues by removing any minHeight or height options in the Dialog constructor and by removing the overflow property from .ui-dialog-content. If no minHeight/height are set in the Dialog constructor both FF3 and IE7 render the Dialog's content wrapped nicely. If the overflow property is removed (set to 'auto') then scrolling never appears – yippee!

But wait, hold your horses, it doesn't stop there. While my form dialog renders nicely without a minHeight/height option in the constructor, my confirmation dialog requires a minHeight of zero in order to wrap the Dialog's content appropriately. I tried reproducing the same CSS values included in the form dialog (which sets a width and margin (on the form), wraps fields in a fieldset and includes a yui-g to space the fields), but to no avail. Could the icon classes be doing something funny? If I get to the bottom of this particular nugget I'll be sure to share my findings with you, gentle reader, or please feel free to comment. For now, back to the drawing board.

Monday, 19 January 2009

jQuery UI Dialog v. YUI Dialog

Background

I've recently been seeking to implement dialog boxes in the online learning application I am building (perhaps for launch by March '09 – watch this space). Having learned some hard lessons about getting these seemingly straightforward bits of display to render correctly while working for the start up www.bview.co.uk, I was determined to harness the collective intelligence embedded in one of the great UI frameworks of the enlightened times we live in. A fortnight later, after much pain, frustration and incandescant programming-induced rage, I've settled for a slightly 'hacked' implementation of jQuery UI's Dialog.

In the past 6 months I've morphed from a Java/Spring MVC/Hibernate web application developer into a Google App Engine/Python developer with sole responsibility for the design and implementation of my application's user interface. So, I am a bit novice when it comes to more advanced UI-coding and best practices, but I do have a good appreciation for JavaScript APIs, such as jQuery, and I have sought to take advantage of robust UI APIs in building my app. With the understanding that there are gargantuan gaps in my knowledge and skill set, I'd like to share with readers my experiences with YUI's and jQuery's Dialogs. On the one hand, I might get some guidance from more capable readers, and on the other I might help other noobs avoid some of the intense pain I suffered especially as regards the freshly born (and slightly flakey) jQuery Dialog.

I built my initial UI using YUI Grids and LayoutManager. I found this process to be pretty straightforward and have been really impressed with these products' reliability and performance cross-browser (resizing is weird in Chrome, but that seems to be a more general issue). When it came time to add buttons to the site, I naturally opted for YUI Button, a basic implementation of which I was able to get running super fast. However, customising the look and feel seemed to require more effort than I was willing to put in at this early stage of development, and I left that to one side. The complexity of YUI and the steepness of the learning curve really hit home, however, when I sought to implement YUI Dialog.

The Use Case

I've got a list of items. Next to each item is a cross icon the user may click to delete. The goal is to display a Yes/No dialog to confirm a user's intent to delete a given item before initiating an AJAX call to delete the item.

Attempt 1: YUI Dialog

Having had a reasonably carefree relationship with YUI up to this point, I initially sought to use YUI's Dialog. I was able to get a basic example from YUI's site running in short order:

YAHOO.namespace("example.container");
function init() {
YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1",{
width: "20em",
fixedcenter: false,
visible: false,
draggable:false,
close: true,
text: "Delete this",
icon: YAHOO.widget.SimpleDialog.ICON_WARN,
constraintoviewport: true,
buttons: [ { text:"Yes", handler:handleYes, isDefault:true },
{ text:"No", handler:handleNo } ],
context: ["show", "bl", "bl"]
} );

YAHOO.example.container.simpledialog1.setHeader("Are you sure?");
YAHOO.example.container.simpledialog1.render("container");

YAHOO.util.Event.addListener("show", "click",
YAHOO.example.container.simpledialog1.show,
YAHOO.example.container.simpledialog1, true);
YAHOO.util.Event.addListener("hide", "click",
YAHOO.example.container.simpledialog1.hide,
YAHOO.example.container.simpledialog1, true);
}
YAHOO.util.Event.addListener(window, "load", init);

In my markup I added:

div class="yui-skin-sam"
div id="container"
button id="show"Show simpledialog1/button
button id="hide"Hide simpledialog1/button
/div
/div

This worked like a dream. Problems came thick and fast, however, when I sought to extend this simple use case to fit the needs of my app. Essentially I could not figure out how to dymanically instantiate multiple dialogs, each one associated with a particular delete cross. For instance, where I retrieved all elements with a particular class (e.g. all elements with .delete-cross) I was unable to loop the returned array and create a new YAHOO.widget.SimpleDialog with a variable name and etc dynamically generated from the element's id attribute. Ultimately I was only able to create one YAHOO.widget.SimpleDialog which was associated with the last element in the collection. It was around this time that Twitter lit up with news of jQuery's 1.3 release and I thought, 'I wonder if my beloved jQuery's UI API is as kick ass as its JavaScript API?'

Attempt 2 : jQuery Dialog

I went to the jQuery UI site and hit the Gallery / Themeroller, which had me very excited as it looked like I could get something much more inline with my app's look and feel 'out of the box' than with YUI and perhaps roll it out for my buttons and etc. I got the resources downloaded and into my project, no drama. I got the basic use case running in a snap, i.e. ("#dialog").dialog("open"). I was feeling full of hope and optimism and generally love for my old pal jQuery. These rosy feelings soured as, again, going beyond the basic use case was exceptionally problematic.

Once I started to customise the dialog that was instantiated in the JavaScript, things went haywire. In particular setting autoOpen to false (it defaults to true) caused me no end of hassle. The documentation claims: "When autoOpen is true the dialog will open automatically when dialog is called. If false it will stay hidden until .dialog("open") is called on it." Indeed, setting autoOpen did not hide the dialog div on page load. I attempted to hide the dialog div on page load by: a) setting style="display: none"; b) adding .ui-helper-hidden to the markup. Both of these approaches did hide the dialog div on page load, but when .dialog("open")was invoked, the dialog did not render correctly; the content area of the div was obscured because it was not clearing between the title and content areas of the dialog.

OK, back to the jQuery UI theming API where I discovered some utility classes, but things got especially hairy when, if I added .ui-helper-clearfix, the dialog div was displayed on page load, but when opened the dialog's content was rendered incorrectly. Calgon – take me away! I resolved these issues, but am not satisfied with my hack-around and I would like to be able to use the API in a more pure way.

I added an empty dialog div in the markup:

div id="dialog" title="Are you sure?" class="ui-helper-clearfix"
p
span id="icon"/span span id="message"/span
/p
/div

I then dynamically generate the spans' content in the JavaScript:

$("#message").html("Delete this?");
$("#icon").addClass("ui-icon").addClass("ui-icon-alert");

I then instantiate and open the dialog:

$("#dialog").dialog({
autoOpen: false,
bgiframe: true,
buttons: {
"Yes": function() {
delete();
},
"No": function() {
$(this).dialog("close");
}
},
minHeight: 0,
modal: true,
overlay: {
backgroundColor: '#000000',
opacity: 0.5
},
resizable: false
});
$("#dialog").dialog("open");

At this point, I thought I was good to go and could go in peace that'd I'd gotten this running in an acceptable, if less than optimal way. Oh, silly, silly me.

While I found tweaking the CSS used in the dialog to get it just right for my app much easier than with YUI, overriding the minHeight default of 47px by setting the minHeight option in the constructor caused IE7 to yelp in pain. So, bring on the browser sniffing:

if($.browser.msie){
$("#dialog").dialog({
autoOpen: false,
bgiframe: true,
buttons: {
"Yes":function() {
delete();
},
"No": function() {
$(this).dialog("close");
}
},
modal: true,
resizable: false
});
$("#dialog").dialog("open");
} else {
$("#dialog").dialog({
autoOpen: false,
bgiframe: true,
buttons: {
"Yes": function() {
delete();
},
"No": function() {
$(this).dialog("close");
}
},
minHeight: 0,
modal: true,
overlay: {
backgroundColor: '#000000',
opacity: 0.5
},
resizable: false
});
$("#dialog").dialog("open");
}

This solution is still being reworked as I hope to get overlay to play nice in IE7, but this is pretty much how I got where I needed to go. It is much less elegant than I'd like, and while it is acceptable in FF3 and IE7 it starts scrolling the content area in Chrome (but is anyone using it?).

Conclusion

While I believe that YUI might provide a more robust solution, I find building up the complexity of an implementation too difficult considering pressures in terms of releasing my app this quarter (already a quarter behind schedule, but then I made major tech shift to GAE in November so can live with that). If it was more clear how to dynamically instantiate the YUI widget, I might've stuck with it and struggled with customising the skin, but as the jQuery option allowed much faster take-up and customisation, it is much more practical for me at this time. I am hopeful that as the jQuery UI project matures it will become as resiliant and reliable as its JavaScript API.