853a046ed37d3ac4cb32d3c18b7e9a642b8024ed
[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',
147             function($scope , egCore , $sce) {
148
149                 function loadRecord() {
150                     egCore.pcrud.retrieve('bre', $scope.recordId, {
151                         flesh : 1,
152                         flesh_fields : {
153                             bre : ['creator','editor']
154                         }
155                     }).then(function(rec) {
156                         rec.owner(egCore.org.get(rec.owner()));
157                         $scope.record = rec;
158                     });
159                     egCore.net.request(
160                         'open-ils.search',
161                         'open-ils.search.biblio.record.mods_slim.retrieve.authoritative',
162                         $scope.recordId
163                     ).then(function(mvr) {
164                         $scope.mvr = mvr;
165                     });
166                     $scope.bib_cn = null;
167                     $scope.bib_cn_tooltip = '';
168                     var label_class = egCore.env.aous['cat.default_classification_scheme'] || 1;
169                     egCore.net.request(
170                         'open-ils.cat',
171                         'open-ils.cat.biblio.record.marc_cn.retrieve',
172                         $scope.recordId,
173                         label_class
174                     ).then(function(cn_array) {
175                         var tooltip = '';
176                         if (cn_array.length > 0) {
177                             for (var field in cn_array[0]) {
178                                 $scope.bib_cn = cn_array[0][field];
179                             }
180                             for (var i in cn_array) {
181                                 for (var field in cn_array[i]) {
182                                     tooltip += 
183                                         field + ' : ' + cn_array[i][field] + '<br>';
184                                 }
185                             }
186                             $scope.bib_cn_tooltip = $sce.trustAsHtml(tooltip);
187                         }
188                     });
189                 }
190
191                 $scope.$watch('recordId', 
192                     function(newVal, oldVal) {
193                         if (newVal && newVal !== oldVal) {
194                             loadRecord();
195                         }
196                     }
197                 );
198
199
200                 if ($scope.recordId) 
201                     loadRecord();
202
203                 $scope.toggle_expand_summary = function() {
204                     if ($scope.collapseRecordSummary) {
205                         $scope.collapseRecordSummary = false;
206                         egCore.hatch.removeItem('eg.cat.record.summary.collapse');
207                     } else {
208                         $scope.collapseRecordSummary = true;
209                         egCore.hatch.setItem('eg.cat.record.summary.collapse', true);
210                     }
211                 }
212             
213                 $scope.collapse_summary = function() {
214                     return $scope.collapseRecordSummary;
215                 }
216             
217                 egCore.hatch.getItem('eg.cat.record.summary.collapse')
218                 .then(function(val) {$scope.collapseRecordSummary = Boolean(val)});
219
220             }
221         ]
222     }
223 })