pandora/static/js/pandora/URL.js

276 lines
12 KiB
JavaScript
Raw Normal View History

2011-07-29 18:37:11 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-06-20 18:34:23 +00:00
2011-05-25 19:42:45 +00:00
pandora.URL = (function() {
var previousUI = {},
previousURL = '',
2011-09-17 07:07:59 +00:00
regexps = {
'^$': function(pathname, search) {
2011-09-17 12:16:31 +00:00
if (/^\?url=/.test(search)) {
2011-09-17 07:07:59 +00:00
document.location = decodeURIComponent(search.substr(5));
} else {
2011-09-17 12:16:31 +00:00
if (!search && pandora.user.ui.showHome) {
pandora.$ui.home = pandora.ui.home().showScreen();
Ox.print('LIST', pandora.user.ui.list)
2011-09-17 13:04:10 +00:00
pandora.user.ui.list && pandora.Query.fromString(
'find=list:' + pandora.user.ui.list
);
} else {
pandora.Query.fromString(search);
pandora.UI.set({
section: 'items',
item: ''
});
2011-09-17 12:16:31 +00:00
}
}
},
2011-09-17 07:07:59 +00:00
'^home$': function(pathname, search) {
pandora.$ui.home = pandora.ui.home().showScreen();
2011-08-07 17:05:49 +00:00
},
2011-09-17 07:07:59 +00:00
'^(items|edits|texts)$': function(pathname, search) {
2011-05-25 19:42:45 +00:00
pandora.UI.set({
2011-09-17 07:07:59 +00:00
section: pathname
2011-05-25 19:42:45 +00:00
});
},
2011-09-17 07:07:59 +00:00
'^(about|contact|faq|news|software|terms|tour)$': function(pathname, search) {
pandora.$ui.siteDialog = pandora.ui.siteDialog(pathname).open();
},
2011-09-17 07:07:59 +00:00
'^help$': function(pathname, search) {
pandora.$ui.helpDialog = pandora.ui.helpDialog().open();
},
'^(signin|signout|signup)$': function(pathname, search) {
if ((pathname == 'signout') == (pandora.user.level != 'guest')) {
pandora.$ui.accountDialog = (
pandora.user.level == 'guest'
2011-09-17 07:07:59 +00:00
? pandora.ui.accountDialog(pathname)
: pandora.ui.accountSignoutDialog()
).open();
}
},
2011-09-17 07:07:59 +00:00
'^preferences$': function(pathname, search) {
pandora.$ui.preferencesDialog = pandora.ui.preferencesDialog().open();
},
2011-09-17 07:07:59 +00:00
'^(calendar|calendars|clip|clips|flow|grid|list|map|maps|timelines)$': function(pathname, search) {
2011-05-25 19:42:45 +00:00
pandora.UI.set({
section: 'items',
item: ''
});
pandora.UI.set('lists|' + pandora.user.ui.list + '|listView', pathname);
2011-09-17 07:07:59 +00:00
pandora.Query.fromString(search);
2011-08-07 17:05:49 +00:00
},
2011-09-17 07:07:59 +00:00
'^[0-9A-Z]': function(pathname, search) {
var split = pathname.split('/'),
2011-05-25 19:42:45 +00:00
item = split[0],
2011-09-17 07:07:59 +00:00
points,
time = split.length > 1
? /[\d\.:,-]+/.exec(split[split.length - 1])
: null,
2011-05-25 19:42:45 +00:00
view = new RegExp(
'^(' + pandora.site.itemViews.map(function(v) {
return v.id;
}).join('|') + ')$'
2011-09-17 07:07:59 +00:00
).exec(split[1]);
view = view ? view[0]
2011-09-17 07:07:59 +00:00
: time ? pandora.user.ui.videoView
: pandora.user.ui.itemView;
pandora.UI.set(Ox.extend({
2011-05-25 19:42:45 +00:00
section: 'items',
item: item,
itemView: view
}, ['player', 'timeline'].indexOf(view) > -1 ? {
videoView: view
} : {}));
2011-09-17 07:07:59 +00:00
if (time) {
points = time[0].split(',');
if (points.length == 2) {
points = Ox.flatten([points[0], points[1].split('-')]);
if (points[2] == '') {
points[2] = '-1';
}
}
// fixme: this is duplicated, see Ox.VideoPlayer() parsePositionInput()
points = points.map(function(point, i) {
var parts = point.split(':').reverse();
while (parts.length > 3) {
parts.pop();
}
2011-09-17 07:07:59 +00:00
return parts.reduce(function(prev, curr, i) {
return prev + (parseFloat(curr) || 0) * Math.pow(60, i);
}, 0);
2011-09-17 07:07:59 +00:00
});
pandora.UI.set('videoPoints|' + item, {
'in': points.length == 1 ? 0 : points[points.length - 2],
out: points.length == 1 ? 0 : points[points.length - 1],
position: points[0],
});
points = points.map(function(point) {
return point == -1 ? '' : Ox.formatDuration(point, 3).replace(/\.000$/, '');
});
split[split.length - 1] = points[0] + (
points.length == 1 ? '' : ',' + points[1] + '-' + points[2]
);
pandora.URL.replace(split.join('/'));
} else if (!pandora.user.ui.videoPoints[item]) {
pandora.UI.set('videoPoints|' + item, {
'in': 0, out: 0, position: 0
});
}
2011-09-17 07:07:59 +00:00
},
'.*': function(pathname, search) {
var query = search || 'find=' + pathname;
2011-09-17 09:15:24 +00:00
pandora.UI.set({
section: 'items',
item: ''
});
2011-09-17 07:07:59 +00:00
pandora.Query.fromString(query);
pandora.URL.replace('?' + query);
2011-05-25 19:42:45 +00:00
}
};
2011-09-17 07:07:59 +00:00
function saveURL() {
previousURL = document.location.pathname + document.location.search;
}
2011-05-25 19:42:45 +00:00
return {
2011-09-17 07:07:59 +00:00
parse: function() {
var pathname = document.location.pathname.substr(1),
search = document.location.search.substr(1);
if (/\/$/.test(pathname)) {
pathname = pathname.substr(0, pathname.length - 1);
}
/*
var url = document.location.pathname.substr(1)
+ document.location.search
+ document.location.hash;
*/
Ox.forEach(regexps, function(fn, re) {
re = new RegExp(re);
if (re.test(pathname)) {
Ox.print('URL re ' + re);
fn(pathname, search);
return false;
}
});
2011-09-17 13:04:10 +00:00
pandora.Query.updateGroups();
2011-09-17 07:07:59 +00:00
pandora.user.ui.showHome = false;
},
push: function(title, url) {
2011-05-25 19:42:45 +00:00
if (arguments.length == 1) { // fixme: remove later
url = title;
}
2011-08-07 17:05:49 +00:00
if (url[0] != '/') {
url = '/' + url;
}
2011-09-17 07:07:59 +00:00
saveURL();
2011-08-07 17:05:49 +00:00
history.pushState({}, pandora.site.site.name + (title ? ' - ' + title : ''), url);
2011-05-25 19:42:45 +00:00
},
2011-09-17 07:07:59 +00:00
pushPrevious: function() {
history.pushState({}, '', previousURL);
2011-05-25 19:42:45 +00:00
},
2011-09-17 07:07:59 +00:00
set: function(title, url) {
if (arguments.length == 1) { // fixme: remove later
url = title;
}
if (url[0] != '/') {
url = '/' + url;
}
history.pushState({}, pandora.site.site.name + (title ? ' - ' + title : ''), url);
this._update();
},
replace: function(title, url) {
if (arguments.length == 1) { // fixme: remove later
url = title;
}
if (url[0] != '/') {
url = '/' + url;
}
2011-09-17 07:07:59 +00:00
saveURL();
history.replaceState({}, pandora.site.site.name + (title ? ' - ' + title : ''), url);
},
2011-05-25 19:42:45 +00:00
update: function() {
this.set(pandora.Query.toString());
},
_update: function() {
// fixme: make this method private
var previousUI = pandora.UI.getPrevious();
Ox.print('now', pandora.user.ui, 'previously', previousUI)
2011-08-17 15:35:30 +00:00
Ox.Request.cancel();
2011-08-19 20:38:19 +00:00
$('video').each(function() {
2011-08-19 20:55:02 +00:00
$(this).trigger('stop');
2011-08-19 20:38:19 +00:00
});
2011-05-25 19:42:45 +00:00
this.parse();
if (pandora.user.ui.section != previousUI.section) {
2011-06-06 15:48:11 +00:00
pandora.$ui.appPanel.replaceElement(1, pandora.$ui.mainPanel = pandora.ui.mainPanel());
} else if (!pandora.user.ui.item && !previousUI.item) {
// list to list
var isClipView = pandora.isClipView(),
list = pandora.user.ui.lists[pandora.user.ui.list],
previousList = previousUI.lists[previousUI.list],
wasClipView = pandora.isClipView(previousList.listView);
if (list.listView != previousList.listView) {
pandora.$ui.mainMenu.checkItem('viewMenu_movies_' + list.listView);
pandora.$ui.viewSelect.options({value: list.listView});
Ox.print('is', isClipView, 'was', wasClipView);
if (isClipView != wasClipView) {
pandora.$ui.mainMenu.replaceMenu('sortMenu', pandora.getSortMenu());
pandora.$ui.sortSelect.replaceWith(pandora.$ui.sortSelect = pandora.ui.sortSelect());
if (isClipView && !wasClipView) {
pandora.UI.set('lists|' + pandora.user.ui.list + '|selected', []);
}
}
}
if (!Ox.isEqual(list.sort, previousList.sort)) {
pandora.$ui.mainMenu.checkItem('sortMenu_sortmovies_' + list.sort[0].key);
pandora.$ui.mainMenu.checkItem('sortMenu_ordermovies_' + (
list.sort[0].operator == '' ? pandora.getSortOperator(list.sort[0].key)
: list.sort[0].operator == '+' ? 'ascending' : 'descending'
));
pandora.$ui.sortSelect.options({value: list.sort[0].key});
}
pandora.$ui.leftPanel.replaceElement(2, pandora.$ui.info = pandora.ui.info());
Ox.print('EQUAL?', pandora.user.ui.query, previousUI.query, Ox.isEqual(pandora.user.ui.query, previousUI.query))
if (Ox.isEqual(pandora.user.ui.query, previousUI.query)) {
pandora.$ui.contentPanel.replaceElement(1, pandora.$ui.list = pandora.ui.list());
} else {
pandora.$ui.mainPanel.replaceElement(1, pandora.$ui.rightPanel = pandora.ui.rightPanel());
}
2011-09-18 02:22:50 +00:00
// fixme: should list selection and deselection happen here?
// (home and menu may cause a list switch)
} else if (!pandora.user.ui.item || !previousUI.item) {
// list to item or item to list
2011-06-06 15:48:11 +00:00
pandora.$ui.leftPanel.replaceElement(2, pandora.$ui.info = pandora.ui.info());
2011-08-08 13:58:28 +00:00
pandora.$ui.mainPanel.replaceElement(1, pandora.$ui.rightPanel = pandora.ui.rightPanel());
2011-05-25 19:42:45 +00:00
} else {
// item to item
if (pandora.user.ui.item != previousUI.item) {
2011-09-18 21:18:05 +00:00
pandora.$ui.leftPanel.replaceElement(2, pandora.$ui.info = pandora.ui.info());
}
2011-08-08 13:58:28 +00:00
pandora.$ui.contentPanel.replaceElement(1, pandora.ui.item());
2011-05-25 19:42:45 +00:00
}
2011-09-17 07:07:59 +00:00
// fixme: should be 'video', not 'player'
2011-05-25 19:42:45 +00:00
if (
previousUI.item &&
['player', 'timeline'].indexOf(previousUI.itemView) > -1
2011-05-25 19:42:45 +00:00
) {
var $item = pandora.$ui[
previousUI.itemView == 'player' ? 'player' : 'editor'
];
$item && pandora.UI.set(
'videoPoints|' + previousUI.item + '|position',
$item.options('position')
2011-05-25 19:42:45 +00:00
);
}
}
2011-06-20 18:34:23 +00:00
};
2011-05-25 19:42:45 +00:00
}());