Archive for the ‘Uncategorized’ Category

Prototyping a todo list in Google App Engine

Thursday, October 14th, 2010

In this walkthrough we’ll build a simple to-do list on Google App Engine so that users can log in and add new items to the to-do list, which are stored and displayed back to them later. (Much of this walkthrough follows the Getting Started documentation from the Google App Engine docs, and I’ll refer to those sections as they match up.)

The first thing to do is to install Google App Engine (available on Mac, Windows and Linux) and the Google App Engine Launcher (available on Mac and Windows platforms). These instructions for installation are helpful; you’ll need Python 2.5 installed, on a Mac it should already be present.

Once it’s installed (on a Mac be sure to copy the App Engine Launcher to your desktop rather than running it from the disk image), open the Google App Engine Launcher, which will show an empty list. To create a new application, click the + button. Enter the name of your app (use a name that’s all lowercase letters and numbers) and where you want to store the files (putting this in your repository would be great, for example).

This will create a folder with three files in it: app.yaml (for configuration), index.yaml (for advanced use of the database) and the file we most care about main.py where our server-side code exists. You can open this folder in the Finder/Explorer and open main.py in your favorite Python IDE or text editor.

Back to the App Engine Launcher: select your application from the list, click “Run” and (hopefully) the small icon next to your app will turn green. Now just click “Browse” to open the application in your browser. “Hello World!” without writing a line of code. Congratulations.

Also, now your application is running on a mini-web server on your machine. This is a huge advantage because you can quickly and easily make changes to the code on your own machine and immediately see the effects by refreshing your browser. You don’t need to install, maintain or configure a web server, App Engine’s mini-server is fine. To test this quick updating, change “Hello World” in main.py to “Hello IO Lab” and refresh to prove to yourself that updates work.

Now, Google provides all these neat services, so let’s take advantage of one: users. This way we can welcome the user personally rather than welcoming the entire class. (Getting Started explanation | Reference Documentation)

Copying from the documentation, add this new Python library import at the top of your main.py:

	from google.appengine.api import users

And inside the get(self): method, let’s replace the existing single line with the following:

    user = users.get_current_user()

    if user:
      self.response.out.write('Hello, ' + user.nickname())
    else:
      self.redirect(users.create_login_url(self.request.uri))

(Be careful about indentation. Python requires that all indentation be done the same way: either all with spaces or all with tabs. And indentation must line up or your program will fail to execute.)

If you refresh the page now, your local mini-server will come up with a little fake login page, where you can enter any username you want and click “Login”. You should then be welcomed by email address. How would this work in the real world? Let’s upload our app to Google and see. (Getting Started docs)

From the Launcher, click “Dashboard” for this app. Oh noes, it doesn’t exist! Click “Return to Applications screen” to see your list of Applications (you’ll probably have none) and click the button to “Create Application”. Pick an available subdomain name (which is also called your appid) and give your application a title. If this is your first app, Google will make you jump through some hoop that includes SMS verification. This shouldn’t take too long, but you can keep testing on your own machine without uploading, if you want.

You’ll need to specify your selected appid in the app.yaml configuration file in your application’s folder. Enter it after application: .

Now, back in the Launcher, just click “Deploy”, enter your Google username and password and your application files will be uploaded to Google’s servers. Then go to http://<your-appid>.appspot.com and your code is running, except with a real Google login. Congratulations, your application is running in the cloud! Clicking “Dashboard” in the Launcher now will show you a web page with stats and various information about your app’s execution on Google servers.

Most of the time, though, we want to test by using the mini web-server, which picks up changes automatically. So let’s switch back to that by clicking “Run” again. (Deploying your application to the production server will stop it on your mini server.)

Now, we’re combining our presentation and behavior here, which we’ve told you is a bad thing. So let’s move the presentation into an HTML file and keep just the logic in Python.

Create a new file in the same folder called index.html (the HTML skeleton file on the course web site is a good place to start). Here you can fill in the <title> element, make things look nice, and not be mixed up with Python. We’ll leave a little placeholder for the user’s nickname, using a templating language. (Getting Started docs | Django Templating docs)

    <html>
    ...
    <h1>Hello, {{nickname}}</h1>

Now, in main.py, we’ll import two additional libraries:

    import os
    from google.appengine.ext.webapp import template

