otsdaq_utilities  v2_05_02_indev
IconEditor.js
1 var iconTable_wrapper;
2 var iconDropdown_wrapper;
3 var save_wrapper;
4 var currentSelectedValue_;
5 var megaLibrary_;
6 var changesMade_ = false;
7 var saveClicked_ = false;
8 
9 function init(){
10  megaLibrary_ = new Array();
11  makeServerRequest("");
12 }
13 
14 function finishInit(){
15 
16  iconTable_wrapper = document.getElementById("iconTable");
17  iconDropdown_wrapper = document.getElementById("iconDropdown");
18  save_wrapper = document.getElementById("save");
19  currentSelectedValue_ = "";
20  chooseIcon();
21 
22 }
23 
24 function chooseIcon(){
25  iconDropdown_wrapper.innerHTML = "";
26  var icon_list = document.createElement("select");
27  iconDropdown_wrapper.appendChild(icon_list);
28 
29  checkForChanges();
30 
31  //Dan, I added the first option to be "Select" so that the user must pick something to call onchange -Ethan
32  var iconOption = document.createElement("option");
33  icon_list.appendChild(iconOption);
34  iconOption.value="select";
35  iconOption.innerHTML="Select";
36 
37  iconOption = document.createElement("option");
38  icon_list.appendChild(iconOption);
39  iconOption.value="new";
40  iconOption.innerHTML="Create New Icon";
41  icon_list.setAttribute("onchange","iconSelected(this.value)");
42 
43  for(a = 0; a < megaLibrary_.length; a++)
44  {
45  iconOption = document.createElement("option");
46  icon_list.appendChild(iconOption);
47  iconOption.value = a;
48  iconOption.innerHTML = megaLibrary_[a][0];
49  }
50 
51 
52 }
53 
54 function checkForChanges(){
55 
56  save_wrapper.innerHTML = "";
57  var submitButton = document.createElement("button");
58  submitButton.setAttribute("onClick", "submitChangesToServer()");
59  submitButton.innerHTML = "Submit Changes to Server";
60 
61  if(!saveClicked_){
62  submitButton.disabled = "true";
63  }
64 
65  save_wrapper.appendChild(submitButton);
66 
67 }
68 
69 function iconSelected(selectedValue){
70 
71 
72  checkForChanges();
73 
74  //Dan, I added the code below -Ethan
75 
76  //If they chose the select option redirect the dropdown to the option they chose before
77  if (selectedValue=="select"){
78  var icon_list_options = document.getElementById("iconDropdown").childNodes[0].childNodes;
79  for (var opt = 0; opt < icon_list_options.length; opt++){
80  if (icon_list_options[opt].value == currentSelectedValue_){
81  icon_list_options[opt].selected = true;
82  break;
83  }
84  }
85  return;
86  }
87  //After the selected option gets redirected onchange will be called. Return so that their progress is not lost.
88  if (selectedValue == currentSelectedValue_){
89  return;
90  }
91 
92  //End of the code that I added -Ethan
93 
94  //Reset all values
95  iconTable_wrapper.innerHTML = "";
96 
97 
98  console.log("selectedIcon " + selectedValue);
99 
100  var altLabel = document.createElement("td");
101  var titleLabel = document.createElement("td");
102  var uniqueLabel = document.createElement("td");
103  var permissionNeededLabel = document.createElement("td");
104  var imageLabel = document.createElement("td");
105  var linkLabel = document.createElement("td");
106 
107  altLabel.innerHTML = "Title"; //swapped.. because the meaning over time was swapped at DesktopIcons.js
108  titleLabel.innerHTML = "Alt"; //swapped.. because the meaning over time was swapped at DesktopIcons.js
109  uniqueLabel.innerHTML = "Unique";
110  permissionNeededLabel.innerHTML = "Permission Needed";
111  imageLabel.innerHTML = "Image";
112  linkLabel.innerHTML = "Link";
113 
114  var alt = document.createElement("td");
115  var title = document.createElement("td");
116  var unique = document.createElement("td");
117  var permissionNeeded = document.createElement("td");
118  var image = document.createElement("td");
119  var link = document.createElement("td");
120 
121 
122  var alt_tr = document.createElement("tr");
123  var title_tr = document.createElement("tr");
124  var unique_tr = document.createElement("tr");
125  var permissionNeeded_tr = document.createElement("tr");
126  var image_tr = document.createElement("tr");
127  var link_tr = document.createElement("tr");
128 
129 
130  var altField = document.createElement("input");
131  var titleField = document.createElement("input");
132  var uniqueField = document.createElement("input");
133  var permissionNeededField = document.createElement("input");
134  var imageField = document.createElement("input");
135  var linkField = document.createElement("input");
136 
137  if(selectedValue != "new")
138  {
139  //console.log(selectedValue);
140  altField.value = megaLibrary_[selectedValue][0];
141  titleField.value = megaLibrary_[selectedValue][1];
142  uniqueField.value = megaLibrary_[selectedValue][2];
143  permissionNeededField.value = megaLibrary_[selectedValue][3];
144  imageField.value = megaLibrary_[selectedValue][4];
145  linkField.value = megaLibrary_[selectedValue][5];
146 
147  //console.log("new");
148  }
149 
150  altField.setAttribute("oninput", "activateSave()");
151  titleField.setAttribute("oninput", "activateSave()");
152  uniqueField.setAttribute("oninput", "activateSave()");
153  permissionNeededField.setAttribute("oninput", "activateSave()");
154  imageField.setAttribute("oninput", "activateSave()");
155  linkField.setAttribute("oninput", "activateSave()");
156 
157  alt.appendChild(altField);
158  title.appendChild(titleField);
159  unique.appendChild(uniqueField);
160  permissionNeeded.appendChild(permissionNeededField);
161  image.appendChild(imageField);
162  link.appendChild(linkField);
163 
164  alt_tr.appendChild(altLabel);
165  title_tr.appendChild(titleLabel);
166  unique_tr.appendChild(uniqueLabel);
167  permissionNeeded_tr.appendChild(permissionNeededLabel);
168  image_tr.appendChild(imageLabel);
169  link_tr.appendChild(linkLabel);
170 
171  alt_tr.appendChild(alt);
172  title_tr.appendChild(title);
173  unique_tr.appendChild(unique);
174  permissionNeeded_tr.appendChild(permissionNeeded);
175  image_tr.appendChild(image);
176  link_tr.appendChild(link);
177 
178  iconTable.appendChild(alt_tr);
179  iconTable.appendChild(title_tr);
180  iconTable.appendChild(unique_tr);
181  iconTable.appendChild(permissionNeeded_tr);
182  iconTable.appendChild(image_tr);
183  iconTable.appendChild(link_tr);
184 
185  var index;
186 
187  /*console.log("selected value?:" + selectedValue);
188  for(var it = 0; it < megaLibrary_.length; it++)
189  {
190  console.log("it " + megaLibrary_[it][0] + " " + selectedValue);
191 
192  if(megaLibrary_[it][0] == selectedValue)
193  {
194  index = it;
195  console.log("selected:" + index);
196 
197  }
198 
199  }
200  console.log("selected?:" + index);
201  */
202 
203  var deleteButton = document.createElement("button");
204  deleteButton.setAttribute("onClick", "deleteIcon(" + selectedValue + ")");
205  deleteButton.innerHTML = "Delete";
206  var submitRow = document.createElement("tr");
207 
208  var button_td = document.createElement("td");
209  button_td.appendChild(deleteButton);
210 
211  if(selectedValue == "new"){
212  var addButton = document.createElement("button");
213  addButton.setAttribute("onClick", "addIcon()");
214  addButton.innerHTML = "Add";
215  addButton.setAttribute("id", "save");
216  button_td.appendChild(addButton);
217  }else{
218  var saveButton = document.createElement("button");
219  saveButton.setAttribute("onClick", "saveIcon()");
220  saveButton.innerHTML = "Save";
221  button_td.appendChild(saveButton);
222  }
223 
224 
225  submitRow.appendChild(document.createElement("td"));
226  submitRow.appendChild(button_td);
227  /* .appendChild(document.createElement("td")
228  .appendChild(button)));*/
229  iconTable.appendChild(submitRow);
230  currentSelectedValue_ = selectedValue;
231 
232 }
233 
234 function deleteIcon(index){
235 
236  saveClicked_ = true; //overload usage so "Submit" button is enabled
237 
238  console.log(index);
239  console.log(megaLibrary_);
240 
241  megaLibrary_.splice(index, 1);
242  console.log(megaLibrary_);
243  currentSelectedValue_ = ""; // nothing currently selected now
244 
245  chooseIcon();
246  iconTable_wrapper.innerHTML = "";
247 
248 }
249 
250 function addIcon(){
251 
252  changesMade_ = true;
253  //console.log("addIcon()");
254  //console.log(megaLibrary_.length);
255 
256  var currentIcon =[document.getElementsByTagName("input")[0].value,
257  document.getElementsByTagName("input")[1].value,
258  document.getElementsByTagName("input")[2].value,
259  document.getElementsByTagName("input")[3].value,
260  document.getElementsByTagName("input")[4].value,
261  document.getElementsByTagName("input")[5].value];
262 
263  megaLibrary_.push(currentIcon);
264  //console.log(megaLibrary_.length);
265 
266  chooseIcon();
267 
268  //console.log(megaLibrary_[megaLibrary_.length-1][1]);
269 // //console.log(megaLibrary_[megaLibrary_.length][1]);
270  iconSelected(megaLibrary_.length-1);
271 
272 
273 
274 }
275 
276 function saveIcon(){
277 
278  //Called by Save()
279  if(!changesMade_)
280  {
281  alert("No changes have been made!");
282  return;
283  }
284  //console.log("saveIcon()");
285  //console.log(megaLibrary_);
286  saveClicked_ = true;
287  checkForChanges();
288  var currentIcon =[document.getElementsByTagName("input")[0].value,
289  document.getElementsByTagName("input")[1].value,
290  document.getElementsByTagName("input")[2].value,
291  document.getElementsByTagName("input")[3].value,
292  document.getElementsByTagName("input")[4].value,
293  document.getElementsByTagName("input")[5].value];
294 
295  var icon_list_options = document.getElementById("iconDropdown").childNodes[0].childNodes;
296  for (var opt = 0; opt < icon_list_options.length; opt++){
297  if (icon_list_options[opt].value == currentSelectedValue_){
298  icon_list_options[opt].selected = true;
299  break;
300  }
301  }
302  //console.log(opt);
303  megaLibrary_[opt-2] = currentIcon;
304  //console.log(megaLibrary_);
305 
306 }
307 
308 function submitChangesToServer(){
309 
310  //console.log("SubmitChangesToServer()");
311  var iconList = "iconList=";
312 
313  //console.log("Mega Library Size: " + megaLibrary_.length-1);
314  //console.log("Mega Library 0 Size: " + megaLibrary_[0].length-1);
315 
316  for(var icon = 0; icon < (megaLibrary_.length); icon++)
317  {
318  for(var details=0; details < (megaLibrary_[icon].length); details++)
319  {
320  //console.log(megaLibrary_[icon][details]);
321  iconList += (megaLibrary_[icon][details] + ",");
322  }
323  }
324  //console.log(iconList);
325 
326  iconList = iconList.substring(0, iconList.length - 1);
327 
328  console.log(iconList);
329  makeServerRequest(iconList);
330 }
331 
332 function activateSave(){
333 
334  //Called by oninput() from input fields
335  //console.log("activateSave()");
336  changesMade_ = true;
337 
338 }
339 
340 
341 function makeServerRequest(data){
342 
343  DesktopContent.XMLHttpRequest("iconEditor", data, iconEditorHandler, undefined, undefined);
344 }
345 
346 var iconEditorHandler = function(req){
347 
348  var iconArray = req.responseText.split(",");
349  console.log(iconArray);
350  var numberOfIconFields = 6;
351  megaLibrary_.length = 0;
352 
353  for(var i=0;i<(iconArray.length-1);i+=numberOfIconFields) //add icons
354  megaLibrary_.push([iconArray[i], iconArray[i+1], iconArray[i+2], iconArray[i+3], iconArray[i+4], iconArray[i+5]]);
355  console.log(megaLibrary_);
356 
357  finishInit();
358  return;
359 }