Skip to content

Commit

Permalink
js refactoring; possible regressions in IE behavior
Browse files Browse the repository at this point in the history
(which seems to behave differently offline than online...)
Squashed commit of the following:

commit b30a9a6d4cd9d9c132da3d42587bb0fa479ba287
Author: Eric Liang <forward.eric@gmail.com>
Date:   Thu Feb 19 01:03:16 2009 -0800

    Thu Feb 19 01:03:16 PST 2009

commit 7d8654cd4d324359bba784de0dd9feeee72afd98
Author: Eric Liang <forward.eric@gmail.com>
Date:   Thu Feb 19 00:25:40 2009 -0800

    Thu Feb 19 00:25:40 PST 2009

commit eae09a45b4d35be84874deea047d8abeca548224
Author: Eric Liang <forward.eric@gmail.com>
Date:   Thu Feb 19 00:15:30 2009 -0800

    Thu Feb 19 00:15:30 PST 2009

commit 8cbdfa871184c83b1ce7f137f2d5286e04b5455a
Author: Eric Liang <forward.eric@gmail.com>
Date:   Wed Feb 18 23:39:34 2009 -0800

    Wed Feb 18 23:39:34 PST 2009

commit 919c1c3d72143250029924163c6f8f961df9c583
Author: Eric Liang <forward.eric@gmail.com>
Date:   Wed Feb 18 23:33:28 2009 -0800

    Wed Feb 18 23:33:28 PST 2009

commit 953608008a244cb7041c1be329329c084b9588a8
Author: Eric Liang <forward.eric@gmail.com>
Date:   Wed Feb 18 23:31:40 2009 -0800

    Wed Feb 18 23:31:40 PST 2009

commit 0b7faf594bebb8b7a4e41dbe6b8357cdcadfaa78
Author: Eric Liang <forward.eric@gmail.com>
Date:   Wed Feb 18 23:12:42 2009 -0800

    Wed Feb 18 23:12:42 PST 2009

commit 53de5a7b9d1e3e4ee01a94ab553fe583591381e9
Author: Eric Liang <forward.eric@gmail.com>
Date:   Wed Feb 18 22:39:42 2009 -0800

    Wed Feb 18 22:39:42 PST 2009

commit 5badbc3714203594c81b67a4ba56dfc0df3d37ef
Author: Eric Liang <forward.eric@gmail.com>
Date:   Wed Feb 18 22:31:52 2009 -0800

    Wed Feb 18 22:31:52 PST 2009

commit a998e2281fc9502cf124fab813c446909e8f3035
Author: Eric Liang <forward.eric@gmail.com>
Date:   Wed Feb 18 21:24:00 2009 -0800

    Wed Feb 18 21:24:00 PST 2009
  • Loading branch information
Eric Liang committed Feb 19, 2009
1 parent 22c5304 commit 9947dde
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 382 deletions.
1 change: 1 addition & 0 deletions cover/templates/frontpage.html
Expand Up @@ -12,6 +12,7 @@
{% endif %}
<script type="text/javascript" src="{{ MEDIA_URL }}history2.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}state.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}repr.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}grid.js"></script>
<!--[if lt IE 7]>
<style type="text/css">
Expand Down
62 changes: 46 additions & 16 deletions media/grid.js
Expand Up @@ -20,30 +20,66 @@ $(document).ready(function() {
return;
event.preventDefault();
var choice_id = hash_of($(this).attr("href")).substring(8); // #choice_
State.submit_poll(choice_id, $(this));
$(this).addClass("active");
$.ajax({
type: "GET",
dataType: "html",
url: "/ajax/poll",
data: {"choice": choice_id},
success: function(r) {
$("div #poll").html(r);
},
error: function(xhr) {
$("div #poll").html(xhr.responseText);
}
});
});

$("a.poll_results_only").live("click", function(event) {
if (is_nonlocal(event))
return;
event.preventDefault();
var poll_id = hash_of($(this).attr("href")).substring(6); // #poll_
State.get_poll(poll_id, $(this));
$(this).addClass("active");
$.ajax({
type: "GET",
dataType: "html",
url: "/ajax/poll",
data: {"poll": poll_id},
success: function(r) {
$("div #poll").html(r);
},
error: function(xhr) {
$("div #poll").html(xhr.responseText);
}
});
});

$("a.embeddable").live("click", function(event) {
if (is_nonlocal(event))
return;
event.preventDefault();
State.current().enter($(this));
State.sync({}, {'link': $(this)});
State.scrollup();
});

$("a.pagelink").live("click", function(event) {
if (is_nonlocal(event))
return;
event.preventDefault();
State.current().page($(this).attr("id").substring(2)).enter($(this));
State.sync({'page': $(this).attr("id").substring(2)}, {'link': $(this)});
State.scrollup();
});

$("a.gs-title").live("click", function(event) {
if (is_nonlocal(event))
return;
if ($(this).attr("href").match(/\.[a-z]+$/)) {
$(this).attr("target", null);
return; // it's probably non-html
}
event.preventDefault();
State.sync({}, {'link': $(this)});
State.scrollup();
});

Expand All @@ -53,12 +89,6 @@ $(document).ready(function() {
$(this).attr("href", stripped);
});

// redirect to hash id if possible for better functionality
if (window.location.pathname != "/") {
location.replace("/" + new State(window.location.pathname));
return;
}

