Login

XFN Assist

Author:
Turophile
Posted:
December 28, 2008
Language:
JavaScript
Version:
Not specified
Score:
0 (after 0 ratings)

This code assumes your XFN field is called 'rel' in models. See the last call (addLoadEvent down the bottom) for the two lines requiring modification; otherwise, aside from the Django-styling involved, it can be used anywhere.

The accompanying model.py and admin.py information can be found here: http://www.djangosnippets.org/snippets/1265/.

I'm no master programmer, so feedback/comments/criticism is more than welcome.

EDIT: Fixed my esoteric dev. comments and removed references to spawning more overlords.

  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
/*
Thank Simon Willison for addLoadEvent(func):
See: http://simonwillison.net/2004/May/26/addLoadEvent/
*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/*
This function is like many others out there...
I need to do more work and create a centralised library.
I don't like the way Prototype does it, so here's my NodeFactory.
Must add, a friend gave me a hand fixing it; 
I was using a series of tuples in an array, not dictionaries.
*/

function NodeFactory(dict, dd, doc) {
    var output; var i;
    if (typeof dict === 'string') {
        output = doc.createTextNode(dict);
    } else if (typeof dict === 'object') {
        output = doc.createElement(dict.name);
        if (dict.attribute) {
            for (i in dict.attribute) {
                if (dict.attribute.hasOwnProperty(i)) {
                    output.setAttribute(i, dict.attribute[i]);
                }
            }
        }
        if (dict.children) {
            for (i = 0; i < dict.children.length; i++) {
                output.appendChild(this.NodeFactory(dict.children[i], dd, doc));
            }
        }
        if (dict.id) {
            dd[dict.id] = output;
        }
    }
    return output;
}


/* This is a REALLY ugly sort of hack. */
function GenXFN(name)
{
  /* If it toggles 'me' */
  if(name == "me")
  {
    /* and 'me' is checked */
    if(XFNDD['me'].checked == 1)
    {
      /* clear everything */
      for(i in XFNDD)
      {
        XFNDD[i].checked = 0;
      }
      /* re-check 'me' - faster than an if in the for */
      XFNDD['me'].checked = 1;
    }
  }
  else
  {
  /* otherwise if there is a name value set
     clear 'me' */
      if(name) { XFNDD['me'].checked = 0; }
  }
    XFN_STRING = ""
    for(i in XFNDD)
    {
      if(XFNDD[i].checked == 1)
      {
        XFN_STRING += i + " "
      }
    }
/* 
This goes through every value in the order it was added then appends it.
To modify the order you move the NodeFactory call order
(found near the bottom of the DrawXFN function).
It then spits out everything except the last space.
*/
    XFN_REL.value = XFN_STRING.substring(0,(XFN_STRING.length - 1));
}

/* Spawns 'li' items to save some space. */
function Spawn(name, value, label, type) {
  litem = {'name':'li',
       'children': [
        {'name':'label',
         'children': [
          {'name':'input',
           'id': value,
           'attribute': {
             'name': name,
             'class':'radiolist inline',
             'type': type,
             'value': value,
             'onclick':'GenXFN(this.value);'
           },
           'children': [
           ]},
          label,
         ]}
       ]}
  return litem;
}

