Bug 34107: Switch order for id and name feeding the first column in datatables
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / vue / components / ERM / EHoldingsLocalTitlesList.vue
1 <template>
2     <div>
3         <div v-if="!initialized">{{ $__("Loading") }}</div>
4         <div v-else id="titles_list">
5             <Toolbar :options="this.toolbar_options" />
6             <div
7                 v-if="title_count > 0"
8                 id="title_list_result"
9                 class="page-section"
10             >
11                 <KohaTable
12                     ref="table"
13                     v-bind="tableOptions"
14                     @show="doShow"
15                     @edit="doEdit"
16                     @delete="doDelete"
17                 ></KohaTable>
18             </div>
19             <div v-else class="dialog message">
20                 {{ $__("There are no titles defined") }}
21             </div>
22         </div>
23     </div>
24 </template>
25
26 <script>
27 import Toolbar from "../Toolbar.vue"
28 import { inject, ref, reactive } from "vue"
29 import { storeToRefs } from "pinia"
30 import { APIClient } from "../../fetch/api-client.js"
31 import KohaTable from "../KohaTable.vue"
32
33 export default {
34     setup() {
35         const AVStore = inject("AVStore")
36         const { av_title_publication_types } = storeToRefs(AVStore)
37         const { get_lib_from_av, map_av_dt_filter } = AVStore
38
39         const { setConfirmationDialog, setMessage } = inject("mainStore")
40
41         const table = ref()
42         const filters = reactive({
43             publication_title: "",
44             publication_type: "",
45         })
46
47         return {
48             av_title_publication_types,
49             get_lib_from_av,
50             map_av_dt_filter,
51             escape_str,
52             table,
53             filters,
54             setConfirmationDialog,
55             setMessage,
56             eholdings_titles_table_settings,
57         }
58     },
59     data: function () {
60         this.filters = {
61             publication_title: this.$route.query.publication_title || "",
62             publication_type: this.$route.query.publication_type || "",
63         }
64         let filters = this.filters
65
66         return {
67             title_count: 0,
68             initialized: false,
69             tableOptions: {
70                 columns: this.getTableColumns(),
71                 url: "/api/v1/erm/eholdings/local/titles",
72                 options: {
73                     embed: "resources.package",
74                     searchCols: [
75                         { search: filters.publication_title },
76                         null,
77                         { search: filters.publication_type },
78                         null,
79                     ],
80                 },
81                 table_settings: this.eholdings_titles_table_settings,
82                 add_filters: true,
83                 filters_options: {
84                     2: () =>
85                         this.map_av_dt_filter("av_title_publication_types"),
86                 },
87                 actions: {
88                     0: ["show"],
89                     "-1": ["edit", "delete"],
90                 },
91             },
92             cannot_search: false,
93             toolbar_options: [
94                 {
95                     to: "EHoldingsLocalTitlesFormAdd",
96                     button_title: this.$__("New title"),
97                 },
98                 {
99                     to: "EHoldingsLocalTitlesFormImport",
100                     button_title: this.$__("Import from list"),
101                 },
102             ],
103         }
104     },
105     beforeRouteEnter(to, from, next) {
106         next(vm => {
107             vm.getTitleCount().then(() => (vm.initialized = true))
108         })
109     },
110     methods: {
111         async getTitleCount() {
112             const client = APIClient.erm
113             await client.localTitles.count().then(
114                 count => {
115                     this.title_count = count
116                 },
117                 error => {}
118             )
119         },
120         doShow: function ({ title_id }, dt, event) {
121             event.preventDefault()
122             this.$router.push({
123                 name: "EHoldingsLocalTitlesShow",
124                 params: { title_id },
125             })
126         },
127         doEdit: function ({ title_id }, dt, event) {
128             this.$router.push({
129                 name: "EHoldingsLocalTitlesFormAddEdit",
130                 params: { title_id },
131             })
132         },
133         doDelete: function (title, dt, event) {
134             this.setConfirmationDialog(
135                 {
136                     title: this.$__(
137                         "Are you sure you want to remove this title?"
138                     ),
139                     message: title.publication_title,
140                     accept_label: this.$__("Yes, delete"),
141                     cancel_label: this.$__("No, do not delete"),
142                 },
143                 () => {
144                     const client = APIClient.erm
145                     client.localTitles.delete(title.title_id).then(
146                         success => {
147                             this.setMessage(
148                                 this.$__("Local title %s deleted").format(
149                                     title.publication_title
150                                 ),
151                                 true
152                             )
153                             dt.draw()
154                         },
155                         error => {}
156                     )
157                 }
158             )
159         },
160         getTableColumns: function () {
161             let get_lib_from_av = this.get_lib_from_av
162             let escape_str = this.escape_str
163
164             return [
165                 {
166                     title: __("Title"),
167                     data: "me.publication_title:me.title_id",
168                     searchable: true,
169                     orderable: true,
170                     render: function (data, type, row, meta) {
171                         return (
172                             '<a href="/cgi-bin/koha/erm/eholdings/local/titles/' +
173                             row.title_id +
174                             '" class="show">' +
175                             escape_str(
176                                 `${row.publication_title} (#${row.title_id})`
177                             ) +
178                             "</a>"
179                         )
180                     },
181                 },
182                 {
183                     title: __("Contributors"),
184                     data: "first_author:first_editor",
185                     searchable: true,
186                     orderable: true,
187                     render: function (data, type, row, meta) {
188                         return (
189                             escape_str(row.first_author) +
190                             (row.first_author && row.first_editor
191                                 ? "<br/>"
192                                 : "") +
193                             escape_str(row.first_editor)
194                         )
195                     },
196                 },
197                 {
198                     title: __("Publication type"),
199                     data: "publication_type",
200                     searchable: true,
201                     orderable: true,
202                     render: function (data, type, row, meta) {
203                         return escape_str(
204                             get_lib_from_av(
205                                 "av_title_publication_types",
206                                 row.publication_type
207                             )
208                         )
209                     },
210                 },
211                 {
212                     title: __("Identifier"),
213                     data: "print_identifier:online_identifier",
214                     searchable: true,
215                     orderable: true,
216                     render: function (data, type, row, meta) {
217                         let print_identifier = row.print_identifier
218                         let online_identifier = row.online_identifier
219                         return (
220                             (print_identifier
221                                 ? escape_str(
222                                       _("ISBN (Print): %s").format(
223                                           print_identifier
224                                       )
225                                   )
226                                 : "") +
227                             (online_identifier
228                                 ? escape_str(
229                                       _("ISBN (Online): %s").format(
230                                           online_identifier
231                                       )
232                                   )
233                                 : "")
234                         )
235                     },
236                 },
237             ]
238         },
239     },
240     components: { Toolbar, KohaTable },
241     name: "EHoldingsLocalTitlesList",
242 }
243 </script>