How can I make Switch/Case only look at specific breakpoint? - vue.js

I'm running a switch/case that looks at the Vuetify breakpoint, but instead of just taking the name and giving me the int I want, it always ends up taking whatever the highest number is for the "limitSize" variable.
This is for a news slider I'm working on where, depending on the breakpoint, it shows either one, two, or three elements in the slider. I've tried giving the variable a default value, but that didn't work. I'd preferably like it to go SM & down is 1, MD is 2 and LG & Up is 3, but I'm not sure what the right way to achieve that is. Any help would be greatly appreciated.
The following is the Switch/Case which sits inside a computed property. I've also attached an image of the current results of the code (Image is on MD window when I'd want 2)
VueJS
pageOfWhatsNews() {
var limitSize;
switch (this.$vuetify.breakpoint.name) {
case "xs":
limitSize = 1;
case "sm":
limitSize = 1;
case "md":
limitSize = 2;
case "lg":
limitSize = 3;
case "xl":
limitSize = 3;
}
var start = (this.currentPage - 1) * limitSize;
var end = (this.currentPage - 1) * limitSize + limitSize;
return this.whatsNews.slice(start, end);
}

Well, it's because you are missing break statements in between your switch cases. Look at the correct syntax of a switch-case block below:
Switch-Case Syntax
const expr = 'Papayas';
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log(`Sorry, we are out of ${expr}.`);
}
Solution
function pageOfWhatsNews() {
var limitSize;
switch (this.$vuetify.breakpoint.name) {
case "xs":
limitSize = 1;
break;
case "sm":
limitSize = 1;
break;
case "md":
limitSize = 2;
break;
case "lg":
limitSize = 3;
break;
case "xl":
limitSize = 3;
break;
}
var start = (this.currentPage - 1) * limitSize;
var end = (this.currentPage - 1) * limitSize + limitSize;
console.log(limitSize);
// return this.whatsNews.slice(start, end);
}
// Just for demo
this.$vuetify = {breakpoint: {name: 'md'}};
pageOfWhatsNews();
Optimized Solution
I would also suggest you put cases of md, lg & xl breakpoints and rest of the breakpoints case fallback to the default case.
function pageOfWhatsNews() {
var limitSize;
switch (this.$vuetify.breakpoint.name) {
case "md":
limitSize = 2;
break;
case "lg":
limitSize = 3;
break;
case "xl":
limitSize = 3;
break;
default:
limitSize = 1;
}
var start = (this.currentPage - 1) * limitSize;
var end = (this.currentPage - 1) * limitSize + limitSize;
console.log(limitSize);
// return this.whatsNews.slice(start, end);
}
// Just for demo
this.$vuetify = {breakpoint: {name: 'sm'}};
pageOfWhatsNews();

Related

How to call a computed property in dynamic style?

I want to style Ant Design Vue button(inside a table row of Ant Design table) dynamically:
<template #status="{ text }">
<a-button ghost
:style="{'border-color': getColor(text) }">
</a-button>
</template>
And here is my computed propery(in script section):
const getColor = computed((status) => {
let color = '';
switch(status) {
case 'StatusA':
color = '#F97316';
break;
case 'StatusB':
color = '#EC4899';
break;
case 'StatusC':
color = '#8B5CF6'
break;
case 'StatusD':
color = '#16A34A';
break;
default:
color = "#5685EE";
}
return color;
})
But it is not working.
Error: This expression is not callable.Type 'String' has no call signatures
How do I do this?
Thanks.
Try to use computed property in options API by returning a function that takes the status as parameter:
setup(){
...
},
computed:{
getColor(){
return (status)=>{
let color = '';
switch(status) {
case 'StatusA':
color = '#F97316';
break;
case 'StatusB':
color = '#EC4899';
break;
case 'StatusC':
color = '#8B5CF6'
break;
case 'StatusD':
color = '#16A34A';
break;
default:
color = "#5685EE";
}
return color;
}
}
}
or just use function inside the setup :
const getColor = (status) => {
let color = '';
switch(status) {
case 'StatusA':
color = '#F97316';
break;
case 'StatusB':
color = '#EC4899';
break;
case 'StatusC':
color = '#8B5CF6'
break;
case 'StatusD':
color = '#16A34A';
break;
default:
color = "#5685EE";
}
return color;
}