function DrawXFN() {

/* Single Entities in a Django-Admin Line are fine as they're not too big */

node_me = {
  'name': 'div',
  'attribute': {
  'class': 'form-row xfn_me'
  },
  'children': [
    {'name':'div',
     'children': [
      {'name':'label',
       'children': ['My Site:']},
        {'name':'input', 'id':'me', 'attribute': {'id': 'xfn_me',
         'type':'checkbox', 'name':'me',
         'onclick':'GenXFN(this.name);'}}
      ]
    }
  ]
  }

node_met = {
  'name': 'div',
  'attribute': {
  'class': 'form-row xfn_physical'
  },
  'children': [
    {'name':'div',
     'children': [
      {'name':'label',
       'children': ['Have Met:']},
        {'name':'input', 'id':'met', 'attribute': {'id': 'xfn_physical',
         'type':'checkbox', 'name':'met',
         'onclick':'GenXFN(this.name);'}}
      ]
    }
  ]
  }

/*
These ones use space like nobody's business.  
Spawn was created to deal with population/space constraints.
*/

node_friendship = {
  'name':'div',
  'attribute': {
  'class': 'form-row xfn_friendship'
  }, 
  'children': [
    {'name':'label',
     'children': ['Friendship']},
    {'name':'ul',
     'attribute': {'id':'xfn_friendship','class':'radiolist inline',},
     'children': [
      Spawn('xfn_friendship', 'contact', ' Contact', 'radio'),
      Spawn('xfn_friendship', 'acquaintance', ' Acquaintance', 'radio'),
      Spawn('xfn_friendship', 'friend', ' Friend', 'radio'),
      Spawn('xfn_friendship', '', ' None', 'radio'),
     ]
    }
   ]
  }

node_professional = {
  'name':'div',
  'attribute': {
  'class': 'form-row xfn_professional'
  }, 
  'children': [
    {'name':'label',
     'children': ['Professional']},
    {'name':'ul',
     'attribute': {'id':'xfn_professional','class':'radiolist inline',},
     'children': [
      Spawn('xfn_professional','co-worker', ' Co-Worker', 'checkbox'),
      Spawn('xfn_professional','colleague', ' Colleague', 'checkbox'),
     ]
    }
   ]
  }

node_geographical = {
  'name':'div',
  'attribute': {
  'class': 'form-row xfn_geographical'
  }, 
  'children': [
    {'name':'label',
     'children': ['Geographical']},
    {'name':'ul',
     'attribute': {'id':'xfn_geographical','class':'radiolist inline',},
     'children': [
      Spawn('xfn_geographical','co-resident', ' Co-Resident', 'radio'),
      Spawn('xfn_geographical','neighbor', 'Neighbour', 'radio'),
      Spawn('xfn_geographical','', ' None', 'radio'),
     ]
    }
   ]
  }



node_family = {
  'name':'div',
  'attribute': {
  'class': 'form-row xfn_family'
  }, 
  'children': [
    {'name':'label',
     'children': ['Family']},
    {'name':'ul',
     'attribute': {'id':'xfn_family','class':'radiolist inline',},
     'children': [
      Spawn('xfn_family','child', ' Child', 'radio'),
      Spawn('xfn_family','parent', ' Parent', 'radio'),
      Spawn('xfn_family','sibling', ' Sibling', 'radio'),
      Spawn('xfn_family','spouse', ' Spouse', 'radio'),
      Spawn('xfn_family','kin', ' Kin', 'radio'),
      Spawn('xfn_family','', ' None', 'radio'),
     ]
    }
   ]
  }



node_romantic = {
  'name':'div',
  'attribute': {
  'class': 'form-row xfn_romantic'
  }, 
  'children': [
    {'name':'label',
     'children': ['Romantic']},
    {'name':'ul',
     'attribute': {'id':'xfn_romantic','class':'radiolist inline',},
     'children': [
      Spawn('xfn_romantic','muse', ' Muse', 'checkbox'),
      Spawn('xfn_romantic','crush', ' Crush', 'checkbox'),
      Spawn('xfn_romantic','date', ' Date', 'checkbox'),
      Spawn('xfn_romantic','sweetheart', ' Sweetheart', 'checkbox'),
     ]
    }
   ]
  }

  /* Could probably wrap this in a function to save some space */

  XFN_DIV.appendChild(NodeFactory(node_me, XFNDD, document))
  XFN_DIV.appendChild(NodeFactory(node_friendship, XFNDD, document))
  XFN_DIV.appendChild(NodeFactory(node_met, XFNDD, document))
  XFN_DIV.appendChild(NodeFactory(node_professional, XFNDD, document))
  XFN_DIV.appendChild(NodeFactory(node_geographical, XFNDD, document))
  XFN_DIV.appendChild(NodeFactory(node_family, XFNDD, document))
  XFN_DIV.appendChild(NodeFactory(node_romantic, XFNDD, document))

  /* Load the Values from the String before the user can mess with it. */
  PreloadXFN(XFN_REL);
}

function PreloadXFN(form_value)
{
  val = form_value.value;
  values = val.split(" ");
  for(i in values)
  {
    XFNDD[values[i]].checked = 1;
  }
  GenXFN();
}

/*
The entire process is hinged on this start up.
*/

addLoadEvent(function() 
{
XFNDD = {};
XFN = {};

XFN_DIV = document.getElementsByTagName('fieldset')[2];
XFN_REL = document.getElementById('id_rel');

/*
You can also mask either with .style.visibility = "hidden";
or .style.display = "none"; I suggest the latter, but both work fine.
Personally I don't hide it, because I have form validation too and
any click will change it.

OPTIONALLY: Set onkeypress to check it.  I didn't bother in this code.
*/

DrawXFN();
});

More like this

  1. Django Collapsed Stacked Inlines by applecat 1 year, 9 months ago
  2. Django Collapsed Stacked Inlines by mkarajohn 3 years, 10 months ago
  3. Dynamically adding forms to a formset. OOP version. by halfnibble 9 years, 6 months ago
  4. Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 11 years, 7 months ago
  5. Django admin inline ordering - javascript only implementation by ojhilt 11 years, 11 months ago

Comments

Please login first before commenting.