Responsive.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* *
  2. *
  3. * (c) 2010-2019 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import H from './Globals.js';
  12. /**
  13. * A callback function to gain complete control on when the responsive rule
  14. * applies.
  15. *
  16. * @callback Highcharts.ResponsiveCallbackFunction
  17. *
  18. * @param {Highcharts.Chart} this
  19. * Chart context.
  20. *
  21. * @return {boolean}
  22. * Return `true` if it applies.
  23. */
  24. import './Chart.js';
  25. import U from './Utilities.js';
  26. var isArray = U.isArray, isObject = U.isObject, objectEach = U.objectEach, pick = U.pick, splat = U.splat;
  27. var Chart = H.Chart;
  28. /**
  29. * Allows setting a set of rules to apply for different screen or chart
  30. * sizes. Each rule specifies additional chart options.
  31. *
  32. * @sample {highstock} stock/demo/responsive/
  33. * Stock chart
  34. * @sample highcharts/responsive/axis/
  35. * Axis
  36. * @sample highcharts/responsive/legend/
  37. * Legend
  38. * @sample highcharts/responsive/classname/
  39. * Class name
  40. *
  41. * @since 5.0.0
  42. * @apioption responsive
  43. */
  44. /**
  45. * A set of rules for responsive settings. The rules are executed from
  46. * the top down.
  47. *
  48. * @sample {highcharts} highcharts/responsive/axis/
  49. * Axis changes
  50. * @sample {highstock} highcharts/responsive/axis/
  51. * Axis changes
  52. * @sample {highmaps} highcharts/responsive/axis/
  53. * Axis changes
  54. *
  55. * @type {Array<*>}
  56. * @since 5.0.0
  57. * @apioption responsive.rules
  58. */
  59. /**
  60. * A full set of chart options to apply as overrides to the general
  61. * chart options. The chart options are applied when the given rule
  62. * is active.
  63. *
  64. * A special case is configuration objects that take arrays, for example
  65. * [xAxis](#xAxis), [yAxis](#yAxis) or [series](#series). For these
  66. * collections, an `id` option is used to map the new option set to
  67. * an existing object. If an existing object of the same id is not found,
  68. * the item of the same indexupdated. So for example, setting `chartOptions`
  69. * with two series items without an `id`, will cause the existing chart's
  70. * two series to be updated with respective options.
  71. *
  72. * @sample {highstock} stock/demo/responsive/
  73. * Stock chart
  74. * @sample highcharts/responsive/axis/
  75. * Axis
  76. * @sample highcharts/responsive/legend/
  77. * Legend
  78. * @sample highcharts/responsive/classname/
  79. * Class name
  80. *
  81. * @type {Highcharts.Options}
  82. * @since 5.0.0
  83. * @apioption responsive.rules.chartOptions
  84. */
  85. /**
  86. * Under which conditions the rule applies.
  87. *
  88. * @since 5.0.0
  89. * @apioption responsive.rules.condition
  90. */
  91. /**
  92. * A callback function to gain complete control on when the responsive
  93. * rule applies. Return `true` if it applies. This opens for checking
  94. * against other metrics than the chart size, for example the document
  95. * size or other elements.
  96. *
  97. * @type {Highcharts.ResponsiveCallbackFunction}
  98. * @since 5.0.0
  99. * @context Highcharts.Chart
  100. * @apioption responsive.rules.condition.callback
  101. */
  102. /**
  103. * The responsive rule applies if the chart height is less than this.
  104. *
  105. * @type {number}
  106. * @since 5.0.0
  107. * @apioption responsive.rules.condition.maxHeight
  108. */
  109. /**
  110. * The responsive rule applies if the chart width is less than this.
  111. *
  112. * @sample highcharts/responsive/axis/
  113. * Max width is 500
  114. *
  115. * @type {number}
  116. * @since 5.0.0
  117. * @apioption responsive.rules.condition.maxWidth
  118. */
  119. /**
  120. * The responsive rule applies if the chart height is greater than this.
  121. *
  122. * @type {number}
  123. * @default 0
  124. * @since 5.0.0
  125. * @apioption responsive.rules.condition.minHeight
  126. */
  127. /**
  128. * The responsive rule applies if the chart width is greater than this.
  129. *
  130. * @type {number}
  131. * @default 0
  132. * @since 5.0.0
  133. * @apioption responsive.rules.condition.minWidth
  134. */
  135. /* eslint-disable no-invalid-this, valid-jsdoc */
  136. /**
  137. * Update the chart based on the current chart/document size and options for
  138. * responsiveness.
  139. *
  140. * @private
  141. * @function Highcharts.Chart#setResponsive
  142. * @param {boolean} [redraw=true]
  143. * @param {boolean} [reset=false]
  144. * Reset by un-applying all rules. Chart.update resets all rules before
  145. * applying updated options.
  146. * @return {void}
  147. */
  148. Chart.prototype.setResponsive = function (redraw, reset) {
  149. var options = this.options.responsive, ruleIds = [], currentResponsive = this.currentResponsive, currentRuleIds, undoOptions;
  150. if (!reset && options && options.rules) {
  151. options.rules.forEach(function (rule) {
  152. if (typeof rule._id === 'undefined') {
  153. rule._id = H.uniqueKey();
  154. }
  155. this.matchResponsiveRule(rule, ruleIds /* , redraw */);
  156. }, this);
  157. }
  158. // Merge matching rules
  159. var mergedOptions = H.merge.apply(0, ruleIds.map(function (ruleId) {
  160. return H.find(options.rules, function (rule) {
  161. return rule._id === ruleId;
  162. }).chartOptions;
  163. }));
  164. mergedOptions.isResponsiveOptions = true;
  165. // Stringified key for the rules that currently apply.
  166. ruleIds = (ruleIds.toString() || void 0);
  167. currentRuleIds = currentResponsive && currentResponsive.ruleIds;
  168. // Changes in what rules apply
  169. if (ruleIds !== currentRuleIds) {
  170. // Undo previous rules. Before we apply a new set of rules, we need to
  171. // roll back completely to base options (#6291).
  172. if (currentResponsive) {
  173. this.update(currentResponsive.undoOptions, redraw, true);
  174. }
  175. if (ruleIds) {
  176. // Get undo-options for matching rules
  177. undoOptions = this.currentOptions(mergedOptions);
  178. undoOptions.isResponsiveOptions = true;
  179. this.currentResponsive = {
  180. ruleIds: ruleIds,
  181. mergedOptions: mergedOptions,
  182. undoOptions: undoOptions
  183. };
  184. this.update(mergedOptions, redraw, true);
  185. }
  186. else {
  187. this.currentResponsive = void 0;
  188. }
  189. }
  190. };
  191. /**
  192. * Handle a single responsiveness rule.
  193. *
  194. * @private
  195. * @function Highcharts.Chart#matchResponsiveRule
  196. * @param {Highcharts.ResponsiveRulesOptions} rule
  197. * @param {Array<string>} matches
  198. * @return {void}
  199. */
  200. Chart.prototype.matchResponsiveRule = function (rule, matches) {
  201. var condition = rule.condition, fn = condition.callback || function () {
  202. return (this.chartWidth <= pick(condition.maxWidth, Number.MAX_VALUE) &&
  203. this.chartHeight <=
  204. pick(condition.maxHeight, Number.MAX_VALUE) &&
  205. this.chartWidth >= pick(condition.minWidth, 0) &&
  206. this.chartHeight >= pick(condition.minHeight, 0));
  207. };
  208. if (fn.call(this)) {
  209. matches.push(rule._id);
  210. }
  211. };
  212. /**
  213. * Get the current values for a given set of options. Used before we update
  214. * the chart with a new responsiveness rule.
  215. * TODO: Restore axis options (by id?)
  216. *
  217. * @private
  218. * @function Highcharts.Chart#currentOptions
  219. * @param {Highcharts.Options} options
  220. * @return {Highcharts.Options}
  221. */
  222. Chart.prototype.currentOptions = function (options) {
  223. var chart = this, ret = {};
  224. /**
  225. * Recurse over a set of options and its current values,
  226. * and store the current values in the ret object.
  227. */
  228. function getCurrent(options, curr, ret, depth) {
  229. var i;
  230. objectEach(options, function (val, key) {
  231. if (!depth &&
  232. chart.collectionsWithUpdate.indexOf(key) > -1) {
  233. val = splat(val);
  234. ret[key] = [];
  235. // Iterate over collections like series, xAxis or yAxis and map
  236. // the items by index.
  237. for (i = 0; i < val.length; i++) {
  238. if (curr[key][i]) { // Item exists in current data (#6347)
  239. ret[key][i] = {};
  240. getCurrent(val[i], curr[key][i], ret[key][i], depth + 1);
  241. }
  242. }
  243. }
  244. else if (isObject(val)) {
  245. ret[key] = isArray(val) ? [] : {};
  246. getCurrent(val, curr[key] || {}, ret[key], depth + 1);
  247. }
  248. else if (typeof curr[key] === 'undefined') { // #10286
  249. ret[key] = null;
  250. }
  251. else {
  252. ret[key] = curr[key];
  253. }
  254. });
  255. }
  256. getCurrent(options, this.options, ret, 0);
  257. return ret;
  258. };