]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/biblio/monographPartMerge.js
LP 2061136 follow-up: ng lint --fix
[working/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                        g.ischanged(true);
75                     });
76
77            if (cpmList.length > 0) {
78                pcrud.apply( cpmList);
79            }
80  
81            /*
82             * Close the connection and commit the transaction.  This is necessary to do before
83             * the subsequent delete operation (because of ON DELETE CASCADE issues).
84             */
85
86            pcrud.disconnect(); 
87
88            /*
89             * Update the AutoGrid to delete the items being mapped out of existence so that 
90             * the display reflects the updated situation.
91             * Then use a PermaCrud connection to delete/eliminate the object.  This
92             * code is adapted from the delete case in AutoGrid.js. Note that this code
93             * uses a total==1 as the exit condition (because you are not deleting the 
94             * winning/prevailing/surviving part.
95             */
96
97            dojo.forEach (items, 
98                function(item) { 
99                   if (item.id != parseInt(obj.itemID)) {
100                      var fmObject = new fieldmapper[gridControl.fmClass]().fromStoreItem(item);
101                      new openils.PermaCrud()['eliminate'](
102                             fmObject, {
103                                 oncomplete : function(r) {
104                                     gridControl.store.deleteItem(item);
105                                     if (--total == 1 && gridControl.onPostSubmit) {
106                                         gridControl.onPostSubmit();
107                                     }
108                                 }
109                             }
110                       );
111                    }
112                 }
113            );
114
115         };
116
117         mergeDiv.innerHTML = "<div class=\"biblio-merge-prevail-title\" >" +
118                               dojo.i18n.getLocalization("openils.biblio","biblio_messages").CLICK_PREVAILING +
119                               "</div>";  
120         mergeDiv.items = items;
121
122         /* 
123          * Create a DIV for each selected item, and put in the container DIV
124          */
125         for (var i = 0; i < total; i++) {
126             var newDiv = dojo.create("div"); 
127             newDiv.className = "biblio-merge-item";
128             newDiv.itemID = items[i].id;
129             newDiv.onclick = function() {mergeDiv.processMerge(this);};
130             var newText = new String(items[i].label);
131             
132             /* To make spacing more visible, replace spaces with a middot character */
133             newText = newText.replace(/ /g, String.fromCharCode(183) /* middot*/);
134             newDiv.appendChild(document.createTextNode( newText ));  
135             mergeDiv.appendChild(newDiv);
136         }
137         mergePartsPopup.setContent(mergeDiv); 
138
139         mergePartsPopup.show();
140
141     };
142
143 }