summaryrefslogtreecommitdiffstats
path: root/tabellio/searchform/js_macros.pt
blob: d60fca1a60f547640b5e3eca9dd49b120999529f (plain)
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
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      xmlns:i18n="http://xml.zope.org/namespaces/i18n"
      i18n:domain="plone">
<body>

<script tppe="text/javascript" metal:define-macro="labelboxes">
function update_input(input) {
  var s = '';
  jq(input).nextAll('span.labelbox').each(function() {
      s = s + ' ' + jq(this).attr('class').split(' ')[1];
    });
  jq(input).attr('value', s);
}

function create_labelbox(input_box, id, value)
{
  labelbox = $('<span class="labelbox ' + id + '">' + value + '<' + '/span>');
  $(labelbox).click(function() {
    $(this).remove();
    update_input(jq(input_box).prevAll('input')[0]);
  });
  return labelbox;
}

function autocomplete_ready(event, data, formatted) {
    (function($) {
        var input_box = $(event.target);
        // XXX: don't insert item if it's already present
        labelbox = create_labelbox(input_box, data[1], data[0]);
        $(input_box).before(labelbox);
        update_input(jq(input_box).prevAll('input')[0]);
        $(input_box).attr('value', '');
    }(jQuery));
}

function sync_elements(index, elem, url)
{
  $(elem).parent().find('input').wrapAll('<div class="labelboxes">');
  $('div.labelboxes').click(function() { $(this).find('input').focus(); });
  hidden = $(elem).prev();
  values = $(hidden).attr('value').trim().split(' ');
  for (i=0; i < values.length; i++) {
    if (values[i]) {
      labelbox = create_labelbox(elem, values[i], '...');
      $(hidden).after(labelbox);
      $(labelbox).load(url + '?id=' + values[i]);
    }
  }
}

function sync_authors(index, elem) { sync_elements(index, elem, PORTAL_URL+'/listAuthors'); }
function sync_polgroups(index, elem) { sync_elements(index, elem, PORTAL_URL+'/listPolgroups'); }
function sync_topics(index, elem) { sync_elements(index, elem, PORTAL_URL+'/listTopics'); }

if(typeof(String.prototype.trim) === "undefined") {
  String.prototype.trim = function()
  {
    return String(this).replace(/^\s+|\s+$/g, '');
  };
}

(function($) {
  $().ready(function() {
            $('.author-autocomplete').each(sync_authors).autocomplete(PORTAL_URL+'/listAuthors', {
                'cacheLength': 0, 'matchContains': true, 'scroll': true, 'max': 30}).result(autocomplete_ready);
            $('.polgroup-autocomplete').each(sync_polgroups).autocomplete(PORTAL_URL+'/listPolgroups', {
            }).result(autocomplete_ready);
            $('.topic-autocomplete').each(sync_topics);
            $('.topic-autocomplete').each(function(index, elem) {
              $(elem).hide();
              var add_topic = $('<a class="add-topic-link" href="#"' + '>Ajouter une matière<' + '/a>');
              add_topic.prepOverlay({
                      subtype: 'ajax',
                      filter: '#content>*',
                      urlmatch: '.*',
                      urlreplace: '/thesaurus/plone-popup/'
              });
              $(add_topic).insertAfter(elem);
            });
  });
})(jQuery);
</script>

<script type="text/javascript" metal:define-macro="livereload">
function load_results(elem)
{
  var base = $(elem).parents('.results-var');
  var base_spec = ' #' + $(elem).parents('.results-var').parent()[0].id + ' .results-var > div';
  jQuery.get($(elem).attr('href'), null, function(data, textStatus, jqXHR) {
    if (textStatus == 'notmodified')
      return;
    if (textStatus == 'success') {
      $(base).html($(data).find(base_spec));
      $(base).find('.listingBar a').click(function() { return load_results($(this)); });
    }
  });
  return false;
}

(function($) {
  $().ready(function() {
    $('.results-var .listingBar a').click(function() { return load_results($(this)); });
  });
})(jQuery);
</script>

<script type="text/javascript" metal:define-macro="sorton">
(function($) {
  $().ready(function() {
    $('#formfield-document-widgets-sort_on').hide();
    $('#formfield-dossier-widgets-sort_on').hide();
    $('#formfield-question-widgets-sort_on').hide();
    $('#resultsbox').each(function() {
       form = $($(this).prevAll('input').attr('value')).find('form');
       field_array = $(form).find('div.field');
       sort_on_options = $(field_array[field_array.length-1]).find('option').clone();
       $('#sorton select').append(sort_on_options).change(function() {
         selected_option = $(this).val();
         $(field_array[field_array.length-1]).find('option').each(function() {
           if ($(this).attr('value') == selected_option) {
             $(this).attr('selected', 'selected');
             $(form).submit();
           } else {
             $(this).attr('selected', null);
           }
         });
       });
    });
  });
})(jQuery);

</script>

<script type="text/javascript" metal:define-macro="droppablecheckboxes">
function sync_from_checkboxes(elem, dummyinput)
{
  t = '';
  $(elem).find('span.option').each(function(idx, el) {
    if ($(el).find('input').attr('checked')) {
      if (t.length > 0) {
        t = t + ', ';
      }
      t = t + $(el).find('label span').text();
    }
  });
  $(dummyinput).attr('value', t);
}

function setup_droppedcheckboxes(index, elem)
{
  var dummyinput = $('<input class="dummyinput" type="text" readonly="readonly"/>');
  sync_from_checkboxes(elem, dummyinput);
  dummyinput.click(function() {
        $(elem).toggle().parent().toggleClass('openboxes');
  });
  $(elem).parent().find('.fieldErrorBox').after(dummyinput);
  $(elem).find('span.option').click(function() {
        sync_from_checkboxes(elem, dummyinput);
  });
  $(elem).hide();
}

(function($) {
  $().ready(function() {
    $('div.droppedcheckboxes').each(setup_droppedcheckboxes);
  });
})(jQuery);
</script>


</body>
</html>