refactor UI.js, fixes a bug where list deselection wouldn't work after loading an item of that list and going back

This commit is contained in:
rolux 2011-11-05 13:09:51 +00:00
parent a4085880b4
commit d67a1cdfba
2 changed files with 56 additions and 44 deletions

View file

@ -25,12 +25,16 @@ pandora.UI = (function() {
// key foo.bar.baz sets pandora.user.ui.foo.bar.baz // key foo.bar.baz sets pandora.user.ui.foo.bar.baz
// val null removes a key // val null removes a key
that.set = function(/* {key: val}[, flag] or key, val[, flag] */) { that.set = function(/* {key: val}[, flag] or key, val[, flag] */) {
var add = {}, var add = {},
args, args,
doNotTriggerEvents, doNotTriggerEvents,
item,
list,
listSettings = pandora.site.listSettings, listSettings = pandora.site.listSettings,
set = {}, set = {},
trigger = {}; trigger = {};
if (Ox.isObject(arguments[0])) { if (Ox.isObject(arguments[0])) {
args = arguments[0]; args = arguments[0];
triggerEvents = Ox.isUndefined(arguments[1]) ? true : arguments[1]; triggerEvents = Ox.isUndefined(arguments[1]) ? true : arguments[1];
@ -38,15 +42,18 @@ pandora.UI = (function() {
args = Ox.makeObject([arguments[0], arguments[1]]); args = Ox.makeObject([arguments[0], arguments[1]]);
triggerEvents = Ox.isUndefined(arguments[2]) ? true : arguments[1]; triggerEvents = Ox.isUndefined(arguments[2]) ? true : arguments[1];
} }
Ox.Log('', 'UI SET', args)
Ox.Log('UI SET', args)
self.previousUI = Ox.clone(pandora.user.ui, true); self.previousUI = Ox.clone(pandora.user.ui, true);
self.previousUI._list = pandora.getListsState(self.previousUI.find); self.previousUI._list = pandora.getListsState(self.previousUI.find);
if ('find' in args) { if ('find' in args) {
// the challenge here is that find may change list, // the challenge here is that find may change list,
// and list may then change listSort and listView, // and list may then change listSort and listView,
// which we don't want to trigger, since find triggers // which we don't want to trigger, since find triggers
// (values we put in add will be changed, but won't trigger) // (values we put in add will be changed, but won't trigger)
var list = pandora.getListsState(args.find); list = pandora.getListsState(args.find);
pandora.user.ui._list = list; pandora.user.ui._list = list;
pandora.user.ui._groupsState = pandora.getGroupsState(args.find); pandora.user.ui._groupsState = pandora.getGroupsState(args.find);
pandora.user.ui._findState = pandora.getFindState(args.find); pandora.user.ui._findState = pandora.getFindState(args.find);
@ -56,23 +63,16 @@ pandora.UI = (function() {
args['item'] = ''; args['item'] = '';
} }
add['itemFind'] = pandora.site.user.ui.itemFind; add['itemFind'] = pandora.site.user.ui.itemFind;
if (!pandora.user.ui.lists[list]) {
add['lists.' + that.encode(list)] = {};
}
if (list != self.previousUI._list) { if (list != self.previousUI._list) {
Ox.Log('', 'FIND HAS CHANGED LIST') Ox.Log('', 'FIND HAS CHANGED LIST')
if (!pandora.user.ui.lists[list]) { // if find has changed list
add['lists.' + that.encode(list)] = {};
}
Ox.forEach(listSettings, function(listSetting, setting) { Ox.forEach(listSettings, function(listSetting, setting) {
// then for each setting that corresponds to a list setting
if (!pandora.user.ui.lists[list]) { if (!pandora.user.ui.lists[list]) {
// add default list setting and copy to settings // either add the default setting
add[
'lists.' + that.encode(list) + '.' + listSetting
] = pandora.site.user.ui[setting];
add[setting] = pandora.site.user.ui[setting]; add[setting] = pandora.site.user.ui[setting];
} else { } else {
// copy lists setting to settings // or the existing list setting
add[setting] = pandora.user.ui.lists[list][listSetting] add[setting] = pandora.user.ui.lists[list][listSetting]
} }
}); });
@ -80,43 +80,55 @@ pandora.UI = (function() {
add.itemFind = pandora.isItemFind(args.find) add.itemFind = pandora.isItemFind(args.find)
? args.find : pandora.site.user.ui.itemFind; ? args.find : pandora.site.user.ui.itemFind;
} }
// it is important to check for find first, so that if find // it is important to check for find first, so that if find
// changes list, pandora.user.ui._list is correct here // changes list, pandora.user.ui._list is correct here
var item = args['item'] || pandora.user.ui.item, item = args['item'] || pandora.user.ui.item,
list = pandora.user.ui._list || ''; list = pandora.user.ui._list || self.previousUI._list;
if (!pandora.user.ui.lists[list]) { if (!pandora.user.ui.lists[list]) {
add['lists.' + that.encode(list)] = {}; add['lists.' + that.encode(list)] = {};
} }
Ox.forEach(args, function(val, key) { Ox.forEach(listSettings, function(listSetting, setting) {
if (Object.keys(listSettings).indexOf(key) > -1) { // for each setting that corresponds to a list setting
// if applicable, copy setting to list setting // set that list setting to
add['lists.' + that.encode(list) + '.' + listSettings[key]] = val; var key = 'lists.' + that.encode(list) + '.' + listSetting;
} if (setting in args) {
if (key == 'item' && val) { // the setting passed to UI.set
// when switching to an item, update list selection add[key] = args[setting];
add['listSelection'] = [val]; } else if (setting in add) {
if (!pandora.user.ui.lists[list]) { // or the setting changed via find
add['lists.' + that.encode(list)] = {}; add[key] = add[setting];
} } else if (!pandora.user.ui.lists[list]) {
add['lists.' + that.encode(list) + '.selection'] = [val]; // or the default setting
} add[key] = pandora.site.user.ui[setting];
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};
}
if (key == 'itemView' && ['video', 'timeline'].indexOf(val) > -1) {
// when switching to a video view, add it as default video view
add.videoView = val;
} }
}); });
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
&& ['video', 'timeline'].indexOf(pandora.user.ui.itemView) > -1
&& !pandora.user.ui.videoPoints[item]
) {
// if the item view won't be changed, remains a video view,
// and there are no video points yet, add default video points
add['videoPoints.' + item] = {'in': 0, out: 0, position: 0};
}
}
if (['video', 'timeline'].indexOf(args.itemView) > -1) {
// when switching to a video view, add it as default video view
args.videoView = args.itemView;
if (!pandora.user.ui.videoPoints[item]) {
// if there are no video points yet, add default video points
add['videoPoints.' + item] = {'in': 0, out: 0, position: 0};
}
}
[args, add].forEach(function(obj, isAdd) { [args, add].forEach(function(obj, isAdd) {
Ox.forEach(obj, function(val, key) { Ox.forEach(obj, function(val, key) {
Ox.Log('', 'key/val', key, val) Ox.Log('', 'key/val', key, val)

View file

@ -174,7 +174,7 @@ pandora.enableDragAndDrop = function($list, canMove) {
drag.source = pandora.getListData(), drag.source = pandora.getListData(),
drag.targets = {}; drag.targets = {};
Ox.forEach(pandora.$ui.folderList, function($list) { Ox.forEach(pandora.$ui.folderList, function($list) {
$list.addClass('OxDroppable').find('.OxItem').each(function() { $list.addClass('OxDroppable').$element.find('.OxItem').each(function() {
var $item = $(this), var $item = $(this),
id = $item.data('id'), id = $item.data('id'),
data = $list.value(id); data = $list.value(id);