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.

81 lines
2.7 KiB

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