Files
web/web_widget_slick/static/tests/js/web_widget_slick.js
Brenton Hughes d59af25db3 [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
2017-09-11 07:44:34 -07:00

226 lines
8.1 KiB
JavaScript

/* Copyright 2017 LasLabs Inc.
* License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). */
odoo.define_section('web_widget_slick', ['web.core', 'web.form_common'], function(test) {
"use strict";
function appendWidget (core, formCommon, $fix) {
var fieldManager = new formCommon.DefaultFieldManager(null, {});
var node = {'attrs': {}};
var FieldSlickImages = core.form_widget_registry.get('one2many_slick_images');
var widget = new FieldSlickImages(fieldManager, node);
widget.appendTo($fix);
return widget;
}
function imageUrl (modelName, fieldName, id) {
return '/web/image/' + modelName + '/' + id + '/' + fieldName;
}
test('It should add a slick widget',
function(assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
var slickContainerCount = $fix.find('.slick-container').length;
assert.strictEqual(slickContainerCount, 1);
}
);
test('.init() should add defaults to options',
function(assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var fieldManager = new formCommon.DefaultFieldManager(null, {});
var node = {'attrs': {}};
var FieldSlickImages = core.form_widget_registry.get('one2many_slick_images');
var widget = new FieldSlickImages(fieldManager, node);
widget.appendTo($fix);
widget.defaults.testing = 'tested';
widget.init(fieldManager, node);
assert.strictEqual(widget.options.testing, 'tested');
}
);
test('.destroy_content() should remove images',
function(assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
var $slickImage = $('<div><img></div>');
widget.$slick.slick('slickAdd', $slickImage);
widget.destroy_content();
var slickImageCount = widget.$slick.find('img').length;
assert.strictEqual(slickImageCount, 0);
}
);
test('.destroy_content() should remove carousel',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
widget.destroy_content();
var slickChildren = widget.$slick.children().length;
assert.strictEqual(slickChildren, 0);
}
);
test('.render_value() should add images corresponding to field value',
function(assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
var fieldValues = [1, 2];
widget.set({'value': fieldValues});
widget.render_value();
var slickImages = widget.$slick.find('img');
var slickImageUrls = slickImages.map(function() {
return $(this).data('lazy');
}).get();
var modelName = widget.options.modelName;
var fieldName = widget.options.fieldName;
var expectedUrls = fieldValues.map(function(id) {
return '/web/image/' + modelName + '/' + id + '/' + fieldName;
});
assert.deepEqual(slickImageUrls, expectedUrls);
}
);
test('._resizeCarousel() should resize the widget',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var $nosheet = $('<div class="o_form_nosheet"></div>');
$fix.append($nosheet);
var widget = appendWidget(core, formCommon, $nosheet);
var setWidth = 50;
widget.$slick.outerWidth(setWidth);
widget._resizeCarousel();
assert.notStrictEqual(widget.$slick.outerWidth(), setWidth);
}
);
test('._resizeCarousel() should be called when container is resized',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var $nosheet = $('<div class="o_form_nosheet"></div>');
$fix.append($nosheet);
var widget = appendWidget(core, formCommon, $nosheet);
var setWidth = 50;
widget.$slick.outerWidth(setWidth);
core.bus.trigger('resize');
assert.notStrictEqual(widget.$slick.outerWidth(), setWidth);
}
);
test('._resizeLabelWidth() should return the width of the preceding ' +
'sibling label cell if it exists',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
var width = 100;
var $cell = $('<td style="width:10px;"></td>');
var $labelCell = $('<td class="o_td_label"></td>');
$labelCell.outerWidth(width);
widget.$slick.append($labelCell);
widget.$slick.append($cell);
assert.strictEqual(widget._resizeLabelWidth($cell), width);
}
);
test('._resizeLabelWidth() should return 0 if the previous sibling cell ' +
' of the provided element is not a label cell',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
var width = '100px';
var $cell = $('<td></td>');
widget.$slick.append($('<td style="width:' + width + ';"></td>'));
widget.$slick.append($cell);
assert.strictEqual(widget._resizeLabelWidth($cell), 0);
}
);
test('._resizeMarginWidth() should return the total left and right ' +
' margins of the provided element',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
var elementStyle = 'margin-left: 12px; margin-right: 7px;';
var marginTotal = 19;
var $element = $('<div style="' + elementStyle + '"></div>');
widget.$slick.append($element);
assert.strictEqual(widget._resizeMarginWidth($element), marginTotal);
}
);
test('._resizeMaxWidth() should return the width of the closest sheet element',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var $sheet1 = $('<div class="o_form_sheet"></div>');
var $sheet2 = $('<div class="o_form_sheet"></div>');
var expectedWidth = 266;
$sheet1.width(700);
$sheet2.width(expectedWidth);
$sheet1.append($sheet2);
$fix.append($sheet1);
var widget = appendWidget(core, formCommon, $sheet2);
assert.strictEqual(widget._resizeMaxWidth(), expectedWidth);
}
);
test('._resizeMaxWidth() should return the width of the closest nosheet element',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var $nosheet1 = $('<div class="o_form_nosheet"></div>');
var $nosheet2 = $('<div class="o_form_nosheet"></div>');
var expectedWidth = 266;
$nosheet1.width(700);
$nosheet2.width(expectedWidth);
$nosheet1.append($nosheet2);
$fix.append($nosheet1);
var widget = appendWidget(core, formCommon, $nosheet2);
assert.strictEqual(widget._resizeMaxWidth(), expectedWidth);
}
);
test('._resizeScaledWidth() should return the provided integer, scaled' +
'to the % width in the provided element style attribute',
function (assert, core, formCommon) {
var $fix = $('#qunit-fixture');
var widget = appendWidget(core, formCommon, $fix);
var givenWidth = 100;
var widthPercent = 54;
var expectedWidth = widthPercent;
var $cell = $('<td style="width:' + widthPercent + '%;"></td>');
assert.strictEqual(widget._resizeScaledWidth($cell, givenWidth), expectedWidth);
}
);
});