Categories
Book Reviews

JavaScript Books That are Easy to Understand

JavaScript may be confusing for some people.

To make learning JavaScript easier, here are a few books to let make learning it easier.

The Principles of Object-Oriented JavaScript by Nicholas C. Zakas

This is a short book that’s focused on the ins and outs of the object oriented parts of JavaScript.

It focuses on data types, objects, constructors, and classes.

These are all the things that can trick even the most experienced developers.

This book makes learning these parts of JavaScript easy by giving us focus and explaining the concepts concisely and with simple examples.

JavaScript for Kids: A Playful Introduction to Programming

Programming is useful for anyone. We can get a job with prorgramming skills or we can use the skill to program our own computers.

JavaScript for Kids: A Playful Introduction to Programming provides us with a gentle introduction to programming that kids can understand.

Explanation of the concepts are easy to understand and simple.

The examples that are included are easy to follow and fun to try.

It’s a good book to learn JavaScript even for adults.

Categories
JavaScript Vue

Add a WYSIWYG Editor into a Vue App with the vue-wysiwyg NPM Package

We can add a WYSIWYG text editor with the vue-wysiwyg package.

First, we install it by running:

npm i vue-wysiwyg

Next, we register the component and import the CSS by writing:

import Vue from "vue";
import App from "./App.vue";
import wysiwyg from "vue-wysiwyg";
import "vue-wysiwyg/dist/vueWysiwyg.css";

Vue.use(wysiwyg, {});
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

Then we can use the wysiwyg component to add the rich text editor to our app by writing:

<template>
  <div>
    <wysiwyg v-model="content"/>
    <div v-html="content"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: ""
    };
  }
};
</script>

We just bind whatever is entered with v-model.

The entered content will be converted to HTML.

So, we can display the content with v-html.

It also takes an object as a second argument to Vue.use to set the options for the text editor.

This argument is optional.

For instance, we can write:

import Vue from "vue";
import App from "./App.vue";
import wysiwyg from "vue-wysiwyg";
import "vue-wysiwyg/dist/vueWysiwyg.css";

Vue.use(wysiwyg, {
  hideModules: { underline: true },
  iconOverrides: { bold: `<span>bold</span>` },
  image: {
    uploadURL: "/api/myEndpoint",
    dropzoneOptions: {}
  },
  maxHeight: "500px"
});
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

Then we can hide modules with hideModules, iconOverrides lets us change the icons.

image lets us upload an image via a URL.

maxHeight changes the maximum height.

vue-wysiwyg is a very flexible text WYSIWYG text editor for Vuejs.

It has many options that we can add to change the appearance of the editor to what we want.

It outputs HTML so we can use the created data anywhere.

Categories
JavaScript Vue

Add a Vuejs Range Slider with vue-slider-component

We can add a range slider in a Vuejs app with the vue-slider-component.

To use it, we install it by running:

npm i vue-slider-component

Then we can use it after registering it:

<template>
  <div>
    <vue-slider v-model="value"/>
    <p>{{value}}</p>
  </div>
</template>

<script>
import VueSlider from "vue-slider-component";
import "vue-slider-component/theme/antd.css";

export default {
  components: {
    VueSlider
  },
  data() {
    return {
      value: 0
    };
  }
};
</script>

We import the component and the styles and then register the component.

Then we add the vue-slider component to add the range slider to our component.

Now we see a slider displayed on the screen.

The min value is 0 and max is 100 by default.

It has many other options.

We can set the interval prop to change the interval.

We can add the mark prop to display numbers on the range slider.

Also, we can add the tooltip prop to display a tooltip with the currently selected value.

For instance, we can write:

<template>
  <div>
    <vue-slider v-model="value" tooltip="always"/>
    <p>{{value}}</p>
  </div>
</template>

<script>
import VueSlider from "vue-slider-component";
import "vue-slider-component/theme/antd.css";

export default {
  components: {
    VueSlider
  },
  data() {
    return {
      value: 0
    };
  }
};
</script>

to show the tooltip.

The min and max values can be changed with the minRange and maxRange props respectively.

The fixed prop keeps the distance between 2 sliders fixed.

For instance, we can write:

<template>
  <div>
    <vue-slider v-model="value" :minRange="20" :maxRange="50"/>
    <p>{{value}}</p>
  </div>
</template>

<script>
import VueSlider from "vue-slider-component";
import "vue-slider-component/theme/antd.css";

export default {
  components: {
    VueSlider
  },
  data() {
    return {
      value: [0, 100]
    };
  }
};
</script>

We have an array for value to enable range mode.

This will make the range slider show 2 slider handles instead of one.

Then we can set the min and max values which will be the positions of 2 sliders.

The vue-slider-component is a range slider that has many options.

We can select a single number or a range of numbers.

The interval can be changed and we can display numbers on the range slider or the tooltip as we wish.

Categories
JavaScript Vue

Add a Color Picker to a Vuejs App

To add a color picker to a Vuejs app, we can use the vue-color package.

To install it, we run:

npm install vue-color