And then render the template with values we store in a simple Python dictionary:

    nickname = user.nickname()
    template_values = {'nickname': nickname}

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

Refreshing the page in your browser now should show a nicely formatted page with a large heading welcoming the user by their nickname. Now let’s make this a little more interactive, by adding a form where a user can add a to-do item. In index.html:

	<form action="" method="post">
		<input type="text" name="item" value="To-do item"/>
		<input type="submit"/>
	</form>

I’m using action="" just because it’s the simplest possible thing: it’ll call back to the same page, but use post instead of the usual get, since we’re submitting data. To respond to the post form submission, we define a new function in the same class:

    def post(self):
      self.response.out.write(self.request.get("item"));

Just enough to prove that it works.

Now, we’ll actually do something with the entered data, like storing it. (Getting Started docs | API Reference) To do this, we need to define a Model, a representation of the data we’re going to store. Import the db library, then define the Todo class which will be stored in the database.

    from google.appengine.ext import db

    class Todo(db.Model):
      author = db.UserProperty()
      item = db.StringProperty()
      completed = db.BooleanProperty()
      date = db.DateTimeProperty(auto_now_add=True)

And then use that class to save new Todo’s in the post function:

    todo = Todo()

    todo.author = users.get_current_user()
    todo.item = self.request.get("item")
    todo.completed = False

    todo.put()

    self.response.out.write("Saved: " + self.request.get("item"));

Now, that data has supposedly been stored, let’s see it. Back in the Launcher, click “SDK Console”. In the Datastore Viewer, you can choose a type of entity and then list the entities in that store. (When you’re running live on the server, you’ll have a similar view from the Dashboard.)

Let’s expose that data back in our little web app. Change the get handler to query for all the Todo items in the datastore and store them in the template_values dictionary:

    todos = Todo.all()

    template_values = {'nickname': nickname, 'todos': todos}

And then change the template to loop through them and create list items for each one:

    <ul>
    {% for todo in todos %}
    	<li>{{ todo.item }} by {{ todo.author.nickname }}</li>
    {% endfor %}
    </ul>

Great, now when we add something and go back to the main page and refresh, we see the new content. Let’s make that step automatic though.

    self.redirect('/')

(This goes at the end of the post function; be sure to remove or comment out any self.response.out.write lines.)

What else could we add to this? You could add CSS and Javascript to this HTML page (and with a small workaround, you can add them as static files). Since these are to-do items, you could add checkboxes and make clicking a checkbox into an AJAX post that would mark the item as completed. You could filter to only show recently added todo items, or only your todo items.

A working version of this code is available in the repository.

Delicious Trailmaker Tutorial

Sunday, September 12th, 2010

In our first class on August 31, we did an in-class demo showing one possible way to implement trails from Vannevar Bush’s Memex using the Delicious API. This tutorial reviews the steps from class. At the end of this tutorial, you will have constructed a small web application that saves a collection of bookmarks to Delicious as a trail. A trail is identified by a set of special tags: trail:[name_of_trail] and step:[step_number].

Getting Started

0. Before getting started, you may want to create a separate Delicious account for experimenting. The Delicious API has no undo, and you don’t want a mistake to erase any existing bookmarks. Also, unless you use a properly configured proxy server, your password will be sent in the clear. You may want to change your password to something that isn’t used by another of your other accounts.

1. Start with a basic HTML page that includes Google-hosted copies of jQuery and jQuery UI. You can download this template at http://courses.ischool.berkeley.edu/i290-iol/f10/resources/html_skeleton.html

The code samples in this tutorial are broken up by explanations. For the uninterrupted code, you can download the completed Trailmaker at http://courses.ischool.berkeley.edu/i290-iol/f10/resources/delicious_trailmaker.html

2. Create the basic structure that you will need in HTML. This includes a <form> to specify a username, a <div> and <ul> to display loaded bookmarks, and a <div> and a <ul> to create a new trail. The following structure is a good start:

<form id="load-bookmarks" method="get">
<h2 id="enter_a_delicious_username">Enter a Delicious username:</h2>
    <input type="text" name="username" id="username" value="" />
    <input type="submit" value="Get Bookmarks" />
</form>

<div id="bookmarks">
<h2 id="bookmarks">Bookmarks</h2>
    <ul></ul>
</div>

