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.

253 lines
7.6 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Preview
  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_preview.PreviewManager', function (require) {
  23. "use strict";
  24. var core = require('web.core');
  25. var ajax = require('web.ajax');
  26. var session = require('web.session');
  27. var framework = require('web.framework');
  28. var Widget = require('web.Widget');
  29. var utils = require('muk_web_utils.utils');
  30. var registry = require('muk_preview.registry');
  31. var QWeb = core.qweb;
  32. var _t = core._t;
  33. var PreviewManager = Widget.extend({
  34. template: "muk_preview.PreviewManager",
  35. events: _.extend({}, Widget.prototype.events, {
  36. 'click .mk_preview_previous a': '_onPreviousClick',
  37. 'click .mk_preview_next a': '_onNextClick',
  38. 'click .mk_preview_page a': '_onPageClick',
  39. 'click .mk_preview_print': '_onPrintClick',
  40. }),
  41. jsLibs: [
  42. '/muk_web_preview/static/lib/printThis/printThis.js',
  43. ],
  44. files: [],
  45. init: function (parent, files, index) {
  46. this._super.apply(this, arguments);
  47. this.files = files;
  48. this.index = index;
  49. this.activeFile = files[index];
  50. this.pagerSize = 9;
  51. },
  52. willStart: function() {
  53. return $.when(
  54. this._super.apply(this, arguments),
  55. ajax.loadLibs(this)
  56. );
  57. },
  58. start: function () {
  59. var res = this._super.apply(this, arguments);
  60. this.$actions = this.$('.mk_preview_actions');
  61. this.$wrapper = this.$('.mk_preview_wrapper');
  62. this.$pager = this.$('.mk_preview_pager');
  63. this._render();
  64. return res;
  65. },
  66. _render: function () {
  67. this._renderPreviewWithLoading();
  68. this._renderIndexPager();
  69. this._updateActions();
  70. },
  71. _renderPreviewWithLoading: function () {
  72. var $loader = this._renderLoader();
  73. this._destroyPreview();
  74. this._renderPreview($loader);
  75. },
  76. _renderLoader: function () {
  77. var $loader = $(QWeb.render('muk_preview.PreviewLoader', {
  78. loading_text: _t("Loading ..."),
  79. loading_text_00: _t("Loading"),
  80. loading_text_25: _t("Loading ."),
  81. loading_text_50: _t("Loading .."),
  82. loading_text_75: _t("Loading ..."),
  83. }));
  84. this.$wrapper.html($loader);
  85. return $loader;
  86. },
  87. _renderPreview: function (element) {
  88. var PreviewWidget = undefined;
  89. var mimetype = this.activeFile.mimetype;
  90. var filename = this.activeFile.filename;
  91. if (mimetype && registry.contains(mimetype)) {
  92. PreviewWidget = registry.get(mimetype);
  93. }
  94. if (!PreviewWidget && filename) {
  95. var extension = filename.split('.').pop();
  96. if (extension && registry.contains(extension)) {
  97. PreviewWidget = registry.get(extension);
  98. }
  99. }
  100. if (!PreviewWidget) {
  101. PreviewWidget = registry.defaultPreview();
  102. }
  103. var content = new PreviewWidget(this,
  104. this.activeFile.url, mimetype, filename
  105. );
  106. content.replace(element);
  107. this.activePreview = content;
  108. },
  109. _renderIndexPager: function () {
  110. this.$pager.find('.pagination').empty();
  111. if (this.files.length <= 1) {
  112. this.$pager.hide();
  113. } else {
  114. var $previous = $("<li>", {
  115. 'class': "page-item mk_preview_previous",
  116. 'title': _t("Previous"),
  117. }).append($("<a>", {
  118. 'class': "page-link",
  119. 'href': "#",
  120. 'html': '<i class="fa fa-angle-double-left" />',
  121. }));
  122. var $next = $("<li>", {
  123. 'class': "page-item mk_preview_next",
  124. 'title': _t("Next"),
  125. }).append($("<a>", {
  126. 'class': "page-link",
  127. 'href': "#",
  128. 'html': '<i class="fa fa-angle-double-right" />',
  129. }));
  130. this.$pager.find('.pagination').append($previous);
  131. this.$pager.find('.pagination').append($next);
  132. var pageList = utils.partitionPageList(
  133. this.files.length,
  134. this.index + 1,
  135. this.pagerSize
  136. );
  137. _.each(pageList, function(page) {
  138. var index = page && page - 1;
  139. var $item = $("<li>", {
  140. 'class': "page-item",
  141. });
  142. if (!page) {
  143. $item.addClass("disabled");
  144. } else if (index === this.index) {
  145. $item.addClass("active");
  146. } else {
  147. $item.addClass("mk_preview_page");
  148. }
  149. $item.append($("<a>", {
  150. 'class': "page-link",
  151. 'data-index': index,
  152. 'text': page || '...',
  153. 'href': "javascript:void(0)",
  154. }));
  155. $item.insertBefore($next);
  156. }, this);
  157. if (this.index === 0) {
  158. $previous.addClass("disabled");
  159. $next.removeClass("disabled");
  160. } else if (this.index === this.files.length - 1) {
  161. $previous.removeClass("disabled");
  162. $next.addClass("disabled");
  163. } else {
  164. $previous.removeClass("disabled");
  165. $next.removeClass("disabled");
  166. }
  167. this.$pager.show();
  168. }
  169. },
  170. _updateActions: function () {
  171. this.$actions.empty();
  172. if (this.activePreview) {
  173. if (this.activePreview.downloadable) {
  174. this.$actions.append($("<a>", {
  175. 'class': "mk_preview_download",
  176. 'html': '<i class="fa fa-download" />',
  177. 'title': _t("Download"),
  178. 'href': this.activeFile.url,
  179. }));
  180. }
  181. if (this.activePreview.printable) {
  182. this.$actions.append($("<a>", {
  183. 'class': "mk_preview_print",
  184. 'html': '<i class="fa fa-print" />',
  185. 'title': _t("Print"),
  186. 'href': '#',
  187. }));
  188. }
  189. _.each(this.activePreview.contentActions(), function(action) {
  190. this.$actions.append(action);
  191. }, this);
  192. }
  193. },
  194. _destroyPreview: function () {
  195. if (this.activePreview) {
  196. this.activePreview.destroy();
  197. }
  198. this.activePreview = undefined;
  199. },
  200. _onPreviousClick: function(event) {
  201. if (this.index > 0) {
  202. this.index = this.index - 1;
  203. this.activeFile = this.files[this.index];
  204. this._render();
  205. }
  206. event.stopPropagation();
  207. event.preventDefault();
  208. },
  209. _onNextClick: function (event) {
  210. if (this.index < this.files.length - 1) {
  211. this.index = this.index + 1;
  212. this.activeFile = this.files[this.index];
  213. this._render();
  214. }
  215. event.stopPropagation();
  216. event.preventDefault();
  217. },
  218. _onPageClick: function(event) {
  219. var $target = $(event.currentTarget);
  220. var index = $target.data('index');
  221. if (index >= 0 && index < this.files.length) {
  222. this.index = index;
  223. this.activeFile = this.files[this.index];
  224. this._render();
  225. }
  226. event.stopPropagation();
  227. event.preventDefault();
  228. },
  229. _onPrintClick: function(event) {
  230. var preview = this.activePreview;
  231. var delay = preview.printDelay;
  232. framework.blockUI();
  233. setTimeout(function() {
  234. framework.unblockUI();
  235. }, delay|| 950);
  236. this.$wrapper.printThis({
  237. importCSS: true,
  238. importStyle: true,
  239. printDelay: delay|| 950,
  240. });
  241. event.stopPropagation();
  242. event.preventDefault();
  243. },
  244. });
  245. return PreviewManager;
  246. });