Skip to content

Instantly share code, notes, and snippets.

@emadb
Created June 16, 2014 14:59
Show Gist options
  • Save emadb/38cf4c94ac858382300c to your computer and use it in GitHub Desktop.
Save emadb/38cf4c94ac858382300c to your computer and use it in GitHub Desktop.
Javascript refactoring
function mouseup(evt) {
if (_isDD) {
return;
}
if (evt.shiftKey == true) {
var indexOf1 = this.cont.list.indexOf(_lastItemSelected);
var indexOf2 = this.cont.list.indexOf(this);
for (var i = indexOf2; i > indexOf1; i--) {
var item = this.cont.list[i];
if (!item.elem.hasClassName('mkdd-selected')) {
this.selectItem(item);
} else {
break;
}
}
for (var j = indexOf2; j < indexOf1 ; j++) {
var item = this.cont.list[j];
if (!item.elem.hasClassName('mkdd-selected')) {
this.selectItem(item);
} else {
break;
}
}
} else {
if (this.elem.hasClassName('mkdd-selected')) {
this.elem.removeClassName('mkdd-selected');
}
else {
this.selectItem(this);
}
}
_lastItemSelected = this;
this.isMouseup = true;
}
@unlucio
Copy link

unlucio commented Jun 16, 2014

non sono ancora convinto di chi sia this pero'
gli darei una botta di roncola e frego l'idea del .sort() a fede:

function mouseup(evt) {
    var contentList = this.cont.list;
    var [start, end] = [contentList.indexOf(_lastItemSelected), contentList.indexOf(this)].sort();

    if (_isDD) {
        return;
    }

    if (evt.shiftKey == true) {
        for (var i = start; i <= end ; i++) {
            this.selectItem(contentList[i]);
        }
    } else {
        if (this.elem.hasClassName('mkdd-selected')) {
            this.elem.removeClassName('mkdd-selected');
        } else {
            this.selectItem(this);
        }
    }

    _lastItemSelected = this;
    this.isMouseup = true;
}

eviterei di controllare se c'e' o meno la classe, tanto cmq ce la metti e ne giova la leggibiita', imho.

poi se

this.elem.removeClassName('mkdd-selected');

fosse cosi' gentile da tornare false nel caso in cui non ce la faccia, quel brutto if/else in fondo potrebbe diventare:

this.elem.removeClassName('mkdd-selected') || this.selectItem(this);

@emadb
Copy link
Author

emadb commented Jun 17, 2014

Questa è la mia:

function selectElement(item){
    if (item.elem.hasClassName('mkdd-selected')) {
        return false;
    } else {
        this.elem.addClassName('mkdd-selected');
    }
    return true;
}

function loopFor(startIndex, endIndex){
    for (var i = startIndex; i < endIndex; i++) {
        var item = this.cont.list[i];
        if (!selectElement(item)){
            break;
        }
    }
}

function mouseup(evt) {
    if (_isDD) {
        return;
    }
    if (evt.shiftKey == true) {
        var indexOf1 = this.cont.list.indexOf(_lastItemSelected);
        var indexOf2 = this.cont.list.indexOf(this);

        loopFor(indexOf1 - 1, indexOf2 + 1);
        loopFor(indexOf2, indexOf1);

    } else {
        if (!selectElement(item)){
            this.elem.removeClassName('mkdd-selected');
        }
    }
    _lastItemSelected = this;
    this.isMouseup = true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment