You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Web Utils
  6. * (see https://mukit.at).
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. **********************************************************************************/
  22. odoo.define('muk_web_utils.utils', function (require) {
  23. "use strict";
  24. var core = require('web.core');
  25. var _t = core._t;
  26. var QWeb = core.qweb;
  27. var isUrl = function(string) {
  28. var protocol = string.match(/^(?:\w+:)?\/\/(\S+)$/);
  29. if (protocol && protocol[1]) {
  30. var localHost = (/^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/).test(protocol[1]);
  31. var nonLocalHost = (/^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/).test(protocol[1]);
  32. return !!(localHost || nonLocalHost);
  33. }
  34. return false;
  35. }
  36. var parseText2Html= function(text) {
  37. return text
  38. .replace(/((?:https?|ftp):\/\/[\S]+)/g,'<a href="$1">$1</a> ')
  39. .replace(/[\n\r]/g,'<br/>');
  40. }
  41. var closedRange = function(start, end) {
  42. return _.range(start, end + 1);
  43. }
  44. var partitionPageList = function(pages, page, size) {
  45. if (!size || size < 5) {
  46. throw "The size must be at least 5 to partition the list.";
  47. }
  48. var sideSize = size < 9 ? 1 : 2;
  49. var leftSize = (size - sideSize * 2 - 3) >> 1;
  50. var rightSize = (size - sideSize * 2 - 2) >> 1;
  51. if (pages <= size) {
  52. return closedRange(1, pages);
  53. }
  54. if (page <= size - sideSize - 1 - rightSize) {
  55. return closedRange(1, size - sideSize - 1)
  56. .concat([false])
  57. .concat(closedRange(pages - sideSize + 1, pages));
  58. }
  59. if (page >= pages - sideSize - 1 - rightSize) {
  60. return closedRange(1, sideSize)
  61. .concat([false])
  62. .concat(closedRange(pages - sideSize - 1 - rightSize - leftSize, pages));
  63. }
  64. return closedRange(1, sideSize)
  65. .concat([false])
  66. .concat(closedRange(page - leftSize, page + rightSize))
  67. .concat([false])
  68. .concat(closedRange(pages - sideSize + 1, pages));
  69. }
  70. return {
  71. isUrl: isUrl,
  72. closedRange: closedRange,
  73. parseText2Html: parseText2Html,
  74. partitionPageList: partitionPageList,
  75. };
  76. });