@ -15,7 +15,6 @@ define(
template : 'Settings/Quality/Profile/EditQualityProfileViewTemplate' ,
ui : {
available : '.x-available-list' ,
allowed : '.x-allowed-list' ,
cutoff : '.x-cutoff'
} ,
@ -26,101 +25,62 @@ define(
initialize : function ( options ) {
this . profileCollection = options . profileCollection ;
this . availableCollection = new Backbone . Collection ( this . model . get ( 'available' ) ) ;
this . availableCollection . comparator = function ( model ) { return - model . get ( 'weight' ) ; } ;
this . availableCollection . sort ( ) ;
this . allowedCollection = new Backbone . Collection ( this . model . get ( 'allowed' ) ) ;
this . allowedCollection . comparator = function ( model ) { return - model . get ( 'weight' ) ; } ;
this . allowedCollection . sort ( ) ;
this . allowedCollection . comparator = undefined ;
this . allowedCollection = new Backbone . Collection ( _ . toArray ( this . model . get ( 'items' ) ) . reverse ( ) ) ;
} ,
onRender : function ( ) {
var listViewAvailable = new BackboneSortableCollectionView ( {
el : this . ui . available ,
modelView : EditQualityProfileItemView ,
selectable : false ,
sortable : false ,
collection : this . availableCollection
} ) ;
var listViewAllowed = new BackboneSortableCollectionView ( {
el : this . ui . allowed ,
modelView : EditQualityProfileItemView ,
selectable : false ,
sortable : true ,
onRender : function ( ) {
var MyCollectionView = BackboneSortableCollectionView . extend ( {
events : {
// Backbone.CollectionView used mousedown for the click event, which interferes with the sortable.
"click li, td" : "_listItem_onMousedown" ,
"dblclick li, td" : "_listItem_onDoubleClick" ,
"click" : "_listBackground_onClick" ,
"click ul.collection-list, table.collection-list" : "_listBackground_onClick" ,
"keydown" : "_onKeydown"
}
} ) ;
var listViewAllowed = new MyCollectionView ( {
el : this . ui . allowed ,
modelView : EditQualityProfileItemView ,
selectable : true ,
selectMultiple : true ,
clickToSelect : true ,
clickToToggle : true ,
sortable : true ,
sortableOptions : {
handle : '.x-drag-handle'
} ,
collection : this . allowedCollection
} ) ;
listViewAvailable . render ( ) ;
listViewAllowed . setSelectedModels ( this . allowedCollection . filter ( function ( item ) { return item . get ( 'allowed' ) === true ; } ) ) ;
listViewAllowed . render ( ) ;
this . listenTo ( listViewAvailable , 'doubleClick' , this . _moveQuality ) ;
this . listenTo ( listViewAllowed , 'doubleClick' , this . _moveQuality ) ;
this . listenTo ( listViewAvailable , 'moveClicked' , this . _moveQuality ) ;
this . listenTo ( listViewAllowed , 'moveClicked' , this . _moveQuality ) ;
this . listenTo ( listViewAllowed , 'selectionChanged' , this . _selectionChanged ) ;
this . listenTo ( listViewAllowed , 'sortStop' , this . _updateModel ) ;
} ,
_moveQuality : function ( event ) {
var quality ;
var qualityId = event . get ( 'id' ) ;
_selectionChanged : function ( newSelectedModels , oldSelectedModels ) {
var addedModels = _ . difference ( newSelectedModels , oldSelectedModels ) ;
var removeModels = _ . difference ( oldSelectedModels , newSelectedModels ) ;
if ( this . availableCollection . get ( qualityId ) ) {
quality = this . availableCollection . get ( qualityId ) ;
var idealIndex = 0 ;
var idealMismatches = 1000 ;
// Insert it at the best possible spot.
for ( var i = 0 ; i <= this . allowedCollection . length ; i ++ ) {
var mismatches = 0 ;
for ( var j = 0 ; j < i ; j ++ ) {
if ( this . allowedCollection . at ( j ) . get ( 'weight' ) < quality . get ( 'weight' ) )
mismatches ++ ;
}
for ( j = i ; j < this . allowedCollection . length ; j ++ ) {
if ( this . allowedCollection . at ( j ) . get ( 'weight' ) > quality . get ( 'weight' ) )
mismatches ++ ;
}
if ( mismatches <= idealMismatches ) {
idealIndex = i ;
idealMismatches = mismatches ;
}
}
this . availableCollection . remove ( quality ) ;
this . allowedCollection . add ( quality , { at : idealIndex } ) ;
}
else if ( this . allowedCollection . get ( qualityId ) ) {
quality = this . allowedCollection . get ( qualityId ) ;
this . allowedCollection . remove ( quality ) ;
this . availableCollection . add ( quality ) ;
}
else {
throw 'couldnt find quality id ' + qualityId ;
}
_ . each ( removeModels , function ( item ) { item . set ( 'allowed' , false ) ; } ) ;
_ . each ( addedModels , function ( item ) { item . set ( 'allowed' , true ) ; } ) ;
this . _updateModel ( ) ;
} ,
_updateModel : function ( ) {
this . model . set ( 'available' , this . availableCollection . toJSON ( ) . reverse ( ) ) ;
this . model . set ( 'allowed' , this . allowedCollection . toJSON ( ) . reverse ( ) ) ;
this . model . set ( 'items' , this . allowedCollection . toJSON ( ) . reverse ( ) ) ;
this . render ( ) ;
} ,
_saveQualityProfile : function ( ) {
var self = this ;
var cutoff = _ . findWhere ( this . model . get ( ' allowed') , { id : parseInt ( this . ui . cutoff . val ( ) , 10 ) } ) ;
var cutoff = _ . findWhere ( _ . pluck ( this . model . get ( 'items' ) , 'quality' ) , { id : parseInt ( self . ui . cutoff . val ( ) , 10 ) } ) ;
this . model . set ( 'cutoff' , cutoff ) ;
var promise = this . model . save ( ) ;