<div id="new-trail">
<h2 id="new_trail">New Trail</h2>
    <ul></ul>
    <form id="save-trail" method="post">
        Username: <input id="save-username" type="text" name="save-username" /><br />
        Password: <input id="save-password" type="password" name="save-password" /><br />
        <input type="button" name="make-trail" value="Make New Trail" id="make-trail" />                
    </form>
</div>

Loading Bookmarks

3. When a user enters an account in the #load-bookmarks form, we want to load the Delicious bookmarks for that account. Delicious provides a feeds API that lets us get the bookmarks for a user at http://feeds.delicious.com/v2/json/{username}.

First, we create a global object called “delicious” to store settings. Then we want to attach the JavaScript for this document once the page is “ready,” so we use $(document).ready() inside a <script> tag in the document <head>. You put an anonymous callback function inside .ready() which executes once the page is in memory:

<script>
// Create an empty global object where we can store settings for connecting to Delicious
var delicious = {};

$(document).ready(function() {
    // What you want to happen when the page loads
});
</script>

In this ready function, we’ll attach a function to the submit event of the #load-bookmarks form. When the form is submitted (by pressing the return key or by clicking on the “Get Bookmarks” button), this function will run. The last thing this function does should be return false;. The browser’s default behavior when submitting a form is to load a new page, and we use return false to prevent this action.

$('#load-bookmarks').submit(function(){
    // What you want to happen when the load bookmarks form is submitted
    return false;
});

CSS selectors are the basis for nearly all of the jQuery Javascript library. Most programming occurs by selecting some group of elements on the current page and manipulating them. Because the form has the id attribute set to ‘load-bookmarks’, we can select this element using #load-bookmarks—the pound sign (#) is used to indicate an id in CSS. Then we attach a function to the submit() event of the form. jQuery can attach functions (which is called “binding”) to many kinds of events, which are described in the jQuery documentation.

The functions that we use on inside .ready() and .submit() do not have a name—they are “anonymous functions,” which are a feature of Javascript and very common. They allow you to define commands that you want to happen inline, without defining a function somewhere else in your code. We’ll discuss anonymous functions more in lecture two; You can also read this guide to anonymous functions in JavaScript.

4. When the form is submitted, we want to get the value of the account name entered in the form, which we do with var username = $('#username').val(); Although Javascript does not require var in variable declarations, you should always use it. Then we use the getJSON() method to get the bookmarks from Delicious. You can provide a function to getJSON that is called when the request is complete. In fact, if you want anything to happen when the request is returned, you have to provide a function to getJSON because of the asynchronous nature of AJAX requests. This function receives the response from the server, which you can use to construct an HTML list item and add it to the list of bookmarks.

var username = $('#username').val();
// This cross-domain request requires that you use '?callback=?' because it is done using JSONP
$.getJSON('http://feeds.delicious.com/v2/json/' + username + '?callback=?',
function(json){
    // json contains the response provided by the server
    // You can use console.log(json) to examine the response
   $(json).each(function(index) {
       // this.u // url
       // this.d // description
       // this.n // extended notes
       // this.t // array of tags

        // Create an HTML string for the bookmark
        // We also use the .data method to store the notes and tags
        // of the bookmark in the DOM with the object
        // Finally, we add it to the list of bookmarks with appendTo()
       $('<li></li>').html('<a href="' + this.u + '">' + this.d + '</a>')
        .data('extended', this.n)
        .data('tags', this.t)
        .appendTo('#bookmarks ul');
   });

   // Later, you'll add dragging functionality here

});
return false;    

Now entering a Delicious user’s account name and clicking “Load Bookmarks” works.

Creating a new trail

5.We want to let users drag loaded bookmarks to the New Trail box and rearrange them before saving the trail. Before we do this, we should make the #bookmarks box and the #new-trail box actually appear side-by-side. Add these lines to between the <style></style> tags in your document head. This makes each box roughly half the width of the screen, and the float: left makes them appear side-by-side:

#bookmarks, #new-trail {
    float: left;
    width: 48%;
    margin-right: 1%;
    min-height: 300px;
    border: 1px solid #666;
}

6. The jQuery UI library makes it easy to script complex actions like dragging and dropping. First, we want to make each of the loaded bookmarks draggable. We can do this with the .draggable() method when we load each bookmark. Add the following line at the end of your getJSON callback function:

$('#bookmarks li').draggable({revert: true});

