1 dojo.provide("openils.vandelay.TreeDndSource");
2 dojo.require("dijit._tree.dndSource");
4 /* This class specifically serves the eg/vandelay/match_set interface
5 * for editing Vandelay Match Set trees. It should probably have a more
6 * specific name that reflects that.
9 "openils.vandelay.TreeDndSource", dijit._tree.dndSource, {
10 "_is_replaceable": function(spoint, dpoint, disroot) {
11 /* An OP can replace anything, but non-OPs can only replace other
16 else if (!dpoint.bool_op())
20 "constructor": function() {
21 /* Given a tree object, there seems to be no way to access its
22 * dndController, which seems to be the only thing that knows
23 * about a tree's selected nodes. So we register instances
24 * in a global variable in order to find them later. :-(
26 if (!window._tree_dnd_controllers)
27 window._tree_dnd_controllers = [];
29 window._tree_dnd_controllers.push(this);
32 this.tree.model.store, "onNew", this,
33 function() { this.redraw_expression_preview(); }
36 this.tree.model.store, "onDelete", this,
37 function() { this.redraw_expression_preview(); }
40 "redraw_expression_preview": function() {
41 if (typeof(window.redraw_expression_preview) == "function") {
42 window.redraw_expression_preview();
44 console.log("no redraw_expression_preview function registered");
47 "checkItemAcceptance": function(target, source, position) {
48 if (!source._ready || source == this) return;
50 if (this.tree.model.replace_mode) {
51 var ditem = dijit.getEnclosingWidget(target).item;
53 position == "over" && this._is_replaceable(
54 source.getAllNodes()[0].match_point,
55 this.tree.model.store.getValue(ditem, "match_point"),
56 ditem === this.tree.model.root
62 this.tree.model.mayHaveChildren(
63 dijit.getEnclosingWidget(target).item
67 /* code in match_set.js makes sure that source._ready gets set true
68 * only when we want the item to be draggable */
70 "itemCreator": function(nodes, somethingelse) {
71 var default_items = this.inherited(arguments);
72 for (var i = 0; i < default_items.length; i++)
73 default_items[i].match_point = nodes[i].match_point;
76 "onDndDrop": function(source, nodes, copy) {
78 !this.tree.model.replace_mode ||
79 this.containerState != "Over" ||
80 this.dropPosition == "Before" ||
81 this.dropPosition == "After" ||
84 return this.inherited(arguments);
87 /* This method only comes into play for our "replace mode" */
89 var target_widget = dijit.getEnclosingWidget(this.targetAnchor);
90 var new_params = this.itemCreator(nodes, this.targetAnchor)[0];
92 /* Here, we morph target_widget.item into the new item */
94 var store = this.tree.model.store;
95 var item = target_widget.item;
96 for (var k in new_params) {
97 if (k == "id") continue; /* can't use this / don't need it */
98 store.setValue(item, k, new_params[k]);
100 if (this.tree.model.root === item) { /* replacing root node */
101 if (!new_params.match_point.bool_op()) {
102 /* If we're here, we've replaced the root node with
103 * something that isn't a bool op, so we need to nuke
104 * any children that the item has.
106 store.setValue(item, "children", []);
109 if (typeof(window.render_vmsp_label) == "function") {
113 window.render_vmsp_label(new_params.match_point)
117 this.redraw_expression_preview();
119 /* just because this is at the end of the default implementation: */