jaliswall.js 5.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";"object"!=typeof window.CP&&(window.CP={}),window.CP.PenTimer={programNoLongerBeingMonitored:!1,timeOfFirstCallToShouldStopLoop:0,_loopExits:{},_loopTimers:{},START_MONITORING_AFTER:2e3,STOP_ALL_MONITORING_TIMEOUT:5e3,MAX_TIME_IN_LOOP_WO_EXIT:2200,exitedLoop:function(o){this._loopExits[o]=!0},shouldStopLoop:function(o){if(this.programKilledSoStopMonitoring)return!0;if(this.programNoLongerBeingMonitored)return!1;if(this._loopExits[o])return!1;var t=this._getTime();if(0===this.timeOfFirstCallToShouldStopLoop)return this.timeOfFirstCallToShouldStopLoop=t,!1;var i=t-this.timeOfFirstCallToShouldStopLoop;if(i<this.START_MONITORING_AFTER)return!1;if(i>this.STOP_ALL_MONITORING_TIMEOUT)return this.programNoLongerBeingMonitored=!0,!1;try{this._checkOnInfiniteLoop(o,t)}catch(n){return this._sendErrorMessageToEditor(),this.programKilledSoStopMonitoring=!0,!0}return!1},_sendErrorMessageToEditor:function(){try{if(this._shouldPostMessage()){var o={action:"infinite-loop",line:this._findAroundLineNumber()};parent.postMessage(JSON.stringify(o),"*")}else this._throwAnErrorToStopPen()}catch(t){this._throwAnErrorToStopPen()}},_shouldPostMessage:function(){return document.location.href.match(/boomerang/)},_throwAnErrorToStopPen:function(){throw"We found an infinite loop in your Pen. We've stopped the Pen from running. Please correct it or contact support@codepen.io."},_findAroundLineNumber:function(){var o=new Error,t=0;if(o.stack){var i=o.stack.match(/boomerang\S+:(\d+):\d+/);i&&(t=i[1])}return t},_checkOnInfiniteLoop:function(o,t){if(!this._loopTimers[o])return this._loopTimers[o]=t,!1;var i=t-this._loopTimers[o];if(i>this.MAX_TIME_IN_LOOP_WO_EXIT)throw"Infinite Loop found on loop: "+o},_getTime:function(){return+new Date}},window.CP.shouldStopExecution=function(o){return window.CP.PenTimer.shouldStopLoop(o)},window.CP.exitedLoop=function(o){window.CP.PenTimer.exitedLoop(o)};
  2. (function ($) {
  3. $.fn.jaliswall = function (options) {
  4. this.each(function () {
  5. var defaults = {
  6. item: '.wall-item',
  7. columnClass: '.wall-column',
  8. resize: true
  9. };
  10. var prm = $.extend(defaults, options);
  11. var container = $(this);
  12. var items = container.find(prm.item);
  13. var elemsDatas = [];
  14. var columns = [];
  15. var nbCols = getNbCols();
  16. init();
  17. function init() {
  18. nbCols = getNbCols();
  19. recordAndRemove();
  20. print();
  21. if (prm.resize) {
  22. $(window).resize(function () {
  23. if (nbCols != getNbCols()) {
  24. nbCols = getNbCols();
  25. setColPos();
  26. print();
  27. }
  28. });
  29. }
  30. }
  31. function getNbCols() {
  32. var instanceForCompute = false;
  33. if (container.find(prm.columnClass).length == 0) {
  34. instanceForCompute = true;
  35. container.append('<div class=\'' + parseSelector(prm.columnClass) + '\'></div>');
  36. }
  37. var colWidth = container.find(prm.columnClass).outerWidth(true);
  38. var wallWidth = container.innerWidth();
  39. if (instanceForCompute)
  40. container.find(prm.columnClass).remove();
  41. return Math.round(wallWidth / colWidth);
  42. }
  43. function recordAndRemove() {
  44. items.each(function (index) {
  45. var item = $(this);
  46. elemsDatas.push({
  47. content: item.html(),
  48. class: item.attr('class'),
  49. href: item.attr('href'),
  50. id: item.attr('id'),
  51. colid: index % nbCols
  52. });
  53. item.remove();
  54. });
  55. }
  56. function setColPos() {
  57. for (var i in elemsDatas) {
  58. if (window.CP.shouldStopExecution(1)) {
  59. break;
  60. }
  61. elemsDatas[i].colid = i % nbCols;
  62. }
  63. window.CP.exitedLoop(1);
  64. }
  65. function parseSelector(selector) {
  66. return selector.slice(1, selector.length);
  67. }
  68. function print() {
  69. var tree = '';
  70. for (var i = 0; i < nbCols; i++) {
  71. if (window.CP.shouldStopExecution(2)) {
  72. break;
  73. }
  74. tree += '<div class=\'' + parseSelector(prm.columnClass) + '\'></div>';
  75. }
  76. window.CP.exitedLoop(2);
  77. container.html(tree);
  78. for (var i in elemsDatas) {
  79. var html = '';
  80. var content = elemsDatas[i].content != undefined ? elemsDatas[i].content : '';
  81. var href = elemsDatas[i].href != href ? elemsDatas[i].href : '';
  82. var classe = elemsDatas[i].class != undefined ? elemsDatas[i].class : '';
  83. var id = elemsDatas[i].id != undefined ? elemsDatas[i].id : '';
  84. if (elemsDatas[i].href != undefined) {
  85. html += '<a ' + getAttr(href, 'href') + ' ' + getAttr(classe, 'class') + ' ' + getAttr(id, 'id') + '>' + content + '</a>';
  86. } else {
  87. html += '<div ' + getAttr(classe, 'class') + ' ' + getAttr(id, 'id') + '>' + content + '</a>';
  88. }
  89. container.children(prm.columnClass).eq(i % nbCols).append(html);
  90. }
  91. }
  92. function getAttr(attr, type) {
  93. return attr != undefined ? type + '=\'' + attr + '\'' : '';
  94. }
  95. });
  96. return this;
  97. };
  98. }(jQuery));