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.

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