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.

225 lines
8.1 KiB

[MIG] web_widget_slick: Migrate to v10 * Add local slick files to use instead of CDN * Override CSS to display widget correctly * Adjust arrow button size/placement * Bump version * Rename __openerp__.py -> __manifest__.py * Rename widget_slick.js -> web_widget_slick.js * Update copyright, license (AGPL -> LGPL) * Update readme * Correct eslint errors * Change module name in various places for consistency * Improve styling of widget arrows, dots * Change css -> less * Remove unneeded slick files * Copyright 2017 -> 2016-2017 * Add OCA to authors * Use OCA icon * Fix readme * Clean up assets * Fix file permissions * Update readme with reference to example module * Fix formatting error, incorrect link * Add javascript tests * Add note to readme about functional testing with example module * Fix/cleanup javascript * Fix destroy_content() method * Move slide navigation out of slide addition loop * Remove unused variables * Remove unneeded DOM append * Reorganize files/directories * Adjust template tags (templates -> template) * Add slick-field class to field template instead of using jQuery * Misc cleanup * Adjust breakpoint settings to show fewer images by default * Enable adaptiveHeight by default * Add .img and .img-responsive classes to images * Fix dragging issues by preventing default mousedown and touchstart event behavior * Set swipeToSlide default to true * Change how slick slides are populated to allow grid mode * Fix issue causing carousel images to display improperly in some situations * Add better functional testing instructions to readme * Add roadmap to readme * Make minor styling changes * Fix issue with template loading w/ PhantomJS * Clean up template, use css class provided by widget * Remove unneeded dependency from tests * Break up render_value method * Break up destroy_content method * Add unslicking to destroy_content, add test * Clean up qweb template formatting * Fix indentation * Change widget name * Add Slick copyright information * Add padding left/right, move arrows in to avoid clipping when widget not in a sheet tag * Apply dot and arrow styles only when needed * Add _resizeCarousel() and related methods to ensure accurate carousel sizing in various views * Resize carousel on core.bus resize * Account for differences in group layouts and labels, sheet/no-sheet layouts * Adjust, clean up less * Clean up js
7 years ago
  1. /* Copyright 2017 LasLabs Inc.
  2. * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). */
  3. odoo.define_section('web_widget_slick', ['web.core', 'web.form_common'], function(test) {
  4. "use strict";
  5. function appendWidget (core, formCommon, $fix) {
  6. var fieldManager = new formCommon.DefaultFieldManager(null, {});
  7. var node = {'attrs': {}};
  8. var FieldSlickImages = core.form_widget_registry.get('one2many_slick_images');
  9. var widget = new FieldSlickImages(fieldManager, node);
  10. widget.appendTo($fix);
  11. return widget;
  12. }
  13. function imageUrl (modelName, fieldName, id) {
  14. return '/web/image/' + modelName + '/' + id + '/' + fieldName;
  15. }
  16. test('It should add a slick widget',
  17. function(assert, core, formCommon) {
  18. var $fix = $('#qunit-fixture');
  19. var widget = appendWidget(core, formCommon, $fix);
  20. var slickContainerCount = $fix.find('.slick-container').length;
  21. assert.strictEqual(slickContainerCount, 1);
  22. }
  23. );
  24. test('.init() should add defaults to options',
  25. function(assert, core, formCommon) {
  26. var $fix = $('#qunit-fixture');
  27. var fieldManager = new formCommon.DefaultFieldManager(null, {});
  28. var node = {'attrs': {}};
  29. var FieldSlickImages = core.form_widget_registry.get('one2many_slick_images');
  30. var widget = new FieldSlickImages(fieldManager, node);
  31. widget.appendTo($fix);
  32. widget.defaults.testing = 'tested';
  33. widget.init(fieldManager, node);
  34. assert.strictEqual(widget.options.testing, 'tested');
  35. }
  36. );
  37. test('.destroy_content() should remove images',
  38. function(assert, core, formCommon) {
  39. var $fix = $('#qunit-fixture');
  40. var widget = appendWidget(core, formCommon, $fix);
  41. var $slickImage = $('<div><img></div>');
  42. widget.$slick.slick('slickAdd', $slickImage);
  43. widget.destroy_content();
  44. var slickImageCount = widget.$slick.find('img').length;
  45. assert.strictEqual(slickImageCount, 0);
  46. }
  47. );
  48. test('.destroy_content() should remove carousel',
  49. function (assert, core, formCommon) {
  50. var $fix = $('#qunit-fixture');
  51. var widget = appendWidget(core, formCommon, $fix);
  52. widget.destroy_content();
  53. var slickChildren = widget.$slick.children().length;
  54. assert.strictEqual(slickChildren, 0);
  55. }
  56. );
  57. test('.render_value() should add images corresponding to field value',
  58. function(assert, core, formCommon) {
  59. var $fix = $('#qunit-fixture');
  60. var widget = appendWidget(core, formCommon, $fix);
  61. var fieldValues = [1, 2];
  62. widget.set({'value': fieldValues});
  63. widget.render_value();
  64. var slickImages = widget.$slick.find('img');
  65. var slickImageUrls = slickImages.map(function() {
  66. return $(this).data('lazy');
  67. }).get();
  68. var modelName = widget.options.modelName;
  69. var fieldName = widget.options.fieldName;
  70. var expectedUrls = fieldValues.map(function(id) {
  71. return '/web/image/' + modelName + '/' + id + '/' + fieldName;
  72. });
  73. assert.deepEqual(slickImageUrls, expectedUrls);
  74. }
  75. );
  76. test('._resizeCarousel() should resize the widget',
  77. function (assert, core, formCommon) {
  78. var $fix = $('#qunit-fixture');
  79. var $nosheet = $('<div class="o_form_nosheet"></div>');
  80. $fix.append($nosheet);
  81. var widget = appendWidget(core, formCommon, $nosheet);
  82. var setWidth = 50;
  83. widget.$slick.outerWidth(setWidth);
  84. widget._resizeCarousel();
  85. assert.notStrictEqual(widget.$slick.outerWidth(), setWidth);
  86. }
  87. );
  88. test('._resizeCarousel() should be called when container is resized',
  89. function (assert, core, formCommon) {
  90. var $fix = $('#qunit-fixture');
  91. var $nosheet = $('<div class="o_form_nosheet"></div>');
  92. $fix.append($nosheet);
  93. var widget = appendWidget(core, formCommon, $nosheet);
  94. var setWidth = 50;
  95. widget.$slick.outerWidth(setWidth);
  96. core.bus.trigger('resize');
  97. assert.notStrictEqual(widget.$slick.outerWidth(), setWidth);
  98. }
  99. );
  100. test('._resizeLabelWidth() should return the width of the preceding ' +
  101. 'sibling label cell if it exists',
  102. function (assert, core, formCommon) {
  103. var $fix = $('#qunit-fixture');
  104. var widget = appendWidget(core, formCommon, $fix);
  105. var width = 100;
  106. var $cell = $('<td style="width:10px;"></td>');
  107. var $labelCell = $('<td class="o_td_label"></td>');
  108. $labelCell.outerWidth(width);
  109. widget.$slick.append($labelCell);
  110. widget.$slick.append($cell);
  111. assert.strictEqual(widget._resizeLabelWidth($cell), width);
  112. }
  113. );
  114. test('._resizeLabelWidth() should return 0 if the previous sibling cell ' +
  115. ' of the provided element is not a label cell',
  116. function (assert, core, formCommon) {
  117. var $fix = $('#qunit-fixture');
  118. var widget = appendWidget(core, formCommon, $fix);
  119. var width = '100px';
  120. var $cell = $('<td></td>');
  121. widget.$slick.append($('<td style="width:' + width + ';"></td>'));
  122. widget.$slick.append($cell);
  123. assert.strictEqual(widget._resizeLabelWidth($cell), 0);
  124. }
  125. );
  126. test('._resizeMarginWidth() should return the total left and right ' +
  127. ' margins of the provided element',
  128. function (assert, core, formCommon) {
  129. var $fix = $('#qunit-fixture');
  130. var widget = appendWidget(core, formCommon, $fix);
  131. var elementStyle = 'margin-left: 12px; margin-right: 7px;';
  132. var marginTotal = 19;
  133. var $element = $('<div style="' + elementStyle + '"></div>');
  134. widget.$slick.append($element);
  135. assert.strictEqual(widget._resizeMarginWidth($element), marginTotal);
  136. }
  137. );
  138. test('._resizeMaxWidth() should return the width of the closest sheet element',
  139. function (assert, core, formCommon) {
  140. var $fix = $('#qunit-fixture');
  141. var $sheet1 = $('<div class="o_form_sheet"></div>');
  142. var $sheet2 = $('<div class="o_form_sheet"></div>');
  143. var expectedWidth = 266;
  144. $sheet1.width(700);
  145. $sheet2.width(expectedWidth);
  146. $sheet1.append($sheet2);
  147. $fix.append($sheet1);
  148. var widget = appendWidget(core, formCommon, $sheet2);
  149. assert.strictEqual(widget._resizeMaxWidth(), expectedWidth);
  150. }
  151. );
  152. test('._resizeMaxWidth() should return the width of the closest nosheet element',
  153. function (assert, core, formCommon) {
  154. var $fix = $('#qunit-fixture');
  155. var $nosheet1 = $('<div class="o_form_nosheet"></div>');
  156. var $nosheet2 = $('<div class="o_form_nosheet"></div>');
  157. var expectedWidth = 266;
  158. $nosheet1.width(700);
  159. $nosheet2.width(expectedWidth);
  160. $nosheet1.append($nosheet2);
  161. $fix.append($nosheet1);
  162. var widget = appendWidget(core, formCommon, $nosheet2);
  163. assert.strictEqual(widget._resizeMaxWidth(), expectedWidth);
  164. }
  165. );
  166. test('._resizeScaledWidth() should return the provided integer, scaled' +
  167. 'to the % width in the provided element style attribute',
  168. function (assert, core, formCommon) {
  169. var $fix = $('#qunit-fixture');
  170. var widget = appendWidget(core, formCommon, $fix);
  171. var givenWidth = 100;
  172. var widthPercent = 54;
  173. var expectedWidth = widthPercent;
  174. var $cell = $('<td style="width:' + widthPercent + '%;"></td>');
  175. assert.strictEqual(widget._resizeScaledWidth($cell, givenWidth), expectedWidth);
  176. }
  177. );
  178. });