]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/services/record.js
lp1739292 merge UI displays record summary
[working/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             mode: '<'
144         },
145         templateUrl : function(element, attrs) {
146             if (attrs.mode == "slim") {
147                 return  './cat/share/t_record_summary_slim';
148             }
149             return './cat/share/t_record_summary';
150         },
151         controller : 
152                    ['$scope','egCore','$sce','egBibDisplay',
153             function($scope , egCore , $sce , egBibDisplay) {
154
155                 function loadRecord() {
156                     egCore.pcrud.retrieve('bre', $scope.recordId, {
157                         flesh : 1,
158                         flesh_fields : {
159                             bre : ['creator','editor','flat_display_entries']
160                         }
161                     }).then(function(rec) {
162                         rec.owner(egCore.org.get(rec.owner()));
163                         $scope.record = rec;
164                         $scope.rec_display = 
165                             egBibDisplay.mfdeToHash(rec.flat_display_entries());
166                     });
167                     $scope.bib_cn = null;
168                     $scope.bib_cn_tooltip = '';
169                     var label_class = 1;
170                     egCore.org.settings(['cat.default_classification_scheme'])
171                     .then(function(s) {
172                         var scheme = s['cat.default_classification_scheme'];
173                         label_class = scheme || 1;
174
175                         return egCore.net.request(
176                             'open-ils.cat',
177                             'open-ils.cat.biblio.record.marc_cn.retrieve',
178                             $scope.recordId,
179                             label_class
180                         )
181                     }).then(function(cn_array) {
182                         var tooltip = '';
183                         if (cn_array.length > 0) {
184                             for (var field in cn_array[0]) {
185                                 $scope.bib_cn = cn_array[0][field];
186                             }
187                             for (var i in cn_array) {
188                                 for (var field in cn_array[i]) {
189                                     tooltip += 
190                                         field + ' : ' + cn_array[i][field] + '<br>';
191                                 }
192                             }
193                             $scope.bib_cn_tooltip = $sce.trustAsHtml(tooltip);
194                         }
195                     });
196                 }
197
198                 $scope.$watch('recordId', 
199                     function(newVal, oldVal) {
200                         if (newVal && newVal !== oldVal) {
201                             loadRecord();
202                         }
203                     }
204                 );
205
206
207                 if ($scope.recordId) 
208                     loadRecord();
209
210                 $scope.toggle_expand_summary = function() {
211                     if ($scope.collapseRecordSummary) {
212                         $scope.collapseRecordSummary = false;
213                         egCore.hatch.removeItem('eg.cat.record.summary.collapse');
214                     } else {
215                         $scope.collapseRecordSummary = true;
216                         egCore.hatch.setItem('eg.cat.record.summary.collapse', true);
217                     }
218                 }
219             
220                 $scope.collapse_summary = function() {
221                     return $scope.collapseRecordSummary;
222                 }
223             
224                 egCore.hatch.getItem('eg.cat.record.summary.collapse')
225                 .then(function(val) {$scope.collapseRecordSummary = Boolean(val)});
226
227             }
228         ]
229     }
230 })
231
232 /**
233  * Utility functions for translating bib record display fields into
234  * various formats / structures.
235  *
236  * Note that 'mwde' objects (which are proper IDL objects) only contain
237  * the prescribed fields from the IDL (and database view), while the
238  * 'mfde' hash-based objects contain all configured display fields,
239  * including custom fields.
240  * 
241  * MWDE objects are best suited to cases where the available set of
242  * display fields must be auto-generated from the IDL.  They work well
243  * with egGrids because it can automatically determine from the IDL
244  * which fields should be added to the column picker.
245  *
246  * MFDE lists are well suited to cases where the set of fields to
247  * display is known in advance (e.g. hard-coded in the template) or when
248  * the caller needs data for custom fields.  FWIW, MFDE data is slightly
249  * leaner for retrieval in that it does not require the JSON round-trip
250  * for delivery.
251  *
252  * Example:
253  *
254  *  --
255  *  // MVR-style canned fields
256  *
257  *  $scope.record = copy.call_number().record();
258  *
259  *  // translate wide display entry values inline
260  *  egBibDisplay.mwdeJSONToJS($scope.record.wide_display_entry());
261  *
262  *  <div>Title:</div>
263  *  <div>{{record.wide_display_entry().title()}}</div>
264  *
265  *  ---
266  *  //  Display any field using known keys
267  *
268  *  $scope.all_display_fields = 
269  *      egBibDisplay.mfdeToHash(record.flat_display_entries());
270  *
271  *  <div>Title:</div>
272  *  <div>{{all_display_fields.title}}</div>
273  *
274  *  ---
275  *  // Display all fields dynamically, using confgured labels
276  *
277  *  $scope.all_display_fields_with_meta = 
278  *      egBibDisplay.mfdeToMetaHash(record.flat_display_entries());
279  *
280  *  <div ng-repeat="(key, content) in all_display_fields_with_meta">
281  *    <div>Field Label</div><div>{{content.label}}</div>
282  *    <div ng-if="content.multi == 't'">
283  *      <div ng-repeat="val in content.value">
284  *        <div>Field Value</div><div>{{val}}</div>
285  *      </div>
286  *    </div>
287  *    <div ng-if="content.multi == 'f'">
288  *      <div>Field Value</div><div>{{content.value}}</div>
289  *    </div>
290  *  </div>
291  *
292  */
293 .factory('egBibDisplay', ['$q', 'egCore', function($q, egCore) {
294     var service = {};
295
296     /**
297      * Converts JSON-encoded values within a mwde object to Javascript
298      * native strings, numbers, and arrays.
299      *
300      * @collapseMulti collapse multi=true array values down to a single 
301      * comma-separated string.  This is useful for quickly  building 
302      * displays (e.g. grids) without having to first munge the array 
303      * into a string.
304      */
305     service.mwdeJSONToJS = function(entry, collapseMulti) {
306         angular.forEach(egCore.idl.classes.mwde.fields, function(f) {
307             if (f.virtual) return;
308             var val = JSON.parse(entry[f.name]());
309             if (collapseMulti && angular.isArray(val))
310                 val = val.join(', ');
311             entry[f.name](val);
312         });
313     }
314
315     /**
316      * Converts a list of 'mfde' entry objects to a simple key=>value hash.
317      * Non-multi values are strings or numbers.
318      * Multi values are arrays of strings or numbers.
319      *
320      * @collapseMulti See egBibDisplay.mwdeJSONToJS()
321      */
322     service.mfdeToHash = function(entries, collapseMulti) {
323         var hash = service.mfdeToMetaHash(entries, collapseMulti);
324         angular.forEach(hash, 
325             function(sub_hash, name) { hash[name] = sub_hash.value });
326         return hash;
327     }
328
329     /**
330      * Converts a list of 'mfde' entry objects to a nested hash like so:
331      * {name => field_name, label => field_label, value => scalar_or_array}
332      * The scalar_or_array value is a string/number or an array of
333      * string/numbers
334      *
335      * @collapseMulti See egBibDisplay.mwdeJSONToJS()
336      */
337     service.mfdeToMetaHash = function(entries, collapseMulti) {
338         var hash = {};
339         angular.forEach(entries, function(entry) {
340
341             if (!hash[entry.name()]) {
342                 hash[entry.name()] = {
343                     name : entry.name(),
344                     label : entry.label(),
345                     multi : entry.multi() == 't',
346                     value : entry.multi() == 't' ? [] : null
347                 }
348             }
349
350             if (entry.multi() == 't') {
351                 if (collapseMulti) {
352                     if (angular.isArray(hash[entry.name()].value)) {
353                         // start a new collapsed string
354                         hash[entry.name()].value = entry.value();
355                     } else {
356                         // append to collapsed string in progress
357                         hash[entry.name()].value += ', ' + entry.value();
358                     }
359                 } else {
360                     hash[entry.name()].value.push(entry.value());
361                 }
362             } else {
363                 hash[entry.name()].value = entry.value();
364             }
365         });
366
367         return hash;
368     }
369
370     return service;
371 }])