Permalink
Browse files

Added media package.

Media package includes a javascript file to "Add another" block for inlined
forms. The script works for the admin, and can be configured for front-end use
too.
Localisations for Hungarian are provided as well.

Widgets:
* added widget for JQuery/FancyEdit based WYSIWYG textarea editing.
  • Loading branch information...
1 parent 9e21732 commit ceec3179ee0bb4db15a7dbc65f301f4675c9a9e5 @nagyv committed Nov 9, 2009
View
No changes.
@@ -0,0 +1,113 @@
+/**
+ * Returns the get values received by this script
+ *
+ * The values can containt alphanumeric characters, dots and commas.
+ * A semicolon is used to separate several values, they will be returned as
+ * distinct values for this call (aka elements of an array).
+ *
+ * example:
+ * <script src="myscript.js?getvar=hello;bello"></script>
+ * return an array of Array('hello', 'bello')
+ */
+function script_get_values(varname){
+ var gets = new Array();
+ try {
+ $('script[src*="custom-forms.js"]').each(function(i){
+ gets[i] = $(this).attr("src").match(new RegExp("\\?.*" + varname + "=([\\w\\-,;.]*)"))[1];
+ });
+ } catch (e) {
+ return false;
+ }
+ // flatten the array
+ gets = gets.join(";");
+ return gets.split(";");
+}
+
+/**
+ * Adds an "Add another" link after the specified fieldsets
+ *
+ * Based on http://www.djangosnippets.org/snippets/1594/ by MasonM
+ */
+var AddRemoveFields = {
+ html_template : '<ul class="ctrlHolder">'+
+ '<li><a class="add" href="#" onclick="return AddRemoveFields.add_inline_form(\'{{prefix}}\')">' + gettext('Add another') + '</a></li>'+
+ '</ul>',
+ init: function() {
+ var base_ids = script_get_values('fieldsets');
+ for(i in base_ids) {
+ AddRemoveFields.init_single(base_ids[i]);
+ }
+ },
+ init_single: function(base_id) {
+ $('#id_' + base_id + "-TOTAL_FORMS").parents('fieldset').append(
+ AddRemoveFields.html_template.replace("{{prefix}}", base_id)
+ );
+ },
+ increment_form_ids: function(el, to, name) {
+ var from = to-1
+ $('[id*="' + name + '"]', el).each(function(i){
+ var old_name = $(this).attr('name')
+ if (old_name != undefined) {$(this).attr('name', old_name.replace(from, to))}
+ var old_id = $(this).attr('id')
+ if (old_id != undefined) {$(this).attr('id', old_id.replace(from, to))}
+ try {
+ $(this).val('')
+ }
+ catch(e){
+
+ }
+ })
+ return el;
+ },
+ add_inline_form: function(name) {
+ var parent = $('#id_'+name+'-0-id').parents('fieldset')[0]
+ var last = $(parent).children()[$(parent).children().length-2]
+ var count = $('input#id_'+name+'-TOTAL_FORMS').val()
+ var copy = AddRemoveFields.increment_form_ids($(last).clone(true), count, name)
+ $(last).after(copy)
+ $('input#id_'+name+'-TOTAL_FORMS').val(count+1)
+ return false;
+ },
+};
+
+/**
+ * Disables all the elements in a fieldset except the first checkbox, then
+ * adds a toggle functionality to that checkbox.
+ *
+ * The fieldset is identified by the checkbox's ID. The script is written to work
+ * with uniForm forms.
+ *
+ * @author nagyv
+ */
+function CreateSwitchingCheckbox() {
+ var billing = $("#id_bill-add_billing");
+ if (billing) {
+ billing.parent().parent().find('input').attr("disabled", "disabled");
+ billing.parent().parent().find('textarea').attr("disabled", "disabled");
+ billing.parent().parent().find('select').attr("disabled", "disabled");
+ $("#id_bill-add_billing").attr("disabled", false);
+ };
+ $("#id_bill-add_billing").click(function(){
+ if ( $("#id_bill-add_billing").attr('checked') ) {
+ billing.parent().parent().find('input').attr("disabled", false);
+ billing.parent().parent().find('textarea').attr("disabled", false);
+ billing.parent().parent().find('select').attr("disabled", false);
+ }
+ else {
+ billing.parent().parent().find('input').attr("disabled", "disabled");
+ billing.parent().parent().find('textarea').attr("disabled", "disabled");
+ billing.parent().parent().find('select').attr("disabled", "disabled");
+ $("#id_bill-add_billing").attr("disabled", false);
+ };
+ });
+};
+
+$(document).ready(function(){
+ AddRemoveFields.init();
+ if ($("#id_project-deadline").length == 1) {
+ $("#id_project-deadline").datepicker({
+ dateFormat: 'yy-mm-dd'
+ });
+ };
+ CreateSwitchingCheckbox();
+});
Binary file not shown.
@@ -0,0 +1,23 @@
+# Hungarian translation of Javascript files.
+# Copyright (C) 2009 THE Javascript files'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the Javascript files package.
+# nagyv <viktor@coosci.com>, 2009.
+# , fuzzy
+#
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: Javascript files 1.0\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2009-11-09 12:36+0100\n"
+"PO-Revision-Date: 2009-11-09 12:42+0100\n"
+"Last-Translator: nagyv <viktor@coosci.com>\n"
+"Language-Team: Hungarian <hu@gnome.hu>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit"
+
+#: static/media/js/custom-forms.js:33
+msgid "Add another"
+msgstr "Új hozzáadása"
+
@@ -0,0 +1 @@
+from widget import *
View
@@ -0,0 +1,8 @@
+from django import forms
+from django.conf import settings
+
+class GUITextAreaWidget(forms.Textarea):
+ class Media:
+ js = ("js/jquery.wymeditor.min.js",
+ "js/fancyedit.js")
+

0 comments on commit ceec317

Please sign in to comment.