Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draggable mixer wizard #1116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ sources.js = [
'./js/libraries/jbox/jBox.min.js',
'./js/libraries/switchery/switchery.js',
'./js/libraries/jquery.ba-throttle-debounce.js',
'./js/libraries/jsrender.min.js',
'./js/helpers.js',
'./node_modules/inflection/inflection.min.js',
'./node_modules/bluebird/js/browser/bluebird.min.js',
Expand Down Expand Up @@ -111,6 +112,7 @@ sources.js = [
'./js/tabs.js',
'./js/preset_definitions.js',
'./tabs/*.js',
'./modals/*.js',
'./js/eventFrequencyAnalyzer.js',
'./js/periodicStatusUpdater.js',
'./js/serial_queue.js',
Expand Down
3 changes: 3 additions & 0 deletions js/libraries/jsrender.min.js

Large diffs are not rendered by default.

27 changes: 22 additions & 5 deletions main.css
Original file line number Diff line number Diff line change
Expand Up @@ -2159,8 +2159,25 @@ ol li {
margin-left: 1em;
}

.red-background,
.red-background td,
.red-background th {
background-color: tomato !important;
}
.mixer-wizard * {
user-select: none;
}

.mixer-wizard .position [draggable] {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}

.drop-zone {
border: solid 2px transparent;
}

.drop-zone.drop-active {
border: dashed 2px black;
}

.drop-zone.drop-active * {
pointer-events: none;
}
114 changes: 114 additions & 0 deletions modals/mixer_wizard_modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
'use strict';

var helper = helper || {};

const templateCache = {};

helper.setupModal = function ($trigger, templateId, title, model, onOpen, onClose) {
if (!templateCache[templateId]) {
templateCache[templateId] = $.templates(templateId);
$.views.converters("i18n", chrome.i18n.getMessage, templateCache[templateId]);
}

const html = templateCache[templateId].render(model);

return new jBox("Modal", {
width: 480,
height: 410,
closeButton: "title",
animation: false,
attach: $trigger,
title: title,
content: html,
onOpen,
onClose
});
};

helper.setupMixerWizard = function ($trigger, templateId, title) {
const model = {
mappings: [
{
positionText: "motorWizard0",
positionIndex: 0,
motorIndex: 0,
},
{
positionText: "motorWizard1",
positionIndex: 1,
motorIndex: 1,
},
{
positionText: "motorWizard2",
positionIndex: 2,
motorIndex: 2,
},
{
positionText: "motorWizard3",
positionIndex: 3,
motorIndex: 3,
}
]
};

const dropZoneSelector = ".drop-zone";
const draggableSelector = "[draggable]";
return helper.setupModal(
$trigger,
templateId,
title,
model,
function () {
const $container = this.container;

$container.on("dragstart", draggableSelector, function (event) {
const $target = $(event.target);

const motorIndex = $target.attr("data-motor-index");
event.originalEvent.dataTransfer.setData("text", motorIndex);
});

$container.on("dragenter", dropZoneSelector, function (event) {
event.preventDefault();

const $target = $(event.target);
const $dropZone = $target.closest(dropZoneSelector);
$dropZone.addClass("drop-active");
});

$container.on("dragleave", dropZoneSelector, function (event) {
const $target = $(event.target);
const $dropZone = $target.closest(dropZoneSelector);
$dropZone.removeClass("drop-active");
});

$container.on("dragover", dropZoneSelector, function (event) {
event.preventDefault();

event.originalEvent.dataTransfer.dropEffect = 'move';
});

$container.on("drop", dropZoneSelector, function (event) {
event.preventDefault();

const $target = $(event.target);
const $dropZone = $target.closest(dropZoneSelector);
$dropZone.removeClass("drop-active");

const motorIndex = event.originalEvent.dataTransfer.getData("text");
event.originalEvent.dataTransfer.clearData();
const $heldElement = $container.find(`${draggableSelector}[data-motor-index=${motorIndex}]`);
const $heldElementOriginalDropZone = $heldElement.closest(dropZoneSelector);

const $replacedElement = $dropZone.find(draggableSelector);

$dropZone.append($heldElement);
$heldElementOriginalDropZone.append($replacedElement);
});
},
function () {
const $container = this.container;
$container.off();
}
);
};
90 changes: 29 additions & 61 deletions tabs/mixer.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,69 +169,37 @@ <h1 class="modal__title modal__title--warning" data-i18n="presetsApplyHeader"></
<a id="execute-button" class="modal__button modal__button--main" data-i18n="mixerButtonSaveAndReboot"></a>
</div>
</div>
<div id="mixerWizardContent" class="is-hidden">
<div class="modal__content">
<h1 class="modal__title modal__title--warning" data-i18n="mixerWizardModalTitle"></h1>
<div class="modal__text">
<p data-i18n="mixerWizardInfo"></p>
<table style="margin-top: 1em;" class="mixer-table">
<thead>
<th data-i18n="mixerWizardMotorPosition"></th>
<th data-i18n="mixerWizardMotorIndex"></th>
</thead>
<tbody>
<tr>
<th data-i18n="motorWizard0"></th>
<td>
<select class="wizard-motor-select" data-motor="0">
<option id="0" selected="selected">Motor #1</option>
<option id="1">Motor #2</option>
<option id="2">Motor #3</option>
<option id="3">Motor #4</option>
</select>
</td>
</tr>
<tr>
<th data-i18n="motorWizard1"></th>
<td>
<select class="wizard-motor-select" data-motor="1">
<option id="0">Motor #1</option>
<option id="1" selected="selected">Motor #2</option>
<option id="2">Motor #3</option>
<option id="3">Motor #4</option>
</select>
</td>
</tr>
<tr>
<th data-i18n="motorWizard2"></th>
<td>
<select class="wizard-motor-select" data-motor="2">
<option id="0">Motor #1</option>
<option id="1">Motor #2</option>
<option id="2" selected="selected">Motor #3</option>
<option id="3">Motor #4</option>
</select>
</td>
</tr>
<tr>
<th data-i18n="motorWizard3"></th>
<td>
<select class="wizard-motor-select" data-motor="3">
<option id="0">Motor #1</option>
<option id="1">Motor #2</option>
<option id="2">Motor #3</option>
<option id="3" selected="selected">Motor #4</option>
</select>
</td>
</tr>
</tbody>
</table>
<script id="mixerWizardTemplate" type="text/x-jsrender">
<div class="mixer-wizard">
<div class="modal__content">
<h1 class="modal__title modal__title--warning">{{i18n: "mixerWizardModalTitle"}}</h1>
<div class="modal__text">
<p>{{i18n: "mixerWizardInfo"}}</p>
<table style="margin-top: 1em;" class="mixer-table">
<thead>
<th>{{i18n: "mixerWizardMotorPosition"}}</th>
<th>{{i18n: "mixerWizardMotorIndex"}}</th>
</thead>
<tbody>
{{for mappings}}
<tr>
<th>{{i18n: positionText}}</th>
<td class="position drop-zone" data-position-index="{{> positionIndex}}">
<div data-motor-index="{{> motorIndex}}" draggable="true">
<span>Motor #{{> motorIndex + 1}}</span>
</div>
</td>
</tr>
{{/for}}
</tbody>
</table>
</div>
</div>
<div class="modal__buttons">
<a id="wizard-execute-button" class="modal__button modal__button--main">{{i18n: "mixerWizardModalApply"}}</a>
</div>
</div>
<div class="modal__buttons">
<a id="wizard-execute-button" class="modal__button modal__button--main" data-i18n="mixerWizardModalApply"></a>
</div>
</div>
</script>
<div class="content_toolbar">
<div class="btn save_btn">
<a id="save-button" class="save" href="#" data-i18n="configurationButtonSave"></a>
Expand Down
67 changes: 16 additions & 51 deletions tabs/mixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,66 +277,31 @@ TABS.mixer.initialize = function (callback, scrollPosition) {
$mixerPreset = $('#mixer-preset'),
$wizardButton = $("#mixer-wizard");

motorWizardModal = new jBox('Modal', {
width: 480,
height: 410,
closeButton: 'title',
animation: false,
attach: $wizardButton,
title: chrome.i18n.getMessage("mixerWizardModalTitle"),
content: $('#mixerWizardContent')
});

function validateMixerWizard() {
let errorCount = 0;
for (let i = 0; i < 4; i++) {
const $elements = $('[data-motor] option:selected[id=' + i + ']'),
assignedRulesCount = $elements.length;

if (assignedRulesCount != 1) {
errorCount++;
$elements.closest('tr').addClass("red-background");
} else {
$elements.closest('tr').removeClass("red-background");
}

}

return (errorCount == 0);
}

$(".wizard-motor-select").change(validateMixerWizard);

$("#wizard-execute-button").click(function () {

// Validate mixer settings
if (!validateMixerWizard()) {
return;
}
motorWizardModal = helper.setupMixerWizard(
$wizardButton,
"#mixerWizardTemplate",
chrome.i18n.getMessage("mixerWizardModalTitle")
);

$(document).on("click", "#wizard-execute-button", function () {
MOTOR_RULES.flush();

for (let i = 0; i < 4; i++) {
const $selects = $(".wizard-motor-select");
let rule = -1;

$selects.each(function () {
if (parseInt($(this).find(":selected").attr("id"), 10) == i) {
rule = parseInt($(this).attr("data-motor"), 10);
}
});
$positions = $(this).closest(".mixer-wizard").find(".position");

const r = currentMixerPreset.motorMixer[rule];
for (let positionIndex = 0; positionIndex < $positions.length; positionIndex++) {
const $position = $positions.filter(`[data-position-index="${positionIndex}"]`);
const $motor = $position.find("[draggable]");
const motorIndex = parseInt($motor.attr("data-motor-index"), 10);

const rule = currentMixerPreset.motorMixer[motorIndex];
MOTOR_RULES.put(
new MotorMixRule(
r.getThrottle(),
r.getRoll(),
r.getPitch(),
r.getYaw()
rule.getThrottle(),
rule.getRoll(),
rule.getPitch(),
rule.getYaw()
)
);

}

renderMotorMixRules();
Expand Down