Merge branch 'extended-cookie'
[scuttle] / templates / dynamictags.inc.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2005 - 2010 Marcus Campbell
4 http://scuttle.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ***************************************************************************/
20
21 $tagservice  =& ServiceFactory::getServiceInstance('TagService');
22 $userservice =& ServiceFactory::getServiceInstance('UserService');
23
24 $logged_on_userid = $userservice->getCurrentUserId();
25
26 $userPopularTags        =& $tagservice->getPopularTags($logged_on_userid, 25, $logged_on_userid);
27 $userAllTags            =& $tagservice->getPopularTags($logged_on_userid, 10000, $logged_on_userid);
28 $userPopularTagsCloud   =& $tagservice->tagCloud($userPopularTags, 5, 90, 175);
29 $userPopularTagsCount   = count($userPopularTags);
30
31 if ($userPopularTagsCount > 0) {
32 ?>
33
34 <script type="text/javascript">
35 Array.prototype.contains = function (ele) {
36     for (var i = 0; i < this.length; i++) {
37         if (this[i] == ele) {
38             return true;
39         }
40     }
41     return false;
42 };
43
44 Array.prototype.remove = function (ele) {
45     var arr = new Array();
46     var count = 0;
47     for (var i = 0; i < this.length; i++) {
48         if (this[i] != ele) {
49             arr[count] = this[i];
50             count++;
51         }
52     }
53     return arr;
54 };
55
56 function addonload(addition) {
57     var existing = window.onload;
58     window.onload = function () {
59         existing;
60         addition;
61     }
62 }
63
64 addonload(
65     function () {
66         var taglist = document.getElementById('tags');
67         var tags = taglist.value.split(', ');
68         
69         var populartags = document.getElementById('popularTags').getElementsByTagName('span');
70         
71         for (var i = 0; i < populartags.length; i++) {
72             if (tags.contains(populartags[i].innerHTML)) {
73                 populartags[i].className = 'selected';
74             }
75         }
76     }
77 );
78
79 function addTag(ele) {
80     var thisTag = ele.innerHTML;
81     var taglist = document.getElementById('tags');
82     var tags = taglist.value.split(', ');
83     
84     // If tag is already listed, remove it
85     if (tags.contains(thisTag)) {
86         tags = tags.remove(thisTag);
87         ele.className = 'unselected';
88         
89     // Otherwise add it
90     } else {
91         tags.splice(0, 0, thisTag);
92         ele.className = 'selected';
93     }
94     
95     taglist.value = tags.join(', ');
96     
97     document.getElementById('tags').focus();
98 }
99
100 document.write('<div class="collapsible">');
101 document.write('<h3><?php echo T_('Popular Tags'); ?><\/h3>');
102 document.write('<p id="popularTags" class="tags">');
103
104 <?php
105 $taglist = '';
106 foreach(array_keys($userPopularTagsCloud) as $key) {
107     $row =& $userPopularTagsCloud[$key];
108     $entries = T_ngettext('bookmark', 'bookmarks', $row['bCount']);
109     $taglist .= '<span title="'. $row['bCount'] .' '. $entries .'" style="font-size:'. $row['size'] .'" onclick="addTag(this)">'. filter($row['tag']) .'<\/span> ';
110 }
111 ?>
112
113 document.write('<?php echo $taglist ?>');
114 document.write('<\/p>');
115 document.write('<\/div>');
116
117 var availableTags = [
118 <?php
119
120 foreach(array_keys($userAllTags) as $key) {
121   print '"'.$userAllTags[$key]['tag'].'",'."\n";
122 }
123 ?>
124 ];
125 function split( val ) {
126   return val.split( /,\s*/ );
127 }
128 function extractLast( term ) {
129   return split( term ).pop();
130 }
131
132 $( "#tags" )
133   // don't navigate away from the field on tab when selecting an item
134   .bind( "keydown", function( event ) {
135     if ( event.keyCode === $.ui.keyCode.TAB &&
136         $( this ).data( "autocomplete" ).menu.active ) {
137       event.preventDefault();
138     }
139   })
140   .autocomplete({
141     minLength: 2,
142     delay: 50,
143     source: function( request, response ) {
144       // if the term is >2 in legth delegate back to autocomplete
145       if (extractLast ( request.term ).length < 3) {
146         response( [] );
147       } else {
148         response( $.ui.autocomplete.filter(
149           availableTags, extractLast( request.term ) ) );
150       }
151     },
152     focus: function() {
153       // prevent value inserted on focus
154       return false;
155     },
156     select: function( event, ui ) {
157       var terms = split( this.value );
158       // remove the current input
159       terms.pop();
160       // add the selected item
161       var newterm = ui.item.value;
162       terms.push( newterm );
163
164       $("#popularTags").children().each(function() {
165         if (this.innerHTML == newterm) {
166           this.className = 'selected';
167         }
168       });
169       // add placeholder to get the comma-and-space at the end
170       terms.push( "" );
171       this.value = terms.join( ", " );
172       return false;
173     }
174 });
175
176
177 </script>
178
179 <?php } ?>

Benjamin Mako Hill || Want to submit a patch?