};
+ this.getAll = function(callback, id_only) {
+ /* For some uses of the li table, we may not really know about "all"
+ * the lineitems that the user thinks we know about. If we're a paged
+ * picklist, for example, we only know about the lineitems we've
+ * displayed, but not necessarily all the lineitems on the picklist.
+ * So we reach out to pcrud to inform us.
+ */
+
+ var oncomplete = function(r) {
+ var id_list = openils.Util.readResponse(r);
+ if (id_only)
+ callback(id_list);
+ else
+ self.fetchLineitemsById(id_list, callback);
+ };
+
+ if (this.isPL) {
+ this.pcrud.search(
+ "jub", {"picklist": this.isPL}, {
+ "id_list": true, /* sic, even if id_only */
+ "async": true,
+ "oncomplete": oncomplete
+ }
+ );
+ return;
+ } else if (this.isPO) {
+ this.pcrud.search(
+ "jub", {"purchase_order": this.isPO}, {
+ "id_list": true,
+ "async": true,
+ "oncomplete": oncomplete
+ }
+ );
+ return;
+ } else if (this.isUni && this.pager) {
+ this.pager.getAllLineitemIDs(oncomplete);
+ return;
+ }
+
+ /* If execution reaches this point, we don't need or can't perform
+ * any special tricks to find out the "real" list of "all" lineitems
+ * in this context, so we fall back to the old method.
+ */
+ callback(this.getSelected(true, null, id_only));
+ };
+
/** @param all If true, assume all are selected */
- this.getSelected = function(all) {
- var selected = [];
+ this.getSelected = function(
+ all,
+ callback /* If you want a "good" idea of "all" lineitems, you must
+ provide a callback that accepts an array parameter, rather than
+ relying on the return value of this method itself. */,
+ id_only
+ ) {
+ if (all && callback)
+ return this.getAll(callback, id_only);
+
var indices = {}; /* use to uniqify. needed in paging situations. */
- dojo.forEach(self.selectors,
+ dojo.forEach(this.selectors,
function(i) {
if(i.checked || all)
indices[i.parentNode.parentNode.getAttribute('li')] = true;
}
);
- return openils.Util.objectProperties(indices).map(
- function(liId) { return self.liCache[liId]; }
- );
+
+ var result = openils.Util.objectProperties(indices);
+
+ if (!id_only)
+ result = result.map(function(liId) { return self.liCache[liId]; });
+
+ if (callback)
+ callback(result);
+ else
+ return result;
};
this.setRowAttr = function(td, liWrapper, field, type) {
var row = self.rowTemplate.cloneNode(true);
if (!skip_final_placement) {
self.tbody.appendChild(row);
- self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
}
+ self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
// sort the lineitem notes on edit_time
if(!li.lineitem_notes()) li.lineitem_notes([]);
);
};
+ /* For a given list of lineitem ids, build a list of full lineitems
+ * re-using the fetching logic that is otherwise typical to use in this
+ * module.
+ *
+ * If we've already got a lineitem in the cache, just use that.
+ *
+ * Once we've built a list of lineitems, call callback(thatlist).
+ */
+ this.fetchLineitemsById = function(id_list, callback) {
+ var total = id_list.length;
+ var result_list = [];
+
+ var inner = function(li) {
+ result_list.push(li)
+ if (--total <= 0)
+ callback(result_list);
+ };
+
+ id_list.forEach(function(id) { self._fetchLineitem(id, inner); });
+ };
+
this._fetchLineitem = function(liId, handler, force) {
var li = this.liCache[liId];
this._createPO = function(fields) {
- this.show('acq-lit-progress-numbers');
- var po = new fieldmapper.acqpo();
- po.provider(this.createPoProviderSelector.attr('value'));
- po.ordering_agency(this.createPoAgencySelector.attr('value'));
- po.prepayment_required(fields.prepayment_required[0] ? true : false);
+ var wantall = (fields.create_from == "all");
+
+ /* If we're a picklist or purchase order already and the user wants
+ * all lineitems, we might have pages' worth of lineitems haven't all
+ * been loaded yet, so getSelected() won't find them. The server,
+ * however, should know about all our lineitems, so let's ask the
+ * server for a complete list.
+ */
+
+ if (wantall) {
+ this.getSelected(
+ true, function(list) {
+ self._createPOFromLineitems(fields, list);
+ }, /* id_list */ true
+ );
+ } else {
+ this._createPOFromLineitems(fields, this.getSelected(false, null, true /* id_list */));
+ }
+ };
- var selected = this.getSelected( (fields.create_from == 'all') );
- if(selected.length == 0) return;
+ this._createPOFromLineitems = function(fields, selected) {
+ if (selected.length == 0) return;
- var max = selected.length * 3;
+ this.show("acq-lit-progress-numbers");
+ var po = new fieldmapper.acqpo();
+ po.provider(this.createPoProviderSelector.attr("value"));
+ po.ordering_agency(this.createPoAgencySelector.attr("value"));
+ po.prepayment_required(fields.prepayment_required[0] ? true : false);
- var self = this;
fieldmapper.standardRequest(
- ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
+ ["open-ils.acq", "open-ils.acq.purchase_order.create"],
{ async: true,
params: [
openils.User.authtoken,
- po,
- {
- lineitems : selected.map(function(li) { return li.id() }),
+ po, {
+ lineitems : selected,
create_assets : fields.create_assets[0],
}
],
onresponse : function(r) {
var resp = openils.Util.readResponse(r);
self._updateProgressNumbers(resp);
- if(resp.complete)
- location.href = oilsBasePath + '/acq/po/view/' + resp.purchase_order.id();
+ if (resp.complete) {
+ location.href = oilsBasePath + "/acq/po/view/" +
+ resp.purchase_order.id();
+ }
}
}
);
- }
+ };
+
this.batchFundWidget = null;
}
this._savePl = function(values) {
- var self = this;
- var selected = this.getSelected( (values.which == 'all') );
- openils.Util.show('acq-lit-generic-progress');
+ this.getSelected(
+ (values.which == 'all'),
+ function(list) { self._savePlFromLineitems(values, list); }
+ );
+ };
+
+ this._savePlFromLineitems = function(values, selected) {
+ openils.Util.show("acq-lit-generic-progress");
if(values.new_name) {
openils.acq.Picklist.create(
- {name: values.new_name},
+ {name: values.new_name},
function(id) {
- self._updateLiList(id, selected, 0,
- function(){
- location.href = oilsBasePath + '/acq/picklist/view/' + id;
- });
+ self._updateLiList(
+ id, selected, 0,
+ function() {
+ location.href =
+ oilsBasePath + "/acq/picklist/view/" + id;
+ }
+ );
}
);
} else if(values.existing_pl) {
// update lineitems to use an existing picklist
- self._updateLiList(values.existing_pl, selected, 0,
+ self._updateLiList(
+ values.existing_pl, selected, 0,
function(){
- location.href = oilsBasePath + '/acq/picklist/view/' + values.existing_pl;
- });
+ location.href =
+ oilsBasePath + "/acq/picklist/view/" +
+ values.existing_pl;
+ }
+ );
}
- }
+ };
this._updateLiState = function(values, state) {
- var self = this;
- var selected = this.getSelected( (values.which == 'all') );
+ progressDialog.show(true);
+ this.getSelected(
+ (values.which == 'all'),
+ function(list) {
+ self._updateLiStateFromLineitems(values, state, list);
+ }
+ );
+ };
+
+ this._updateLiStateFromLineitems = function(values, state, selected) {
if(!selected.length) return;
dojo.forEach(selected, function(li) {li.state(state);});
- self._updateLiList(null, selected, 0,
+ self._updateLiList(null, selected, 0,
// TODO consider inline updates for efficiency
function() { location.href = location.href }
);
- }
+ };
this._updateLiList = function(pl, list, idx, oncomplete) {
if(idx >= list.length) return oncomplete();
var self = this;
this.liPager = liPager;
- this.liPager.liTable.isUni = true;
this.poGrid = poGrid;
this.plGrid = plGrid;
}
};
- this._dataLoader = function() {
+ this._dataLoader = function(opts) {
/* This function must contain references to "self" only, not "this." */
var grid = self.result_types[self.result_type].interface;
+
+ if (!opts)
+ opts = {};
+
self.count_results = 0;
- self.params[4].offset = grid.displayOffset;
- self.params[4].limit = grid.displayLimit;
-
- fieldmapper.standardRequest(
- ["open-ils.acq", self.method_name], {
- "params": self.params,
- "async": true,
- "onresponse": function(r) {
- if (r = openils.Util.readResponse(r)) {
- if (!self.count_results++)
- self.show(self.result_type);
- self.add(self.result_type, r);
- }
- },
- "oncomplete": function() { self.resultsComplete(); }
- }
- );
+
+ var use_params = dojo.clone(self.params); /* need copy, not ref */
+
+ if (!opts.skip_paging) {
+ use_params[4].offset = grid.displayOffset;
+ use_params[4].limit = grid.displayLimit;
+ }
+
+ var method = self.method_name;
+ if (opts.atomic)
+ method += ".atomic";
+
+ if (opts.id_list)
+ use_params[4].id_list = true;
+
+ var request_options = {
+ "params": use_params,
+ "async": true
+ };
+
+ if (typeof opts.onresponse != "undefined") {
+ request_options.onresponse = opts.onresponse;
+ } else {
+ /* normal onresponse handler for most times we call this method */
+ request_options.onresponse = function(r) {
+ if (r = openils.Util.readResponse(r)) {
+ if (!self.count_results++)
+ self.show(self.result_type);
+ self.add(self.result_type, r);
+ }
+ };
+ }
+
+ if (typeof opts.oncomplete != "undefined") {
+ request_options.oncomplete = opts.oncomplete;
+ } else {
+ /* normal oncomplete handler for most times we call this method */
+ request_options.oncomplete = function() { self.resultsComplete(); };
+ }
+
+ fieldmapper.standardRequest(["open-ils.acq", method], request_options);
};
this.add = function(which, what) {