Vehicle Spinner move faster when moving mouse faster

How can I speed up the rotation speed in the below script. Currently the movement is calculated like so:
handleMove($event) {
if (this.isMoving && this.isDragging) {
const positions = {
x: $event.pageX || $event.touches[0].pageX,
y: $event.pageY || $event.touches[0].pageY
}
this.changeFrame(positions);
this.lastX = positions.x;
this.lastY = positions.y;
}
},
changeFrame(positions) {
this.speedController += 1;
if ((this.speedController < this.speed)) {
return;
}
if (this.speedController > this.speed) {
this.speedController = 0;
}
if (positions.x > this.lastX) {
if (this.frame >= 0 && this.frame < this.images.length) {
this.frame += 1;
} else if (this.loop) {
this.frame = 1;
}
} else if (positions.x < this.lastX) {
if (this.frame >= 0 && this.frame - 1 > 0) {
this.frame -= 1;
} else if (this.loop) {
this.frame = this.images.length;
}
}
}
I have tried to change section where it increases the the frame number, to this.frame += positions.x - this.lastX however I then get the following error:
[Vue warn]: You may have an infinite update loop in a component render function.
What is the best way to do this? You can see the script running here.
Edit
I have updated the script, however it is really glitchy! Use the link for a live example.
changeFrame(positions) {
const diff = positions.x - this.startX;
let frameDelta = diff / this.speed;
this.frame += Math.round(frameDelta);
if (this.frame < 0) {
this.frame += this.images.length;
} else if (this.frame > this.images.length - 1) {
this.frame = this.frame % this.images.length;
}
}
Your logic is that as long as position.x > this.lastX that you increment this.frame by 1. If you want it to spin faster, then increment it by a larger number. The same goes for decrementing the number.
However, this speed depends on how often changeFrame() is fired, and that can be variable due to browser performance and etc. If you want the most accurate results, you should simply increment/decrement the frame based on how much the position has changed.
If you say, want to change the speed so that for every 10px of cursor movement you increment the frame by one, you can do this:
const diff = positions.x - this.lastX;
// Let's say we increment/decrement the frame for every 10px travelled
const rateOfSpin = 10;
// Number of frames of change, adjusted based on desired rate of spin
const frameDelta = diff / rateOfSpin;
this.frame += frameDelta;
// If we go below zero, then we start from the end
if (this.frame < 0) {
this.frame += this.images.length;
// Otherwise we simply get the modulus
} else if (this.frame > this.images.length) {
this.frame = this.frame % this.images.length;
}

YADCF range_number - is it possible to add a preset select list to to/from range?

I want to add a select list to to/from fields of range_number so user can pick from set amounts to set range.
best option would be to use the custom function filtering filter_type: 'custom_func'
see showcase, first column , code sample and everything can be found on that page
{
column_number: 0,
filter_type: 'custom_func',
custom_func: myCustomFilterFunction,
...
(possible) custom func implementation
function myCustomFilterFunction(filterVal, columnVal) {
var found;
if (columnVal === '') {
return true;
}
switch (filterVal) {
case 'happy':
found = columnVal.search(/:-\]|:\)|Happy|JOY|:D/g);
break;
case 'sad':
found = columnVal.search(/:\(|Sad|:'\(/g);
break;
case 'angry':
found = columnVal.search(/!!!|Arr\.\.\./g);
break;
case 'lucky':
found = columnVal.search(/777|Bingo/g);
break;
case 'january':
found = columnVal.search(/01|Jan/g);
break;
default:
found = 1;
break;
}
if (found !== -1) {
return true;
}
return false;
}

How to implement methods and state for vue.js component

I'm a beginner in VueJS. And as part of my learning process, I'm building a knob for my Pomodoro app. This is my fiddle.
I copied the knob code from codepen, which is implemented using jquery. As you can see in the fiddle most of the job is done by jquery.
I need to try and do this using Vue.js, using its methods and states.
How to refactor this code to a better Vue.JS code? Any suggestions much appreciated.
Vue.component('timer', {
mounted() {
var knob = $('.knob');
var angle = 0;
var minangle = 0;
var maxangle = 270;
var xDirection = "";
var yDirection = "";
var oldX = 0;
var oldY = 0;
function moveKnob(direction) {
if(direction == 'up') {
if((angle + 2) <= maxangle) {
angle = angle + 2;
setAngle();
}
}
else if(direction == 'down') {
if((angle - 2) >= minangle) {
angle = angle - 2;
setAngle();
}
}
}
function setAngle() {
// rotate knob
knob.css({
'-moz-transform':'rotate('+angle+'deg)',
'-webkit-transform':'rotate('+angle+'deg)',
'-o-transform':'rotate('+angle+'deg)',
'-ms-transform':'rotate('+angle+'deg)',
'transform':'rotate('+angle+'deg)'
});
// highlight ticks
var activeTicks = (Math.round(angle / 10) + 1);
$('.tick').removeClass('activetick');
$('.tick').slice(0,activeTicks).addClass('activetick');
// update % value in text
var pc = Math.round((angle/270)*100);
$('.current-value').text(pc+'%');
}
var RAD2DEG = 180 / Math.PI;
knob.centerX = knob.offset().left + knob.width()/2;
knob.centerY = knob.offset().top + knob.height()/2;
var offset, dragging=false;
knob.mousedown(function(e) {
dragging = true;
offset = Math.atan2(knob.centerY - e.pageY, e.pageX - knob.centerX);
})
$(document).mouseup(function() {
dragging = false
})
$(document).mousemove(function(e) {
if (dragging) {
if (oldX < e.pageX) {
xDirection = "right";
} else {
xDirection = "left";
}
oldX = e.pageX;
if(xDirection === "left") {
moveKnob('down');
} else {
moveKnob('up');
}
return false;
}
})
}
});
This example runs without jQuery.
https://jsfiddle.net/guanzo/d6vashmu/6/
Declare all the variables you need in the data function.
Declare all functions under the methods property.
Declare variables that are derived from other variables in the computed property, such as knobStyle, activeTicks, and currentValue, which are all computed from angle. Whenever angle changes, these 3 computed properties will automatically update.
Regarding the general usage of Vue, you should focus on manipulating the data, and letting Vue update the DOM for you.

needed thumbnail tiles scroller/slideshow/slider

I'm looking for a thumbnail tiles scroller/slideshow/slider
Here is the example http://safari.to/clients
Really appreciate if anyone could help :) Thanks
When you see something on any website, its easy to inspect and see how they are doing it.
The website mentioned by you is doing it using their own script instead of a plug in
See the following code from http://safari.to/assets/js/script.js. You will also need to see how they are styling the sliders by inspecting their CSS code
// Agencies slide
var clients = Math.floor(Math.random() * 2) + 1; // nth-child indices start at 1
if ( clients == 1){
$('.agencies.clients').hide();
}
else
{
$('.brands.clients').hide();
}
$('.agencies menu a').bind({
click: function()
{
if(sliding) return false;
var pointer = $(this);
var width = $('.agencies .scroller li').length * 137;
var current = parseInt($('.agencies .scroller').css('left'));
var distance = 0;
if(pointer.is('.right-pointer'))
{
if(current == -1920) distance = current - 137;
else distance = current - 960;
if((width + current) < 960)
distance = current;
}
else
{
distance = current + 1097;
if(distance > 0)
distance = 0;
}
sliding = true;
$('.scroller').animate({
left: distance + 'px'
}, 300,
function()
{
sliding = false;
});
}
});