7. Now we need a place to drop these draggable elements. We’ll make the entire #new-trail box a droppable area, and define a function that copies the bookmark to the list when the user drags a loaded bookmark to the area. Notice that the droppable method only takes one argument, a Javascript object (indicated by the curly braces {}) that provides various parameters by name. This is common practice in jQuery and jQuery UI. Here we are providing the accept and drop parameters.

$('#new-trail').droppable({
    accept: 'li', // This droppable area accepts li elements
    drop: function(event, ui) {
        // Don't confuse ul, the <ul> unordered-list with ui, the user interface element
        // .draggable('disable') says that we want to remove the draggable behavior
        $(ui.draggable).draggable('disable').appendTo('#new-trail ul');
    }
});    

We also want to make the list of bookmarks in the new trail list sortable, to allow a user to re-arrange the order of the points in the trail. To do this we can use the .sortable() function. Insert this line in your document .ready() function. Simple, right?

$('#new-trail ul').sortable();    

Saving the trail

8. Finally, we want to send each bookmark in this newly created trail to a specified user account when it is saved. We start by attaching a function to the submit of the #save-trail form. Remember to return false at the end of the function!

$('#save-trail').submit(function() {
    // Let's ask the user for a name for the trail
    // We are storing the name that the user enters as the text of the
    // h2 in the #new-trail div
    // The || syntax here lets us specify a default value
    $('#new-trail h2').text(prompt('Enter a name for your trail:') || 'My New Trail');

    // Store the username and password to send with each request
    // This isn't the best security practice, but we do it here
    // in the interest of brevity
    delicious.username = $('#save-username').val();
    delicious.password = $('#save-password').val();
    delicious.stepNum = 0;

    saveTrail();
    return false;
});    

In the saveTrail() function we want to gather the information for a single bookmark and post it to Delicious. We have to do this one list item at a time because the Delicious API only provides a call to save a single bookmark. When we have successfully saved one bookmark, we move on to the next one. When they are all done, we let the user know that saving was successful.

In order to update a bookmarks on Delicious, you need to re-save all the information from the old bookmark (including URL, description, notes, and tags) with your changes. Fortunately, we stored all this information using jQuery’s .data() method when we initially loaded the bookmarks. .data() lets you store any information you want in the DOM with an element.

function saveTrail () {
    // We need to keep track of which bookmark number we are saving, so we
    // can use the `step:2` syntax that we have established
    // When the user submitted the form we started with stepNum = 0,
    // so we can increment it each time we call saveTrail
    delicious.stepNum++;

    // Change spaces in the trail name to underscores to follow our trail syntax
    // By default, the .replace() method doesn't replace ALL the occurrances
    // of a string, so we are using the global flag in our regular expression
    // to replace everything. The global flag is set with the "g" after
    // the regular expression (/ /g)
    var newTrailName = 'trail:' + $('#new-trail h2').text().toLowerCase().replace(/ /g, '_');

    // Get the first bookmark to save, which is the first element of the #new-trail list
    var bookmark = $('#new-trail li:first');

    // Assemble the data to send to Delicious
    var postData = {
        url: bookmark.find('a').attr('href'),
        description: bookmark.find('a').html(),
        extended: bookmark.data('extended'),
        tags: bookmark.data('tags').join(' ') + ' ' + newTrailName + ' ' + 'step:' + delicious.stepNum,
        method: 'posts/add',
        username: delicious.username,
        password: delicious.password
    };

Here we have constructed postData, which is a Javascript objet with keys and values for the data we are going to send to the server. The first four values are for Delicious: the url we are saving, its description (which is the title, in Delicious parlance), the extended notes for the bookmark, and the tags. Note the most important part is occurring on the line with the tags assignment: we are getting the existing tags and adding the new trail name, as well as the step number.

We stored the first line of the #new-trail list in bookmark so we could use it repeatedly. The .find() method lets us search for some potion of the document within the current selection. So .find('a').attr('href') finds the link tag within the bookmark and gets the href attribute for it.

We are also sending the method of the Delicious API that we are calling and the user’s Delicious username and password.

Next, we use jQuery’s $.post method to send the data to Delicious. You are welcome to use this code as is without inspecting it closely.

// Send the data to Delicious through a proxy and handle the response
// Use $.post if the script is located on the same server
// Otherwise, use $.get to avoid cross-domain problems
//$.post('delicious_proxy.php',
// If you are running this on your own machine, you may need to swap the $.post line for this $.getJSON line
$.getJSON('http://courses.ischool.berkeley.edu/i290-iol/f10/resources/delicious_proxy.php?callback=?',
  postData,
 function(rsp){
        if (rsp.result_code === "access denied") {
            alert('The provided Delicious username and password are incorrect.');
        } else if (rsp.result_code === "something went wrong") {
            alert('There was an unspecified error communicating with Delicious.');
        } else if (rsp.result_code === "done") {
            // Bookmark was saved properly
            $('#new-trail li:first').remove(); // Remove the bookmark we just saved
            if ($('#new-trail li').length > 0) {
                // If there are any bookmarks left to save
                // Save the next bookmark in the trail in 1000ms (1 second)
                // We have to wait this period of time to comply with the
                // terms of the Delicious API. If we don't we may have access denied.
                setTimeout(saveTrail, 1000);
            } else {
                // We're done saving the trail
                delicious.password = null; // Don't store the user's password any longer than we need to
                alert ("Your trail has been saved!");
            }
        }
    });
}

