2011-07-29 18:37:11 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
2012-02-03 09:15:38 +00:00
|
|
|
'use strict';
|
2012-01-30 20:48:04 +00:00
|
|
|
|
2011-05-25 19:42:45 +00:00
|
|
|
pandora.UI = (function() {
|
2011-09-27 14:14:40 +00:00
|
|
|
|
2011-09-23 10:44:54 +00:00
|
|
|
var self = {}, that = {};
|
2011-09-27 14:14:40 +00:00
|
|
|
|
2011-09-23 10:44:54 +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
|
|
|
|
2011-09-23 10:44:54 +00:00
|
|
|
that.getPrevious = function(key) {
|
2011-09-27 23:30:13 +00:00
|
|
|
// fixme: probably unneeded by now
|
2011-09-23 10:44:54 +00:00
|
|
|
return !key ? self.previousUI : self.previousUI[key];
|
|
|
|
};
|
2011-09-27 14:14:40 +00:00
|
|
|
|
2012-06-30 20:01:43 +00:00
|
|
|
that.reset = function() {
|
|
|
|
pandora.api.resetUI({}, function() {
|
|
|
|
pandora.user.ui = pandora.site.user.ui;
|
|
|
|
pandora.user.ui._list = pandora.getListState(pandora.user.ui.find);
|
|
|
|
pandora.user.ui._filterState = pandora.getFilterState(pandora.user.ui.find);
|
|
|
|
pandora.user.ui._findState = pandora.getFindState(pandora.user.ui.find);
|
|
|
|
Ox.Theme(pandora.user.ui.theme);
|
|
|
|
pandora.$ui.appPanel.reload();
|
|
|
|
});
|
2011-10-22 14:50:53 +00:00
|
|
|
};
|
|
|
|
|
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-11-05 13:09:51 +00:00
|
|
|
|
2011-09-27 22:12:37 +00:00
|
|
|
var add = {},
|
2011-09-28 17:32:03 +00:00
|
|
|
args,
|
2011-11-05 13:09:51 +00:00
|
|
|
item,
|
|
|
|
list,
|
2011-09-27 22:12:37 +00:00
|
|
|
listSettings = pandora.site.listSettings,
|
2011-11-07 21:25:57 +00:00
|
|
|
listView,
|
2011-09-27 14:14:40 +00:00
|
|
|
set = {},
|
2011-11-05 17:20:40 +00:00
|
|
|
trigger = {},
|
|
|
|
triggerEvents;
|
2011-11-05 13:09:51 +00:00
|
|
|
|
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-11-05 13:09:51 +00:00
|
|
|
|
2012-02-17 16:13:48 +00:00
|
|
|
Ox.Log('UI', 'SET', JSON.stringify(args));
|
2011-11-05 13:09:51 +00:00
|
|
|
|
2011-09-23 10:44:54 +00:00
|
|
|
self.previousUI = Ox.clone(pandora.user.ui, true);
|
2011-11-06 08:28:10 +00:00
|
|
|
self.previousUI._list = pandora.getListState(self.previousUI.find);
|
2011-11-05 13:09:51 +00:00
|
|
|
|
2011-10-13 16:48:08 +00:00
|
|
|
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)
|
2011-11-06 08:28:10 +00:00
|
|
|
list = pandora.getListState(args.find);
|
2011-10-13 16:48:08 +00:00
|
|
|
pandora.user.ui._list = list;
|
2011-11-06 08:28:10 +00:00
|
|
|
pandora.user.ui._filterState = pandora.getFilterState(args.find);
|
2011-10-13 16:48:08 +00:00
|
|
|
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
|
2011-10-29 11:39:00 +00:00
|
|
|
args['item'] = '';
|
2011-10-13 16:48:08 +00:00
|
|
|
}
|
|
|
|
if (list != self.previousUI._list) {
|
2011-11-08 09:03:23 +00:00
|
|
|
Ox.Log('UI', 'FIND HAS CHANGED LIST')
|
2011-11-05 13:09:51 +00:00
|
|
|
// if find has changed list
|
2011-10-13 16:48:08 +00:00
|
|
|
Ox.forEach(listSettings, function(listSetting, setting) {
|
2011-11-05 13:09:51 +00:00
|
|
|
// then for each setting that corresponds to a list setting
|
2011-10-13 13:39:56 +00:00
|
|
|
if (!pandora.user.ui.lists[list]) {
|
2011-11-05 13:09:51 +00:00
|
|
|
// either add the default setting
|
2011-10-13 16:48:08 +00:00
|
|
|
add[setting] = pandora.site.user.ui[setting];
|
|
|
|
} else {
|
2011-11-05 13:09:51 +00:00
|
|
|
// or the existing list setting
|
2011-10-13 16:48:08 +00:00
|
|
|
add[setting] = pandora.user.ui.lists[list][listSetting]
|
2011-10-13 13:39:56 +00:00
|
|
|
}
|
2011-10-13 16:48:08 +00:00
|
|
|
});
|
|
|
|
}
|
2012-02-01 12:01:39 +00:00
|
|
|
add.itemFind = pandora.getItemFind(args.find);
|
2011-11-07 19:40:09 +00:00
|
|
|
} else {
|
|
|
|
list = self.previousUI._list;
|
2011-10-13 16:48:08 +00:00
|
|
|
}
|
2011-11-07 19:40:09 +00:00
|
|
|
// it is important to check for find first, so that
|
|
|
|
// if find changes list, list is correct here
|
2011-11-07 21:04:44 +00:00
|
|
|
item = args.item || pandora.user.ui.item;
|
2011-11-07 21:25:57 +00:00
|
|
|
listView = add.listView || args.listView;
|
2011-11-07 21:04:44 +00:00
|
|
|
|
2011-11-07 21:25:57 +00:00
|
|
|
if (listView) {
|
|
|
|
if (pandora.isClipView(listView)) {
|
2011-11-07 21:04:44 +00:00
|
|
|
// when switching to a clip view, clear list selection
|
2011-11-07 21:25:57 +00:00
|
|
|
// (but don't trigger an additional event)
|
|
|
|
add.listSelection = [];
|
2011-11-07 21:04:44 +00:00
|
|
|
} else if (['text', 'position'].indexOf(pandora.user.ui.listSort[0].key) > -1) {
|
2012-01-30 20:48:04 +00:00
|
|
|
// when switching to a non-clip view, with a sort key
|
2011-11-08 09:03:23 +00:00
|
|
|
// that only exists in clip view, reset sort to default
|
2011-11-07 21:04:44 +00:00
|
|
|
args.listSort = pandora.site.user.ui.listSort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-23 14:25:23 +00:00
|
|
|
if (!pandora.user.ui.lists[list]) {
|
|
|
|
add['lists.' + that.encode(list)] = {};
|
|
|
|
}
|
2011-11-05 13:09:51 +00:00
|
|
|
Ox.forEach(listSettings, function(listSetting, setting) {
|
|
|
|
// for each setting that corresponds to a list setting
|
|
|
|
// set that list setting to
|
|
|
|
var key = 'lists.' + that.encode(list) + '.' + listSetting;
|
|
|
|
if (setting in args) {
|
|
|
|
// the setting passed to UI.set
|
|
|
|
add[key] = args[setting];
|
|
|
|
} else if (setting in add) {
|
|
|
|
// or the setting changed via find
|
|
|
|
add[key] = add[setting];
|
|
|
|
} else if (!pandora.user.ui.lists[list]) {
|
|
|
|
// or the default setting
|
|
|
|
add[key] = pandora.site.user.ui[setting];
|
2011-09-23 10:44:54 +00:00
|
|
|
}
|
2011-11-05 13:09:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (args.item) {
|
|
|
|
// when switching to an item, update list selection
|
|
|
|
add['listSelection'] = [args.item];
|
|
|
|
add['lists.' + that.encode(list) + '.selection'] = [args.item];
|
|
|
|
if (
|
|
|
|
!args.itemView
|
2012-04-17 08:03:11 +00:00
|
|
|
&& ['timeline', 'player', 'editor'].indexOf(pandora.user.ui.itemView) > -1
|
2011-10-20 12:20:26 +00:00
|
|
|
&& !pandora.user.ui.videoPoints[item]
|
2011-11-08 09:03:23 +00:00
|
|
|
&& !args['videoPoints.' + item]
|
2011-11-05 13:09:51 +00:00
|
|
|
) {
|
2011-11-08 09:03:23 +00:00
|
|
|
// if the item view doesn't change, remains a video view,
|
|
|
|
// video points don't exist yet, and won't be set,
|
|
|
|
// add default video points
|
2012-01-09 09:55:52 +00:00
|
|
|
add['videoPoints.' + item] = {annotation: '', 'in': 0, out: 0, position: 0};
|
2011-10-20 12:20:26 +00:00
|
|
|
}
|
2011-11-05 13:09:51 +00:00
|
|
|
}
|
|
|
|
|
2012-04-17 08:03:11 +00:00
|
|
|
if (['timeline', 'player', 'editor'].indexOf(args.itemView) > -1) {
|
2011-11-05 13:09:51 +00:00
|
|
|
// when switching to a video view, add it as default video view
|
|
|
|
args.videoView = args.itemView;
|
2011-11-08 09:03:23 +00:00
|
|
|
if (
|
|
|
|
!pandora.user.ui.videoPoints[item]
|
|
|
|
&& !args['videoPoints.' + item]
|
|
|
|
) {
|
|
|
|
// if video points don't exist yet, and won't be set,
|
|
|
|
// add default video points
|
2012-01-09 09:55:52 +00:00
|
|
|
add['videoPoints.' + item] = {annotation: '', 'in': 0, out: 0, position: 0};
|
2011-10-17 10:19:17 +00:00
|
|
|
}
|
2011-11-05 13:09:51 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 20:48:04 +00:00
|
|
|
// items in args trigger events, items in add do not
|
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) {
|
2011-10-13 16:48:08 +00:00
|
|
|
// 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-23 10:44:54 +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) {
|
2012-05-22 14:08:27 +00:00
|
|
|
Ox.UI.isElement(element) && element.triggerEvent('pandora_' + key.toLowerCase(), {
|
2011-09-27 22:12:37 +00:00
|
|
|
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
|
|
|
|
));
|
2012-01-30 20:48:04 +00:00
|
|
|
|
2011-09-23 10:44:54 +00:00
|
|
|
};
|
2011-09-27 14:14:40 +00:00
|
|
|
|
2011-09-23 10:44:54 +00:00
|
|
|
return that;
|
2011-09-27 14:14:40 +00:00
|
|
|
|
2011-05-25 19:42:45 +00:00
|
|
|
}());
|
|
|
|
|