/media/com_easyblog/scripts_/responsive.js

https://gitlab.com/Mansikka/effigy-pro · JavaScript · 229 lines · 140 code · 37 blank · 52 comment · 26 complexity · 2ffec5401777680eca17101454ed8e64 MD5 · raw file

  1. EasyBlog.module('responsive', function($) {
  2. var module = this;
  3. // $(selector).responsive({condition});
  4. // $(selector).responsive([{condition1}, {condition2}]);
  5. $.fn.responsive = function() {
  6. var node = this;
  7. /* conditions = {
  8. at: 0, // threshold value
  9. switchTo: '', // classname to apply to the node
  10. alsoSwitch: {
  11. 'selector': 'class'
  12. }
  13. switchStylesheet: '',
  14. targetFunction: '',
  15. reverseFunction: ''
  16. } */
  17. var options = {
  18. elementWidth: function() {
  19. return $(node).outerWidth(true);
  20. },
  21. conditions: $.makeArray(arguments)
  22. };
  23. $.responsive.process.call(node, options);
  24. };
  25. /*
  26. $.responsive({
  27. elementWidth: function() {} // width calculation of the target element
  28. }, {
  29. condition1
  30. });
  31. $.responsive({
  32. elementWidth: function() {} // width calculation of the target element
  33. }, [{
  34. condition1
  35. }, {
  36. condition2
  37. }]);
  38. */
  39. $.responsive = function(elem, options) {
  40. // make sure that single condition object gets convert into array any how
  41. options.conditions = $.makeArray(options.conditions);
  42. /*var defaultOptions = {
  43. // main element width to calculate
  44. elementWidth: function() {}, // a function that returns pixel value
  45. // array of conditions of ascending thresholdWidth
  46. conditions: [{
  47. // threshold for this condition
  48. at: 0,
  49. // condition specific options
  50. switchTo: '',
  51. alsoSwitch: {}, // objects with element and class
  52. switchStylesheet: '',
  53. targetFunction: '', // function to run
  54. reverseFunction: '' // reverse function that reverses any action in target function
  55. }]
  56. }*/
  57. $.responsive.process.call($(elem), options);
  58. };
  59. $.responsive.process = function(options) {
  60. var node = this;
  61. var totalConditions = options.conditions.length;
  62. $(window).resize(function() {
  63. $.responsive.sortConditions(options);
  64. var elementWidth;
  65. // calculate element width
  66. if ($.isFunction(options.elementWidth)) {
  67. elementWidth = options.elementWidth();
  68. } else {
  69. elementWidth = options.elementWidth;
  70. }
  71. // loop through each condition
  72. $.each(options.conditions, function(i, condition) {
  73. var conditionOptions = $.responsive.properConditions(condition);
  74. var thresholdWidth = condition.at;
  75. // calculate threshold width
  76. if ($.isFunction(condition.at)) {
  77. thresholdWidth = condition.at();
  78. } else {
  79. thresholdWidth = condition.at;
  80. }
  81. // perform resize if element <= threshold
  82. if (elementWidth <= thresholdWidth) {
  83. // remove all other condition first
  84. $.responsive.resetToDefault.call(node, options.conditions, i);
  85. // apply current condition
  86. $.responsive.resize.call(node, conditionOptions);
  87. return false;
  88. } else {
  89. $.responsive.deresize.call(node, conditionOptions);
  90. }
  91. });
  92. }).resize();
  93. };
  94. $.responsive.resize = function(condition) {
  95. var node = this;
  96. if (condition.switchTo) {
  97. $.each(condition.switchTo, function(i, classname) {
  98. node.addClass(classname);
  99. });
  100. }
  101. if (condition.alsoSwitch) {
  102. $.each(condition.alsoSwitch, function(selector, classname) {
  103. $(selector).addClass(classname);
  104. });
  105. }
  106. if (condition.targetFunction) {
  107. condition.targetFunction();
  108. }
  109. if (condition.switchStylesheet) {
  110. $.each(condition.switchStylesheet, function(i, stylesheet) {
  111. var tmp = $('link[href$="' + stylesheet + '"]');
  112. if (tmp.length < 1) {
  113. $('<link/>', {
  114. rel: 'stylesheet',
  115. type: 'text/css',
  116. href: stylesheet
  117. }).appendTo('head');
  118. }
  119. });
  120. }
  121. };
  122. $.responsive.deresize = function(condition) {
  123. var node = this;
  124. if (condition.switchTo) {
  125. $.each(condition.switchTo, function(i, classname) {
  126. node.removeClass(classname);
  127. });
  128. }
  129. if (condition.alsoSwitch) {
  130. $.each(condition.alsoSwitch, function(selector, classname) {
  131. $(selector).removeClass(classname);
  132. });
  133. }
  134. if (condition.reverseFunction) {
  135. condition.reverseFunction();
  136. }
  137. if (condition.switchStylesheet) {
  138. $.each(condition.switchStylesheet, function(i, stylesheet) {
  139. $('link[href$="' + stylesheet + '"]').remove();
  140. });
  141. }
  142. };
  143. $.responsive.resetToDefault = function(options, current) {
  144. var node = this;
  145. $.each(options, function(i, condition) {
  146. if (current && i == current) {
  147. return true;
  148. } else {
  149. $.responsive.deresize.call(node, condition);
  150. }
  151. });
  152. };
  153. $.responsive.properConditions = function(condition) {
  154. var conditionOptions = {
  155. at: condition.at,
  156. alsoSwitch: condition.alsoSwitch,
  157. switchTo: $.makeArray(condition.switchTo),
  158. switchStylesheet: $.makeArray(condition.switchStylesheet),
  159. targetFunction: condition.targetFunction,
  160. reverseFunction: condition.reverseFunction
  161. };
  162. return conditionOptions;
  163. };
  164. $.responsive.sortConditions = function(options) {
  165. var totalConditions = options.conditions.length;
  166. for (var i = 0; i < totalConditions; i++) {
  167. for (var j = i + 1; j < totalConditions; j++) {
  168. var a, b;
  169. if ($.isFunction(options.conditions[i].at)) {
  170. a = options.conditions[i].at();
  171. } else {
  172. a = options.conditions[i].at;
  173. }
  174. if ($.isFunction(options.conditions[j].at)) {
  175. b = options.conditions[j].at();
  176. } else {
  177. b = options.conditions[j].at;
  178. }
  179. if (a > b) {
  180. var tmp = options.conditions[i];
  181. options.conditions[i] = options.conditions[j];
  182. options.conditions[j] = tmp;
  183. }
  184. }
  185. }
  186. };
  187. module.resolve();
  188. });