Commit 62c152ce authored by Clement Ho's avatar Clement Ho

[skip ci] refactor

parent bc81c01b
import CollapsedAvatar from './avatar'; import CollapsedAvatar from './avatar';
export default { export default {
name: 'CollapsedMultipleAssignees', name: 'CollapsedAssignees',
data() {
return {
defaultMaxCounter: 99,
};
},
props: { props: {
users: { type: Array, required: true }, users: { type: Array, required: true },
}, },
computed: { computed: {
title() { title() {
const maxRender = Math.min(5, this.users.length); const maxRender = Math.min(this.defaultRenderCount, this.users.length);
const renderUsers = this.users.slice(0, maxRender); const renderUsers = this.users.slice(0, maxRender);
const names = []; const names = [];
...@@ -22,26 +27,45 @@ export default { ...@@ -22,26 +27,45 @@ export default {
counter() { counter() {
let counter = `+${this.users.length - 1}`; let counter = `+${this.users.length - 1}`;
if (this.users.length > 99) { if (this.users.length > this.defaultMaxCounter) {
counter = '99+'; counter = `${this.defaultMaxCounter}+`;
} }
return counter; return counter;
}, },
hasNoAssignees() {
return this.users.length === 0;
},
hasTwoAssignees() {
return this.users.length === 2;
},
moreThanOneAssignees() {
return this.users.length > 1;
},
morethanTwoAssignees() {
return this.users.length > 2;
},
}, },
components: { components: {
'collapsed-avatar': CollapsedAvatar, 'collapsed-avatar': CollapsedAvatar,
}, },
template: ` template: `
<div class="sidebar-collapsed-icon sidebar-collapsed-user multiple-users" <div>
data-container="body" <div v-if="hasNoAssignees" class="sidebar-collapsed-icon sidebar-collapsed-user">
data-placement="left" <i aria-hidden="true" class="fa fa-user"></i>
data-toggle="tooltip" </div>
:data-original-title="title" > <div v-else class="sidebar-collapsed-icon sidebar-collapsed-user"
<collapsed-avatar :user="users[0]" /> :class="{'multiple-users': moreThanOneAssignees}"
<button class="btn-link" type="button"> data-container="body"
<span class="avatar-counter sidebar-avatar-counter">{{counter}}</span> data-placement="left"
</button> data-toggle="tooltip"
:data-original-title="title" >
<collapsed-avatar :user="users[0]" />
<collapsed-avatar :user="users[1]" v-if="hasTwoAssignees" />
<button class="btn-link" type="button" v-if="morethanTwoAssignees">
<span class="avatar-counter sidebar-avatar-counter">{{counter}}</span>
</button>
</div>
</div> </div>
`, `,
}; };
export default {
name: 'CollapsedNoAssignee',
props: {
users: { type: Array, required: true },
},
template: `
<div class="sidebar-collapsed-icon sidebar-collapsed-user">
<i aria-hidden="true" class="fa fa-user"></i>
</div>
`,
};
import CollapsedAvatar from './avatar';
export default {
name: 'CollapsedSingleAssignee',
props: {
users: { type: Array, required: true },
},
components: {
'collapsed-avatar': CollapsedAvatar,
},
template: `
<div class="sidebar-collapsed-icon sidebar-collapsed-user"
data-container="body"
data-placement="left"
data-toggle="tooltip"
:data-original-title="users[0].name" >
<collapsed-avatar :user="users[0]" />
</div>
`,
};
import CollapsedAvatar from './avatar';
export default {
name: 'CollapsedTwoAssignees',
props: {
users: { type: Array, required: true },
},
computed: {
title() {
return `${this.users[0].name}, ${this.users[1].name}`;
},
},
components: {
'collapsed-avatar': CollapsedAvatar,
},
template: `
<div class="sidebar-collapsed-icon sidebar-collapsed-user multiple-users"
data-container="body"
data-placement="left"
data-toggle="tooltip"
:data-original-title="title" >
<collapsed-avatar :user="users[0]" />
<collapsed-avatar :user="users[1]" />
</div>
`,
};
...@@ -3,7 +3,6 @@ export default { ...@@ -3,7 +3,6 @@ export default {
data() { data() {
return { return {
showLess: true, showLess: true,
defaultRenderCount: 5,
}; };
}, },
props: { props: {
...@@ -14,10 +13,10 @@ export default { ...@@ -14,10 +13,10 @@ export default {
return this.assignees.rootPath; return this.assignees.rootPath;
}, },
renderShowMoreSection() { renderShowMoreSection() {
return this.assignees.users.length > this.defaultRenderCount; return this.assignees.users.length > this.assignees.defaultRenderCount;
}, },
numberOfHiddenAssignees() { numberOfHiddenAssignees() {
return this.assignees.users.length - this.defaultRenderCount; return this.assignees.users.length - this.assignees.defaultRenderCount;
}, },
isHiddenAssignees() { isHiddenAssignees() {
return this.numberOfHiddenAssignees > 0; return this.numberOfHiddenAssignees > 0;
...@@ -28,7 +27,7 @@ export default { ...@@ -28,7 +27,7 @@ export default {
this.showLess = !this.showLess; this.showLess = !this.showLess;
}, },
renderAssignee(index) { renderAssignee(index) {
return !this.showLess || (index < this.defaultRenderCount && this.showLess); return !this.showLess || (index < this.assignees.defaultRenderCount && this.showLess);
}, },
assigneeUrl(username) { assigneeUrl(username) {
return `${this.rootPath}${username}`; return `${this.rootPath}${username}`;
......
...@@ -5,10 +5,7 @@ import NoAssignee from './components/expanded/no_assignee'; ...@@ -5,10 +5,7 @@ import NoAssignee from './components/expanded/no_assignee';
import SingleAssignee from './components/expanded/single_assignee'; import SingleAssignee from './components/expanded/single_assignee';
import MultipleAssignees from './components/expanded/multiple_assignees'; import MultipleAssignees from './components/expanded/multiple_assignees';
import CollapsedNoAssignee from './components/collapsed/no_assignee'; import CollapsedAssignees from './components/collapsed/assignees';
import CollapsedSingleAssignee from './components/collapsed/single_assignee';
import CollapsedTwoAssignees from './components/collapsed/two_assignees';
import CollapsedMultipleAssignees from './components/collapsed/multiple_assignees';
import SidebarAssigneesService from './services/sidebar_assignees_service'; import SidebarAssigneesService from './services/sidebar_assignees_service';
import SidebarAssigneesStore from './stores/sidebar_assignees_store'; import SidebarAssigneesStore from './stores/sidebar_assignees_store';
...@@ -19,6 +16,8 @@ const sidebarAssigneesOptions = () => ({ ...@@ -19,6 +16,8 @@ const sidebarAssigneesOptions = () => ({
data() { data() {
const selector = this.$options.el; const selector = this.$options.el;
const element = document.querySelector(selector); const element = document.querySelector(selector);
// Get data from data attributes passed from haml
const rootPath = element.dataset.rootPath; const rootPath = element.dataset.rootPath;
const path = element.dataset.path; const path = element.dataset.path;
const field = element.dataset.field; const field = element.dataset.field;
...@@ -49,34 +48,27 @@ const sidebarAssigneesOptions = () => ({ ...@@ -49,34 +48,27 @@ const sidebarAssigneesOptions = () => ({
hideComponent() { hideComponent() {
return !this.assignees.saved; return !this.assignees.saved;
}, },
collapsedComponentName() {
switch (this.numberOfAssignees) {
case 0:
return 'collapsed-no-assignee';
case 1:
return 'collapsed-single-assignee';
case 2:
return 'collapsed-two-assignees';
default:
return 'collapsed-multiple-assignees';
}
},
}, },
components: { components: {
'no-assignee': NoAssignee, 'no-assignee': NoAssignee,
'single-assignee': SingleAssignee, 'single-assignee': SingleAssignee,
'multiple-assignees': MultipleAssignees, 'multiple-assignees': MultipleAssignees,
'assignee-title': AssigneeTitle, 'assignee-title': AssigneeTitle,
'collapsed-single-assignee': CollapsedSingleAssignee, 'collapsed-assignees': CollapsedAssignees,
'collapsed-no-assignee': CollapsedNoAssignee,
'collapsed-two-assignees': CollapsedTwoAssignees,
'collapsed-multiple-assignees': CollapsedMultipleAssignees,
}, },
template: ` template: `
<div> <div>
<component :is="collapsedComponentName" :users="assignees.users" /> <collapsed-assignees :users="assignees.users"/>
<assignee-title :numberOfAssignees="assignees.users.length" :loading="assignees.loading" :editable="assignees.editable"/> <assignee-title
<component class="value" :is="componentName" :assignees="assignees" :class="{ hidden: hideComponent }" /> :numberOfAssignees="assignees.users.length"
:loading="assignees.loading"
:editable="assignees.editable"
/>
<component v-if="assignees.saved"
class="value"
:is="componentName"
:assignees="assignees"
/>
</div> </div>
`, `,
}); });
......
...@@ -10,6 +10,7 @@ export default class SidebarAssigneesStore { ...@@ -10,6 +10,7 @@ export default class SidebarAssigneesStore {
this.saved = true; this.saved = true;
this.loading = false; this.loading = false;
this.editable = editable; this.editable = editable;
this.defaultRenderCount = 5;
} }
addUser(user, saved = false) { addUser(user, saved = false) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment