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');
24 /** Declare the Fund class with dojo */
25 dojo.declare('openils.acq.Fund', null, {
26 /* add instance methods here if necessary */
29 openils.acq.Fund.cache = {};
30 openils.acq.Fund._cachecomplete = false;
32 openils.acq.Fund.createStore = function(onComplete, limitPerm) {
33 /** Fetches the list of funds and builds a grid from them */
38 while(msg = r.recv()) {
39 var src = msg.content();
40 if(e = openils.Event.parse(src))
42 openils.acq.Fund.cache[src.id()] = src;
45 openils.acq.Fund._cachecomplete = true;
46 onComplete(acqf.toStoreData(items));
49 fieldmapper.standardRequest(
50 ['open-ils.acq', 'open-ils.acq.fund.org.retrieve'],
52 params: [openils.User.authtoken, null, {flesh_summary:1, limit_perm:limitPerm}],
60 * @param fields Key/value pairs used to create the new fund
62 openils.acq.Fund.create = function(fields, onCreateComplete) {
65 for(var field in fields)
66 fund[field](fields[field]);
68 fieldmapper.standardRequest(
69 ['open-ils.acq', 'open-ils.acq.fund.create'],
71 params: [openils.User.authtoken, fund],
72 oncomplete: function(r) {
74 var id = msg.content();
83 openils.acq.Fund.createAllocation = function(fields, onCreateComplete) {
84 var alloc = new acqfa()
85 for(var field in fields)
86 alloc[field](fields[field]);
87 fieldmapper.standardRequest(
88 ['open-ils.acq', 'open-ils.acq.fund_allocation.create'],
91 params: [openils.User.authtoken, alloc],
92 oncomplete: function(r) {
94 var id = msg.content();
104 * Synchronous fund retrievel method
106 openils.acq.Fund.retrieve = function(id) {
107 if(openils.acq.Fund.cache[id])
108 return openils.acq.Fund.cache[id];
109 openils.acq.Fund.cache[id] = fieldmapper.standardRequest(
110 ['open-ils.acq', 'open-ils.acq.fund.retrieve'],
111 [openils.User.authtoken, id]
113 return openils.acq.Fund.cache[id];
117 openils.acq.Fund.deleteFromGrid = function(grid, onComplete) {
119 var selected = grid.selection.getSelected();
120 for(var rowIdx in selected)
121 list.push(grid.model.getDatum(selected[rowIdx], 0));
122 openils.acq.Fund.deleteList(list, onComplete);
125 openils.acq.Fund.deleteList = function(list, onComplete) {
126 openils.acq.Fund._deleteList(list, 0, onComplete);
129 openils.acq.Fund._deleteList = function(list, idx, onComplete) {
130 if(idx >= list.length)
133 var fundId = list[idx];
134 delete openils.acq.Fund.cache[list[idx]];
136 fieldmapper.standardRequest(
137 ['open-ils.acq', 'open-ils.acq.fund.delete'],
139 params: [openils.User.authtoken, fundId],
140 oncomplete: function(r) {
141 stat = r.recv().content();
142 /* XXX CHECH FOR EVENT */
143 openils.acq.Fund._deleteList(list, ++idx, onComplete);
149 openils.acq.Fund.nameMapping = function(oncomplete) {
152 var buildMap = function() {
153 for (var i in openils.acq.Fund.cache) {
154 var item = openils.acq.Fund.cache[i];
156 names.push(item.name());
157 oncomplete(ids, names);
161 if (openils.acq.Fund._cachecomplete) {
162 buildMap(oncomplete);
164 openils.acq.Fund.createStore(buildMap);
169 * Sets the store for an existing openils.widget.FundFilteringSelect
170 * using the funds where the user has the requested permission.
171 * @param perm The permission to check
172 * @param selector The pre-created dijit.form.FilteringSelect object.
175 openils.acq.Fund.storeCache = [];
177 openils.acq.Fund.buildPermFundSelector = function(perm, selector) {
178 dojo.require('dojo.data.ItemFileReadStore');
180 function hookupStore(store) {
181 selector.store = store;
185 function buildPicker(r) {
188 while (msg = r.recv()) {
189 var fund = msg.content();
193 var store = new dojo.data.ItemFileReadStore({data:acqf.toStoreData(fundList)});
196 openils.acq.Fund.storeCache[perm] = store;
199 if (openils.acq.Fund.storeCache[perm]) {
200 hookupStore(openils.acq.Fund.storeCache[perm]);
202 fieldmapper.standardRequest(
203 ['open-ils.acq', 'open-ils.acq.fund.org.retrieve'],
204 { params: [openils.User.authtoken, null,
205 {flesh_summary:1, limit_perm:perm}],
206 oncomplete: buildPicker,