File size: 2,138 Bytes
4b876a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
;(function() {
  /*
  Confirm message on reload or go back from site
   */
  var myEvent = window.attachEvent || window.addEventListener;
  var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compatable

  myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
      var confirmationMessage = ' If you continue you will lose all unsaved information!';  // a space
      (e || window.event).returnValue = confirmationMessage;
      return confirmationMessage;
  });

  /*
  Compare two arrays
   */
  Array.prototype.diff = function(a) {
      return this.filter(function(i) {return a.indexOf(i) < 0;});
  };
  Array.prototype.diffInvers = function(a) {
      return this.filter(function(i) {return a.indexOf(i) >= 0;});
  };
  Object.defineProperty(Array.prototype, 'diff', { enumerable: false });
  Object.defineProperty(Array.prototype, 'diffInvers', { enumerable: false });

  /*
  Sorting array
   */
  function dynamicSort(property) {
      var sortOrder = 1;
      if(property[0] === "-") {
          sortOrder = -1;
          property = property.substr(1);
      }
      return function (a,b) {
          var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
          return result * sortOrder;
      }
  }
  Object.defineProperty(Array.prototype, "sortBy", {
      enumerable: false,
      writable: true,
      value: function() {
          return this.sort(dynamicSort.apply(null, arguments));
      }
  });

  /*
  Shuffle array
   */
  Array.prototype.shuffle = function (array) {
    var currentIndex = array.length, temporaryValue, randomIndex ;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;

      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }

    return array;
  };
  Object.defineProperty(Array.prototype, 'shuffle', { enumerable: false });
})();