PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/jquery.gridder.js

https://gitlab.com/oytunistrator/gridder
JavaScript | 238 lines | 163 code | 40 blank | 35 comment | 15 complexity | 79ea3a30c7d4d3b64fe48e8c36ff0184 MD5 | raw file
  1. ;(function($) {
  2. //Ensures there will be no 'console is undefined' errors in IE
  3. window.console = window.console || (function(){
  4. var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile = c.clear = c.exception = c.trace = c.assert = function(){};
  5. return c;
  6. })();
  7. /* Custom Easing */
  8. $.fn.extend($.easing,{
  9. def:"easeInOutExpo", easeInOutExpo:function(e,f,a,h,g){if(f===0){return a;}if(f===g){return a+h;}if((f/=g/2)<1){return h/2*Math.pow(2,10*(f-1))+a;}return h/2*(-Math.pow(2,-10*--f)+2)+a;}
  10. });
  11. /* KEYPRESS LEFT & RIGHT ARROW */
  12. /* This will work only if a current gridder is opened. */
  13. $(document).keydown(function(e) {
  14. var keycode = e.keyCode;
  15. var $current_gridder = $(".currentGridder");
  16. var $current_target = $current_gridder.find(".gridder-show");
  17. if($current_gridder.length){
  18. if ( keycode === 37 ) {
  19. console.log("Pressed Left Arrow");
  20. $current_target.prev().prev().trigger("click");
  21. e.preventDefault();
  22. }
  23. if ( keycode === 39 ) {
  24. console.log("Pressed Right Arrow");
  25. $current_target.next().trigger("click");
  26. e.preventDefault();
  27. }
  28. }else{
  29. console.log("No active gridder.");
  30. }
  31. });
  32. $.fn.gridderExpander = function(options) {
  33. /* GET DEFAULT OPTIONS OR USE THE ONE PASSED IN THE FUNCTION */
  34. var settings = $.extend( {}, $.fn.gridderExpander.defaults, options );
  35. return this.each(function() {
  36. var mybloc;
  37. var _this = $(this);
  38. var visible = false;
  39. // START CALLBACK
  40. settings.onStart(_this);
  41. // CLOSE FUNCTION
  42. function closeExpander(base) {
  43. // SCROLL TO CORRECT POSITION FIRST
  44. if(settings.scroll){
  45. $("html, body").animate({
  46. scrollTop: base.find(".selectedItem").offset().top - settings.scrollOffset
  47. }, {
  48. duration: 200,
  49. easing: settings.animationEasing
  50. });
  51. }
  52. _this.removeClass("hasSelectedItem");
  53. // REMOVES GRIDDER EXPAND AREA
  54. visible = false;
  55. base.find(".selectedItem").removeClass("selectedItem");
  56. base.find(".gridder-show").slideUp(settings.animationSpeed, settings.animationEasing, function() {
  57. base.find(".gridder-show").remove();
  58. settings.onClosed(base);
  59. });
  60. /* REMOVE CURRENT ACTIVE GRIDDER */
  61. $(".currentGridder").removeClass("currentGridder");
  62. }
  63. // OPEN EXPANDER
  64. function openExpander(myself) {
  65. /* CURRENT ACTIVE GRIDDER */
  66. $(".currentGridder").removeClass("currentGridder");
  67. _this.addClass("currentGridder");
  68. /* ENSURES THE CORRECT BLOC IS ACTIVE */
  69. if (!myself.hasClass("selectedItem")) {
  70. _this.find(".selectedItem").removeClass("selectedItem");
  71. myself.addClass("selectedItem");
  72. } else {
  73. // THE SAME IS ALREADY OPEN, LET"S CLOSE IT
  74. closeExpander(_this, settings);
  75. return;
  76. }
  77. /* REMOVES PREVIOUS BLOC */
  78. _this.find(".gridder-show").remove();
  79. /* ADD CLASS TO THE GRIDDER CONTAINER
  80. * So you can apply global style when item selected.
  81. */
  82. if (!_this.hasClass("hasSelectedItem")) {
  83. _this.addClass("hasSelectedItem");
  84. }
  85. /* ADD LOADING BLOC */
  86. var $htmlcontent = $("<div class=\"gridder-show loading\"></div>");
  87. mybloc = $htmlcontent.insertAfter(myself);
  88. /* GET CONTENT VIA AJAX OR #ID*/
  89. var thecontent = "";
  90. if( myself.data("griddercontent").indexOf("#") === 0 ) {
  91. // Load #ID Content
  92. thecontent = $(myself.data("griddercontent")).html();
  93. processContent(myself, thecontent);
  94. }else{
  95. // Load AJAX Content
  96. $.ajax({
  97. type: "POST",
  98. url: myself.data("griddercontent"),
  99. success: function(data) {
  100. thecontent = data;
  101. processContent(myself, thecontent);
  102. },
  103. error: function (request) {
  104. thecontent = request.responseText;
  105. processContent(myself, thecontent);
  106. }
  107. });
  108. }
  109. }
  110. // PROCESS CONTENT
  111. function processContent(myself, thecontent){
  112. /* FORMAT OUTPUT */
  113. var htmlcontent = "<div class=\"gridder-padding\">";
  114. if(settings.showNav){
  115. /* CHECK IF PREV AND NEXT BUTTON HAVE ITEMS */
  116. var prevItem = ($(".selectedItem").prev());
  117. var nextItem = ($(".selectedItem").next().next());
  118. htmlcontent += "<div class=\"gridder-navigation\">";
  119. htmlcontent += "<a href=\"#\" class=\"gridder-close\">"+settings.closeText+"</a>";
  120. htmlcontent += "<a href=\"#\" class=\"gridder-nav prev "+(!prevItem.length?"disabled":"")+"\">"+settings.prevText+"</a>";
  121. htmlcontent += "<a href=\"#\" class=\"gridder-nav next "+(!nextItem.length?"disabled":"")+"\">"+settings.nextText+"</a>";
  122. htmlcontent += "</div>";
  123. }
  124. htmlcontent += "<div class=\"gridder-expanded-content\">";
  125. htmlcontent += thecontent;
  126. htmlcontent += "</div>";
  127. htmlcontent += "</div>";
  128. // IF EXPANDER IS ALREADY EXPANDED
  129. if (!visible) {
  130. mybloc.hide().append(htmlcontent).slideDown(settings.animationSpeed, settings.animationEasing, function () {
  131. visible = true;
  132. /* AFTER EXPAND CALLBACK */
  133. if ($.isFunction(settings.onContent)) {
  134. settings.onContent(mybloc);
  135. }
  136. });
  137. } else {
  138. mybloc.html(htmlcontent);
  139. mybloc.find(".gridder-padding").fadeIn(settings.animationSpeed, settings.animationEasing, function () {
  140. visible = true;
  141. /* CHANGED CALLBACK */
  142. if ($.isFunction(settings.onContent)) {
  143. settings.onContent(mybloc);
  144. }
  145. });
  146. }
  147. /* SCROLL TO CORRECT POSITION AFTER */
  148. if (settings.scroll) {
  149. var offset = (settings.scrollTo === "panel" ? myself.offset().top + myself.height() - settings.scrollOffset : myself.offset().top - settings.scrollOffset);
  150. $("html, body").animate({
  151. scrollTop: offset
  152. }, {
  153. duration: settings.animationSpeed,
  154. easing: settings.animationEasing
  155. });
  156. }
  157. /* REMOVE LOADING CLASS */
  158. mybloc.removeClass("loading");
  159. }
  160. /* CLICK EVENT */
  161. _this.on("click", ".gridder-list", function (e) {
  162. e.preventDefault();
  163. var myself = $(this);
  164. openExpander(myself);
  165. });
  166. /* NEXT BUTTON */
  167. _this.on("click", ".gridder-nav.next", function(e) {
  168. e.preventDefault();
  169. $(this).parents(".gridder-show").next().trigger("click");
  170. });
  171. /* PREVIOUS BUTTON */
  172. _this.on("click", ".gridder-nav.prev", function(e) {
  173. e.preventDefault();
  174. $(this).parents(".gridder-show").prev().prev().trigger("click");
  175. });
  176. /* CLOSE BUTTON */
  177. _this.on("click", ".gridder-close", function(e) {
  178. e.preventDefault();
  179. closeExpander(_this);
  180. });
  181. });
  182. };
  183. // Default Options
  184. $.fn.gridderExpander.defaults = {
  185. scroll: true,
  186. scrollOffset: 30,
  187. scrollTo: "panel", // panel or listitem
  188. animationSpeed: 400,
  189. animationEasing: "easeInOutExpo",
  190. showNav: true,
  191. nextText: "Next",
  192. prevText: "Previous",
  193. closeText: "Close",
  194. onStart: function(){},
  195. onContent: function(){},
  196. onClosed: function(){}
  197. };
  198. })(jQuery);