You’re done with the basic tutorial! If you have any questions, ask ryan@ischool or npdoty@ischool via email, or bring them to lecture two.

Improvements

We can also add some extra niceties, like making sure links open in a new window so a person doesn’t lose his or her work in creating a new trail. Adding this code to the ready function makes all links open in a new window.

$('a').live('click', function() {
    window.open($(this).attr('href'));
    return false;
});    

You might want to provide some feedback while the trail is being saved. Since each bookmark has to be saved with a delay of 1 second, saving a trail could take a long time, and the user should know that something is happening.

You might disable the “Save Trail” button when the form is submitted using jQuery’s .attr() method

Also, our JavaScript does not check if a user actually entered a name for his or her trail, nor if the user entered a username or password. You could implement a check to make sure these values have been entered before saving the trail.

Additional Information

The Delicious API has a variety of methods available. To update an existing bookmark, use the posts/add method and include the replace=no parameter.

If you are going to be using the Delicious API via JavaScript you should change your password to something different from your other online accounts. Because of browsers limitations to prevent cross-site scripting, you have to send your password as a URL GET request. Although this connection happens via https, your password would appear in log files. If you’re interested in why this has to be the case, ask us. Alternately, you can download the PHP proxy script yourself, run it in your own web space (change the .txt extension to .php), and POST securely to it.

Using separate files for CSS and JS

Wednesday, September 9th, 2009

One of your classmates rightly noted that Nick and I have said that you can (and often should) keep your HTML, CSS, and JavaScript in separate files, but have not provided any instructions about how to do that.

Single File

Using a single file for HTML, CSS, and JS

Using a single file for HTML, CSS, and JS

The single-file approach is good for quick one-off tests or demonstrations. This is where you put all your CSS and JavaScript in the same file as your HTML. Your single .html document will look something like this:

<html>
<head>
    <style type="text/css">
    /* CSS goes here in the style tag. */
    </style>

    <script type="text/javascript">
    // JavaScript goes here in the script tag.
    </script>
</head>
<body>
    HTML content goes here in the body tag.
</body>
</html>

Although it is fast to get started and there are fewer files to keep track of, there are some significant downsides to keeping everything in one HTML file. For example, if you put your CSS and JavaScript in the same file with your HTML, you can’t reuse the same styles and code on other pages without needless duplication.

Multiple Files

Multiple Files for HTML, CSS, and JS

Multiple Files for HTML, CSS, and JS

The alternative is to store your HTML, CSS, and JavaScript in separate files. In the main .html file you use a <link> tag to include the external stylesheet and a <script> tag to include the external JavaScript. Now your .html document will look like this:

<html>
<head>
    <link rel="stylesheet" href="file.css" type="text/css" media="screen" />
    <script type="text/javascript" src="file.js"></script>
</head>
<body>
    HTML content goes here in the body tag.
</body>
</html>

Note that the CSS file is specified by href="..." while the JavaScript file is specified by src="..."

Delicious Trailmaker Tutorial

Tuesday, September 1st, 2009

In our first class on September 1, we did an in-class demo showing one possible way to implement trails from Vannevar Bush’s Memex using the Delicious API. This tutorial reviews the steps from class. At the end of this tutorial, you will have constructed a small web application that saves a collection of bookmarks to Delicious as a trail. A trail is identified by a set of special tags: trail:[name_of_trail] and step:[step_number].

