Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@fgandellini

This comment has been minimized.

Copy link

fgandellini commented Jun 16, 2014

function mouseup(evt) {
  if (_isDD) {
    return;
  }

  if (evt.shiftKey == true) {
    var range = extractRange();
    select(range);
  } else {
    toggleCurrentSelection();
  }
  _lastItemSelected = this;
  this.isMouseup = true;
}

function toggleCurrentSelection() {
  if (this.elem.hasClassName('mkdd-selected')) {
      this.elem.removeClassName('mkdd-selected');
  }
  else {
      this.selectItem(this);
  }
}

function select(range) {
  for (var i = range[0]; i < range[1]; i++) {
    var item = this.cont.list[i];
    if (!item.elem.hasClassName('mkdd-selected')) {
        this.selectItem(item);
    } else {
        break;
    }
  }
}

function extractRange() {
  return [
    this.cont.list.indexOf(_lastItemSelected),
    this.cont.list.indexOf(this)
  ].sort();
}

ecco io propongo una cosa simile a questa come prima "botta", occhio che non l'ho provato! Mi aspetto che non vada del tutto...è una proposta! :)

@unlucio

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.