Bug 32468: Use fetchLocalTitleCount to know if local titles exist
[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-if="title_count" id="titles_list">
5             <Toolbar />
6             <div v-if="title_count" id="title_list_result" class="page-section">
7                 <table v-if="title_count" :id="table_id"></table>
8             </div>
9             <div v-else-if="initialized" class="dialog message">
10                 {{ $__("There are no titles defined") }}
11             </div>
12         </div>
13     </div>
14 </template>
15
16 <script>
17 import Toolbar from "./EHoldingsLocalTitlesToolbar.vue"
18 import { inject, createVNode, render } from "vue"
19 import { storeToRefs } from "pinia"
20 import { fetchLocalTitleCount } from "../../fetch"
21 import { useDataTable } from "../../composables/datatables"
22
23 export default {
24     setup() {
25         const AVStore = inject("AVStore")
26         const { av_title_publication_types } = storeToRefs(AVStore)
27         const { get_lib_from_av, map_av_dt_filter } = AVStore
28
29         const table_id = "title_list"
30         useDataTable(table_id)
31
32         return {
33             av_title_publication_types,
34             get_lib_from_av,
35             map_av_dt_filter,
36             table_id,
37         }
38     },
39     data: function () {
40         return {
41             title_count: undefined,
42             initialized: false,
43             filters: {
44                 publication_title: this.$route.query.publication_title || "",
45                 publication_type: this.$route.query.publication_type || "",
46             },
47             cannot_search: false,
48         }
49     },
50     beforeRouteEnter(to, from, next) {
51         next(vm => {
52             vm.getTitleCount().then(() => vm.build_datatable())
53         })
54     },
55     methods: {
56         async getTitleCount() {
57             this.title_count = await fetchLocalTitleCount()
58             this.initialized = true
59         },
60         show_title: function (title_id) {
61             this.$router.push(
62                 "/cgi-bin/koha/erm/eholdings/local/titles/" + title_id
63             )
64         },
65         edit_title: function (title_id) {
66             this.$router.push(
67                 "/cgi-bin/koha/erm/eholdings/local/titles/edit/" + title_id
68             )
69         },
70         delete_title: function (title_id) {
71             this.$router.push(
72                 "/cgi-bin/koha/erm/eholdings/local/titles/delete/" + title_id
73             )
74         },
75         build_datatable: function () {
76             let show_title = this.show_title
77             let edit_title = this.edit_title
78             let delete_title = this.delete_title
79             let get_lib_from_av = this.get_lib_from_av
80             let map_av_dt_filter = this.map_av_dt_filter
81             let filters = this.filters
82             let table_id = this.table_id
83
84             window["av_title_publication_types"] = map_av_dt_filter(
85                 "av_title_publication_types"
86             )
87
88             $("#" + table_id).kohaTable(
89                 {
90                     ajax: {
91                         url: "/api/v1/erm/eholdings/local/titles",
92                     },
93                     embed: ["resources.package"],
94                     order: [[0, "asc"]],
95                     autoWidth: false,
96                     searchCols: [
97                         { search: filters.publication_title },
98                         null,
99                         { search: filters.publication_type },
100                         null,
101                     ],
102                     columns: [
103                         {
104                             title: __("Title"),
105                             data: "me.publication_title",
106                             searchable: true,
107                             orderable: true,
108                             render: function (data, type, row, meta) {
109                                 // Rendering done in drawCallback
110                                 return ""
111                             },
112                         },
113                         {
114                             title: __("Contributors"),
115                             data: "first_author:first_editor",
116                             searchable: true,
117                             orderable: true,
118                             render: function (data, type, row, meta) {
119                                 return (
120                                     escape_str(row.first_author) +
121                                     (row.first_author && row.first_editor
122                                         ? "<br/>"
123                                         : "") +
124                                     escape_str(row.first_editor)
125                                 )
126                             },
127                         },
128                         {
129                             title: __("Publication type"),
130                             data: "publication_type",
131                             searchable: true,
132                             orderable: true,
133                             render: function (data, type, row, meta) {
134                                 return escape_str(
135                                     get_lib_from_av(
136                                         "av_title_publication_types",
137                                         row.publication_type
138                                     )
139                                 )
140                             },
141                         },
142                         {
143                             title: __("Identifier"),
144                             data: "print_identifier:online_identifier",
145                             searchable: true,
146                             orderable: true,
147                             render: function (data, type, row, meta) {
148                                 let print_identifier = row.print_identifier
149                                 let online_identifier = row.online_identifier
150                                 return (
151                                     (print_identifier
152                                         ? escape_str(
153                                               _("ISBN (Print): %s").format(
154                                                   print_identifier
155                                               )
156                                           )
157                                         : "") +
158                                     (online_identifier
159                                         ? escape_str(
160                                               _("ISBN (Online): %s").format(
161                                                   online_identifier
162                                               )
163                                           )
164                                         : "")
165                                 )
166                             },
167                         },
168                         {
169                             title: __("Actions"),
170                             data: function (row, type, val, meta) {
171                                 return '<div class="actions"></div>'
172                             },
173                             className: "actions noExport",
174                             searchable: false,
175                             orderable: false,
176                         },
177                     ],
178                     drawCallback: function (settings) {
179                         var api = new $.fn.dataTable.Api(settings)
180
181                         $.each(
182                             $(this).find("td .actions"),
183                             function (index, e) {
184                                 let tr = $(this).parent().parent()
185                                 let title_id = api.row(tr).data().title_id
186                                 let editButton = createVNode(
187                                     "a",
188                                     {
189                                         class: "btn btn-default btn-xs",
190                                         role: "button",
191                                         onClick: () => {
192                                             edit_title(title_id)
193                                         },
194                                     },
195                                     [
196                                         createVNode("i", {
197                                             class: "fa fa-pencil",
198                                             "aria-hidden": "true",
199                                         }),
200                                         __("Edit"),
201                                     ]
202                                 )
203
204                                 let deleteButton = createVNode(
205                                     "a",
206                                     {
207                                         class: "btn btn-default btn-xs",
208                                         role: "button",
209                                         onClick: () => {
210                                             delete_title(title_id)
211                                         },
212                                     },
213                                     [
214                                         createVNode("i", {
215                                             class: "fa fa-trash",
216                                             "aria-hidden": "true",
217                                         }),
218                                         __("Delete"),
219                                     ]
220                                 )
221
222                                 let n = createVNode("span", {}, [
223                                     editButton,
224                                     " ",
225                                     deleteButton,
226                                 ])
227                                 render(n, e)
228                             }
229                         )
230
231                         $.each(
232                             $(this).find("tbody tr td:first-child"),
233                             function (index, e) {
234                                 let tr = $(this).parent()
235                                 let row = api.row(tr).data()
236                                 if (!row) return // Happen if the table is empty
237                                 let n = createVNode(
238                                     "a",
239                                     {
240                                         role: "button",
241                                         href:
242                                             "/cgi-bin/koha/erm/eholdings/local/titles/" +
243                                             row.title_id,
244                                         onClick: e => {
245                                             e.preventDefault()
246                                             show_title(row.title_id)
247                                         },
248                                     },
249                                     `${row.publication_title} (#${row.title_id})`
250                                 )
251                                 render(n, e)
252                             }
253                         )
254                     },
255                     preDrawCallback: function (settings) {
256                         $("#" + table_id)
257                             .find("thead th")
258                             .eq(2)
259                             .attr("data-filter", "av_title_publication_types")
260                     },
261                 },
262                 eholdings_titles_table_settings,
263                 1
264             )
265         },
266     },
267     components: { Toolbar },
268     name: "EHoldingsLocalTitlesList",
269 }
270 </script>