Then we can use it by writing:

<template>
  <div>
    <photoshop-picker v-model="colors"/>
    <p>{{colors}}</p>
  </div>
</template>

<script>
import { Photoshop } from "vue-color";

export default {
  data() {
    return {
      colors: {}
    };
  },
  components: {
    "photoshop-picker": Photoshop
  }
};
</script>

We just register the component and bind the selected value to the colors state.

The Photoshop-style color picker looks like the color picker in Photoshop.

Other styles include the Material style, compact style, swatches, slider, and Sketch-style.

They can be imported the same way.

The selected color will have several properties with color values.

For example, we may get something like:

{ "hsl": { "h": 0, "s": 0.26726650614624253, "l": 0.27429116, "a": 1 }, "hex": "#593333", "hex8": "#593333FF", "rgba": { "r": 89, "g": 51, "b": 51, "a": 1 }, "hsv": { "h": 0, "s": 0.42179999999999995, "v": 0.34759999999999996, "a": 1 }, "oldHue": 0, "source": "hsva", "a": 1 }

for colors.

We get the RGB, hex, and HSL values all in one object.

The vue-color package lets us add a color picker to our Vuejs app easily.

It’s easy to use and comes in many styles.

Categories
JavaScript Vue

Add a Time Picker to a Vue App with vue-timeselector

To make adding a time picker easier, we can use the vue-timeselector package.

First, we install it by running:

npm i vue-timeselector

Then we can use it by registering the component and putting it in the template:

<template>
  <div id="app">
    <timeselector v-model="time"></timeselector>
    <p>{{time}}</p>
  </div>
</template>

<script>
import Timeselector from "vue-timeselector";

export default {
  components: {
    Timeselector
  },
  data() {
    return {
      time: undefined
    };
  }
};
</script>

v-model binds the time property to the timeselector‘s value.

We can also use the placeholder and required props like a regular input component:

<template>
  <div id="app">
    <timeselector v-model="time" placeholder="Select a time" required></timeselector>
    <p>{{time}}</p>
  </div>
</template>

<script>
import Timeselector from "vue-timeselector";

export default {
  components: {
    Timeselector
  },
  data() {
    return {
      time: undefined
    };
  }
};
</script>

We can also add the disabled prop to disable interactivity:

<template>
  <div id="app">
    <timeselector v-model="time" disabled></timeselector>
    <p>{{time}}</p>
  </div>
</template>

<script>
import Timeselector from "vue-timeselector";

export default {
  components: {
    Timeselector
  },
  data() {
    return {
      time: undefined
    };
  }
};
</script>

The date format can be customized with the returnFormat prop:

<template>
  <div id="app">
    <timeselector v-model="time" returnFormat="HH" @formatedTime="formatTime"></timeselector>
    <p>{{formattedTime}}</p>
  </div>
</template>

<script>
import Timeselector from "vue-timeselector";

export default {
  components: {
    Timeselector
  },
  data() {
    return {
      time: undefined,
      formattedTime: undefined
    };
  },
  methods: {
    formatTime(time) {
      this.formattedTime = time;
    }
  }
};
</script>

We pass in the formatTime method which has the formatted time set as the value of the time parameter.

Then we set that value to this.formattedTime and display it.

returnFormat sets the format of the formatted time.

The interval of the hours and minutes can be changed with the interval prop:

<template>
  <div id="app">
    <timeselector v-model="time" displaySeconds :interval="{h:2, m:1, s:10}"></timeselector>
    <p>{{formattedTime}}</p>
  </div>
</template>

<script>
import Timeselector from "vue-timeselector";

export default {
  components: {
    Timeselector
  },
  data() {
    return {
      time: undefined
    };
  }
};
</script>

We set the interval prop to an object, h is for hour the hour interval, m for minute, and s for seconds.

Now we see that the hour picker only displays the even hours.

We can also disable some choices from the time picker with the disabled prop:

<template>
  <div id="app">
    <timeselector v-model="time" displaySeconds :disable="{h:[1, 5], m:null, s:[10,20,25]}"></timeselector>
    <p>{{time}}</p>
  </div>
</template>

<script>
import Timeselector from "vue-timeselector";

export default {
  components: {
    Timeselector
  },
  data() {
    return {
      time: undefined
    };
  }
};
</script>

We have the disable prop with an object to disable hour 1 and 2

vue-timeselector also provides slots to let us change the labels display.

There’s the hours, minutes, seconds, ampm and clear-ico slots to let us change the text of those labels.

For example, we can write:

<template>
  <div id="app">
    <timeselector v-model="time">
      <template slot="hours">
        <span>Hours</span>
      </template>
    </timeselector>
    <p>{{time}}</p>
  </div>
</template>

<script>
import Timeselector from "vue-timeselector";

export default {
  components: {
    Timeselector
  },
  data() {
    return {
      time: undefined
    };
  }
};
</script>

to set the hours heading to Hours.

vue-timeselector is an easy to use package to let us add a time picker to our Vue app.

It supports various formatting and customization options.