date_picker.vue 3.83 KB
Newer Older
Clement Ho's avatar
Clement Ho committed
1
2
3
4
5
6
7
8
<script>
  import datePicker from '../pikaday.vue';
  import loadingIcon from '../loading_icon.vue';
  import toggleSidebar from './toggle_sidebar.vue';
  import collapsedCalendarIcon from './collapsed_calendar_icon.vue';
  import { dateInWords } from '../../../lib/utils/datetime_utility';

  export default {
Filipa Lacerda's avatar
Filipa Lacerda committed
9
10
11
12
13
14
15
    name: 'SidebarDatePicker',
    components: {
      datePicker,
      toggleSidebar,
      loadingIcon,
      collapsedCalendarIcon,
    },
Clement Ho's avatar
Clement Ho committed
16
    props: {
17
18
19
20
21
      blockClass: {
        type: String,
        required: false,
        default: '',
      },
Clement Ho's avatar
Clement Ho committed
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
      collapsed: {
        type: Boolean,
        required: false,
        default: true,
      },
      showToggleSidebar: {
        type: Boolean,
        required: false,
        default: false,
      },
      isLoading: {
        type: Boolean,
        required: false,
        default: false,
      },
      editable: {
        type: Boolean,
        required: false,
        default: false,
      },
      label: {
        type: String,
        required: false,
        default: 'Date picker',
      },
      selectedDate: {
        type: Date,
        required: false,
Filipa Lacerda's avatar
Filipa Lacerda committed
50
        default: null,
Clement Ho's avatar
Clement Ho committed
51
52
53
54
      },
      minDate: {
        type: Date,
        required: false,
Filipa Lacerda's avatar
Filipa Lacerda committed
55
        default: null,
Clement Ho's avatar
Clement Ho committed
56
57
58
59
      },
      maxDate: {
        type: Date,
        required: false,
Filipa Lacerda's avatar
Filipa Lacerda committed
60
        default: null,
Clement Ho's avatar
Clement Ho committed
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
      },
    },
    data() {
      return {
        editing: false,
      };
    },
    computed: {
      selectedAndEditable() {
        return this.selectedDate && this.editable;
      },
      selectedDateWords() {
        return dateInWords(this.selectedDate, true);
      },
      collapsedText() {
        return this.selectedDateWords ? this.selectedDateWords : 'None';
      },
    },
    methods: {
      stopEditing() {
        this.editing = false;
      },
      toggleDatePicker() {
        this.editing = !this.editing;
      },
      newDateSelected(date = null) {
        this.date = date;
        this.editing = false;
        this.$emit('saveDate', date);
      },
      toggleSidebar() {
        this.$emit('toggleCollapse');
      },
    },
  };
</script>

<template>
99
100
101
102
  <div
    class="block"
    :class="blockClass"
  >
Clement Ho's avatar
Clement Ho committed
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    <div class="issuable-sidebar-header">
      <toggle-sidebar
        :collapsed="collapsed"
        @toggle="toggleSidebar"
      />
    </div>
    <collapsed-calendar-icon
      class="sidebar-collapsed-icon"
      :text="collapsedText"
    />
    <div class="title">
      {{ label }}
      <loading-icon
        v-if="isLoading"
        :inline="true"
      />
      <div class="pull-right">
        <button
          v-if="editable && !editing"
          type="button"
          class="btn-blank btn-link btn-primary-hover-link btn-sidebar-action"
          @click="toggleDatePicker"
        >
          Edit
        </button>
        <toggle-sidebar
          v-if="showToggleSidebar"
          :collapsed="collapsed"
          @toggle="toggleSidebar"
        />
      </div>
    </div>
    <div class="value">
      <date-picker
        v-if="editing"
        :selected-date="selectedDate"
        :min-date="minDate"
        :max-date="maxDate"
        :label="label"
        @newDateSelected="newDateSelected"
        @hidePicker="stopEditing"
      />
      <span
        v-else
        class="value-content"
      >
        <template v-if="selectedDate">
          <strong>{{ selectedDateWords }}</strong>
          <span
            v-if="selectedAndEditable"
            class="no-value"
          >
            -
            <button
              type="button"
              class="btn-blank btn-link btn-secondary-hover-link"
              @click="newDateSelected(null)"
            >
              remove
            </button>
          </span>
        </template>
        <span
          v-else
          class="no-value"
        >
          None
        </span>
      </span>
    </div>
  </div>
</template>