Getting Started

0. Before getting started, you may want to create a separate Delicious account for experimenting. The Delicious API has no undo, and you don’t want a mistake to erase any existing bookmarks. Also, unless you use a properly configured proxy server, your password will be sent in the clear. You may want to change your password to something that isn’t used by another of your other accounts.

1. Start with a basic XHTML page that includes Google-hosted copies of jQuery and jQuery UI. You can download this template at http://courses.ischool.berkeley.edu/i290-4/f09/resources/jquery_template.html

The code samples in this tutorial are broken up by explanations. For the uninterrupted code, you can download the Trailmaker at http://courses.ischool.berkeley.edu/i290-4/f09/resources/delicious_trailmaker.html

2. Create the basic structure that you will need in HTML. This includes a <form> to specify a username, a <div> and <ul> to display loaded bookmarks, and a <div> and a <ul> to create a new trail. The following structure is a good start:

<form id="load-bookmarks" method="get">
<h2 id="enter_a_delicious_username">Enter a Delicious username:</h2>
    <input type="text" name="username" id="username" value="" />
    <input type="submit" value="Get Bookmarks" />
</form>

<div id="bookmarks">
<h2 id="bookmarks">Bookmarks</h2>
    <ul></ul>
</div>

<div id="new-trail">
<h2 id="new_trail">New Trail</h2>
    <ul></ul>
    <form id="save-trail" method="post">
        Username: <input id="save-username" type="text" name="save-username" /><br />
        Password: <input id="save-password" type="password" name="save-password" /><br />
        <input type="button" name="make-trail" value="Make New Trail" id="make-trail" />                
    </form>
</div>

Loading Bookmarks

3. When a user enters an account in the #load-bookmarks form, we want to load the Delicious bookmarks for that account. Delicious provides a feed API that lets us get the bookmarks for a user at http://feeds.delicious.com/v2/json/{username}.

We want to attach all of the Javascript for this document once the page is “ready,” so we use $(document).ready() inside a <script> tag in the document <head>. You put an anonymous function inside .ready() which executes once the page is in memory:

<script>
$(document).ready(function() {
    // What you want to happen when the page loads
});
</script>

In this ready function, we’ll attach a function to the submit event of the #load-bookmarks form. When the form is submitted (by pressing the return key or by clicking on the “Get Bookmarks” button), this function will run. The last thing this function does should be return false;. The browser’s default behavior when submitting a form is to load a new page, and we use return false to prevent this action.

$('#load-bookmarks').submit(function(){
    // What you want to happen when the load bookmarks form is submitted
    return false;
});

CSS selectors are the basis for nearly all of the jQuery Javascript library. Most programming occurs by selecting some group of elements on the current page and manipulating them. Because the form has the id attribute set to ‘load-bookmarks’, we can select this element using #load-bookmarks—the pound sign (#) is used to indicate an id in CSS. Then we attach a function to the submit() event of the form. jQuery can attach functions (which is called “binding”) to many kinds of events, which are described in the jQuery documentation.

The functions that we use on inside .ready() and .submit() do not have a name—they are “anonymous functions,” which are a feature of Javascript and very common. They allow you to define commands that you want to happen inline, without defining a function somewhere else in your code. We’ll discuss anonymous functions more in lecture two; You can also read this guide to anonymous functions in Javascript.

4. When the form is submitted, we want to get the value of the account name entered in the form, which we do with var username = $('#username').val(); Although Javascript does not require var in variable declarations, you should always use it. Then we use the getJSON() method to get the bookmarks from Delicious. You can provide a function to getJSON that is called when the request is complete. In fact, if you want anything to happen when the request is returned, you have to provide a function to getJSON because of the asynchronous nature of AJAX requests. This function receives the response from the server, which you can use to construct an HTML list item and add it to the list of bookmarks.

var username = $('#username').val();
// This cross-domain request requires that you use '?callback=?' because it is done using JSONP
$.getJSON('http://feeds.delicious.com/v2/json/' + username + '?callback=?',
function(json){
    // json contains the response provided by the server
    // You can use console.log(json) to examine the response
   $(json).each(function(index) {
       // this.u // url
       // this.d // description
       // this.n // extended notes
       // this.t // array of tags

        // Create an HTML string for the bookmark
        // We also use the .data method to store the notes and tags
        // of the bookmark in the DOM with the object
        // Finally, we add it to the list of bookmarks with appendTo()
       $('<li></li>').html('<a href="' + this.u + '">' + this.d + '</a>')
        .data('extended', this.n)
        .data('tags', this.t)
        .appendTo('#bookmarks ul');
   });

   // Later, you'll add dragging functionality here

});
return false;    

