Skip to content

Commit

Permalink
⭐ new: change slider to ion.rangeSlider
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed May 9, 2016
1 parent 8bede7f commit 059cde5
Show file tree
Hide file tree
Showing 7 changed files with 904 additions and 192 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ A schema-based form generator component for Vue.js
- [bootstrap-datetimepicker](https://github.com/Eonasdan/bootstrap-datetimepicker) for `datetime` fields (optional)
- [spectrum](https://github.com/bgrins/spectrum) for `spectrum` color picker fields (optional)
- [maskedinput](http://digitalbush.com/projects/masked-input-plugin/) for `masked` fields
- [ion.rangeSlider](https://github.com/IonDen/ion.rangeSlider) for range slider fields

## Installation
### NPM
Expand Down Expand Up @@ -158,7 +159,7 @@ npm run test
* [x] HTML5 Color picker
* [x] Color picker with spectrum
* [x] Image editor
* [ ] Better slider
* [x] Better slider (ion.rangeSlider)
* [ ] Groupable fields
* [ ] Validation handling with multiple models
* [ ] Bundle for vendor files
Expand Down
5 changes: 3 additions & 2 deletions dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.1/css/font-awesome.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/7.0.2/css/bootstrap-slider.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.8.0/spectrum.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/ion-rangeslider/2.1.4/css/ion.rangeSlider.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/ion-rangeslider/2.1.4/css/ion.rangeSlider.skinHTML5.css">

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/7.0.2/bootstrap-slider.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.8.0/spectrum.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ion-rangeslider/2.1.4/js/ion.rangeSlider.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.min.js"></script>
</head>
<body>
Expand Down
21 changes: 18 additions & 3 deletions dev/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,28 @@ module.exports = {
min: 1,
max: 10,
sliderOptions: {
tooltip: "show"
grid: true
},
validator: [
validators.integer,
validators.number
]
},
},

{
type: "slider",
label: "Income",
model: "income",
multi: true,
min: 0,
max: 100000,
sliderOptions: {
type: "double",
prefix: "$",
step: 1000
}
},

{
type: "select",
label: "Language",
Expand Down Expand Up @@ -321,7 +336,7 @@ module.exports = {
type: "staticMap",
label: "Map",
model: "address.geo",
visible: true
visible: false
},
{
type: "select",
Expand Down
968 changes: 831 additions & 137 deletions dist/vue-form-generator.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/vue-form-generator.min.js

Large diffs are not rendered by default.

49 changes: 41 additions & 8 deletions src/fields/fieldSlider.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
<template lang="jade">
input.form-control(type="text", number, v-model="value", :data-slider-value="value", :data-slider-min="schema.min", :data-slider-max="schema.max", :disabled="disabled", :placeholder="schema.placeholder")
input(type="text", :data-min="schema.min", :data-max="schema.max", :data-step="schema.step", :data-disable="disabled")
</template>

<script>
import abstractField from "./abstractField";
import { defaults, isArray } from "lodash";
export default {
mixins: [ abstractField ],
watch: {
model: function() {
if ($.fn.slider)
$(this.$el).slider("setValue", this.value);
if ($.fn.ionRangeSlider) {
let valueFrom, valueTo;
if (isArray(this.value)) {
[ valueFrom, valueTo ] = this.value;
} else
valueFrom = this.value;
$(this.$el).data("ionRangeSlider").update({
from: valueFrom,
to: valueTo
});
}
}
},
ready() {
if ($.fn.slider)
$(this.$el).slider(this.schema.sliderOptions);
if ($.fn.ionRangeSlider) {
let valueFrom, valueTo;
if (isArray(this.value)) {
[ valueFrom, valueTo ] = this.value;
} else
valueFrom = this.value;
$(this.$el).ionRangeSlider(defaults(this.schema.sliderOptions || {}, {
type: "single",
grid: true,
hide_min_max: true,
from: valueFrom,
to: valueTo,
onChange: (slider) => {
if (this.schema.sliderOptions.type == "double") {
this.value = [ slider.from, slider.to ];
} else {
this.value = slider.from;
}
}
}));
}
else
console.warn("ion.rangeSlider library is missing. Please download from https://github.com/IonDen/ion.rangeSlider and load the script and CSS in the HTML head section!");
}
};
</script>


<style lang="sass">
.slider.slider-horizontal {
width: 100% !important;
.irs {
width: 100%;
}
</style>
42 changes: 5 additions & 37 deletions test/unit/specs/fields/fieldSlider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ describe("fieldSlider.vue", () => {
label: "Rating",
model: "rating",
min: 1,
max: 10,
placeholder: "Field placeholder"
max: 10
};
let model = { rating: 8 };
let input;

before( () => {
createField(schema, model, false);
input = el.getElementsByTagName("input")[0];
//console.log(input);
});

it("should contain an input text element", () => {
Expand All @@ -38,47 +36,17 @@ describe("fieldSlider.vue", () => {

expect(input).to.be.defined;
expect(input.type).to.be.equal("text");
expect(input.classList.contains("form-control")).to.be.true;
expect(input.placeholder).to.be.equal(schema.placeholder);
expect(input.getAttribute("data-slider-min")).to.be.equal("1");
expect(input.getAttribute("data-slider-max")).to.be.equal("10");
expect(input.disabled).to.be.false;
});

it("should contain the value", (done) => {
vm.$nextTick( () => {
expect(input.getAttribute("data-slider-value")).to.be.equal("8");
expect(input.value).to.be.equal("8");
done();
});
expect(input.getAttribute("data-min")).to.be.equal("1");
expect(input.getAttribute("data-max")).to.be.equal("10");
expect(input.getAttribute("data-disable")).to.be.null;
});

it("should set disabled", (done) => {
field.disabled = true;
vm.$nextTick( () => {
expect(input.disabled).to.be.true;
done();
});
});

it("input value should be the model value after changed", (done) => {
model.rating = 3;
vm.$nextTick( () => {
expect(input.value).to.be.equal("3");
expect(input.getAttribute("data-disable")).to.be.equal("");
done();
});

});

it("model value should be the input value if changed", (done) => {
input.value = "6";
trigger(input, "input");

vm.$nextTick( () => {
expect(model.rating).to.be.equal(6);
done();
});

});

});
Expand Down

0 comments on commit 059cde5

Please sign in to comment.