Skip to content

Instantly share code, notes, and snippets.

@sgruhier
Created December 8, 2010 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgruhier/733454 to your computer and use it in GitHub Desktop.
Save sgruhier/733454 to your computer and use it in GitHub Desktop.
window.SplitView = Backbone.View.extend({
el: $(document.body),
// Delegated mouse down events
events: {
"mousedown .vsplit": "mouseDownVSplit",
"mousedown .hsplit": "mouseDownHSplit"
},
initialize: function(element) {
_.bindAll(this, 'endDrag', 'mouseMove', 'mouseDownVSplit', 'mouseDownHSplit');
},
prepareDrag: function(event) {
$(document).bind("mousemove", this.mouseMove);
$(document).bind("mouseup", this.endDrag);
var element = $(event.target);
_.extend(this.elementInfo, {element: element,
next: element.next(),
prev: element.prev(),
size: element.prev()[this.elementInfo.attr](),
parentSize: this.computeChildrenSize(element.parent(), this.elementInfo.attr)});
event.preventDefault();
},
endDrag: function(event) {
$(document).unbind("mousemove", this.mouseMove)
$(document).unbind("mouseup", this.endDrag)
},
mouseDownVSplit: function(event) {
this.elementInfo = {attr: "width"}
this.prepareDrag(event);
this.mouseInfo = {pos: event.pageX, attr: 'pageX'}
},
mouseDownHSplit: function(event) {
this.elementInfo = {attr: "height"}
this.prepareDrag(event);
this.mouseInfo = {pos: event.pageY, attr: 'pageY'}
},
mouseMove: function(event) {
var info = this.elementInfo,
d = event[this.mouseInfo.attr] - this.mouseInfo.pos,
size = info.size + d;
info.prev[info.attr](size);
var parentSize = this.computeChildrenSize(info.element.parent(), info.attr),
delta = info.parentSize - parentSize;
if (delta != 0) {
info.prev[info.attr](size + delta);
}
event.preventDefault();
},
computeChildrenSize: function(element, attr) {
return _.inject(element.children(), function(memo, elt){ return memo + $(elt)[attr](); }, 0)
}
});
@miratcan
Copy link

miratcan commented Sep 9, 2011

It's ok on chrome but not worknig on firefox. :-|

@sgruhier
Copy link
Author

sgruhier commented Sep 9, 2011

I'll check

@sgruhier
Copy link
Author

sgruhier commented Sep 9, 2011

FF version ?

@miratcan
Copy link

miratcan commented Sep 9, 2011

Firefox 6.0.2.

@miratcan
Copy link

miratcan commented Sep 9, 2011

I am doing my example it works fine on chrome but there is a problem with catching mouseup event i think. may be your problem can be it too.

@sgruhier
Copy link
Author

sgruhier commented Sep 9, 2011

fixed

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