]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/acq/Picklist.js
Merging acq-experiment to trunk, since rel_1_4 has been branched.
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / acq / Picklist.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * David J. Fiander <david@fiander.info>
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 if(!dojo._hasResource['openils.acq.Picklist']) {
18 dojo._hasResource['openils.acq.Picklist'] = true;
19 dojo.provide('openils.acq.Picklist');
20
21 dojo.require('dojo.data.ItemFileWriteStore');
22 dojo.require('dojox.grid.Grid');
23 dojo.require('dojox.grid._data.model');
24 dojo.require('fieldmapper.Fieldmapper');
25 dojo.require('fieldmapper.dojoData');
26 dojo.require('openils.Event');
27
28 /** Declare the Picklist class with dojo */
29 dojo.declare('openils.acq.Picklist', null, {
30
31     constructor: function (pl_id, onComplete, args) {
32
33         var pl_this = this;             // 'this' doesn't exist inside callbacks
34         var liArgs = (args && args.liArgs) ? args.liArgs : {flesh_attrs:1, clear_marc:1};
35         var mkStore = function (r) {
36             var storeData;
37             var msg;
38             pl_this._items = [];
39
40             while (msg = r.recv()) {
41                 var data = msg.content();
42                 pl_this._data[data.id()] = data;
43                 pl_this._items.push(data);
44             }
45
46             storeData = jub.toStoreData(pl_this._items, null, {virtualFields:['estimated_price', 'actual_price']});
47             pl_this._store = new dojo.data.ItemFileWriteStore({data:storeData});
48             pl_this._model = new dojox.grid.data.DojoData(null, pl_this._store,
49                                    {rowsPerPage:20, clientSort:true,
50                                 query:{id:'*'}});
51             onComplete(pl_this._model);
52         };
53
54         this._id = pl_id;
55         this._data = {};
56         this._plist = null;
57         //
58         // Fetch the picklist information
59         fieldmapper.standardRequest(
60             ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
61             {   async: false,
62                 params: [openils.User.authtoken, pl_id, {flesh_lineitem_count:1}],
63                 oncomplete: function(r) {
64                     var pl = r.recv().content(); 
65                     if(e = openils.Event.parse(pl))
66                         return alert(pl);
67                     pl_this._plist = pl;
68                 }
69             });
70
71         // Fetch the title list for the picklist, asynchronously
72         fieldmapper.standardRequest(
73             ['open-ils.acq', 'open-ils.acq.lineitem.picklist.retrieve'],
74             { async: true,
75               params: [openils.User.authtoken, pl_id, liArgs],
76               oncomplete: mkStore
77         });
78     },
79
80     id: function () {
81         return this._id;
82     },
83     name: function() {
84         return this._plist.name();
85     },
86     owner: function() {
87         return this._plist.owner();
88     },
89     create_time: function() {
90         return this._plist.create_time();
91     },
92     edit_time: function() {
93         return this._plist.edit_time();
94     },
95
96     find_attr: function(id, at_name, at_type) {
97         attr_list = this._data[id].attributes();
98         for (var i in attr_list) {
99             var attr = attr_list[i];
100             if (attr.attr_type() == at_type && attr.attr_name() == at_name) {
101             return attr.attr_value();
102             }
103         }
104         return '';
105     },
106 });
107
108 /** Creates a new picklist. fields.name is required */ 
109 openils.acq.Picklist.create = function(fields, oncomplete) {
110     var picklist = new acqpl();
111     picklist.owner(fields.owner || new openils.User().user.id());
112     picklist.name(fields.name);
113
114     fieldmapper.standardRequest(
115         ['open-ils.acq', 'open-ils.acq.picklist.create'],
116         {   async: true,
117             params: [openils.User.authtoken, picklist],
118             oncomplete: function(r) { 
119                 // XXX event/error handling
120                 oncomplete(r.recv().content());
121             }
122         }
123     );
124 }
125
126 /** Creates a new picklist. fields.name is required */ 
127 openils.acq.Picklist.update = function(picklist, oncomplete) {
128     fieldmapper.standardRequest(
129         ['open-ils.acq', 'open-ils.acq.picklist.update'],
130         {   async: true,
131             params: [openils.User.authtoken, picklist],
132             oncomplete: function(r) { 
133                 // XXX event/error handling
134                 oncomplete(r.recv().content());
135             }
136         }
137     );
138 }
139
140 /** Deletes a list of picklists
141  * @param list Array of picklist IDs
142  */
143 openils.acq.Picklist.deleteList = function(list, onComplete) {
144     openils.acq.Picklist._deleteList(list, 0, onComplete);
145 }
146
147 /* iterate through the list of IDs deleting asynchronously as we go... */
148 openils.acq.Picklist._deleteList = function(list, idx, onComplete) {
149     if(idx >= list.length)
150         return onComplete();
151     fieldmapper.standardRequest(
152         ['open-ils.acq', 'open-ils.acq.picklist.delete'],
153         {   async: true,
154             params: [openils.User.authtoken, list[idx]],
155             oncomplete: function(r) {
156                 msg = r.recv()
157                 stat = msg.content();
158                 /* XXX CHECH FOR EVENT */
159                 openils.acq.Picklist._deleteList(list, ++idx, onComplete);
160             }
161         }
162     );
163 }
164
165 }
166