Pflichtenhefte: Drag & Drop von Textblöcken
[kivitendo-erp.git] / js / requirement_spec.js
1 /* Functions used for the requirement specs tree view */
2
3 function check_move(data) {
4   var dragged_type = data.o.data('type');
5   var dropped_type = data.r.data('type');
6
7   // console.debug("dragged " + dragged_type + " dropped " + dropped_type + " dir " + data.p);
8
9   if ((dragged_type == "sections") || (dragged_type == "textblocks-front") || (dragged_type == "textblocks-back"))
10     return false;
11
12   if (dragged_type == "textblock") {
13     if ((dropped_type == "textblocks-front") || (dropped_type == "textblocks-back"))
14       return (data.p == "inside") || (data.p == "last");
15     if (dropped_type == "textblock")
16       return (data.p == "before") || (data.p == "after");
17
18     return false;
19   }
20
21   if (dragged_type == "section") {
22     if (dropped_type == "sections")
23       return (data.p == "inside") || (data.p == "last");
24     if (dropped_type == "section")
25       return (data.p == "before") || (data.p == "after");
26
27     return false;
28   }
29
30   // dragged_type == (sub) function blocks
31   if ((dropped_type == "textblock") || (dropped_type == "textblocks-front") || (dropped_type == "textblocks-back"))
32     return false;
33
34   var dropped_depth = dropped_type == "sections" ? 0 : dropped_type == "section" ? 1 : data.r.parent().parent().data('type') != "functionblock" ? 2 : 3;
35   if ((data.p == "inside") || (data.p == "last"))
36     dropped_depth++;
37
38   var dragged_depth = 1 + data.o.children('ul').size();
39
40   // console.debug("dropped_depth " + dropped_depth + " dragged_depth " + dragged_depth);
41
42   return (2 <= dropped_depth) && ((dragged_depth + dropped_depth) <= 4);
43 }
44
45 function node_moved(event) {
46   console.debug("node moved");
47   var move_obj   = $.jstree._reference('#tree')._get_move();
48   var dragged    = move_obj.o;
49   var dropped    = move_obj.r;
50   var controller = dragged.data("type") == "textblock" ? "RequirementSpecTextBlock" : "RequirementSpecItem";
51   var data       = {
52     action:               controller + "/dragged_and_dropped",
53     requirement_spec_id:  $('#requirement_spec_id').val(),
54     id:                   dragged.data("id"),
55     dropped_id:           dropped.data("id"),
56     dropped_type:         dropped.data("type"),
57     position:             move_obj.p,
58     current_content_type: $('#current_content_type').val(),
59     current_content_id:   $('#current_content_id').val()
60   };
61   // console.debug("controller: " + controller);
62   // console.debug(data);
63
64   $.post("controller.pl", data, eval_json_result);
65
66   return true;
67 }
68
69 function node_clicked(event) {
70   var node = $.jstree._reference('#tree')._get_node(event.target);
71   var type = node ? node.data('type') : undefined;
72
73   if (!type)
74     return;
75
76   if ('sections' ==  type) {
77     $('#current_content_type').val('sections');
78     $('#current_content_id').val('');
79     return;
80   }
81
82   var url = 'controller.pl?action='
83   $.get('controller.pl', {
84     action:               (/^textblock/ ? 'RequirementSpecTextBlock' : 'RequirementSpecItem') + '/ajax_list.js',
85     current_content_type: $('#current_content_type').val(),
86     current_content_id:   $('#current_content_id').val(),
87     clicked_type:         type,
88     clicked_id:           node.data('id')
89   }, function(new_data) {
90     $('#current_content_type').val(type);
91     $('#current_content_id').val(node.data('id'));
92     eval_json_result(new_data);
93   });
94 }
95
96 function section_form_requested(data) {
97   $('#new-section-button').removeAttr('disabled');
98   if (data.status == "ok")
99     $('#content-column').html(data.html);
100   else
101     alert('oh yeah response: ' + data.status + "\n" + data.error);
102 }
103
104 function section_form_submitted(data) {
105   alert('oh yeah response: ' + data.status);
106 }
107
108 function server_side_error(things_to_enable) {
109   alert('Server-side error.');
110   if (things_to_enable)
111     $(things_to_enable).removeAttr('disabled');
112 }
113
114 function new_section_form() {
115   $('#new-section-button').attr('disabled', 'disabled');
116   $.ajax({
117     type: 'POST',
118     url: 'controller.pl',
119     data: 'action=RequirementSpecItem/new.json&requirement_spec_id=' + $('#requirement_spec_id').val() + '&item_type=section',
120     success: section_form_requested,
121     error: function() { server_side_error('#new-section-button'); }
122   });
123 }
124
125 function submit_section_form(id) {
126   $.ajax({
127     type: 'POST',
128     url: 'controller.pl',
129     data: 'action=RequirementSpecItem/create.json&' + $('section-form-' + id).serialize(),
130     success: section_form_submitted
131   });
132 }
133
134 function cancel_section_form(id) {
135   $('#content-column').html('intentionally empty');
136 }