PageRenderTime 21ms CodeModel.GetById 4ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/admin/media/js/actions.js

https://code.google.com/p/mango-py/
JavaScript | 139 lines | 136 code | 0 blank | 3 comment | 23 complexity | 0f1d7ea3b04a4be897f38c02ba584f8c MD5 | raw file
  1(function($) {
  2	$.fn.actions = function(opts) {
  3		var options = $.extend({}, $.fn.actions.defaults, opts);
  4		var actionCheckboxes = $(this);
  5		var list_editable_changed = false;
  6		checker = function(checked) {
  7			if (checked) {
  8				showQuestion();
  9			} else {
 10				reset();
 11			}
 12			$(actionCheckboxes).attr("checked", checked)
 13				.parent().parent().toggleClass(options.selectedClass, checked);
 14		}
 15		updateCounter = function() {
 16			var sel = $(actionCheckboxes).filter(":checked").length;
 17			$(options.counterContainer).html(interpolate(
 18			ngettext('%(sel)s of %(cnt)s selected', '%(sel)s of %(cnt)s selected', sel), {
 19				sel: sel,
 20				cnt: _actions_icnt
 21			}, true));
 22			$(options.allToggle).attr("checked", function() {
 23				if (sel == actionCheckboxes.length) {
 24					value = true;
 25					showQuestion();
 26				} else {
 27					value = false;
 28					clearAcross();
 29				}
 30				return value;
 31			});
 32		}
 33		showQuestion = function() {
 34			$(options.acrossClears).hide();
 35			$(options.acrossQuestions).show();
 36			$(options.allContainer).hide();
 37		}
 38		showClear = function() {
 39			$(options.acrossClears).show();
 40			$(options.acrossQuestions).hide();
 41			$(options.actionContainer).toggleClass(options.selectedClass);
 42			$(options.allContainer).show();
 43			$(options.counterContainer).hide();
 44		}
 45		reset = function() {
 46			$(options.acrossClears).hide();
 47			$(options.acrossQuestions).hide();
 48			$(options.allContainer).hide();
 49			$(options.counterContainer).show();
 50		}
 51		clearAcross = function() {
 52			reset();
 53			$(options.acrossInput).val(0);
 54			$(options.actionContainer).removeClass(options.selectedClass);
 55		}
 56		// Show counter by default
 57		$(options.counterContainer).show();
 58		// Check state of checkboxes and reinit state if needed
 59		$(this).filter(":checked").each(function(i) {
 60			$(this).parent().parent().toggleClass(options.selectedClass);
 61			updateCounter();
 62			if ($(options.acrossInput).val() == 1) {
 63				showClear();
 64			}
 65		});
 66		$(options.allToggle).show().click(function() {
 67			checker($(this).attr("checked"));
 68			updateCounter();
 69		});
 70		$("div.actions span.question a").click(function(event) {
 71			event.preventDefault();
 72			$(options.acrossInput).val(1);
 73			showClear();
 74		});
 75		$("div.actions span.clear a").click(function(event) {
 76			event.preventDefault();
 77			$(options.allToggle).attr("checked", false);
 78			clearAcross();
 79			checker(0);
 80			updateCounter();
 81		});
 82		lastChecked = null;
 83		$(actionCheckboxes).click(function(event) {
 84			if (!event) { var event = window.event; }
 85			var target = event.target ? event.target : event.srcElement;
 86			if (lastChecked && $.data(lastChecked) != $.data(target) && event.shiftKey == true) {
 87				var inrange = false;
 88				$(lastChecked).attr("checked", target.checked)
 89					.parent().parent().toggleClass(options.selectedClass, target.checked);
 90				$(actionCheckboxes).each(function() {
 91					if ($.data(this) == $.data(lastChecked) || $.data(this) == $.data(target)) {
 92						inrange = (inrange) ? false : true;
 93					}
 94					if (inrange) {
 95						$(this).attr("checked", target.checked)
 96							.parent().parent().toggleClass(options.selectedClass, target.checked);
 97					}
 98				});
 99			}
100			$(target).parent().parent().toggleClass(options.selectedClass, target.checked);
101			lastChecked = target;
102			updateCounter();
103		});
104		$('form#changelist-form table#result_list tr').find('td:gt(0) :input').change(function() {
105			list_editable_changed = true;
106		});
107		$('form#changelist-form button[name="index"]').click(function(event) {
108			if (list_editable_changed) {
109				return confirm(gettext("You have unsaved changes on individual editable fields. If you run an action, your unsaved changes will be lost."));
110			}
111		});
112		$('form#changelist-form input[name="_save"]').click(function(event) {
113			var action_changed = false;
114			$('div.actions select option:selected').each(function() {
115				if ($(this).val()) {
116					action_changed = true;
117				}
118			});
119			if (action_changed) {
120				if (list_editable_changed) {
121					return confirm(gettext("You have selected an action, but you haven't saved your changes to individual fields yet. Please click OK to save. You'll need to re-run the action."));
122				} else {
123					return confirm(gettext("You have selected an action, and you haven't made any changes on individual fields. You're probably looking for the Go button rather than the Save button."));
124				}
125			}
126		});
127	}
128	/* Setup plugin defaults */
129	$.fn.actions.defaults = {
130		actionContainer: "div.actions",
131		counterContainer: "span.action-counter",
132		allContainer: "div.actions span.all",
133		acrossInput: "div.actions input.select-across",
134		acrossQuestions: "div.actions span.question",
135		acrossClears: "div.actions span.clear",
136		allToggle: "#action-toggle",
137		selectedClass: "selected"
138	}
139})(django.jQuery);