]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/services/record.js
LP#1800178 Holdings View should also sort by part
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / services / record.js
1 /**
2  * Simple directive for rending the HTML view of a MARC record.
3  *
4  * <eg-record-html record-id="myRecordIdScopeVariable"></eg-record-id>
5  * OR
6  * <eg-record-html marc-xml="myMarcXmlVariable"></eg-record-html>
7  *
8  * The value of myRecordIdScopeVariable is watched internally and the 
9  * record is updated to match.
10  */
11 angular.module('egCoreMod')
12
13 .directive('egRecordHtml', function() {
14     return {
15         restrict : 'AE',
16         scope : {
17             recordId : '=',
18             marcXml  : '@',
19         },
20         link : function(scope, element, attrs) {
21             scope.element = angular.element(element);
22
23             // kill refs to destroyed DOM elements
24             element.bind("$destroy", function() {
25                 delete scope.element;
26             });
27         },
28         controller : 
29                    ['$scope','egCore',
30             function($scope , egCore) {
31
32                 function loadRecordHtml() {
33                     egCore.net.request(
34                         'open-ils.search',
35                         'open-ils.search.biblio.record.html',
36                         $scope.recordId,
37                         false,
38                         $scope.marcXml
39                     ).then(function(html) {
40                         if (!html) return;
41
42                         // Remove those pesky non-i8n labels / actions.
43                         // Note: for printing, use the browser print page
44                         // option.  The end result is the same.
45                         html = html.replace(
46                             /<button onclick="window.print(.*?)<\/button>/,'');
47                         html = html.replace(/<title>(.*?)<\/title>/,'');
48
49                         // remove reference to nonexistant CSS file
50                         html = html.replace(/<link(.*?)\/>/,'');
51
52                         $scope.element.html(html);
53                     });
54                 }
55
56                 $scope.$watch('recordId', 
57                     function(newVal, oldVal) {
58                         if (newVal && newVal !== oldVal) {
59                             loadRecordHtml();
60                         }
61                     }
62                 );
63                 $scope.$watch('marcXml', 
64                     function(newVal, oldVal) {
65                         if (newVal && newVal !== oldVal) {
66                             loadRecordHtml();
67                         }
68                     }
69                 );
70
71                 if ($scope.recordId || $scope.marcXml) 
72                     loadRecordHtml();
73             }
74         ]
75     }
76 })
77
78 .directive('egRecordBreaker', function() {
79     return {
80         restrict : 'AE',
81         template : '<pre>{{breaker}}</pre>',
82         scope : {
83             recordId : '=',
84             marcXml  : '=',
85         },
86         link : function(scope, element, attrs) {
87             scope.element = angular.element(element);
88
89             // kill refs to destroyed DOM elements
90             element.bind("$destroy", function() {
91                 delete scope.element;
92             });
93         },
94         controller : 
95                    ['$scope','egCore',
96             function($scope , egCore) {
97
98                 function loadRecordBreaker() {
99                     var xml;
100                     if ($scope.marcXml) {
101                         $scope.breaker = new MARC21.Record({ marcxml : $scope.marcXml }).toBreaker();
102                     } else {
103                         egCore.pcrud.retrieve('bre', $scope.recordId)
104                         .then(function(rec) {
105                             $scope.breaker = new MARC21.Record({ marcxml : rec.marc() }).toBreaker();
106                         });
107                     }
108                 }
109
110                 $scope.$watch('recordId', 
111                     function(newVal, oldVal) {
112                         if (newVal && newVal !== oldVal) {
113                             loadRecordBreaker();
114                         }
115                     }
116                 );
117                 $scope.$watch('marcXml', 
118                     function(newVal, oldVal) {
119                         if (newVal && newVal !== oldVal) {
120                             loadRecordBreaker();
121                         }
122                     }
123                 );
124
125                 if ($scope.recordId || $scope.marcXml) 
126                     loadRecordBreaker();
127             }
128         ]
129     }
130 })
131
132 /*
133  * A record='foo' attribute is required as a storage location of the 
134  * retrieved record
135  */
136 .directive('egRecordSummary', function() {
137     return {
138         restrict : 'AE',
139         scope : {
140             recordId : '=',
141             record : '=',
142             noMarcLink : '@'
143         },
144         templateUrl : './cat/share/t_record_summary',
145         controller : 
146                    ['$scope','egCore','$sce','egBibDisplay',
147             function($scope , egCore , $sce , egBibDisplay) {
148
149                 function loadRecord() {
150                     egCore.pcrud.retrieve('bre', $scope.recordId, {
151                         flesh : 1,
152                         flesh_fields : {
153                             bre : ['creator','editor','flat_display_entries']
154                         }
155                     }).then(function(rec) {
156                         rec.owner(egCore.org.get(rec.owner()));
157                         $scope.record = rec;
158                         $scope.rec_display = 
159                             egBibDisplay.mfdeToHash(rec.flat_display_entries());
160                     });
161                     $scope.bib_cn = null;
162                     $scope.bib_cn_tooltip = '';
163                     var label_class = 1;
164                     egCore.org.settings(['cat.default_classification_scheme'])
165                     .then(function(s) {
166                         var scheme = s['cat.default_classification_scheme'];
167                         label_class = scheme || 1;
168
169                         return egCore.net.request(
170                             'open-ils.cat',
171                             'open-ils.cat.biblio.record.marc_cn.retrieve',
172                             $scope.recordId,
173                             label_class
174                         )
175                     }).then(function(cn_array) {
176                         var tooltip = '';
177                         if (cn_array.length > 0) {
178                             for (var field in cn_array[0]) {
179                                 $scope.bib_cn = cn_array[0][field];
180                             }
181                             for (var i in cn_array) {
182                                 for (var field in cn_array[i]) {
183                                     tooltip += 
184                                         field + ' : ' + cn_array[i][field] + '<br>';
185                                 }
186                             }
187                             $scope.bib_cn_tooltip = $sce.trustAsHtml(tooltip);
188                         }
189                     });
190                 }
191
192                 $scope.$watch('recordId', 
193                     function(newVal, oldVal) {
194                         if (newVal && newVal !== oldVal) {
195                             loadRecord();
196                         }
197                     }
198                 );
199
200
201                 if ($scope.recordId) 
202                     loadRecord();
203
204                 $scope.toggle_expand_summary = function() {
205                     if ($scope.collapseRecordSummary) {
206                         $scope.collapseRecordSummary = false;
207                         egCore.hatch.removeItem('eg.cat.record.summary.collapse');
208                     } else {
209                         $scope.collapseRecordSummary = true;
210                         egCore.hatch.setItem('eg.cat.record.summary.collapse', true);
211                     }
212                 }
213             
214                 $scope.collapse_summary = function() {
215                     return $scope.collapseRecordSummary;
216                 }
217             
218                 egCore.hatch.getItem('eg.cat.record.summary.collapse')
219                 .then(function(val) {$scope.collapseRecordSummary = Boolean(val)});
220
221             }
222         ]
223     }
224 })
225
226 /**
227  * Utility functions for translating bib record display fields into
228  * various formats / structures.
229  *
230  * Note that 'mwde' objects (which are proper IDL objects) only contain
231  * the prescribed fields from the IDL (and database view), while the
232  * 'mfde' hash-based objects contain all configured display fields,
233  * including custom fields.
234  * 
235  * MWDE objects are best suited to cases where the available set of
236  * display fields must be auto-generated from the IDL.  They work well
237  * with egGrids because it can automatically determine from the IDL
238  * which fields should be added to the column picker.
239  *
240  * MFDE lists are well suited to cases where the set of fields to
241  * display is known in advance (e.g. hard-coded in the template) or when
242  * the caller needs data for custom fields.  FWIW, MFDE data is slightly
243  * leaner for retrieval in that it does not require the JSON round-trip
244  * for delivery.
245  *
246  * Example:
247  *
248  *  --
249  *  // MVR-style canned fields
250  *
251  *  $scope.record = copy.call_number().record();
252  *
253  *  // translate wide display entry values inline
254  *  egBibDisplay.mwdeJSONToJS($scope.record.wide_display_entry());
255  *
256  *  <div>Title:</div>
257  *  <div>{{record.wide_display_entry().title()}}</div>
258  *
259  *  ---
260  *  //  Display any field using known keys
261  *
262  *  $scope.all_display_fields = 
263  *      egBibDisplay.mfdeToHash(record.flat_display_entries());
264  *
265  *  <div>Title:</div>
266  *  <div>{{all_display_fields.title}}</div>
267  *
268  *  ---
269  *  // Display all fields dynamically, using confgured labels
270  *
271  *  $scope.all_display_fields_with_meta = 
272  *      egBibDisplay.mfdeToMetaHash(record.flat_display_entries());
273  *
274  *  <div ng-repeat="(key, content) in all_display_fields_with_meta">
275  *    <div>Field Label</div><div>{{content.label}}</div>
276  *    <div ng-if="content.multi == 't'">
277  *      <div ng-repeat="val in content.value">
278  *        <div>Field Value</div><div>{{val}}</div>
279  *      </div>
280  *    </div>
281  *    <div ng-if="content.multi == 'f'">
282  *      <div>Field Value</div><div>{{content.value}}</div>
283  *    </div>
284  *  </div>
285  *
286  */
287 .factory('egBibDisplay', ['$q', 'egCore', function($q, egCore) {
288     var service = {};
289
290     /**
291      * Converts JSON-encoded values within a mwde object to Javascript
292      * native strings, numbers, and arrays.
293      *
294      * @collapseMulti collapse multi=true array values down to a single 
295      * comma-separated string.  This is useful for quickly  building 
296      * displays (e.g. grids) without having to first munge the array 
297      * into a string.
298      */
299     service.mwdeJSONToJS = function(entry, collapseMulti) {
300         angular.forEach(egCore.idl.classes.mwde.fields, function(f) {
301             if (f.virtual) return;
302             var val = JSON.parse(entry[f.name]());
303             if (collapseMulti && angular.isArray(val))
304                 val = val.join(', ');
305             entry[f.name](val);
306         });
307     }
308
309     /**
310      * Converts a list of 'mfde' entry objects to a simple key=>value hash.
311      * Non-multi values are strings or numbers.
312      * Multi values are arrays of strings or numbers.
313      *
314      * @collapseMulti See egBibDisplay.mwdeJSONToJS()
315      */
316     service.mfdeToHash = function(entries, collapseMulti) {
317         var hash = service.mfdeToMetaHash(entries, collapseMulti);
318         angular.forEach(hash, 
319             function(sub_hash, name) { hash[name] = sub_hash.value });
320         return hash;
321     }
322
323     /**
324      * Converts a list of 'mfde' entry objects to a nested hash like so:
325      * {name => field_name, label => field_label, value => scalar_or_array}
326      * The scalar_or_array value is a string/number or an array of
327      * string/numbers
328      *
329      * @collapseMulti See egBibDisplay.mwdeJSONToJS()
330      */
331     service.mfdeToMetaHash = function(entries, collapseMulti) {
332         var hash = {};
333         angular.forEach(entries, function(entry) {
334
335             if (!hash[entry.name()]) {
336                 hash[entry.name()] = {
337                     name : entry.name(),
338                     label : entry.label(),
339                     multi : entry.multi() == 't',
340                     value : entry.multi() == 't' ? [] : null
341                 }
342             }
343
344             if (entry.multi() == 't') {
345                 if (collapseMulti) {
346                     if (angular.isArray(hash[entry.name()].value)) {
347                         // start a new collapsed string
348                         hash[entry.name()].value = entry.value();
349                     } else {
350                         // append to collapsed string in progress
351                         hash[entry.name()].value += ', ' + entry.value();
352                     }
353                 } else {
354                     hash[entry.name()].value.push(entry.value());
355                 }
356             } else {
357                 hash[entry.name()].value = entry.value();
358             }
359         });
360
361         return hash;
362     }
363
364     return service;
365 }])