- Author:
- jpic
- Posted:
- March 9, 2009
- Language:
- JavaScript
- Version:
- Not specified
- Score:
- 1 (after 1 ratings)
A proper app was published for this: https://github.com/yourlabs/django-dynamic-fields
Try it out !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | /*global $ */
/*
Set up a change_form template in: /templates/admin/YOURAPP/YOURMODEL/change_form.html
{% extends "admin/templates/admin/change_form.html" %}
{% load i18n admin_modify adminmedia %}
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="/static/jquery.min.js"></script>
<script type="text/javascript" src="/media/js/urlify.js"></script>
<!-- Sorry, you need this one, i might get rid of it later -->
<script type="text/javascript" src="/static/js/php.min.js"></script>
<script>
$(document).ready(function() {
// Load the javascript snippet here because it parses the HTML
$.getScript('/static/js/jpic.js', function() {
// Select a "tab" to focus on
jpicTabManager.select('fieldset_id');
// There you can play with ui
})});
</script>
{% endblock %}
Example UI playing:
ChampsPourLotSansResidenceSeulement = new Array(
'departement',
'cp',
'ville',
'addresse1',
'addresse2',
'secteur',
'quartier',
'ville_diffusion',
'gps'
);
ChampsPourLocationSeulement = new Array(
'estimation_cc',
'loyer_hc',
'prov_charges',
'loyer_cc',
'garantie',
'dg_telecommande',
'taux_honoraires_locataire',
'taux_honoraires_proprietaire',
'honoraires_locataire',
'honoraires_proprietaire',
'frais_dossier',
'part_locataire',
'part_proprietaire',
'numero_clef',
'assurance',
'loyer_impayes',
'premiere_occupation',
'premiere_occupation_duree',
'vacance_locative',
'vacance_locative_duree',
'taux_assurance',
'centre_des_impots',
'compagnie_des_eaux',
'bail',
'Compteur',
'Locations',
'Meubles',
'Charges'
);
ChampsPourVenteSeulement = new Array(
'droits',
'loi_defisc',
'actabilite',
'estimation',
'prix_net_vendeur',
'honoraires',
'taux_ttc',
'alachargede',
'prix_fai',
'frais_notaire_reduit',
'base_honoraires_commercial',
'notaire_vendeur',
'notaire',
'gestionnaire_existe',
'gestionnaire'
);
$(document).ready(function() {
$.getScript('/static/js/jpic.js', function() {
ui.addConstraint('loi_defisc', 'droits', 'A', jpicFieldValueNotEqual);
ui.addConstraint(
ChampsPourLocationSeulement,
'vente_location',
'Location',
jpicFieldValueEqual
);
ui.addConstraint(
ChampsPourVenteSeulement,
'vente_location',
'Vente',
jpicFieldValueEqual
);
ui.addConstraint(
ChampsPourLotSansResidenceSeulement,
'residence',
'',
jpicFieldValueEqual
);
ui.addConstraint([
'actabilite',
'livraison_prevue',
'livraison_prevue_le',
'livraison_realisee_le',
],
'genre',
'Neuf',
jpicFieldValueEqual
);
ui.addConstraint([
'chambres',
'etages',
],
'sous_type',
[
'chambre',
'garage',
],
jpicFieldValueNotLike
);
function maj()
{
ui.update();
var sous_type = new jpicField('sous_type').getValue();
if (sous_type && sous_type.indexOf('Chambre') >= 0)
{
$('#id_type_taille').val('1');
}
var label = "";
if ($('#id_vente_location').val() == 'Location')
{
label = "Clause particulières au bail";
}
else
{
label = "Bloc notes";
}
$('label[for=id_bloc_notes]').html(label+":")
}
$('#id_sous_type').change(maj);
$('#id_residence').change(maj);
$('#id_vente_location').change(maj);
$('#id_droits').change(maj);
$('#id_genre').change(maj);
maj();
jpicTabManager.select('id_mandat');
});
});
*/
window.loadFirebugConsole();
// conditions pattern
function assert() {
var report = '';
for (var i=0; i < this.conditions.length; i++)
{
if (!this.conditions[i].assert())
{
console.log('Assertion '+this.conditions[i].verbose+' #'+ i + '/'+this.conditions.length+' for field '+this.name+' failed');
return false;
}
report += ', '+this.conditions[i].verbose;
}
console.log(this.conditions.length + ' assertions verified OK for field '+this.name+report);
return true;
};
// {{{ field "class"
var jpicField = function(name) {
this.name = name;
this.id = 'id_' + name;
this.conditions = [];
};
jpicField.prototype.assert = assert;
jpicField.prototype.getValue = function() {
if ($('#' + this.id).children(':selected'))
{
var text = $('#' + this.id).children(':selected').text();
if ($('#' + this.id).val())
{
this.value = text;
}
else
{
this.value = false;
}
return this.value;
}
this.value = $('#' + this.id).val();
return this.value;
};
jpicField.prototype.show = function() {
$('#' + this.id).parent().show();
$('label[for=' + this.id + ']').show();
var row = $('#' + this.id).parent().parent();
var showRow = true;
// Don't show the row if any field box is visible
row.find('div.field-box:visible').each(function() {
showRow = false;
});
// Because finding div.field-box:visible sometimes finds field-box as
// visible even though they are in a hidden row with
// Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.0.6) Gecko/2009030910 Gentoo Firefox/3.0.6
// Which will be imposed.
if (!row.is(":visible")) {
showRow = true;
}
if (showRow)
{
row.show();
}
};
jpicField.prototype.hide = function() {
$('#' + this.id).parent().hide();
$('label[for=' + this.id + ']').hide();
var row = $('#' + this.id).parent().parent();
var hideRow = true;
row.find('div.field-box').each(function() {
if($(this).is(":visible"))
{
hideRow = false;
}
});
if (hideRow)
{
row.hide();
}
};
var jpicMapField = function(name) {
this.name = name;
this.id = 'id_' + name;
this.conditions = [];
};
jpicMapField.prototype.assert = assert;
jpicMapField.prototype.hide = function() {
$('#' + this.id).parent().hide();
$('label[for=' + this.id + ']').hide();
$('#id_' + this.name + '_admin_map').hide();
};
jpicMapField.prototype.show = function() {
$('#' + this.id).parent().show();
$('label[for=' + this.id + ']').show();
$('#id_' + this.name + '_admin_map').show();
};
// }}}
// {{{ fieldset/tab
var jpicFieldset = function(name, error, id) {
this.name = name;
this.error = error;
if (id == undefined)
{
this.id = 'id_' + URLify(this.name, 253);
}
else
{
this.id = id;
}
console.log($(this.id));
};
jpicFieldset.factory = function(element) {
var name = element.find("h2").html();
if (!name)
{
return false;
}
var error = element.find('.errorlist').html();
var fieldset = new jpicFieldset(name, error);
element.addClass(fieldset.id);
return fieldset;
};
jpicFieldset.prototype.getTab = function() {
if(this.tab === undefined)
{
this.tab = new jpicTab(this.name, this.error);
}
return this.tab;
};
// TODO: refactor hide/show
jpicFieldset.prototype.show = function() {
if (this.stacked)
{
thisname = this.name
$('div.inline-group').each(function() {
var name = $(this).find("h2").html();
console.log("testing '"+name+"' against '"+this.name+"'");
if (name == thisname)
{
$(this).show();
}
});
}
else
{
if($('fieldset.'+this.id).parent().hasClass('tabular'))
{
$('fieldset.'+this.id).parent().parent().show();
}
else
{
$('fieldset.'+this.id).show();
}
}
};
jpicFieldset.prototype.hide = function() {
if (this.stacked)
{
thisname = this.name
$('div.inline-group').each(function() {
var name = $(this).find("h2").html();
console.log("testing '"+name+"' against '"+this.name+"'");
if (name == thisname)
{
$(this).hide();
}
});
}
else
{
if($('fieldset.'+this.id).parent().hasClass('tabular'))
{
$('fieldset.'+this.id).parent().parent().hide();
}
else
{
$('fieldset.'+this.id).hide();
}
}
};
jpicTab = function(name, error) {
this.name = name;
this.error = error;
this.id = 'id_' + URLify(this.name, 253);
this.conditions = [];
};
jpicTab.prototype.getHtml = function() {
var extra = '';
if (this.error)
{
extra += 'style="color:red"';
}
return '<a href="#" '+extra+' class="jpic_tab '+this.id+'">'+this.name+'</a>';
};
jpicTab.prototype.assert = assert;
jpicTab.prototype.hide = function() {
$('a.'+this.id).hide();
};
jpicTab.prototype.show = function() {
$('a.'+this.id).show();
};
// sort of singleton
var jpicTabManager = new function() {
this.fieldsets = [];
};
jpicTabManager.addFieldset = function(fieldset) {
if (fieldset)
{
this.fieldsets[fieldset.id] = fieldset;
}
};
jpicTabManager.parseFieldsets = function() {
var first = 0;
$("fieldset.module").each(function() {
var fieldset = jpicFieldset.factory($(this));
jpicTabManager.addFieldset(fieldset);
});
$("div.inline-group").each(function() {
var fieldset = jpicFieldset.factory($(this));
fieldset.stacked = true; // will use stacked inline hide/show functions
jpicTabManager.addFieldset(fieldset);
});
};
jpicTabManager.writeTabs = function(element) {
if (element === undefined)
{
if (String(document.location).toLowerCase().indexOf('popup') < 0)
{
$(".breadcrumbs").after().html($(".breadcrumbs").html() + '<div id="jpic_tab_list"></div>');
}
else
{
$("h1").html($('h1').html() + '<div id="jpic_tab_list"></div>');
}
element = $('#jpic_tab_list');
}
var links = [];
for(var tabid in this.fieldsets)
{
var tab = this.fieldsets[tabid].getTab();
links.push(tab.getHtml());
}
element.html(links.join('|'));
for(var tabid in this.fieldsets)
{
tab = this.fieldsets[tabid].getTab();
// connect the tab
$('a.' + tab.id).click(function(e) {
e.preventDefault();
clslist = $(this).attr('class').split(' ');
for (var i in clslist)
{
if (clslist[i].substr(0, 3) == 'id_')
{
break;
}
}
jpicTabManager.select(clslist[i]);
});
}
};
jpicTabManager.select = function(tabid) {
for(var curtabid in this.fieldsets)
{
if (curtabid == tabid)
{
this.fieldsets[curtabid].show();
$('a.'+tabid).addClass('selected');
}
else
{
this.fieldsets[curtabid].hide();
$('a.'+tabid).removeClass('selected');
}
}
};
// }}}
// {{{ condition objects
var jpicFieldValueNotLike = function(field, value) {
this.field = field;
this.value = value;
this.verb = 'like';
this.verbose = this.field.name + ' ' + this.verb + ' ' + this.value;
};
jpicFieldValueNotLike.prototype.assert = function() {
var test = new RegExp(this.value, 'gi');
if (test.test(this.field.getValue()))
{
return false;
}
else
{
return true;
}
};
var jpicFieldValueLike = function(field, value) {
this.field = field;
this.value = value;
this.verb = 'like';
this.verbose = this.field.name + ' ' + this.verb + ' ' + this.value;
};
jpicFieldValueLike.prototype.assert = function() {
var test = new RegExp(this.value, 'gi');
if (test.test(this.field.getValue()))
{
return true;
}
else
{
return false;
}
};
var jpicFieldValueNotEqual = function(field, value) {
this.field = field;
this.value = value;
this.verb = 'not equal';
this.verbose = this.field.name + ' ' + this.verb + ' ' + this.value;
};
jpicFieldValueNotEqual.prototype.assert = function() {
if (this.field.getValue() == this.value)
{
return false;
}
else
{
return true;
}
};
// i should figure how to prototype the constructor when refactoring (later)
var jpicFieldValueEqual = function(field, value) {
this.field = field;
this.value = value;
this.verb = 'equal';
};
jpicFieldValueEqual.prototype.assert = function() {
this.verbose = this.field.name + ' (with value: "'+ this.field.getValue() +'") ' + this.verb + ' "' + this.value +'"';
if (this.field.value == this.value)
{
return true;
}
else
{
return false;
}
};
var jpicFieldValueEqualOrUnset = function(field, value) {
this.field = field;
this.value = value;
this.verb = 'unset or equal to';
this.verbose = this.field.name + ' ' + this.verb + ' ' + this.value;
};
jpicFieldValueEqualOrUnset.prototype.assert = function() {
var value = this.field.getValue();
if (!value)
{
return true;
}
if (value == this.value)
{
return true;
}
else
{
return false;
}
};
// }}}
// {{{ ui
ui = new function() {
this.items = [];
jpicTabManager.parseFieldsets();
jpicTabManager.writeTabs();
for(tabid in jpicTabManager.fieldsets)
{
fieldset = jpicTabManager.fieldsets[tabid];
this.items[fieldset.name] = fieldset.getTab();
}
};
ui.update = function() {
for(name in this.items)
{
var item = this.items[name];
var assert = item.assert();
if (assert)
{
item.show();
}
else
{
item.hide();
}
}
};
ui.addItem = function(item) {
this.items[item.name] = item;
};
ui.getItem = function(itemName) {
if (this.items[itemName] === undefined)
{
console.error(this);
}
return this.items[itemName];
};
ui.addItem = function(item) {
this.items[item.name] = item;
};
ui.getOrCreateField = function(fieldName) {
if (this.items[fieldName] === undefined)
{
var field = new jpicField(fieldName);
this.addItem(field);
}
return this.getItem(fieldName);
};
ui.addConstraint = function(slaves, master, masterValues, conditionFunction) {
if(!is_array(slaves))
{
slaves = [slaves,];
}
if(!is_array(masterValues))
{
masterValues = [masterValues,];
}
for (var i=0; i<slaves.length; i++)
{
for (var j=0; j<masterValues.length; j++)
{
if (conditionFunction === undefined)
{
conditionFunction = jpicFieldValueEqualOrUnset;
}
var slave = this.getOrCreateField(slaves[i]);
var masterField = this.getOrCreateField(master);
var condition = new conditionFunction(masterField, masterValues[j]);
slave.conditions.push(condition);
}
}
};
// }}}
/*members assert, conditions, field, getValue, hide, id, items, length,
name, prototype, show, update, val, value
*/
|
More like this
- Django Collapsed Stacked Inlines by applecat 1 year, 9 months ago
- Django Collapsed Stacked Inlines by mkarajohn 3 years, 10 months ago
- Dynamically adding forms to a formset. OOP version. by halfnibble 9 years, 6 months ago
- Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 11 years, 7 months ago
- Django admin inline ordering - javascript only implementation by ojhilt 12 years ago
Comments
That awesome, go and release it as separate app!
#
Supporting model mutation in the admin app was coded on a hurry (it's an ugly hack). I hope to create a document-based contrib.admin-like application later. Such a project will eventually be done correctly if other development providers make money with that snippet as is - i assume.
License: WTFPL
#
Update: Updated example. Fugbix: totally hidden rows would not be shown if it contains a field shown for the first time. Minor performance optimisation.
Supports yesterday's trunk:
commit de7fc5ba66931a1bb9cc21effa46d329e3c23152 Author: jbronn jbronn@bcc190cf-cafb-0310-a4f2-bffc1f526a37 Date: Mon Mar 9 19:22:24 2009 +0000
#
Update:
Working on a function to move fields around. You need that when dealing with stackedInlines of related models from other apps, because that won't work with ModelAdmin.fieldsets.
#
@munhitsu: here's the separate app https://github.com/yourlabs/django-dynamic-fields
#
https://github.com/yourlabs/facond
#
Please login first before commenting.