Skip to content

Commit

Permalink
Tests (commented for now) to automatically place the record at the ri…
Browse files Browse the repository at this point in the history
…ght place after manually editing its rank
  • Loading branch information
rtripault committed May 1, 2013
1 parent b00d48f commit c861805
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions manager/components/tvsorter/js/home/tvs.grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ MODx.grid.TemplateTV = function(config) {
};
MODx.grid.TemplateTV.superclass.constructor.call(this, config);
this.on('render', this.prepareDrop, this);
this.on('afteredit', this.manageAccess, this);
this.on('afteredit', this.manageRank, this);


};
Expand Down Expand Up @@ -195,9 +195,7 @@ Ext.extend(MODx.grid.TemplateTV, MODx.grid.Grid, {
/**
* Take care of TVs attached/removed to the template
*/
,manageAccess: function(e) {
if (e.field != 'access') return false;

,manageRank: function(e) {
var store = e.grid.getStore();
var record = e.record;
var total = store.queryBy(function(rec, id) {
Expand All @@ -211,37 +209,52 @@ Ext.extend(MODx.grid.TemplateTV, MODx.grid.Grid, {
});
var currentIdx = store.indexOf(record);

if (e.value === true) {
// Record has been attached
if (actives.length > 0) {
var last = actives.last();
var to = store.indexOf(last);
} else if (total.length > 1) {
last = total.last();
to = store.indexOf(last);
}
record.set('tv_rank', actives.length);
} else {
// Record has been removed
if (inactives.length > 0) {
last = inactives.last();
to = store.indexOf(last) + 1;
} else if (total.length > 0) {
var first = total.first();
to = store.indexOf(first);
}
var rank = record.get('tv_rank');
record.set('tv_rank', '-');
if (e.field == 'access') {
if (e.value === true) {
// Record has been attached
if (actives.length > 0) {
var last = actives.last();
var to = store.indexOf(last);
} else if (total.length > 1) {
last = total.last();
to = store.indexOf(last);
}
record.set('tv_rank', actives.length);
} else {
// Record has been removed
if (inactives.length > 0) {
last = inactives.last();
to = store.indexOf(last) + 1;
} else if (total.length > 0) {
var first = total.first();
to = store.indexOf(first);
}
var rank = record.get('tv_rank');
record.set('tv_rank', '-');

// Decrement the tv_rank for impacted TVs
var impacted = store.queryBy(function(rec, id) {
return (rec.get('tv_rank') > rank && rec.get('category_name') == record.get('category_name'));
});
impacted.each(function(rec, idx, list) {
rec.set('tv_rank', (rec.get('tv_rank') - 1));
});
// Decrement the tv_rank for impacted TVs
var impacted = store.queryBy(function(rec, id) {
return (rec.get('tv_rank') > rank && rec.get('category_name') == record.get('category_name'));
});
impacted.each(function(rec, idx, list) {
rec.set('tv_rank', (rec.get('tv_rank') - 1));
});
}
}

// if (e.field == 'tv_rank') {
// to = e.value;
// if (to == '-') {
// if (inactives.length > 0) {
// last = inactives.last();
// to = store.indexOf(last) + 1;
// } else if (total.length > 0) {
// first = total.first();
// to = store.indexOf(first);
// }
// }
// }

if (to && to != currentIdx) {
store.removeAt(currentIdx);
store.insert(to, record);
Expand Down

0 comments on commit c861805

Please sign in to comment.