ajout de django-qbe pour tests
[auf_rh_dae.git] / project / media / django_qbe / js / qbe.core.js
1 if (!window.qbe) {
2 var qbe = {};
3 }
4 qbe.CurrentModels = [];
5 qbe.CurrentRelations = [];
6 qbe.Core = function() {};
7
8 (function($) {
9 $(document).ready(function() {
10 /**
11 * Handle the loading initial data blocking process
12 */
13 var _loadingData = false;
14
15 /**
16 * Load initial data to edit query
17 */
18 qbe.Core.loadData = function(data) {
19 var initialForms, maxForms, totalForms;
20 _loadingData = true;
21 initialForms = parseInt(data["form-INITIAL_FORMS"]);
22 maxForms = parseInt(data["form-MAX_NUM_FORMS"]);
23 totalForms = parseInt(data["form-TOTAL_FORMS"]);
24 for(var i=initialForms; i<totalForms; i++) {
25 var appModel, splits, show, model, field, sorted;
26 appModel = data["form-"+ i +"-model"];
27 if (!(appModel in qbe.CurrentModels)) {
28 splits = appModel.split(".");
29 app = splits[0];
30 model = splits[1];
31 qbe.Core.addModule(app, model);
32 }
33 qbe.Core.updateModels();
34 $("#id_form-"+ i +"-model").val(appModel);
35 $("#id_form-"+ i +"-model").change();
36 field = data["form-"+ i +"-field"];
37 $("#id_form-"+ i +"-field").val(field);
38 $("#id_form-"+ i +"-field").change();
39 sorted = data["form-"+ i +"-sort"];
40 $("#id_form-"+ i +"-sort").val(sorted);
41 $("#id_form-"+ i +"-show").remove("checked");
42 if (data["form-"+ i +"-show"]) {
43 show = data["form-"+ i +"-show"];
44 if (show && show == "on") {
45 $("#id_form-"+ i +"-show").attr("checked", "checked");
46 }
47 }
48 c = 0;
49 criteria = data["form-"+ i +"-criteria_"+ c];
50 while(criteria) {
51 $("#id_form-"+ i +"-criteria_"+ c).val(criteria);
52 criteria = data["form-"+ i +"-criteria_"+ ++c];
53 }
54 }
55 $("#id_form_limit").val(data["limit"]);
56 var positions, positionSplits, splits, appModel, appName, modelName;
57 positions = data["positions"].split("|");
58 for(var i=0; i<positions.length; i++) {
59 splits = positions[i].split("@");
60 appModel = splits[0].split(".");
61 appName = appModel[0];
62 modelName = appModel[1];
63 positionSplits = splits[1].split(";");
64 if (!(appModel in qbe.CurrentModels)) {
65 $("#qbeModelItem_"+ modelName).toggleClass("selected");
66 qbe.Core.addModule(appName, modelName);
67 }
68 $("#qbeBox_"+ modelName).css({
69 left: positionSplits[0],
70 top: positionSplits[1]
71 });
72 }
73 $("#id_positions").val(data["positions"]);
74 _loadingData = false;
75 };
76
77 /**
78 * Toggle visibility of models
79 */
80 qbe.Core.toggleModel = function () {
81 var id, appName, modelName, idSplits, splits, $this;
82 $this = $(this);
83 idSplits = $this.attr("id").split("qbeModelAnchor_");
84 splits = idSplits[1].split(".");
85 appName = splits[0];
86 modelName = splits[1];
87 $("#qbeModelItem_"+ modelName).toggleClass("selected");
88 if ($("#qbeModelItem_"+ modelName).hasClass("selected")) {
89 qbe.Core.addModule(appName, modelName);
90 } else {
91 qbe.Core.removeModule(appName, modelName);
92 }
93 qbe.Core.updateModels();
94 return false;
95 }
96
97 /**
98 * Invokes the update of the each row
99 */
100 qbe.Core.updateModels = function() {
101 $(this).each(qbe.Core.updateRow);
102 };
103
104 /**
105 * Update the rows with the new models added
106 */
107 qbe.Core.updateRow = function(row) {
108 var options = ['<option value="">----</option>'];
109 for(i=0; i<qbe.CurrentModels.length; i++) {
110 var appModel = qbe.CurrentModels[i];
111 var key = appModel;
112 var value = appModel.replace(".", ": ");
113 options.push('<option value="'+ key +'">'+ value +'</option>');
114 }
115 $(".qbeFillModels").each(function() {
116 var val = $(this).val();
117 $(this).html(options.join(""));
118 $(this).val(val);
119 });
120 qbe.Core.alternatingRows();
121 };
122
123 /**
124 * Set a CSS class for alterned rows
125 */
126 qbe.Core.alternatingRows = function() {
127 var rows = "#qbeConditionsTable tbody tr";
128 $(rows).not(".add-row").removeClass("row1 row2");
129 $(rows +":even").not(".add-row").addClass("row1");
130 $(rows +":odd").not(".add-row").addClass("row2");
131 $(rows +":last").addClass("add-row");
132 };
133
134 /**
135 * Add rows per new relation with the models list hyphen separated
136 */
137 qbe.Core.addRelationsFrom = function(through) {
138 var appModels
139 appModels = through.split("-");
140 for(var i=0; i<appModels.length; i++) {
141 var appModel = appModels[i];
142 var splits = appModel.split(".");
143 qbe.Core.addModule(splits[0], splits[1]);
144 $("#qbeModelItem_"+ splits[1]).addClass("selected");
145 $("#qbeForm .add-row").click();
146 $(".qbeFillModels:last").val(splits[0] +"."+ splits[1]);
147 $(".qbeFillModels:last").change();
148 $(".qbeFillFields:last").val(splits[2]);
149 $(".qbeFillFields:last").change();
150 }
151 };
152
153 /**
154 * Event triggered when the SELECT tag for fill models is changed
155 */
156 qbe.Core.fillModelsEvent = function() {
157 var appModel, key, fields, splits, appModelSplits, prefix, css, cssSplit, domTo, option, optFields, optPrimaries, optForeigns, optManies, style, value;
158 appModel = $(this).val();
159 if (appModel) {
160 appModelSplits = appModel.split(".");
161 fields = qbe.Models[appModelSplits[0]][appModelSplits[1]].fields;
162 splits = $(this).attr("id").split("-");
163 prefix = splits.splice(0, splits.length-1).join("-");
164 css = $(this).attr("class");
165 cssSplit = css.split("to:")
166 domTo = prefix +"-"+ cssSplit[cssSplit.length-1];
167 optFields = [];
168 optPrimaries = [];
169 optForeigns = [];
170 optManies = [];
171 for(key in fields) {
172 // We can't jump fields with no target 'cause they are
173 // ManyToManyField and ForeignKey fields!
174 value = fields[key].label;
175 if (fields[key].type == "ForeignKey") {
176 style = "foreign";
177 option = '<option class="'+ style +'" value="'+ key +'">'+ value +'</option>'
178 optForeigns.push(option);
179 } else if (fields[key].type == "ManyToManyField") {
180 style = "many";
181 option = '<option class="'+ style +'" value="'+ key +'">'+ value +'</option>'
182 optManies.push(option);
183 } else if (fields[key].primary) {
184 style = "primary";
185 option = '<option class="'+ style +'" value="'+ key +'">'+ value +'</option>'
186 optPrimaries.push(option);
187 } else {
188 style = "";
189 option = '<option class="'+ style +'" value="'+ key +'">'+ value +'</option>'
190 optFields.push(option);
191 }
192 }
193 $("#"+ domTo).html('<option value="">*</option>' + optPrimaries.join("") + optForeigns.join("") + optManies.join("") + optFields.join(""));
194 // We need to raise change event
195 $("#"+ domTo).change();
196 }
197 };
198
199 /**
200 * Event triggered when the SELECT tag for fill fields is changed
201 */
202 qbe.Core.fillFieldsEvent = function() {
203 var field, splits, prefix, css, cssSplit, inputs, input, domTo, appModel, appModelSplits, fields, primary, target, targetRel, targetModel, targetStrings, targetString, relations;
204 field = $(this).val();
205 splits = $(this).attr("id").split("-");
206 prefix = splits.splice(0, splits.length-1).join("-");
207 css = $(this).attr("class");
208 cssSplit = css.split("enable:")
209 inputs = cssSplit[cssSplit.length-1].split(",");
210 for(var i=0; i<inputs.length; i++) {
211 input = inputs[i];
212 domTo = prefix +"-"+ input;
213 if (field) {
214 $("#"+ domTo).removeAttr("disabled");
215 } else {
216 $("#"+ domTo).attr("disabled", "disabled");
217 $("#"+ domTo).val("");
218 }
219 if ($("#"+ domTo).is('input[type="text"]')) {
220 appModel = $("#"+ prefix +"-model").val();
221 appModelSplits = appModel.split(".");
222 fields = qbe.Models[appModelSplits[0]][appModelSplits[1]].fields;
223 if (field in fields && fields[field].target && !_loadingData) {
224 target = fields[field].target;
225 if (target.through) {
226 $(this).parent().parent().children("td:last").children("a").click();
227 targetModel = qbe.Models[target.through.name][target.through.model];
228 targetsString = [];
229 relations = targetModel.relations;
230 for(var r=0; r<targetModel.relations.length; r++) {
231 targetRel = targetModel.relations[r];
232 targetString = target.through.name +"."+ target.through.model +"."+ targetRel.source;
233 targetsString.push(targetString);
234 }
235 qbe.Core.addRelationsFrom(targetsString.join("-"));
236 } else {
237 targetString = target.name +"."+ target.model +"."+ target.field;
238 $("#"+ domTo).val(targetString);
239 $("#"+ domTo).prev().val("join");
240 qbe.Core.addRelationsFrom(targetString);
241 }
242 } else {
243 $("#"+ domTo).val("");
244 }
245 }
246 }
247 };
248
249 /**
250 * Adds a model to the layer
251 */
252 qbe.Core.addModule = function (appName, modelName) {
253 var appModel, model, target1, target2;
254 model = qbe.Models[appName][modelName];
255 appModel = appName +"."+ modelName;
256 if (qbe.CurrentModels.indexOf(appModel) < 0) {
257 qbe.CurrentModels.push(appModel);
258 if (model.is_auto) {
259 target1 = model.relations[0].target;
260 target2 = model.relations[1].target;
261 qbe.Core.addModule(target1.name, target1.model);
262 qbe.Core.addModule(target2.name, target2.model);
263 } else {
264 qbe.Diagram.addBox(appName, modelName);
265 }
266 qbe.Core.updateRelations();
267 }
268 };
269
270 /*
271 * Removes a model from the layer
272 */
273 qbe.Core.removeModule = function(appName, modelName) {
274 var appModel = appName +"."+ modelName;
275 var pos = qbe.CurrentModels.indexOf(appModel);
276 if (pos >= 0) {
277 qbe.CurrentModels.splice(pos, 1);
278 var model = qbe.Models[appName][modelName];
279 qbe.Diagram.removeBox(appName, modelName)
280 qbe.Diagram.removeRelations(appName, modelName);
281 }
282 };
283
284 /*
285 * Update relations among models
286 */
287 qbe.Core.updateRelations = function () {
288 var label, labelStyle, paintStyle, backgroundPaintStyle, makeOverlay;
289 var relations, relation, mediumHeight, connections;
290 var sourceAppModel, sourceModelName, sourceAppName, sourceModel, sourceFieldName, sourceId, sourceField, sourceSplits, divSource;
291 var targetModel, targetAppName, targetModelName, targetFieldName, targetId, targetField, divTarget;
292 for(var i=0; i<qbe.CurrentModels.length; i++) {
293 sourceAppModel = qbe.CurrentModels[i];
294 sourceSplits = sourceAppModel.split(".");
295 sourceAppName = sourceSplits[0];
296 sourceModelName = sourceSplits[1];
297 sourceModel = qbe.Models[sourceAppName][sourceModelName];
298 relations = sourceModel.relations;
299 for(var j=0; j<relations.length; j++) {
300 relation = relations[j];
301 sourceFieldName = relation.source;
302 label = qbe.Diagram.Defaults["foreign"].label;
303 labelStyle = qbe.Diagram.Defaults["foreign"].labelStyle;
304 paintStyle = qbe.Diagram.Defaults["foreign"].paintStyle;
305 makeOverlays = qbe.Diagram.Defaults["foreign"].makeOverlays;
306 backgroundPaintStyle = qbe.Diagram.Defaults["foreign"].backgroundPaintStyle;
307 if (relation.target.through) {
308 if (qbe.Models[relation.target.through.name][relation.target.through.model].is_auto) {
309 targetModel = relation.target;
310 label = relation.target.through.model;
311 labelStyle = qbe.Diagram.Defaults["many"].labelStyle;
312 paintStyle = qbe.Diagram.Defaults["many"].paintStyle;
313 makeOverlays = qbe.Diagram.Defaults["many"].makeOverlays;
314 backgroundPaintStyle = qbe.Diagram.Defaults["many"].backgroundPaintStyle;
315 } else {
316 targetModel = relation.target.through;
317 }
318 } else {
319 targetModel = relation.target;
320 }
321 targetAppName = targetModel.name;
322 targetModelName = targetModel.model;
323 targetFieldName = targetModel.field;
324 sourceField = $("#qbeBoxField_"+ sourceAppName +"\\."+ sourceModelName +"\\."+ sourceFieldName);
325 targetField = $("#qbeBoxField_"+ targetAppName +"\\."+ targetModelName +"\\."+ targetFieldName);
326 if (sourceField.length && targetField.length
327 && !qbe.Diagram.hasConnection(sourceField, targetField)) {
328 sourceId = "qbeBox_"+ sourceModelName;
329 targetId = "qbeBox_"+ targetModelName;
330 divSource = document.getElementById(sourceId);
331 divTarget = document.getElementById(targetId);
332 if (divSource && divTarget) {
333 qbe.Diagram.addRelation(sourceId, sourceField, targetId, targetField, label, labelStyle, paintStyle, backgroundPaintStyle, makeOverlays());
334 }
335 }
336 }
337 }
338 }
339
340 });
341 })(jQuery.noConflict());