]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/admin/server/config/metabib_field.js
LP#1744385: Search and Result Display improvements
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / admin / server / config / metabib_field.js
1 angular.module('egAdminConfig',
2     ['ngRoute','ui.bootstrap','egCoreMod','egUiMod','egGridMod','egFmRecordEditorMod'])
3
4 .controller('MetabibField',
5        ['$scope','$q','$timeout','$location','$window','$uibModal','egCore','egGridDataProvider',
6         'egConfirmDialog',
7 function($scope , $q , $timeout , $location , $window , $uibModal , egCore , egGridDataProvider ,
8          egConfirmDialog) {
9
10     egCore.startup.go(); // standalone mode requires manual startup
11
12     $scope.search_class = '';
13     $scope.$watch('search_class', function(newVal, oldVal) {
14         if (newVal != oldVal) {
15             $scope.gridControls.setQuery(generateQuery($scope.search_class));
16             $scope.gridControls.refresh();
17         }
18     });
19
20     $scope.new_record = function() {
21         spawn_editor();
22     }
23
24     $scope.edit_record = function(items) {
25         if (items.length != 1) return;
26         spawn_editor(items[0].id);
27     }
28
29     spawn_editor = function(id) {
30         var templ;
31         if (arguments.length == 1) {
32             templ = '<eg-edit-fm-record idl-class="cmf" mode="update" record-id="id" on-save="ok" on-cancel="cancel"></eg-edit-fm-record>';
33         } else {
34             templ = '<eg-edit-fm-record idl-class="cmf" mode="create" on-save="ok" on-cancel="cancel"></eg-edit-fm-record>';
35         }
36         gridControls = $scope.gridControls;
37         $uibModal.open({
38             template : templ,
39             backdrop: 'static',
40             controller : [
41                         '$scope', '$uibModalInstance',
42                 function($scope ,  $uibModalInstance) {
43                     $scope.id = id;
44
45                     $scope.ok = function($event) {
46                         $uibModalInstance.close();
47                         gridControls.refresh();
48                     }
49     
50                     $scope.cancel = function($event) {
51                         $uibModalInstance.dismiss();
52                     }
53                 }
54             ]
55         });
56     }
57
58     $scope.delete_record = function(selected) {
59         if (!selected || !selected.length) return;
60
61         egCore.pcrud.retrieve('cmf', selected[0].id).then(function(rec) {
62             egConfirmDialog.open(
63                 egCore.strings.EG_CONFIRM_DELETE_RECORD_TITLE,
64                 egCore.strings.EG_CONFIRM_DELETE_RECORD_BODY,
65                 { id : rec.id() } // TODO replace with selector if available?
66             ).result.then(function() {
67                 egCore.pcrud.remove(rec).then(function() {
68                     $scope.gridControls.refresh();
69                 });
70             });
71         });
72     }
73
74     function generateQuery(search_class) {
75         var q = { 'id' : { '!=' : null } };
76
77         if (search_class) {
78             q.field_class = search_class;
79         }
80
81         return q;
82     }
83
84     $scope.gridControls = {
85         activateItem : function (i) { return $scope.edit_record([i]) },
86         setQuery : function() {
87             return generateQuery($scope.search_class);
88         },
89         setSort : function() {
90             return ['label'];
91         }
92     }
93 }])