4 qbe
.CurrentModels
= [];
5 qbe
.CurrentRelations
= [];
6 qbe
.Core
= function() {};
9 $
(document
).ready(function() {
11 * Handle the loading initial data blocking process
13 var _loadingData
= false;
16 * Load initial data to edit query
18 qbe
.Core
.loadData
= function(data
) {
19 var initialForms
, maxForms
, totalForms
;
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(".");
31 qbe
.Core
.addModule(app
, model
);
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");
49 criteria
= data
["form-"+ i
+"-criteria_"+ c
];
51 $
("#id_form-"+ i
+"-criteria_"+ c
).val(criteria
);
52 criteria
= data
["form-"+ i
+"-criteria_"+ ++c
];
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
);
68 $
("#qbeBox_"+ modelName
).css({
69 left
: positionSplits
[0],
70 top
: positionSplits
[1]
73 $
("#id_positions").val(data
["positions"]);
78 * Toggle visibility of models
80 qbe
.Core
.toggleModel
= function () {
81 var id
, appName
, modelName
, idSplits
, splits
, $
this;
83 idSplits
= $
this.attr("id").split("qbeModelAnchor_");
84 splits
= idSplits
[1].split(".");
86 modelName
= splits
[1];
87 $
("#qbeModelItem_"+ modelName
).toggleClass("selected");
88 if ($
("#qbeModelItem_"+ modelName
).hasClass("selected")) {
89 qbe
.Core
.addModule(appName
, modelName
);
91 qbe
.Core
.removeModule(appName
, modelName
);
93 qbe
.Core
.updateModels();
98 * Invokes the update of the each row
100 qbe
.Core
.updateModels
= function() {
101 $
(this).each(qbe
.Core
.updateRow
);
105 * Update the rows with the new models added
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];
112 var value
= appModel
.replace(".", ": ");
113 options
.push('<option value="'+ key
+'">'+ value
+'</option>');
115 $
(".qbeFillModels").each(function() {
116 var val
= $
(this).val();
117 $
(this).html(options
.join(""));
120 qbe
.Core
.alternatingRows();
124 * Set a CSS class for alterned rows
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");
135 * Add rows per new relation with the models list hyphen separated
137 qbe
.Core
.addRelationsFrom
= function(through
) {
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();
154 * Event triggered when the SELECT tag for fill models is changed
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();
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];
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") {
177 option
= '<option class="'+ style
+'" value="'+ key
+'">'+ value
+'</option>'
178 optForeigns
.push(option
);
179 } else if (fields
[key].type
== "ManyToManyField") {
181 option
= '<option class="'+ style
+'" value="'+ key
+'">'+ value
+'</option>'
182 optManies
.push(option
);
183 } else if (fields
[key].primary
) {
185 option
= '<option class="'+ style
+'" value="'+ key
+'">'+ value
+'</option>'
186 optPrimaries
.push(option
);
189 option
= '<option class="'+ style
+'" value="'+ key
+'">'+ value
+'</option>'
190 optFields
.push(option
);
193 $
("#"+ domTo
).html('<option value="">*</option>' + optPrimaries
.join("") + optForeigns
.join("") + optManies
.join("") + optFields
.join(""));
194 // We need to raise change event
195 $
("#"+ domTo
).change();
200 * Event triggered when the SELECT tag for fill fields is changed
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
++) {
212 domTo
= prefix
+"-"+ input
;
214 $
("#"+ domTo
).removeAttr("disabled");
216 $
("#"+ domTo
).attr("disabled", "disabled");
217 $
("#"+ domTo
).val("");
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
];
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
);
235 qbe
.Core
.addRelationsFrom(targetsString
.join("-"));
237 targetString
= target
.name
+"."+ target
.model
+"."+ target
.field
;
238 $
("#"+ domTo
).val(targetString
);
239 $
("#"+ domTo
).prev().val("join");
240 qbe
.Core
.addRelationsFrom(targetString
);
243 $
("#"+ domTo
).val("");
250 * Adds a model to the layer
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
);
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
);
264 qbe
.Diagram
.addBox(appName
, modelName
);
266 qbe
.Core
.updateRelations();
271 * Removes a model from the layer
273 qbe
.Core
.removeModule
= function(appName
, modelName
) {
274 var appModel
= appName
+"."+ modelName
;
275 var pos
= qbe
.CurrentModels
.indexOf(appModel
);
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
);
285 * Update relations among models
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
;
316 targetModel
= relation
.target
.through
;
319 targetModel
= relation
.target
;
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());
341 })(jQuery
.noConflict());