ctucx.git: ctucx.things

simple inventory management web-app

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
<style lang="scss">
@import "../css/_globals";

.lpUnitSelect {
	border: 1px solid transparent;
	cursor: pointer;
	display: inline-block;
	padding: 0 5px;
	position: relative;

	&:hover,
	&.lpHover {
		background: #fff;
		border: 1px solid $border1;

		i {
			opacity: 1;
		}
	}

	i {
		opacity: 0.6;
	}

	&.lpOpen {
		background: #fff;

		.lpUnitDropdown {
			display: block;
		}
	}

	.lpDisplay {
		display: inline-block;
		width: 1.1em;
	}

	.lpUnitDropdown {
		background: #fff;
		border: 1px solid #ccc;
		display: none;
		left: 0;
		padding: 0;
		position: absolute;
		top: -1px;
		z-index: $aboveSidebar+1;

		&.kg {
			top: -30px;
		}

		li {
			list-style: none;
			padding: 2px 14px;

			&:hover {
				background: $blue1;
				color: #fff;
			}
		}
	}
}
</style>

<template>
	<div class="lpUnitSelect" :class="{lpOpen: isOpen, lpHover: isFocused}" @click="toggle($event)">
		<select class="lpUnit lpInvisible" :value="unit" @keyup="keyup($event)" @focus="focusSelect" @blur="blurSelect">
			<option v-for="unit in units" :value="unit">
				{{ unit }}
			</option>
		</select>
		<span class="lpDisplay">{{ unit }}</span>
		<i class="lpSprite lpExpand" />
		<ul :class="'lpUnitDropdown ' + unit">
			<li v-for="unit in units" :class="unit" @click="select(unit)">
				{{ unit }}
			</li>
		</ul>
	</div>
</template>

<script>
export default {
	name: 'UnitSelect',
	props: ['weight', 'unit', 'onChange'],
	data() {
		return {
			units: [
				'g',
				'kg',
			],
			isOpen: false,
			isFocused: false,
		};
	},
	methods: {
		toggle(evt) {
			evt.stopPropagation();
			if (!this.isOpen) {
				this.open();
			} else {
				this.close();
			}
		},
		open() {
			this.isOpen = true;
			this.bindCloseListeners();
		},
		close() {
			this.isOpen = false;
			this.unbindCloseListeners();
		},
		select(unit) {
			if (typeof this.onChange === 'function') {
				this.onChange(unit);
			}
		},
		keyup(evt) {
			if (typeof this.onChange === 'function') {
				this.onChange(evt.target.value);
			}
		},
		bindCloseListeners() {
			window.addEventListener('keyup', this.closeOnEscape);
			window.addEventListener('click', this.closeOnClick);
		},
		unbindCloseListeners() {
			window.removeEventListener('keyup', this.closeOnEscape);
			window.removeEventListener('click', this.closeOnClick);
		},
		closeOnEscape(evt) {
			if (evt.keyCode === 27) {
				this.close();
			}
		},
		closeOnClick(evt) {
			this.close();
		},
		focusSelect() {
			this.isFocused = true;
		},
		blurSelect() {
			this.isFocused = false;
		},
	},
};
</script>