root / src / pithos / content / tags / tags.js @ 46:f16f6e0e5b69
History | View | Annotate | Download (5.4 kB)
1 |
// The Pithos File Manager Firefox Extension is funded by GRNET S.A.
|
---|---|
2 |
// (http://www.grnet.gr)
|
3 |
//
|
4 |
// Copyright (c) 2009, Christos KK Loverdos, Vassilios Karakoidas.
|
5 |
// All rights reserved.
|
6 |
//
|
7 |
// Redistribution and use in source and binary forms, with or without
|
8 |
// modification, are permitted provided that the following conditions are
|
9 |
// met:
|
10 |
//
|
11 |
// - Redistributions of source code must retain the above copyright
|
12 |
// notice, this list of conditions and the following disclaimer.
|
13 |
// - Redistributions in binary form must reproduce the above
|
14 |
// copyright notice, this list of conditions and the following
|
15 |
// disclaimer in the documentation and/or other materials provided
|
16 |
// with the distribution.
|
17 |
// - Neither the name of GRNET S.A. nor the names of its contributors
|
18 |
// may be used to endorse or promote products derived from this
|
19 |
// software without specific prior written permission.
|
20 |
//
|
21 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25 |
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
27 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
28 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
29 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
30 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
31 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32 |
|
33 |
connection_obj = null;
|
34 |
mimes = null;
|
35 |
cache = null;
|
36 |
vfs = null;
|
37 |
$mb = null; |
38 |
uri = null;
|
39 |
filetags = null;
|
40 |
availabletags = null;
|
41 |
filename = null;
|
42 |
uiobj = null;
|
43 |
prompts = null;
|
44 |
|
45 |
function init() { |
46 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
47 |
// Use the arguments passed to us by the caller
|
48 |
connection_obj = new ConnectionObject();
|
49 |
mimes = new Mimes();
|
50 |
cache = new FileCache();
|
51 |
vfs = new VFS();
|
52 |
$mb = jQuery.noConflict();
|
53 |
// Store the available tags for the current user. This should be a simple array
|
54 |
availabletags = connection_obj.user_homepage().tags_list(); |
55 |
|
56 |
prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
|
57 |
// Get the file uri as a parameter
|
58 |
uri = window.arguments[0]; |
59 |
|
60 |
buildFileTags(); |
61 |
|
62 |
$mb('caption').attr({ |
63 |
label: 'Modify tags of '+filename |
64 |
}); |
65 |
|
66 |
uiobj = new buildUI();
|
67 |
|
68 |
uiobj.draw(); |
69 |
} |
70 |
|
71 |
function okDone() { |
72 |
// Commit the changes
|
73 |
var tagObject = new Object(); |
74 |
tagObject.tags = filetags; |
75 |
|
76 |
var resp = connection_obj.rest_request_post(uri,'?update=',jsonToString(tagObject)); |
77 |
if(connection_obj.success(resp.status)) {
|
78 |
promptAlert('Tags update succesfully');
|
79 |
} |
80 |
else {
|
81 |
promptAlert('Unable to update tags for the file');
|
82 |
} |
83 |
window.close(); |
84 |
} |
85 |
|
86 |
function Cancel() { |
87 |
window.close(); |
88 |
} |
89 |
function buildFileTags() { |
90 |
var metadata = vfs.getResourceMetadata(uri);
|
91 |
|
92 |
filetags = metadata.tags; |
93 |
filename = metadata.name; |
94 |
} |
95 |
|
96 |
function buildUI() { |
97 |
this.refreshUI = function() { |
98 |
$mb('listitem','#tagList').remove(); |
99 |
$mb('menuitem','#popupmenu').remove(); |
100 |
this.draw();
|
101 |
} |
102 |
this.draw = function() { |
103 |
|
104 |
for each(tag in filetags) { |
105 |
|
106 |
var listitem = document.createElement('listitem'); |
107 |
$mb('#tagList').append(listitem); |
108 |
$mb(listitem).attr({
|
109 |
label : tag
|
110 |
}); |
111 |
} |
112 |
var firstUnusedTagIndex = null; |
113 |
for each(tag in availabletags) { |
114 |
// Is the particular tag already in use?
|
115 |
if(filetags.indexOf(tag)>=0) { |
116 |
continue;
|
117 |
} |
118 |
if(firstUnusedTagIndex == null) { |
119 |
firstUnusedTagIndex = availabletags.indexOf(tag); |
120 |
} |
121 |
var menuitem = document.createElement('menuitem'); |
122 |
$mb('#popupmenu').append(menuitem); |
123 |
$mb(menuitem).attr({
|
124 |
label: tag
|
125 |
}); |
126 |
} |
127 |
|
128 |
$mb('#menulistid').attr({ label: availabletags[firstUnusedTagIndex]}); |
129 |
|
130 |
|
131 |
} |
132 |
} |
133 |
|
134 |
function addTagToFile(event) { |
135 |
var addTagValue = $mb('#menulistid').attr('label'); |
136 |
if(filetags.indexOf(addTagValue) == -1) { |
137 |
filetags.push(addTagValue); |
138 |
uiobj.refreshUI(); |
139 |
} |
140 |
} |
141 |
|
142 |
function createNewTag() { |
143 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
144 |
|
145 |
input = {value: ""}; |
146 |
check = {value: false}; |
147 |
|
148 |
var results = prompts.prompt(window, 'Create new tag', 'Name of tag:',input,null,check); |
149 |
if (results) {
|
150 |
if(availabletags.indexOf(input.value) == -1) { |
151 |
availabletags.push(input.value); |
152 |
uiobj.refreshUI(); |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
function removeSelectedTag(event) { |
158 |
var addTagValue = $mb('#tagList'); |
159 |
var index = addTagValue[0].currentIndex; |
160 |
if(index>=0) { |
161 |
filetags.splice(index,1);
|
162 |
uiobj.refreshUI(); |
163 |
|
164 |
} |
165 |
} |
166 |
|
167 |
/* Alert functions */
|
168 |
|
169 |
function promptBox(message) { |
170 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
171 |
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService); |
172 |
var result = prompts.confirm(window, "Confirm action", message); |
173 |
return(result);
|
174 |
} |
175 |
|
176 |
function promptAlert(message) { |
177 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
178 |
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] |
179 |
.getService(Components.interfaces.nsIPromptService); |
180 |
prompts.alert(window, "Information", message);
|
181 |
} |