1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Bill Erickson <erickson@esilibrary.com>
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.Fund']) {
18 dojo._hasResource['openils.acq.Fund'] = true;
19 dojo.provide('openils.acq.Fund');
20 dojo.require('fieldmapper.Fieldmapper');
21 dojo.require('fieldmapper.dojoData');
22 dojo.require('openils.Event');
23 dojo.require('openils.Util');
25 /** Declare the Fund class with dojo */
26 dojo.declare('openils.acq.Fund', null, {
27 /* add instance methods here if necessary */
30 openils.acq.Fund.cache = {};
31 openils.acq.Fund._cachecomplete = false;
33 openils.acq.Fund.createStore = function(onComplete, limitPerm) {
34 /** Fetches the list of funds and builds a grid from them */
39 while(src = openils.Util.readResponse(r)) {
40 openils.acq.Fund.cache[src.id()] = src;
43 openils.acq.Fund._cachecomplete = true;
44 onComplete(acqf.toStoreData(items));
47 fieldmapper.standardRequest(
48 ['open-ils.acq', 'open-ils.acq.fund.org.retrieve'],
50 params: [openils.User.authtoken, null, {flesh_summary:1, limit_perm:limitPerm}],
58 * @param fields Key/value pairs used to create the new fund
60 openils.acq.Fund.create = function(fields, onCreateComplete) {
63 for(var field in fields)
64 fund[field](fields[field]);
66 fieldmapper.standardRequest(
67 ['open-ils.acq', 'open-ils.acq.fund.create'],
69 params: [openils.User.authtoken, fund],
70 oncomplete: function(r) {
72 var id = msg.content();
81 openils.acq.Fund.createAllocation = function(fields, onCreateComplete) {
82 var alloc = new acqfa()
83 for(var field in fields)
84 alloc[field](fields[field]);
85 fieldmapper.standardRequest(
86 ['open-ils.acq', 'open-ils.acq.fund_allocation.create'],
89 params: [openils.User.authtoken, alloc],
90 oncomplete: function(r) {
92 var id = msg.content();
102 * Synchronous fund retrievel method
104 openils.acq.Fund.retrieve = function(id) {
105 if(openils.acq.Fund.cache[id])
106 return openils.acq.Fund.cache[id];
107 openils.acq.Fund.cache[id] = fieldmapper.standardRequest(
108 ['open-ils.acq', 'open-ils.acq.fund.retrieve'],
109 [openils.User.authtoken, id]
111 return openils.acq.Fund.cache[id];
115 openils.acq.Fund.deleteFromGrid = function(grid, onComplete) {
117 var selected = grid.selection.getSelected();
118 for(var rowIdx in selected)
119 list.push(grid.model.getDatum(selected[rowIdx], 0));
120 openils.acq.Fund.deleteList(list, onComplete);
123 openils.acq.Fund.deleteList = function(list, onComplete) {
124 openils.acq.Fund._deleteList(list, 0, onComplete);
127 openils.acq.Fund._deleteList = function(list, idx, onComplete) {
128 if(idx >= list.length)
131 var fundId = list[idx];
132 delete openils.acq.Fund.cache[list[idx]];
134 fieldmapper.standardRequest(
135 ['open-ils.acq', 'open-ils.acq.fund.delete'],
137 params: [openils.User.authtoken, fundId],
138 oncomplete: function(r) {
139 stat = r.recv().content();
140 /* XXX CHECH FOR EVENT */
141 openils.acq.Fund._deleteList(list, ++idx, onComplete);
147 openils.acq.Fund.nameMapping = function(oncomplete) {
150 var buildMap = function() {
151 for (var i in openils.acq.Fund.cache) {
152 var item = openils.acq.Fund.cache[i];
154 names.push(item.name());
155 oncomplete(ids, names);
159 if (openils.acq.Fund._cachecomplete) {
160 buildMap(oncomplete);
162 openils.acq.Fund.createStore(buildMap);
167 * Sets the store for an existing openils.widget.FundFilteringSelect
168 * using the funds where the user has the requested permission.
169 * @param perm The permission to check
170 * @param selector The pre-created dijit.form.FilteringSelect object.
173 openils.acq.Fund.storeCache = [];
175 openils.acq.Fund.buildPermFundSelector = function(perm, selector) {
176 dojo.require('dojo.data.ItemFileReadStore');
178 function hookupStore(store) {
179 selector.store = store;
183 function buildPicker(r) {
186 while (msg = r.recv()) {
187 var fund = msg.content();
191 var store = new dojo.data.ItemFileReadStore({data:acqf.toStoreData(fundList)});
194 openils.acq.Fund.storeCache[perm] = store;
197 if (openils.acq.Fund.storeCache[perm]) {
198 hookupStore(openils.acq.Fund.storeCache[perm]);
200 fieldmapper.standardRequest(
201 ['open-ils.acq', 'open-ils.acq.fund.org.retrieve'],
202 { params: [openils.User.authtoken, null,
203 {flesh_summary:1, limit_perm:perm}],
204 oncomplete: buildPicker,