Now entering a Delicious user’s account name and clicking “Load Bookmarks” works.

Creating a new trail

5.We want to let users drag loaded bookmarks to the New Trail box and rearrange them before saving the trail. Before we do this, we should make the #bookmarks box and the #new-trail box actually appear side-by-side. Add these lines to between the <style></style> tags in your document head. This makes each box roughly half the width of the screen, and the float: left makes them appear side-by-side:

#bookmarks, #new-trail {
    float: left;
    width: 48%;
    margin-right: 1%;
    min-height: 300px;
    border: 1px solid #666;
}

6. The jQuery UI library makes it easy to script complex actions like dragging and dropping. First, we want to make each of the loaded bookmarks draggable. We can do this with the .draggable() method when we load each bookmark. Add the following line at the end of your getJSON callback function:

$('#bookmarks li').draggable({revert: true});

7. Now we need a place to drop these draggable elements. We’ll make the entire #new-trail box a droppable area, and define a function that copies the bookmark to the list when the user drags a loaded bookmark to the area. Notice that the droppable method only takes one argument, a Javascript object (indicated by the curly braces {}) that provides various parameters by name. This is common practice in jQuery and jQuery UI. Here we are providing the accept and drop parameters.

$('#new-trail').droppable({
    accept: 'li', // This droppable area accepts li elements
    drop: function(event, ui) {
        // Don't confuse ul, the <ul> unordered-list with ui, the user interface element
        // .draggable('disable') says that we want to remove the draggable behavior
        $(ui.draggable).draggable('disable').appendTo('#new-trail ul');
    }
});    

We also want to make the list of bookmarks in the new trail list sortable, to allow a user to re-arrange the order of the points in the trail. To do this we can use the .sortable() function. Insert this line in your document .ready() function. Simple, right?

$('#new-trail ul').sortable();    

Saving the trail

8. Finally, we want to send each bookmark in this newly created trail to a specified user account when it is saved. We start by attaching a function to the submit of the #save-trail form. Remember to return false at the end of the function!

$('#save-trail').submit(function() {
    // Let's ask the user for a name for the trail
    // We are storing the name that the user enters as the text of the
    // h2 in the #new-trail div
    // The || syntax here lets us specify a default value
    $('#new-trail h2').text(prompt('Enter a name for your trail:') || 'My New Trail');

    // Get the username and password
    // Normally it isn't a good idea to set global variables like this
    // explicitly, but we'll do it here in the interest of brevity
    window.delicious_username = $('#save-username').val();
    window.delicious_password = $('#save-password').val();

    saveTrail();
    return false;
});    

In the saveTrail() function we want to gather the information for a single bookmark and post it to Delicious. We have to do this one list item at a time because the Delicious API only provides a call to save a single bookmark. When we have successfully saved one bookmark, we move on to the next one. When they are all done, we let the user know that saving was successful.

In order to update a bookmarks on Delicious, you need to re-save all the information from the old bookmark (including URL, description, notes, and tags) with your changes. Fortunately, we stored all this information using jQuery’s .data() method when we initially loaded the bookmarks. .data() lets you store any information you want in the DOM with an element.

