comparison rhodecode/public/js/rhodecode.js @ 2394:6776f4e569d7 codereview

Moved select widget to rhodecode.js
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 06 Jun 2012 01:01:33 +0200
parents c2f131502037
children 9f37281195a2
comparison
equal deleted inserted replaced
2393:b94eac50ec4d 2394:6776f4e569d7
203 YUD.setStyle(container,'opacity','0.3'); 203 YUD.setStyle(container,'opacity','0.3');
204 YUC.asyncRequest(method,url,{ 204 YUC.asyncRequest(method,url,{
205 success:s_wrapper, 205 success:s_wrapper,
206 failure:function(o){ 206 failure:function(o){
207 console.log(o); 207 console.log(o);
208 YUD.get(container).innerHTML='ERROR'; 208 YUD.get(container).innerHTML='ERROR '+o.status;
209 YUD.setStyle(container,'opacity','1.0'); 209 YUD.setStyle(container,'opacity','1.0');
210 YUD.setStyle(container,'color','red'); 210 YUD.setStyle(container,'color','red');
211 } 211 }
212 },args); 212 },args);
213 213
1321 1321
1322 var comp = YAHOO.util.Sort.compare; 1322 var comp = YAHOO.util.Sort.compare;
1323 var compState = comp(a_, b_, desc); 1323 var compState = comp(a_, b_, desc);
1324 return compState; 1324 return compState;
1325 }; 1325 };
1326
1327
1328
1329 /* Multi selectors */
1330
1331 var MultiSelectWidget = function(selected_id, available_id, form_id){
1332
1333
1334 //definition of containers ID's
1335 var selected_container = selected_id;
1336 var available_container = available_id;
1337
1338 //temp container for selected storage.
1339 var cache = new Array();
1340 var av_cache = new Array();
1341 var c = YUD.get(selected_container);
1342 var ac = YUD.get(available_container);
1343
1344 //get only selected options for further fullfilment
1345 for(var i = 0;node =c.options[i];i++){
1346 if(node.selected){
1347 //push selected to my temp storage left overs :)
1348 cache.push(node);
1349 }
1350 }
1351
1352 //get all available options to cache
1353 for(var i = 0;node =ac.options[i];i++){
1354 //push selected to my temp storage left overs :)
1355 av_cache.push(node);
1356 }
1357
1358 //fill available only with those not in choosen
1359 ac.options.length=0;
1360 tmp_cache = new Array();
1361
1362 for(var i = 0;node = av_cache[i];i++){
1363 var add = true;
1364 for(var i2 = 0;node_2 = cache[i2];i2++){
1365 if(node.value == node_2.value){
1366 add=false;
1367 break;
1368 }
1369 }
1370 if(add){
1371 tmp_cache.push(new Option(node.text, node.value, false, false));
1372 }
1373 }
1374
1375 for(var i = 0;node = tmp_cache[i];i++){
1376 ac.options[i] = node;
1377 }
1378
1379 function prompts_action_callback(e){
1380
1381 var choosen = YUD.get(selected_container);
1382 var available = YUD.get(available_container);
1383
1384 //get checked and unchecked options from field
1385 function get_checked(from_field){
1386 //temp container for storage.
1387 var sel_cache = new Array();
1388 var oth_cache = new Array();
1389
1390 for(var i = 0;node = from_field.options[i];i++){
1391 if(node.selected){
1392 //push selected fields :)
1393 sel_cache.push(node);
1394 }
1395 else{
1396 oth_cache.push(node)
1397 }
1398 }
1399
1400 return [sel_cache,oth_cache]
1401 }
1402
1403 //fill the field with given options
1404 function fill_with(field,options){
1405 //clear firtst
1406 field.options.length=0;
1407 for(var i = 0;node = options[i];i++){
1408 field.options[i]=new Option(node.text, node.value,
1409 false, false);
1410 }
1411
1412 }
1413 //adds to current field
1414 function add_to(field,options){
1415 for(var i = 0;node = options[i];i++){
1416 field.appendChild(new Option(node.text, node.value,
1417 false, false));
1418 }
1419 }
1420
1421 // add action
1422 if (this.id=='add_element'){
1423 var c = get_checked(available);
1424 add_to(choosen,c[0]);
1425 fill_with(available,c[1]);
1426 }
1427 // remove action
1428 if (this.id=='remove_element'){
1429 var c = get_checked(choosen);
1430 add_to(available,c[0]);
1431 fill_with(choosen,c[1]);
1432 }
1433 // add all elements
1434 if(this.id=='add_all_elements'){
1435 for(var i=0; node = available.options[i];i++){
1436 choosen.appendChild(new Option(node.text,
1437 node.value, false, false));
1438 }
1439 available.options.length = 0;
1440 }
1441 //remove all elements
1442 if(this.id=='remove_all_elements'){
1443 for(var i=0; node = choosen.options[i];i++){
1444 available.appendChild(new Option(node.text,
1445 node.value, false, false));
1446 }
1447 choosen.options.length = 0;
1448 }
1449
1450 }
1451
1452 YUE.addListener(['add_element','remove_element',
1453 'add_all_elements','remove_all_elements'],'click',
1454 prompts_action_callback)
1455 if (form_id !== undefined) {
1456 YUE.addListener(form_id,'submit',function(){
1457 var choosen = YUD.get(selected_container);
1458 for (var i = 0; i < choosen.options.length; i++) {
1459 choosen.options[i].selected = 'selected';
1460 }
1461 });
1462 }
1463 }