var selecting_dates = false;
var down = false;
$("#tags #alltags").width(TAG_EXPANDED);
Expand All @@ -67,7 +97,7 @@ $(document).ready(function() {

if (window.location.hash.length > 1) { // permalink and not lone '#'
setVisible("none");
new State(window.location.hash).noqueue().enter();
new State(Repr.deserialize(window.location.hash), {'atomic': false});
}

/* HISTORY BEGINS - all State changes must be done */
Expand All @@ -83,7 +113,7 @@ $(document).ready(function() {
if (!$(".results").is(":visible"))
$(this).removeClass("activetag");
$(this).removeClass("useless").toggleClass("activetag");
State.current().enter();
State.sync();
State.scrollup();
});

Expand All @@ -93,7 +123,7 @@ $(document).ready(function() {
selecting_dates = false;
$("#tags li").removeClass("useless");
$("#tags .activetag").removeClass("activetag");
State.current().enter();
State.sync();
State.scrollup();
});

Expand All @@ -110,7 +140,7 @@ $(document).ready(function() {
var some_newly_selected = false;
if (!State.select_dates(min, max)[0])
$("#dates li").removeClass("activedate");
State.current().enter();
State.sync();
});

$("#dates li li").mousedown(function() {
Expand Down Expand Up @@ -138,14 +168,14 @@ $(document).ready(function() {
}
if (selecting_dates) {
selecting_dates = false;
State.current().enter();
State.sync();
}
});

$(document).mouseup(function(event) {
if (selecting_dates) {
selecting_dates = false;
State.current().enter();
State.sync();
}
});

Expand Down
20 changes: 17 additions & 3 deletions media/history2.js
Expand Up @@ -15,9 +15,24 @@ History.historyCurrentHash = undefined;
History.historyCallback = undefined;
History.historyQueue = undefined;
History.useIframe = undefined;
History.poller = undefined;

History.callCallback = function(hash) {
History.historyCallback('#' + hash.replace(/^#/, ''));
fail = false;
if (History.useIframe) {
var ihistory = $("#jQuery_history")[0];
var iframe = ihistory.contentWindow.document;
if (iframe.location.hash != History.historyCurrentHash)
fail = true;
} else {
if (location.hash != History.historyCurrentHash)
fail = true;
}
if (fail) {
clearInterval(History.poller);
alert("Assertion failed: hash values changed");
}
};

History.init = function(callback) {
Expand All @@ -28,7 +43,7 @@ History.init = function(callback) {
History.historyCurrentHash = current_hash;

if (History.useIframe) {
// To stop the callback firing twice during initilization if no hash present
// IE quirk?
if (History.historyCurrentHash === '')
History.historyCurrentHash = '#';

Expand All @@ -40,8 +55,7 @@ History.init = function(callback) {
iframe.close();
iframe.location.hash = current_hash;
}
History.callCallback(current_hash);
setInterval(History.historyCheck, 100);
History.poller = setInterval(History.historyCheck, 100);
};

History.historyCheck = function() {
Expand Down
91 changes: 91 additions & 0 deletions media/repr.js
@@ -0,0 +1,91 @@
var DATE_MIN = 100001;
var DATE_MAX = 300001;
var DEFAULT_PAGE = 1;
var DEFAULT_URL = '';
var FS = ',';
var FS2 = '.';

if (location.pathname != "/") {
var page_match = location.pathname.match(/^\/[0-9]+$/);
if (page_match)
DEFAULT_PAGE = page_match.toString().substring(1);
else {
DEFAULT_URL = location.pathname;
if (DEFAULT_URL.charAt(DEFAULT_URL.length-1) == '/')
DEFAULT_URL = DEFAULT_URL.substring(0, DEFAULT_URL.length-1);
}
} else if (STATIC_FRONTPAGE)
DEFAULT_URL = '/cover';

function Repr(dict) {
this.query = '';
this.url = '';
this.page = DEFAULT_PAGE;
this.tags = [];
this.date_min = DATE_MIN;
this.date_max = DATE_MAX;

for (var i in dict)
this[i] = dict[i];

if (!this.url && this.tags.length === 0 && this.date_min == DATE_MIN && this.date_max == DATE_MAX && this.page == DEFAULT_PAGE && !this.query)
this.url = DEFAULT_URL;

this.toString = function() {
return 'Repr Object ' + this.serialize();
};

this.serialize = function(nav_attrs_only) {
var output = [];
var have_embed = false;
if (!nav_attrs_only && this.url) {
if (this.url != DEFAULT_URL)
output[output.length] = this.url;
have_embed = true;
}
if (this.tags.length > 0)
output[output.length] = 'tags=' + this.tags.join(FS2);
if (this.date_min == this.date_max)
output[output.length] = 'month=' + this.date_min;
else {
if (this.date_min != DATE_MIN)
output[output.length] = 'min=' + this.date_min;
if (this.date_max != DATE_MAX)
output[output.length] = 'max=' + this.date_max;
}
if (!nav_attrs_only && !have_embed) {
if (this.query)
output[output.length] = 'query=' + this.query;
else if (output.length === 0 && this.page != DEFAULT_PAGE)
output[output.length] = 'page=' + this.page;
}
return '#' + output.join(FS);
};
}

Repr.deserialize = function(hash) {
var attrs = {};
if (hash.charAt(0) == '#')
hash = hash.substring(1);
var chunks = hash.split(FS);
for (var i in chunks) {
var chunk = chunks[i];
if (attrs['query'])
attrs['query'] += ',' + chunk;
else if (chunk.substring(0,1) == '/')
attrs['url'] = chunk;
else if (chunk.substring(0,5) == 'tags=')
attrs['tags'] = chunk.substring(5).split(FS2);
else if (chunk.substring(0,5) == 'page=')
attrs['page'] = chunk.substring(5);
else if (chunk.substring(0,6) == 'month=')
attrs['date_min'] = attrs['date_max'] = chunk.substring(6);
else if (chunk.substring(0,4) == 'min=')
attrs['date_min'] = chunk.substring(4);
else if (chunk.substring(0,4) == 'max=')
attrs['date_max'] = chunk.substring(4);
else if (chunk.substring(0,6) == 'query=')
attrs['query'] = chunk.substring(6);
}
return new Repr(attrs);
};

0 comments on commit 9947dde

Please sign in to comment.