Advertisement
Guest User

Untitled

a guest
May 27th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private.search = function (values) {
  2.         var
  3.             max = 0,
  4.             row = [],
  5.             pizza = [];
  6.  
  7.         if (values) {
  8.             for (var place in private.PLACES) {
  9.                 var
  10.                     p = [],
  11.                     l = private.PLACES[place].pizza.length;
  12.  
  13.  
  14.                 for (var i = l; i--; ) {
  15.                     var
  16.                         contain = 0,
  17.                         user_components = values,
  18.                         maximum = user_components.length,
  19.                         pizza_components = private.PLACES[place].pizza[i].components.split(','),
  20.                         j = pizza_components.length;
  21.  
  22.                     while (j--) {
  23.                         if (user_components.indexOf(pizza_components[j]) > -1) {
  24.                             contain += 1;
  25.                         } else {
  26.                             maximum += 1;
  27.                         }
  28.                     }
  29.  
  30.                     var
  31.                         o = {
  32.                             name: private.PLACES[place].pizza[i].name,
  33.                             price: private.PLACES[place].pizza[i].price,
  34.                             percent: parseFloat((contain / maximum * 100).toFixed(2)),
  35.                             components: pizza_components
  36.                         }
  37.  
  38.                     p.push(o);
  39.  
  40.                 }
  41.                 max = private.getMax(private.getArrayOfArrayObjs(p, 'percent'));
  42.  
  43.                 var myArray = p.filter(function(o) {
  44.                     return o.percent === max;
  45.                 });
  46.  
  47.                 pizza.push({
  48.                     arr: myArray,
  49.                     address: private.PLACES[place].address,
  50.                     name: place
  51.                 });
  52.             }
  53.  
  54.             private.results.empty().show();
  55.  
  56.             $('<thead>').append(
  57.                 $('<tr>').append(
  58.                     $('<td>', { text: 'Zgdność' }),
  59.                     $('<td>', { text: 'Pizza' }),
  60.                     $('<td>', { text: 'Cena' }),
  61.                     $('<td>', { text: 'Pizzeria' }),
  62.                     $('<td>', { text: 'Adres' }),
  63.                     $('<td>', { text: 'Składniki' })
  64.                 )
  65.             ).appendTo('#results');
  66.  
  67.             $('<tbody>').appendTo('#results');
  68.  
  69.             $.each(pizza, function (index, value) {
  70.  
  71.                 var
  72.                     placeName = value.name,
  73.                     placeAddress = value.address;
  74.  
  75.                 $.each(value.arr, function (index, value) {
  76.  
  77.                     row.push($('<tr>').append(
  78.                         $('<td>', { text: value .percent, class: 'percent' }),
  79.                         $('<td>', { text: value .name}),
  80.                         $('<td>', { text: value .price + 'zł'}),
  81.                         $('<td>', { text: placeName }),
  82.                         $('<td>', { text: placeAddress }),
  83.                         $('<td>', { text: private.getNames(value.components).join(', ') })
  84.                     ));
  85.                 });
  86.  
  87.             });
  88.             row = row.sort(function (a, b) {
  89.                 return parseInt(a.find('.percent').text(), 10) < parseInt(b.find('.percent').text(), 10);
  90.             });
  91.  
  92.             $.each(row, function (index, value) {
  93.                 value.appendTo('#results > tbody');
  94.             });
  95.         } else {
  96.             private.results.hide();
  97.         }
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement