Skip to content

Commit

Permalink
#184 assistant layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Mar 16, 2016
1 parent 689b987 commit e039dce
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Extension/browser/firefox/lib/contentScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ ContentScripts.prototype = {
// Assistant
this.registerPageContentScript([
'libs/jquery-1.8.3.min.js',
'libs/jquery-ui.min.js',
//'libs/jquery-ui.min.js',
'libs/diff_match_patch.js',
'libs/dom.patched.js',
'libs/balalaika.patched.js',
'content-script/i18n-helper.js', // Localization placeholders
'content-script/content-script.js', // Message passing
'content-script/assistant/js/slider-widget.js',
'content-script/assistant/js/start-assistant.js',
'content-script/assistant/js/adguard-selector.js',
'content-script/assistant/js/adguard-rules-constructor.js',
Expand Down
51 changes: 32 additions & 19 deletions Extension/lib/content-script/assistant/js/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,24 +586,24 @@ var AdguardAssistant = function () {

var renderSliderAndBindEvents = function (options) {

var $document = findIframe().contents();
var $slider = $("#slider", $document);

$slider.slider({
min: options.min,
max: options.max,
range: 'min',
value: options.value,
//Prevent the slider from doing anything from the start
start: function () {
return false;
},
change: function (event, ui) {
refreshTicks(ui.value);
var delta = options.value - ui.value;
options.onSliderMove(delta);
}
});
//var $document = findIframe().contents();
//var $slider = $("#slider", $document);

//$slider.slider({
// min: options.min,
// max: options.max,
// range: 'min',
// value: options.value,
// //Prevent the slider from doing anything from the start
// start: function () {
// return false;
// },
// change: function (event, ui) {
// refreshTicks(ui.value);
// var delta = options.value - ui.value;
// options.onSliderMove(delta);
// }
//});

$(document).mouseup(function () {
$('.slider,.ui-slider-handle', $document).unbind('mousemove');
Expand Down Expand Up @@ -703,7 +703,20 @@ var AdguardAssistant = function () {
onSliderMove(elem);
};

renderSliderAndBindEvents(options);
var $document = findIframe().contents();
var $slider = $("#slider", $document);

SliderWidget.init($slider.get(0), {
min: options.min,
max: options.max,
value: options.value,
onValueChanged: function (value) {
var delta = options.value - value;
options.onSliderMove(delta);
}
});

//renderSliderAndBindEvents(options);
};

var handleShowBlockSettings = function (showBlockByUrl, showBlockSimilar) {
Expand Down
136 changes: 136 additions & 0 deletions Extension/lib/content-script/assistant/js/slider-widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* This file is part of Adguard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension).
*
* Adguard Browser Extension is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Adguard Browser Extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Adguard Browser Extension. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Slider widget
*/
var SliderWidget = (function (api, $) {

var PLACEHOLDER_CLASS = "adg-slide ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all";
var HANDLE_CLASS = "ui-slider-handle ui-state-default ui-corner-all";
var TICK_CLASS = "tick ui-widget-content";
var TICK_LEFT_COLOR = "#36BA53";
var TICK_RIGHT_COLOR = "#E0DFDB";

var placeholder = null;

var min = 0;
var max = 4;
var value = null;

var onValueChanged = null;


var refreshTicks = function () {
var ticks = $(placeholder).find(".tick");

for (var i = 0; i < ticks.length; i++) {
if (i + 1 < value) {
$(ticks[i]).css('background-color', TICK_LEFT_COLOR);
} else {
$(ticks[i]).css('background-color', TICK_RIGHT_COLOR);
}
}
};

var render = function () {
var count = max - min;

$(placeholder).addClass(PLACEHOLDER_CLASS);

var handle = $('<a>', {
"href": "#",
"class": HANDLE_CLASS,
"style": "left: " + (value - 1) * 100 / count + "%;"
});
$(placeholder).append(handle);

var prepare = function (i) {
var tick = $('<div>', {"class": TICK_CLASS}).appendTo($(placeholder));
tick.css({
left: (100 / count * i) + '%',
width: (100 / count) + '%'
});
};

for (var i = 0; i < count; i++) {
prepare(i);
}

refreshTicks();
};

var bindEvents = function () {
$(document).mouseup(function () {
$('.slider,.ui-slider-handle', $document).unbind('mousemove');
});

//While the ui-slider-handle is being held down reference it parent.
$('.ui-slider-handle', $document).mousedown(function (e) {
e.preventDefault();
return $(this).parent().mousedown();
});

var $sliderOffsetLeft = $slider.offset().left;
var $sliderWidth = $slider.width();

var getSliderValue = function (pageX) {
return (options.max - options.min) / $sliderWidth * (pageX - $sliderOffsetLeft) + options.min;
};

//This will prevent the slider from moving if the mouse is taken out of the
//slider area before the mouse down has been released.
$slider.hover(function () {
$slider.bind('click', function (e) {
//calculate the correct position of the slider set the value
var value = getSliderValue(e.pageX);
$slider.slider('value', value);
});
$slider.mousedown(function () {
$(this).bind('mousemove', function (e) {
//calculate the correct position of the slider set the value
var value = getSliderValue(e.pageX);
$(this).slider('value', value);
});
}).mouseup(function () {
$(this).unbind('mousemove');
})
}, function () {
$('#slider', $document).unbind('mousemove');
$('#slider', $document).unbind('click');
});
};

/**
*
* @param placeholderElement
* @param options
*/
api.init = function (placeholderElement, options) {
placeholder = placeholderElement;

min = options.min;
max = options.max;
value = options.value;
onValueChanged = options.onValueChanged;

render();
//bindEvents();
};

return api;
})(SliderWidget || {}, $);

0 comments on commit e039dce

Please sign in to comment.