]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/biblio/monographPartMerge.js
LP#1099979 Provide facility to merge parts in the Monograph Parts display
[Evergreen.git] / Open-ILS / web / js / dojo / openils / biblio / monographPartMerge.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2014  C/W MARS Inc.
3  * Dan Pearl <dpearl@cwmars.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17 /*
18  * Support for the facility to merge multiple part designations into
19  * one.
20  */
21
22 if(!dojo._hasResource["openils.biblio.monographPartMerge"]) {
23     dojo._hasResource["openils.biblio.monographPartMerge"] = true;
24     dojo.provide("openils.biblio.monographPartMerge");
25     dojo.declare("openils.biblio.monographPartMerge", null, {}); 
26
27     /*
28      * Generate a pop-up to control part merging 
29      */
30     openils.biblio.monographPartMerge.showMergeDialog = function(gridControl) {
31          dojo.requireLocalization("openils.biblio","biblio_messages");
32
33         var items = gridControl.getSelectedItems();
34         var total = items.length;
35
36         if (total < 2 ) // Validate number of selected items
37             {
38             alert(dojo.i18n.getLocalization("openils.biblio","biblio_messages").SELECT_TWO);
39             return;
40             }
41
42         var mergePartsPopup = new dijit.Dialog({title: 
43                 dojo.i18n.getLocalization("openils.biblio","biblio_messages").MERGE_PARTS_TITLE}); 
44
45         var mergeDiv = dojo.create("div");
46
47        /* 
48         * Establish handler when an item is clicked upon 
49         */ 
50         mergeDiv.processMerge = function (obj) {
51             mergePartsPopup.hide();
52             dojo.require('openils.PermaCrud');  
53
54             var searchParams = {};
55             searchParams["part"] = new Array() ; 
56
57             /*
58              * Establish a list of id's of monograph_parts that are affected by remap. Later, find
59              * all copy_part_map items that reference any of these parts 
60              */
61
62             dojo.forEach (this.items, 
63                function(item) { 
64                       searchParams["part"].push(String(item.id))        /* Must be String in json */
65                });
66             // var testString = searchParams["part"].join(', ');        /* DEBUG */
67
68             var pcrud = new openils.PermaCrud();
69             var cpmList = pcrud.search("acpm", searchParams);
70
71             dojo.forEach(cpmList,
72                     function (g) {
73                        g.part(parseInt(obj.itemID))             /* Assign "winner" DB id of mono_part. */
74                     });
75
76            if (cpmList.length > 0) {
77                pcrud.update( cpmList, {});
78            }
79  
80            /*
81             * Close the connection and commit the transaction.  This is necessary to do before
82             * the subsequent delete operation (because of ON DELETE CASCADE issues).
83             */
84
85            pcrud.disconnect(); 
86
87            /*
88             * Update the AutoGrid to delete the items being mapped out of existence so that 
89             * the display reflects the updated situation.
90             * Then use a PermaCrud connection to delete/eliminate the object.  This
91             * code is adapted from the delete case in AutoGrid.js. Note that this code
92             * uses a total==1 as the exit condition (because you are not deleting the 
93             * winning/prevailing/surviving part.
94             */
95
96            dojo.forEach (items, 
97                function(item) { 
98                   if (item.id != parseInt(obj.itemID)) {
99                      var fmObject = new fieldmapper[gridControl.fmClass]().fromStoreItem(item);
100                      new openils.PermaCrud()['eliminate'](
101                             fmObject, {
102                                 oncomplete : function(r) {
103                                     gridControl.store.deleteItem(item);
104                                     if (--total == 1 && gridControl.onPostSubmit) {
105                                         gridControl.onPostSubmit();
106                                     }
107                                 }
108                             }
109                       );
110                    }
111                 }
112            );
113
114         };
115
116         mergeDiv.innerHTML = "<div class=\"biblio-merge-prevail-title\" >" +
117                               dojo.i18n.getLocalization("openils.biblio","biblio_messages").CLICK_PREVAILING +
118                               "</div>";  
119         mergeDiv.items = items;
120
121         /* 
122          * Create a DIV for each selected item, and put in the container DIV
123          */
124         for (var i = 0; i < total; i++) {
125             var newDiv = dojo.create("div"); 
126             newDiv.className = "biblio-merge-item";
127             newDiv.itemID = items[i].id;
128             newDiv.onclick = function() {mergeDiv.processMerge(this);};
129             var newText = new String(items[i].label);
130             
131             /* To make spacing more visible, replace spaces with a middot character */
132             newText = newText.replace(/ /g, String.fromCharCode(183) /* middot*/);
133             newDiv.appendChild(document.createTextNode( newText ));  
134             mergeDiv.appendChild(newDiv);
135         }
136         mergePartsPopup.setContent(mergeDiv); 
137
138         mergePartsPopup.show();
139
140     };
141
142 }