function saveTrail () {
    // We need to keep track of which bookmark number we are saving, so we
    // can use the `step:2` syntax that we have established
    // Global variables aren't the best way to do this, but they're quick here
    // This line says "if window.stepNum exists, increment it by one.
    // Otherwise, set it to one"
    window.stepNum ? window.stepNum++ : window.stepNum = 1;

    // Change spaces in the trail name to underscores to follow our trail syntax
    // By default, the .replace() method doesn't replace ALL the occurrances
    // of a string, so we are using the global flag in our regular expression
    // to replace everything. The global flag is set with the "g" after
    // the regular expression (/ /g)
    var newTrailName = 'trail:' + $('#new-trail h2').text().toLowerCase().replace(/ /g, '_');

    // Get the first bookmark to save, which is the first element of the #new-trail list
    var bookmark = $('#new-trail li:first');

    // Assemble the data to send to Delicious
    var postData = {
        url: bookmark.find('a').attr('href'),
        description: bookmark.find('a').html(),
        extended: bookmark.data('extended'),
        tags: bookmark.data('tags').join(' ') + ' ' + newTrailName + ' ' + 'step:' + window.stepNum,
        method: 'posts/add',
        username: window.delicious_username,
        password: window.delicious_password
    };

Here we have constructed postData, which is a Javascript objet with keys and values for the data we are going to send to the server. The first four values are for Delicious: the url we are saving, its description (which is the title, in Delicious parlance), the extended notes for the bookmark, and the tags. Note the most important part is occurring on the line with the tags assignment: we are getting the existing tags and adding the new trail name, as well as the step number.

We stored the first line of the #new-trail list in bookmark so we could use it repeatedly. The .find() method lets us search for some potion of the document within the current selection. So .find('a').attr('href') finds the link tag within the bookmark and gets the href attribute for it.

We are also sending the method of the Delicious API that we are calling and the user’s Delicious username and password.

Next, we use jQuery’s $.post method to send the data to Delicious. You are welcome to use this code as is without inspecting it closely.

// Send the data to Delicious through a proxy and handle the response
// Use $.post if the script is located on the same server
// Otherwise, use $.get to avoid cross-domain problems
//$.post('delicious_proxy.php',
// If you are running this on your own machine, you may need to swap the $.post line for this $.getJSON line
$.getJSON('http://courses.ischool.berkeley.edu/i290-4/f09/resources/delicious_proxy.php?callback=?',
  postData,
 function(rsp){
     // POST typeof rsp == string
     // GETJSON typeof rsp == object
    if (typeof rsp == 'string') {
        // Response via POST must be eval'ed
        var rsp = eval( '(' + rsp + ')'); // The double parentheses are crucial. Omitting them will cause a parse error.
    }
        if (rsp.result_code === "access denied") {
            alert('The provided Delicious username and password are incorrect.');
        } else if (rsp.result_code === "something went wrong") {
            alert('There was an unspecified error communicating with Delicious.');
        } else if (rsp.result_code === "done") {
            // Bookmark was saved properly
            $('#new-trail li:first').remove(); // Remove the bookmark we just saved
            if ($('#new-trail li').length > 0) {
                // If there are any bookmarks left to save
                // Save the next bookmark in the trail in 1000ms (1 second)
                // We have to wait this period of time to comply with the
                // terms of the Delicious API. If we don't we may have access denied.
                setTimeout(saveTrail, 1000);
            } else {
                // We're done saving the trail
                window.delicious_password = null; // Don't store the user's password any longer than we need to
                alert ("Your trail has been saved!");
            }
        }
    });
}

You’re done with the basic tutorial! If you have any questions, ask ryan@ischool or npdoty@ischool via email, or bring them to lecture two.

Improvements

We can also add some extra niceties, like making sure links open in a new window so a person doesn’t lose his or her work in creating a new trail. Adding this code to the ready function makes all links open in a new window. (The preferred method of doing this—the target attribute—has been deprecated in XHTML.)

$('a').live('click', function() {
    window.open($(this).attr('href'));
    return false;
});    

You might want to provide some feedback while the trail is being saved. Since each bookmark has to be saved with a delay of 1 second, saving a trail could take a long time, and the user should know that something is happening.

You might disable the “Save Trail” button when the form is submitted using jQuery’s .attr() method

Also, our JavaScript does not check if a user actually entered a name for his or her trail, nor if the user entered a username or password. You could implement a check to make sure these values have been entered before saving the trail.

Additional Information

The Delicious API has a variety of methods available. To update an existing bookmark, use the posts/add method and include the replace=no parameter.

If you are going to be using the Delicious API via Javascript you should change your password to something different from your other online accounts. Because of browsers limitations to prevent cross-site scripting, you have to send your password as a URL GET request. Although this connection happens via https, your password would appear in log files. If you’re interested in why this has to be the case, ask us. Alternately, you can download the PHP proxy script yourself, run it in your own web space (change the .txt extension to .php), and POST securely to it.

Hello world!

Tuesday, September 1st, 2009

Welcome to Information Organization Lab for the fall semester 2009. This is the blog for this course where students can exchange advise and experiences with other students in the class.