1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * David J. Fiander <david@fiander.info>
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.
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 * ---------------------------------------------------------------------------
17 if(!dojo._hasResource['openils.acq.Picklist']) {
18 dojo._hasResource['openils.acq.Picklist'] = true;
19 dojo.provide('openils.acq.Picklist');
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');
28 /** Declare the Picklist class with dojo */
29 dojo.declare('openils.acq.Picklist', null, {
31 constructor: function (pl_id, onComplete, args) {
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) {
40 while (msg = r.recv()) {
41 var data = msg.content();
42 pl_this._data[data.id()] = data;
43 pl_this._items.push(data);
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,
51 onComplete(pl_this._model);
58 // Fetch the picklist information
59 fieldmapper.standardRequest(
60 ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
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))
71 // Fetch the title list for the picklist, asynchronously
72 fieldmapper.standardRequest(
73 ['open-ils.acq', 'open-ils.acq.lineitem.picklist.retrieve'],
75 params: [openils.User.authtoken, pl_id, liArgs],
84 return this._plist.name();
87 return this._plist.owner();
89 create_time: function() {
90 return this._plist.create_time();
92 edit_time: function() {
93 return this._plist.edit_time();
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();
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);
114 fieldmapper.standardRequest(
115 ['open-ils.acq', 'open-ils.acq.picklist.create'],
117 params: [openils.User.authtoken, picklist],
118 oncomplete: function(r) {
119 // XXX event/error handling
120 oncomplete(r.recv().content());
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'],
131 params: [openils.User.authtoken, picklist],
132 oncomplete: function(r) {
133 // XXX event/error handling
134 oncomplete(r.recv().content());
140 /** Deletes a list of picklists
141 * @param list Array of picklist IDs
143 openils.acq.Picklist.deleteList = function(list, onComplete) {
144 openils.acq.Picklist._deleteList(list, 0, onComplete);
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)
151 fieldmapper.standardRequest(
152 ['open-ils.acq', 'open-ils.acq.picklist.delete'],
154 params: [openils.User.authtoken, list[idx]],
155 oncomplete: function(r) {
157 stat = msg.content();
158 /* XXX CHECH FOR EVENT */
159 openils.acq.Picklist._deleteList(list, ++idx, onComplete);