Added basic docs for entities and attributes.
[philo.git] / media / admin / js / TagCreation.js
1 var tagCreation = window.tagCreation;
2
3 (function($) {
4         tagCreation = {
5                 'cache': {},
6                 'addTagFromSlug': function(triggeringLink) {
7                         var id = triggeringLink.id.replace(/^ajax_add_/, '') + '_input';
8                         var slug = document.getElementById(id).value;
9         
10                         var name = slug.split(' ');
11                         for(var i=0;i<name.length;i++) {
12                                 name[i] = name[i].substr(0,1).toUpperCase() + name[i].substr(1);
13                         }
14                         name = name.join(' ');
15                         slug = name.toLowerCase().replace(/ /g, '-').replace(/[^\w-]/g, '');
16         
17                         var href = triggeringLink.href;
18                         var data = {
19                                 'name': name,
20                                 'slug': slug
21                         };
22                         $.post(href, data, function(data){
23                                 newId = html_unescape(data.pk);
24                                 newRepr = html_unescape(data.unicode);
25                                 var toId = id.replace(/_input$/, '_to');
26                                 elem = document.getElementById(toId);
27                                 var o = new Option(newRepr, newId);
28                                 SelectBox.add_to_cache(toId, o);
29                                 SelectBox.redisplay(toId);
30                         }, "json")
31                 },
32                 'init': function(id) {
33                         tagCreation.cache[id] = {}
34                         var input = tagCreation.cache[id].input = document.getElementById(id + '_input');
35                         var select = tagCreation.cache[id].select = document.getElementById(id + '_from');
36                         var addLinkTemplate = document.getElementById('add_' + input.id.replace(/_input$/, '')).cloneNode(true);
37                         var addLink = tagCreation.cache[id].addLink = document.createElement('A');
38                         addLink.id = 'ajax_add_' + id;
39                         addLink.className = addLinkTemplate.className;
40                         addLink.href = addLinkTemplate.href;
41                         addLink.appendChild($(addLinkTemplate).children()[0].cloneNode(false));
42                         addLink.innerHTML += " <span style='vertical-align:text-top;'>Add this tag</span>"
43                         addLink.style.marginLeft = "20px";
44                         addLink.style.display = "block";
45                         addLink.style.backgroundPosition = "10px 5px";
46                         addLink.style.width = "120px";
47                         $(input).after(addLink);
48                         if (window.grappelli) {
49                                 addLink.parentNode.style.backgroundPosition = "6px 8px";
50                         } else {
51                                 addLink.style.marginTop = "5px";
52                         }
53                         tagCreation.toggleButton(id);
54                         addEvent(input, 'keyup', function() {
55                                 tagCreation.toggleButton(id);
56                         })
57                         addEvent(addLink, 'click', function(e) {
58                                 e.preventDefault();
59                                 tagCreation.addTagFromSlug(addLink);
60                         })
61                 },
62                 'toggleButton': function(id) {
63                         var addLink = tagCreation.cache[id].addLink;
64                         var select = $(tagCreation.cache[id].select);
65                         if (select[0].options.length == 0) {
66                                 if (addLink.style.display == 'none') {
67                                         addLink.style.display = 'block';
68                                         select.height(select.height() - $(addLink).outerHeight(false))
69                                 }
70                         } else {
71                                 if (addLink.style.display == 'block') {
72                                         select[0].style.height = null;
73                                         addLink.style.display = 'none';
74                                 }
75                         }
76                 }
77         }
78 }(django.jQuery))