pandora/static/js/pandora/UI.js

158 lines
6.4 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-05-25 19:42:45 +00:00
pandora.UI = (function() {
2011-09-27 14:14:40 +00:00
var self = {}, that = {};
2011-09-27 14:14:40 +00:00
self.previousUI = {};
2011-09-27 14:14:40 +00:00
2011-09-26 16:46:31 +00:00
that.encode = function(val) {
return val.replace(/\./g, '\\.');
};
2011-09-27 14:14:40 +00:00
that.getPrevious = function(key) {
// fixme: probably unneeded by now
return !key ? self.previousUI : self.previousUI[key];
};
2011-09-27 14:14:40 +00:00
2011-10-22 14:50:53 +00:00
that.reset = function() {
pandora.user.ui = pandora.site.user.ui;
pandora.user.ui._list = pandora.getListsState(pandora.user.ui.find);
pandora.user.ui._groupsState = pandora.getGroupsState(pandora.user.ui.find);
pandora.user.ui._findState = pandora.getFindState(pandora.user.ui.find);
};
2011-09-26 16:46:31 +00:00
// sets pandora.user.ui.key to val
// key foo.bar.baz sets pandora.user.ui.foo.bar.baz
// val null removes a key
2011-09-28 17:32:03 +00:00
that.set = function(/* {key: val}[, flag] or key, val[, flag] */) {
2011-09-27 22:12:37 +00:00
var add = {},
2011-09-28 17:32:03 +00:00
args,
doNotTriggerEvents,
2011-09-27 22:12:37 +00:00
listSettings = pandora.site.listSettings,
2011-09-27 14:14:40 +00:00
set = {},
trigger = {};
2011-09-28 17:32:03 +00:00
if (Ox.isObject(arguments[0])) {
args = arguments[0];
triggerEvents = Ox.isUndefined(arguments[1]) ? true : arguments[1];
} else {
args = Ox.makeObject([arguments[0], arguments[1]]);
triggerEvents = Ox.isUndefined(arguments[2]) ? true : arguments[1];
}
2011-09-27 22:12:37 +00:00
Ox.print('UI SET', args)
self.previousUI = Ox.clone(pandora.user.ui, true);
self.previousUI._list = pandora.getListsState(self.previousUI.find);
if ('find' in args) {
// the challenge here is that find may change list,
// and list may then change listSort and listView,
// which we don't want to trigger, since find triggers
// (values we put in add will be changed, but won't trigger)
var list = pandora.getListsState(args.find);
pandora.user.ui._list = list;
pandora.user.ui._groupsState = pandora.getGroupsState(args.find);
pandora.user.ui._findState = pandora.getFindState(args.find);
if (pandora.$ui.appPanel) {
// if we're not on page load,
// switch from item view to list view
args['item'] = '';
}
if (!pandora.user.ui.lists[list]) {
add['lists.' + that.encode(list)] = {};
}
if (list != self.previousUI._list) {
2011-10-23 09:13:57 +00:00
if (!pandora.user.ui.lists[list]) {
add['lists.' + that.encode(list)] = {};
}
Ox.forEach(listSettings, function(listSetting, setting) {
if (!pandora.user.ui.lists[list]) {
// add default list setting and copy to settings
2011-10-26 08:29:07 +00:00
add['lists.' + that.encode(list) + '.' + listSetting] = pandora.site.user.ui[setting];
add[setting] = pandora.site.user.ui[setting];
} else {
// copy lists setting to settings
add[setting] = pandora.user.ui.lists[list][listSetting]
}
});
}
}
// it is important to check for find first, so that if find
// changes list, pandora.user.ui._list is correct here
var item = args['item'] || pandora.user.ui.item,
list = pandora.user.ui._list || '';
Ox.print('item/list', item, list, '...', args['videoPoints.' + item])
2011-10-23 14:25:23 +00:00
if (!pandora.user.ui.lists[list]) {
add['lists.' + that.encode(list)] = {};
}
Ox.forEach(args, function(val, key) {
if (Object.keys(listSettings).indexOf(key) > -1) {
// if applicable, copy setting to list setting
add['lists.' + that.encode(list) + '.' + listSettings[key]] = val;
2011-10-17 10:19:17 +00:00
}
if (key == 'item' && val) {
2011-09-27 22:12:37 +00:00
// when switching to an item, update list selection
add['listSelection'] = [val];
if (!pandora.user.ui.lists[list]) {
add['lists.' + that.encode(list)] = {};
}
add['lists.' + that.encode(list) + '.selection'] = [val];
}
if (!args['videoPoints.' + item] && ((
key == 'item'
&& ['video', 'timeline'].indexOf(pandora.user.ui.itemView) > -1
&& !pandora.user.ui.videoPoints[val]
) || (
key == 'itemView'
&& ['video', 'timeline'].indexOf(val) > -1
&& !pandora.user.ui.videoPoints[item]
))) {
// when switching to a video view, add default videoPoints
add['videoPoints.' + item] = {'in': 0, out: 0, position: 0};
}
2011-10-17 10:19:17 +00:00
if (key == 'itemView' && ['video', 'timeline'].indexOf(val) > -1) {
// when switching to a video view, add it as default video view
add.videoView = val;
}
2011-09-27 22:12:37 +00:00
});
2011-09-28 17:32:03 +00:00
[args, add].forEach(function(obj, isAdd) {
2011-09-27 22:12:37 +00:00
Ox.forEach(obj, function(val, key) {
Ox.print('key/val', key, val)
// make sure to not split at escaped dots ('\.')
var keys = key.replace(/\\\./g, '\n').split('.').map(function(key) {
return key.replace(/\n/g, '.')
}),
2011-09-27 22:12:37 +00:00
ui = pandora.user.ui;
while (keys.length > 1) {
ui = ui[keys.shift()];
2011-05-25 19:42:45 +00:00
}
2011-09-27 22:12:37 +00:00
if (!Ox.isEqual(ui[keys[0]], val)) {
if (val === null) {
delete ui[keys[0]]
} else {
ui[keys[0]] = val;
}
2011-09-27 14:14:40 +00:00
set[key] = val;
2011-09-28 17:32:03 +00:00
if (!isAdd) {
2011-09-27 22:12:37 +00:00
trigger[key] = val;
2011-09-27 14:14:40 +00:00
}
2011-10-08 13:09:16 +00:00
}
2011-09-27 22:12:37 +00:00
});
});
2011-09-27 22:12:37 +00:00
Ox.len(set) && pandora.api.setUI(set);
2011-09-28 17:32:03 +00:00
triggerEvents && Ox.forEach(trigger, function(val, key) {
2011-09-27 22:12:37 +00:00
Ox.forEach(pandora.$ui, function(element) {
element.ox && element.triggerEvent('pandora_' + key.toLowerCase(), {
value: val,
previousValue: self.previousUI[key]
});
2011-05-25 19:42:45 +00:00
});
2011-09-27 22:12:37 +00:00
});
2011-10-10 07:32:50 +00:00
2011-10-01 13:51:18 +00:00
pandora.URL.update(Object.keys(
!pandora.$ui.appPanel ? args : trigger
));
};
2011-09-27 14:14:40 +00:00
return that;
2011-09-27 14:14:40 +00:00
2011-05-25 19:42:45 +00:00
}());