2012-11-01 05:16:07 +08:00
|
|
|
/**
|
|
|
|
* A splitter control.
|
|
|
|
* Turns an existing HTML element into an horizontal splitter control.
|
|
|
|
* @constructor Splitter
|
|
|
|
* @param {Object} params Available parameters:
|
|
|
|
* {Element} container HTML container representing
|
|
|
|
* the splitter
|
2013-03-09 03:58:32 +08:00
|
|
|
* {Number} [snap] Number of pixels to snap to
|
|
|
|
* the edges (0 or 1)
|
2012-11-01 05:16:07 +08:00
|
|
|
* {function} [change] Callback method called when
|
|
|
|
* the splitter value has changed.
|
|
|
|
* The callback is called with
|
|
|
|
* the new value as parameter
|
|
|
|
*/
|
|
|
|
function Splitter (params) {
|
|
|
|
if (!params || !params.container) {
|
|
|
|
throw new Error('params.container undefined in Splitter constructor');
|
|
|
|
}
|
|
|
|
|
|
|
|
var me = this;
|
2013-01-05 21:32:40 +08:00
|
|
|
jsoneditor.util.addEventListener(params.container, "mousedown", function (event) {
|
2012-11-01 05:16:07 +08:00
|
|
|
me.onMouseDown(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.container = params.container;
|
2013-03-09 03:58:32 +08:00
|
|
|
this.snap = Number(params.snap) || 200; // px
|
|
|
|
this.width = undefined;
|
|
|
|
this.value = undefined;
|
2012-11-01 05:16:07 +08:00
|
|
|
this.onChange = (params.change) ? params.change : function () {};
|
|
|
|
this.params = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse down event. Start dragging the splitter.
|
|
|
|
* @param {Event} event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Splitter.prototype.onMouseDown = function (event) {
|
|
|
|
var me = this;
|
|
|
|
var leftButtonDown = event.which ? (event.which == 1) : (event.button == 1);
|
|
|
|
if (!leftButtonDown) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-08 05:35:06 +08:00
|
|
|
jsoneditor.util.addClassName(this.container, 'active');
|
2012-11-01 05:16:07 +08:00
|
|
|
|
|
|
|
if (!this.params.mousedown) {
|
|
|
|
this.params.mousedown = true;
|
|
|
|
this.params.mousemove =
|
2013-01-05 21:32:40 +08:00
|
|
|
jsoneditor.util.addEventListener(document, 'mousemove', function (event) {
|
2012-11-01 05:16:07 +08:00
|
|
|
me.onMouseMove(event);
|
|
|
|
});
|
|
|
|
this.params.mouseup =
|
2013-01-05 21:32:40 +08:00
|
|
|
jsoneditor.util.addEventListener(document, 'mouseup', function (event) {
|
2012-11-01 05:16:07 +08:00
|
|
|
me.onMouseUp(event);
|
|
|
|
});
|
|
|
|
this.params.screenX = event.screenX;
|
2013-03-09 03:58:32 +08:00
|
|
|
this.params.changed = false;
|
2012-11-01 05:16:07 +08:00
|
|
|
this.params.value = this.getValue();
|
|
|
|
}
|
2013-01-05 21:32:40 +08:00
|
|
|
jsoneditor.util.preventDefault(event);
|
2013-03-09 03:58:32 +08:00
|
|
|
jsoneditor.util.stopPropagation(event);
|
2012-11-01 05:16:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle on mouse move event. Used to drag the splitter
|
|
|
|
* @param {Event} event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Splitter.prototype.onMouseMove = function (event) {
|
2013-03-09 03:58:32 +08:00
|
|
|
if (this.width != undefined) {
|
|
|
|
var diff = event.screenX - this.params.screenX;
|
2012-11-01 05:16:07 +08:00
|
|
|
|
2013-03-09 03:58:32 +08:00
|
|
|
var value = this.params.value + diff / this.width;
|
|
|
|
value = this.setValue(value);
|
2012-11-01 05:16:07 +08:00
|
|
|
|
2013-03-09 03:58:32 +08:00
|
|
|
if (value != this.params.value) {
|
|
|
|
// value has been changed
|
|
|
|
this.params.changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onChange(value);
|
|
|
|
}
|
2012-11-01 05:16:07 +08:00
|
|
|
|
2013-01-05 21:32:40 +08:00
|
|
|
jsoneditor.util.preventDefault(event);
|
2013-03-09 03:58:32 +08:00
|
|
|
jsoneditor.util.stopPropagation(event);
|
2012-11-01 05:16:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle on mouse up event
|
|
|
|
* @param {Event} event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Splitter.prototype.onMouseUp = function (event) {
|
2013-03-08 05:35:06 +08:00
|
|
|
jsoneditor.util.removeClassName(this.container, 'active');
|
|
|
|
|
2012-11-01 05:16:07 +08:00
|
|
|
if (this.params.mousedown) {
|
2013-01-05 21:32:40 +08:00
|
|
|
jsoneditor.util.removeEventListener(document, 'mousemove', this.params.mousemove);
|
|
|
|
jsoneditor.util.removeEventListener(document, 'mouseup', this.params.mouseup);
|
2012-11-01 05:16:07 +08:00
|
|
|
this.params.mousemove = undefined;
|
|
|
|
this.params.mouseup = undefined;
|
|
|
|
this.params.mousedown = false;
|
2013-03-08 05:35:06 +08:00
|
|
|
|
|
|
|
var value = this.getValue();
|
2013-03-09 03:58:32 +08:00
|
|
|
if (!this.params.changed) {
|
2013-03-09 04:32:30 +08:00
|
|
|
// value is unchanged -> unsnap when currently snapped
|
2013-03-08 05:35:06 +08:00
|
|
|
if (value == 0) {
|
|
|
|
value = this.setValue(0.2);
|
|
|
|
this.onChange(value);
|
|
|
|
}
|
|
|
|
if (value == 1) {
|
|
|
|
value = this.setValue(0.8);
|
|
|
|
this.onChange(value);
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 05:16:07 +08:00
|
|
|
}
|
2013-01-05 21:32:40 +08:00
|
|
|
jsoneditor.util.preventDefault(event);
|
2013-03-09 03:58:32 +08:00
|
|
|
jsoneditor.util.stopPropagation(event);
|
2012-11-01 05:16:07 +08:00
|
|
|
};
|
|
|
|
|
2013-03-08 05:35:06 +08:00
|
|
|
/**
|
|
|
|
* Set the window width for the splitter
|
|
|
|
* @param {Number} width
|
|
|
|
*/
|
|
|
|
Splitter.prototype.setWidth = function (width) {
|
|
|
|
this.width = width;
|
|
|
|
};
|
|
|
|
|
2012-11-01 05:16:07 +08:00
|
|
|
/**
|
|
|
|
* Set a value for the splitter (UI is not adjusted)
|
2013-03-09 03:58:32 +08:00
|
|
|
* @param {Number} value A number between 0 and 1
|
2012-11-01 05:16:07 +08:00
|
|
|
* @return {Number} value The stored value
|
|
|
|
*/
|
|
|
|
Splitter.prototype.setValue = function (value) {
|
|
|
|
value = Number(value);
|
2013-03-09 03:58:32 +08:00
|
|
|
|
|
|
|
// snap to 0 or 1 when close
|
|
|
|
if (this.width != undefined && this.width > this.snap) {
|
|
|
|
if (value < this.snap / this.width) {
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
if (value > (this.width - this.snap) / this.width) {
|
|
|
|
value = 1;
|
|
|
|
}
|
2012-11-01 05:16:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.value = value;
|
|
|
|
|
|
|
|
try {
|
|
|
|
localStorage['splitterValue'] = value;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the splitter value from local storage
|
|
|
|
* @return {Number} value A value between 0.1 and 0.9
|
|
|
|
*/
|
|
|
|
Splitter.prototype.getValue = function () {
|
|
|
|
var value = this.value;
|
|
|
|
if (value == undefined) {
|
|
|
|
// read from localStorage once
|
|
|
|
try {
|
|
|
|
if (localStorage['splitterValue'] != undefined) {
|
|
|
|
value = Number(localStorage['splitterValue']); // read
|
|
|
|
value = this.setValue(value); // verify and store
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value == undefined) {
|
|
|
|
value = this.setValue(0.5);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|