2016-04-01 13:17:09 +00:00
|
|
|
/*
|
|
|
|
*
|
2019-01-17 13:05:03 +00:00
|
|
|
* (c) Copyright Ascensio System SIA 2010-2019
|
2016-04-01 13:17:09 +00:00
|
|
|
*
|
|
|
|
* This program is a free software product. You can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
|
|
|
* version 3 as published by the Free Software Foundation. In accordance with
|
|
|
|
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
|
|
|
|
* that Ascensio System SIA expressly excludes the warranty of non-infringement
|
|
|
|
* of any third-party rights.
|
|
|
|
*
|
|
|
|
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
|
|
|
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
|
|
|
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
*
|
2019-01-17 13:00:34 +00:00
|
|
|
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
|
|
|
|
* street, Riga, Latvia, EU, LV-1050.
|
2016-04-01 13:17:09 +00:00
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions
|
|
|
|
* of the Program must display Appropriate Legal Notices, as required under
|
|
|
|
* Section 5 of the GNU AGPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7(b) of the License you must retain the original Product
|
|
|
|
* logo when distributing the program. Pursuant to Section 7(e) we decline to
|
|
|
|
* grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
* All the Product's GUI elements, including illustrations and icon sets, as
|
|
|
|
* well as technical writing content are licensed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
|
|
|
|
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
*
|
|
|
|
*/
|
2016-03-11 00:48:53 +00:00
|
|
|
/**
|
|
|
|
* Slider.js
|
|
|
|
*
|
|
|
|
* Created by Julia Radzhabova on 2/18/14
|
2018-03-01 12:16:38 +00:00
|
|
|
* Copyright (c) 2018 Ascensio System SIA. All rights reserved.
|
2016-03-11 00:48:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example usage:
|
|
|
|
* new Common.UI.SingleSlider({
|
|
|
|
* el: $('#id'),
|
|
|
|
* minValue : 0,
|
|
|
|
* maxValue : 100,
|
|
|
|
* value : 100
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @property {Boolean} enableKeyEvents
|
|
|
|
* If true slider increase/decrease its value by {@link #step} when arrow key Up/Down or Right/Left is pressed.
|
|
|
|
*
|
|
|
|
* enableKeyEvents: false,
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* new Common.UI.MultiSlider({
|
|
|
|
* el: $('#id'),
|
|
|
|
* minValue : 0,
|
|
|
|
* maxValue : 100,
|
|
|
|
* values : [0, 100]
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (Common === undefined)
|
|
|
|
var Common = {};
|
|
|
|
|
|
|
|
define([
|
|
|
|
'common/main/lib/component/BaseView',
|
|
|
|
'underscore'
|
|
|
|
], function (base, _) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Common.UI.SingleSlider = Common.UI.BaseView.extend({
|
|
|
|
|
|
|
|
options : {
|
|
|
|
width: 100,
|
|
|
|
minValue: 0,
|
|
|
|
maxValue: 100,
|
|
|
|
step: 1,
|
|
|
|
value: 100,
|
2021-08-03 16:58:38 +00:00
|
|
|
enableKeyEvents: false,
|
|
|
|
direction: 'horizontal' // 'vertical'
|
2016-03-11 00:48:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
disabled: false,
|
|
|
|
|
|
|
|
template : _.template([
|
2021-08-03 16:58:38 +00:00
|
|
|
'<div class="slider single-slider <% if (this.options.direction === \'vertical\') { %>vertical<% } %>" style="">',
|
2021-04-12 18:46:28 +00:00
|
|
|
'<div class="track"></div>',
|
|
|
|
'<div class="thumb" style=""></div>',
|
2016-03-11 00:48:53 +00:00
|
|
|
'<% if (this.options.enableKeyEvents) { %>',
|
|
|
|
'<input type="text" style="position: absolute; top:-10px; width: 1px; height: 1px;">',
|
|
|
|
'<% } %>',
|
|
|
|
'</div>'
|
|
|
|
].join('')),
|
|
|
|
|
|
|
|
initialize : function(options) {
|
|
|
|
Common.UI.BaseView.prototype.initialize.call(this, options);
|
|
|
|
|
2019-07-29 11:24:20 +00:00
|
|
|
var me = this;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
me.width = me.options.width;
|
|
|
|
me.minValue = me.options.minValue;
|
|
|
|
me.maxValue = me.options.maxValue;
|
|
|
|
me.delta = 100/(me.maxValue - me.minValue);
|
|
|
|
me.step = me.options.step;
|
2021-08-03 16:58:38 +00:00
|
|
|
me.direction = me.options.direction;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
if (me.options.el) {
|
|
|
|
me.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setValue(me.options.value);
|
|
|
|
},
|
|
|
|
|
|
|
|
render : function(parentEl) {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
if (!me.rendered) {
|
|
|
|
this.cmpEl = $(this.template({
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (parentEl) {
|
|
|
|
this.setElement(parentEl, false);
|
|
|
|
parentEl.html(this.cmpEl);
|
|
|
|
} else {
|
2019-07-29 11:24:20 +00:00
|
|
|
me.$el.html(this.cmpEl);
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-29 11:24:20 +00:00
|
|
|
this.cmpEl = me.$el;
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.cmpEl.find('.track-center').width(me.options.width - 14);
|
2021-08-03 16:58:38 +00:00
|
|
|
this.cmpEl[me.direction === 'vertical' ? 'height' : 'width'](me.options.width);
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
this.thumb = this.cmpEl.find('.thumb');
|
|
|
|
|
|
|
|
var onMouseUp = function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2021-08-03 16:58:38 +00:00
|
|
|
var pos = Math.max(0, Math.min(100, (Math.round((
|
|
|
|
me.direction === 'vertical' ? (e.pageY*Common.Utils.zoom() - me.cmpEl.offset().top) : (e.pageX*Common.Utils.zoom() - me.cmpEl.offset().left) - me._dragstart
|
|
|
|
) / me.width * 100))));
|
2016-03-11 00:48:53 +00:00
|
|
|
me.setThumbPosition(pos);
|
|
|
|
|
|
|
|
me.lastValue = me.value;
|
2021-08-09 16:11:22 +00:00
|
|
|
me.value = pos/me.delta + me.minValue;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
me.thumb.removeClass('active');
|
|
|
|
$(document).off('mouseup', onMouseUp);
|
|
|
|
$(document).off('mousemove', onMouseMove);
|
|
|
|
|
|
|
|
me._dragstart = undefined;
|
|
|
|
me.trigger('changecomplete', me, me.value, me.lastValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onMouseMove = function (e) {
|
|
|
|
if ( me.disabled ) return;
|
|
|
|
if ( me._dragstart===undefined ) return;
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2021-08-03 16:58:38 +00:00
|
|
|
var pos = Math.max(0, Math.min(100, (Math.round((
|
|
|
|
me.direction === 'vertical' ? (e.pageY*Common.Utils.zoom() - me.cmpEl.offset().top) : (e.pageX*Common.Utils.zoom() - me.cmpEl.offset().left) - me._dragstart
|
|
|
|
) / me.width * 100))));
|
2016-03-11 00:48:53 +00:00
|
|
|
me.setThumbPosition(pos);
|
|
|
|
|
|
|
|
me.lastValue = me.value;
|
2021-08-09 16:11:22 +00:00
|
|
|
me.value = pos/me.delta + me.minValue;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
if (Math.abs(me.value-me.lastValue)>0.001)
|
|
|
|
me.trigger('change', me, me.value, me.lastValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onMouseDown = function (e) {
|
|
|
|
if ( me.disabled ) return;
|
2021-08-03 16:58:38 +00:00
|
|
|
me._dragstart = me.direction === 'vertical' ? (e.pageY*Common.Utils.zoom() - me.thumb.offset().top) : (e.pageX*Common.Utils.zoom() - me.thumb.offset().left) - 7;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
me.thumb.addClass('active');
|
|
|
|
$(document).on('mouseup', onMouseUp);
|
|
|
|
$(document).on('mousemove', onMouseMove);
|
|
|
|
|
|
|
|
if (me.options.enableKeyEvents)
|
|
|
|
setTimeout(function() {me.input.focus();}, 10);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onTrackMouseDown = function (e) {
|
|
|
|
if ( me.disabled ) return;
|
|
|
|
|
2021-08-03 16:58:38 +00:00
|
|
|
var pos = Math.max(0, Math.min(100, (Math.round((
|
|
|
|
me.direction === 'vertical' ? (e.pageY*Common.Utils.zoom() - me.cmpEl.offset().top) : (e.pageX*Common.Utils.zoom() - me.cmpEl.offset().left)
|
|
|
|
) / me.width * 100))));
|
2016-03-11 00:48:53 +00:00
|
|
|
me.setThumbPosition(pos);
|
|
|
|
|
|
|
|
me.lastValue = me.value;
|
2021-08-09 16:11:22 +00:00
|
|
|
me.value = pos/me.delta + me.minValue;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
me.trigger('change', me, me.value, me.lastValue);
|
|
|
|
me.trigger('changecomplete', me, me.value, me.lastValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
var updateslider;
|
|
|
|
|
|
|
|
var moveThumb = function(increase) {
|
|
|
|
me.lastValue = me.value;
|
|
|
|
me.value = Math.max(me.minValue, Math.min(me.maxValue, me.value + ((increase) ? me.step : -me.step)));
|
|
|
|
me.setThumbPosition(Math.round((me.value-me.minValue)*me.delta));
|
|
|
|
me.trigger('change', me, me.value, me.lastValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onKeyDown = function (e) {
|
|
|
|
if ( me.disabled ) return;
|
|
|
|
|
|
|
|
if (e.keyCode==Common.UI.Keys.UP || e.keyCode==Common.UI.Keys.DOWN || e.keyCode==Common.UI.Keys.LEFT || e.keyCode==Common.UI.Keys.RIGHT) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
el.off('keydown', 'input', onKeyDown);
|
|
|
|
updateslider = setInterval(_.bind(moveThumb, me, e.keyCode==Common.UI.Keys.UP || e.keyCode==Common.UI.Keys.RIGHT), 100);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var onKeyUp = function (e) {
|
|
|
|
if ( me.disabled ) return;
|
|
|
|
|
|
|
|
if (e.keyCode==Common.UI.Keys.UP || e.keyCode==Common.UI.Keys.DOWN || Common.UI.Keys.LEFT || Common.UI.Keys.RIGHT) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
clearInterval(updateslider);
|
|
|
|
moveThumb(e.keyCode==Common.UI.Keys.UP || e.keyCode==Common.UI.Keys.RIGHT);
|
|
|
|
el.on('keydown', 'input', onKeyDown);
|
|
|
|
me.trigger('changecomplete', me, me.value, me.lastValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!me.rendered) {
|
|
|
|
var el = me.cmpEl;
|
|
|
|
el.on('mousedown', '.thumb', onMouseDown);
|
|
|
|
el.on('mousedown', '.track', onTrackMouseDown);
|
|
|
|
if (this.options.enableKeyEvents) {
|
|
|
|
el.on('keydown', 'input', onKeyDown);
|
|
|
|
el.on('keyup', 'input', onKeyUp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
me.rendered = true;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2021-08-03 16:58:38 +00:00
|
|
|
setThumbPosition: function(pos) {
|
|
|
|
if (this.direction === 'vertical') {
|
|
|
|
this.thumb.css({top: pos + '%'});
|
|
|
|
} else {
|
|
|
|
this.thumb.css({left: pos + '%'});
|
|
|
|
}
|
2016-03-11 00:48:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setValue: function(value) {
|
|
|
|
this.lastValue = this.value;
|
|
|
|
this.value = Math.max(this.minValue, Math.min(this.maxValue, value));
|
2021-08-09 16:11:22 +00:00
|
|
|
this.setThumbPosition(Math.round((value-this.minValue)*this.delta));
|
2016-03-11 00:48:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getValue: function() {
|
|
|
|
return this.value;
|
|
|
|
},
|
|
|
|
|
|
|
|
setDisabled: function(disabled) {
|
2021-12-14 11:14:39 +00:00
|
|
|
disabled = !!disabled;
|
2016-03-11 00:48:53 +00:00
|
|
|
if (disabled !== this.disabled)
|
|
|
|
this.cmpEl.toggleClass('disabled', disabled);
|
|
|
|
this.disabled = disabled;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Common.UI.MultiSlider = Common.UI.BaseView.extend({
|
|
|
|
|
|
|
|
options : {
|
|
|
|
width: 100,
|
|
|
|
minValue: 0,
|
|
|
|
maxValue: 100,
|
2019-06-06 12:03:38 +00:00
|
|
|
values: [0, 100],
|
2021-08-15 18:20:57 +00:00
|
|
|
includeSnap: false,
|
2021-08-18 08:33:38 +00:00
|
|
|
intervalSnap: undefined,
|
2021-08-15 18:20:57 +00:00
|
|
|
thumbTemplate: '<div class="thumb" style=""></div>'
|
2016-03-11 00:48:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
disabled: false,
|
|
|
|
|
|
|
|
template : _.template([
|
|
|
|
'<div class="slider multi-slider">',
|
|
|
|
'<div class="track">',
|
|
|
|
'<div class="track-left"></div>',
|
|
|
|
'<div class="track-center""></div>',
|
|
|
|
'<div class="track-right" style=""></div>',
|
|
|
|
'</div>',
|
|
|
|
'<% _.each(items, function(item) { %>',
|
2019-06-06 12:03:38 +00:00
|
|
|
'<%= thumbTemplate %>',
|
2016-03-11 00:48:53 +00:00
|
|
|
'<% }); %>',
|
|
|
|
'</div>'
|
|
|
|
].join('')),
|
|
|
|
|
|
|
|
initialize : function(options) {
|
|
|
|
Common.UI.BaseView.prototype.initialize.call(this, options);
|
|
|
|
|
2019-07-29 11:24:20 +00:00
|
|
|
var me = this;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
me.width = me.options.width;
|
|
|
|
me.minValue = me.options.minValue;
|
|
|
|
me.maxValue = me.options.maxValue;
|
|
|
|
me.delta = 100/(me.maxValue - me.minValue);
|
2021-08-15 18:20:57 +00:00
|
|
|
me.includeSnap = me.options.includeSnap;
|
2021-08-18 08:33:38 +00:00
|
|
|
me.intervalSnap = me.options.intervalSnap;
|
2016-03-11 00:48:53 +00:00
|
|
|
me.thumbs = [];
|
|
|
|
if (me.options.el) {
|
|
|
|
me.render();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render : function(parentEl) {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
if (!me.rendered) {
|
|
|
|
this.cmpEl = $(this.template({
|
2019-06-06 12:03:38 +00:00
|
|
|
items: this.options.values,
|
|
|
|
thumbTemplate: this.options.thumbTemplate
|
2016-03-11 00:48:53 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
if (parentEl) {
|
|
|
|
this.setElement(parentEl, false);
|
|
|
|
parentEl.html(this.cmpEl);
|
|
|
|
} else {
|
2019-07-29 11:24:20 +00:00
|
|
|
this.$el.html(this.cmpEl);
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-29 11:24:20 +00:00
|
|
|
this.cmpEl = this.$el;
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var el = this.cmpEl;
|
|
|
|
el.find('.track-center').width(me.options.width - 14);
|
|
|
|
el.width(me.options.width);
|
|
|
|
|
2021-08-13 23:08:53 +00:00
|
|
|
var setCenters = function (index) {
|
2021-08-15 18:20:57 +00:00
|
|
|
if(!me.includeSnap) return;
|
2021-08-15 21:01:34 +00:00
|
|
|
var n = me.minValue;
|
|
|
|
|
|
|
|
var getX = function (position) {
|
|
|
|
return (0.01 * me.width * position + me.cmpEl.offset().left + me._dragstart)/Common.Utils.zoom();
|
|
|
|
};
|
|
|
|
|
2021-08-17 12:13:10 +00:00
|
|
|
me.centers = [];
|
2021-08-13 23:08:53 +00:00
|
|
|
_.each(me.thumbs, function (thumb, indexT) {
|
2021-08-15 21:01:34 +00:00
|
|
|
if ((indexT != index) && (n != thumb.position)) {
|
|
|
|
me.centers.push(getX((thumb.position - n) / 2 + n));
|
2021-08-13 23:08:53 +00:00
|
|
|
n = thumb.position;
|
|
|
|
}
|
|
|
|
});
|
2021-08-15 21:01:34 +00:00
|
|
|
if(n != me.maxValue) me.centers.push(getX((me.maxValue - n) / 2 + n));
|
2021-08-13 23:08:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var resetPageX = function (e) {
|
2021-08-15 18:20:57 +00:00
|
|
|
if(!me.includeSnap) return;
|
2021-08-15 21:01:34 +00:00
|
|
|
_.each(me.centers, function (x) {
|
2021-08-18 08:33:38 +00:00
|
|
|
if((e.pageX <= x + me.intervalSnap) && (e.pageX >= x - me.intervalSnap)) {
|
2021-08-13 23:08:53 +00:00
|
|
|
e.pageX = x;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-03-11 00:48:53 +00:00
|
|
|
var onMouseUp = function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-08-15 18:20:57 +00:00
|
|
|
resetPageX(e);
|
2021-08-13 23:08:53 +00:00
|
|
|
|
2017-04-12 09:57:15 +00:00
|
|
|
var index = e.data.index,
|
2016-03-11 00:48:53 +00:00
|
|
|
lastValue = me.thumbs[index].value,
|
|
|
|
minValue = (index-1<0) ? 0 : me.thumbs[index-1].position,
|
|
|
|
maxValue = (index+1<me.thumbs.length) ? me.thumbs[index+1].position : 100,
|
2017-04-12 09:57:15 +00:00
|
|
|
position = Math.round((e.pageX*Common.Utils.zoom() - me.cmpEl.offset().left - me._dragstart) / me.width * 100),
|
|
|
|
need_sort = position < minValue || position > maxValue,
|
|
|
|
pos = Math.max(0, Math.min(100, position)),
|
2016-03-11 00:48:53 +00:00
|
|
|
value = pos/me.delta + me.minValue;
|
2021-10-01 12:41:37 +00:00
|
|
|
|
2021-10-05 22:15:55 +00:00
|
|
|
if (me.thumbs.length < 3)
|
|
|
|
me.isRemoveThumb = false;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
2019-11-12 14:13:34 +00:00
|
|
|
if (me.isRemoveThumb) {
|
2019-11-15 08:42:13 +00:00
|
|
|
me.trigger('removethumb', me, _.findIndex(me.thumbs, {index: index}));
|
2019-12-03 13:52:47 +00:00
|
|
|
me.trigger('change', me, value, lastValue);
|
2019-11-15 08:42:13 +00:00
|
|
|
me.trigger('changecomplete', me, value, lastValue);
|
|
|
|
} else {
|
|
|
|
me.setThumbPosition(index, pos);
|
|
|
|
me.thumbs[index].value = value;
|
2016-03-11 00:48:53 +00:00
|
|
|
|
2019-11-15 08:42:13 +00:00
|
|
|
if (need_sort)
|
|
|
|
me.sortThumbs();
|
|
|
|
}
|
2017-04-12 09:57:15 +00:00
|
|
|
|
2019-06-06 12:03:38 +00:00
|
|
|
$(document).off('mouseup', me.binding.onMouseUp);
|
|
|
|
$(document).off('mousemove', me.binding.onMouseMove);
|
2016-03-11 00:48:53 +00:00
|
|
|
me._dragstart = undefined;
|
2019-11-15 08:42:13 +00:00
|
|
|
!me.isRemoveThumb && me.trigger('changecomplete', me, value, lastValue);
|
|
|
|
me.isRemoveThumb = undefined;
|
2016-03-11 00:48:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var onMouseMove = function (e) {
|
|
|
|
if ( me.disabled ) return;
|
|
|
|
if ( me._dragstart===undefined ) return;
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-08-15 18:20:57 +00:00
|
|
|
resetPageX(e);
|
2016-03-11 00:48:53 +00:00
|
|
|
|
2017-04-12 09:57:15 +00:00
|
|
|
var index = e.data.index,
|
2016-03-11 00:48:53 +00:00
|
|
|
lastValue = me.thumbs[index].value,
|
|
|
|
minValue = (index-1<0) ? 0 : me.thumbs[index-1].position,
|
|
|
|
maxValue = (index+1<me.thumbs.length) ? me.thumbs[index+1].position : 100,
|
2017-04-12 09:57:15 +00:00
|
|
|
position = Math.round((e.pageX*Common.Utils.zoom() - me.cmpEl.offset().left - me._dragstart) / me.width * 100),
|
|
|
|
need_sort = position < minValue || position > maxValue,
|
|
|
|
pos = Math.max(0, Math.min(100, position)),
|
2016-03-11 00:48:53 +00:00
|
|
|
value = pos/me.delta + me.minValue;
|
|
|
|
|
|
|
|
me.setThumbPosition(index, pos);
|
|
|
|
me.thumbs[index].value = value;
|
|
|
|
|
2017-04-12 09:57:15 +00:00
|
|
|
if (need_sort)
|
|
|
|
me.sortThumbs();
|
|
|
|
|
2019-11-15 08:42:13 +00:00
|
|
|
var positionY = e.pageY*Common.Utils.zoom() - me.cmpEl.offset().top;
|
2019-11-15 11:33:09 +00:00
|
|
|
me.isRemoveThumb = positionY > me.cmpEl.height() || positionY < 0;
|
2019-11-15 08:42:13 +00:00
|
|
|
me.setRemoveThumb(index, me.isRemoveThumb);
|
|
|
|
|
2016-03-11 00:48:53 +00:00
|
|
|
if (Math.abs(value-lastValue)>0.001)
|
|
|
|
me.trigger('change', me, value, lastValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onMouseDown = function (e) {
|
|
|
|
if ( me.disabled ) return;
|
2021-10-05 22:07:36 +00:00
|
|
|
|
2017-04-12 09:57:15 +00:00
|
|
|
var index = e.data.index,
|
2016-03-11 00:48:53 +00:00
|
|
|
thumb = me.thumbs[index].thumb;
|
|
|
|
|
2016-08-19 13:41:48 +00:00
|
|
|
me._dragstart = e.pageX*Common.Utils.zoom() - thumb.offset().left - thumb.width()/2;
|
2021-08-15 21:01:34 +00:00
|
|
|
setCenters(index);
|
2016-03-11 00:48:53 +00:00
|
|
|
me.setActiveThumb(index);
|
|
|
|
|
|
|
|
_.each(me.thumbs, function (item, idx) {
|
|
|
|
(index == idx) ? item.thumb.css('z-index', 500) : item.thumb.css('z-index', '');
|
|
|
|
});
|
|
|
|
|
2019-06-06 12:03:38 +00:00
|
|
|
$(document).on('mouseup', null, e.data, me.binding.onMouseUp);
|
|
|
|
$(document).on('mousemove', null, e.data, me.binding.onMouseMove);
|
2016-03-11 00:48:53 +00:00
|
|
|
};
|
|
|
|
|
2021-08-13 23:08:53 +00:00
|
|
|
|
2019-10-17 09:00:35 +00:00
|
|
|
var onTrackMouseUp = function (e) {
|
2019-11-15 11:05:50 +00:00
|
|
|
if ( me.disabled || !_.isUndefined(me._dragstart) || me.thumbs.length > 9) return;
|
2019-10-17 09:00:35 +00:00
|
|
|
|
|
|
|
var pos = Math.max(0, Math.min(100, (Math.round((e.pageX*Common.Utils.zoom() - me.cmpEl.offset().left) / me.width * 100)))),
|
2019-11-12 11:18:02 +00:00
|
|
|
nearIndex = findThumb(pos),
|
|
|
|
thumbColor = me.thumbs[nearIndex].colorValue,
|
|
|
|
thumbValue = me.thumbs[nearIndex].value,
|
2019-10-17 09:00:35 +00:00
|
|
|
value = pos/me.delta + me.minValue;
|
|
|
|
me.addThumb();
|
2019-11-12 11:18:02 +00:00
|
|
|
var index = me.thumbs.length - 1;
|
|
|
|
me.setThumbPosition(index, pos);
|
|
|
|
me.thumbs[index].value = value;
|
2020-07-13 19:14:51 +00:00
|
|
|
me.trigger('addthumb', me, index, pos);
|
2019-10-17 09:00:35 +00:00
|
|
|
|
|
|
|
me.trigger('change', me);
|
2019-11-12 11:18:02 +00:00
|
|
|
me.trigger('changecomplete', me);
|
2019-10-17 09:00:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*var onTrackMouseDown = function (e) {
|
2016-03-11 00:48:53 +00:00
|
|
|
if ( me.disabled ) return;
|
|
|
|
|
2016-08-19 13:41:48 +00:00
|
|
|
var pos = Math.max(0, Math.min(100, (Math.round((e.pageX*Common.Utils.zoom() - me.cmpEl.offset().left) / me.width * 100)))),
|
2016-03-11 00:48:53 +00:00
|
|
|
index = findThumb(pos),
|
|
|
|
lastValue = me.thumbs[index].value,
|
|
|
|
value = pos/me.delta + me.minValue;
|
|
|
|
|
|
|
|
me.setThumbPosition(index, pos);
|
|
|
|
me.thumbs[index].value = value;
|
|
|
|
|
|
|
|
me.trigger('change', me, value, lastValue);
|
|
|
|
me.trigger('changecomplete', me, value, lastValue);
|
2019-10-17 09:00:35 +00:00
|
|
|
};*/
|
2016-03-11 00:48:53 +00:00
|
|
|
|
|
|
|
var findThumb = function(pos) {
|
|
|
|
var nearest = 100,
|
|
|
|
index = 0,
|
|
|
|
len = me.thumbs.length,
|
|
|
|
dist;
|
|
|
|
|
|
|
|
for (var i=0; i<len; i++) {
|
|
|
|
dist = Math.abs(me.thumbs[i].position - pos);
|
|
|
|
if (Math.abs(dist <= nearest)) {
|
|
|
|
var above = me.thumbs[i + 1];
|
|
|
|
var below = me.thumbs[i - 1];
|
|
|
|
|
|
|
|
if (below !== undefined && pos < below.position) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (above !== undefined && pos > above.position) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
index = i;
|
|
|
|
nearest = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
};
|
|
|
|
|
2019-06-06 12:03:38 +00:00
|
|
|
this.binding = {
|
|
|
|
onMouseUp: _.bind(onMouseUp, this),
|
|
|
|
onMouseMove: _.bind(onMouseMove, this),
|
|
|
|
onMouseDown: _.bind(onMouseDown, this)
|
|
|
|
};
|
|
|
|
|
2016-03-11 00:48:53 +00:00
|
|
|
this.$thumbs = el.find('.thumb');
|
|
|
|
_.each(this.$thumbs, function(item, index) {
|
|
|
|
var thumb = $(item);
|
|
|
|
me.thumbs.push({
|
|
|
|
thumb: thumb,
|
|
|
|
index: index
|
|
|
|
});
|
|
|
|
me.setValue(index, me.options.values[index]);
|
2019-06-06 12:03:38 +00:00
|
|
|
thumb.on('mousedown', null, me.thumbs[index], me.binding.onMouseDown);
|
2016-03-11 00:48:53 +00:00
|
|
|
});
|
|
|
|
me.setActiveThumb(0, true);
|
|
|
|
|
|
|
|
if (!me.rendered) {
|
2019-10-17 09:00:35 +00:00
|
|
|
/*el.on('mousedown', '.track', onTrackMouseDown);*/
|
|
|
|
el.on('mouseup', '.track', onTrackMouseUp);
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me.rendered = true;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setActiveThumb: function(index, suspend) {
|
|
|
|
this.currentThumb = index;
|
2019-11-12 11:18:02 +00:00
|
|
|
this.$thumbs = this.cmpEl.find('.thumb');
|
2016-03-11 00:48:53 +00:00
|
|
|
this.$thumbs.removeClass('active');
|
|
|
|
this.thumbs[index].thumb.addClass('active');
|
|
|
|
if (suspend!==true) this.trigger('thumbclick', this, index);
|
|
|
|
},
|
|
|
|
|
2019-11-12 14:13:34 +00:00
|
|
|
setRemoveThumb: function(index, remove) {
|
2019-11-15 08:42:13 +00:00
|
|
|
var ind = _.findIndex(this.thumbs, {index: index});
|
|
|
|
if (ind !== -1) {
|
|
|
|
if (remove && this.thumbs.length > 2) {
|
2019-11-15 11:05:50 +00:00
|
|
|
this.$el.find('.active').addClass('remove');
|
2019-11-15 08:42:13 +00:00
|
|
|
} else {
|
2019-11-15 11:05:50 +00:00
|
|
|
this.$el.find('.remove').removeClass('remove');
|
2019-11-15 08:42:13 +00:00
|
|
|
}
|
2019-11-12 14:13:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-11 00:48:53 +00:00
|
|
|
setThumbPosition: function(index, x) {
|
|
|
|
this.thumbs[index].position = x;
|
|
|
|
this.thumbs[index].thumb.css({left: x + '%'});
|
|
|
|
},
|
|
|
|
|
|
|
|
setValue: function(index, value) {
|
|
|
|
this.thumbs[index].value = Math.max(this.minValue, Math.min(this.maxValue, value));
|
|
|
|
this.setThumbPosition(index, Math.round((value-this.minValue)*this.delta));
|
|
|
|
},
|
|
|
|
|
|
|
|
getValue: function(index) {
|
|
|
|
return this.thumbs[index].value;
|
|
|
|
},
|
|
|
|
|
|
|
|
getValues: function() {
|
|
|
|
var values = [];
|
|
|
|
_.each (this.thumbs, function(thumb) {
|
|
|
|
values.push(thumb.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return values;
|
|
|
|
},
|
|
|
|
|
|
|
|
setDisabled: function(disabled) {
|
2021-12-14 11:14:39 +00:00
|
|
|
disabled = !!disabled;
|
2016-03-11 00:48:53 +00:00
|
|
|
if (disabled !== this.disabled)
|
|
|
|
this.cmpEl.toggleClass('disabled', disabled);
|
|
|
|
this.disabled = disabled;
|
2017-04-12 09:57:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sortThumbs: function() {
|
|
|
|
this.thumbs.sort(function(a, b) {
|
|
|
|
return (a.position - b.position);
|
|
|
|
});
|
|
|
|
var recalc_indexes = [];
|
|
|
|
_.each (this.thumbs, function(thumb, index) {
|
|
|
|
recalc_indexes.push(thumb.index);
|
|
|
|
thumb.index = index;
|
|
|
|
});
|
|
|
|
return recalc_indexes;
|
2019-06-06 12:03:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setThumbs: function(count) {
|
|
|
|
var length = this.thumbs.length;
|
|
|
|
if (length==count) return;
|
|
|
|
|
|
|
|
for (var i=0; i<Math.abs(count-length); i++)
|
|
|
|
(length<count) ? this.addThumb() : this.removeThumb();
|
|
|
|
},
|
|
|
|
|
|
|
|
addThumb: function() {
|
|
|
|
var el = this.cmpEl,
|
|
|
|
thumb = $(this.options.thumbTemplate),
|
|
|
|
index = this.thumbs.length;
|
|
|
|
el.append(thumb);
|
|
|
|
this.thumbs.push({
|
|
|
|
thumb: thumb,
|
|
|
|
index: index
|
|
|
|
});
|
|
|
|
(index>0) && this.setValue(index, this.getValue(index-1));
|
|
|
|
thumb.on('mousedown', null, this.thumbs[index], this.binding.onMouseDown);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeThumb: function(index) {
|
|
|
|
if (index===undefined) index = this.thumbs.length-1;
|
|
|
|
if (index>0) {
|
|
|
|
this.thumbs[index].thumb.remove();
|
|
|
|
this.thumbs.splice(index, 1);
|
|
|
|
}
|
2016-03-11 00:48:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|