summaryrefslogtreecommitdiff
path: root/addons/web
diff options
context:
space:
mode:
authorJigar Patel <jat@openerp.com>2017-05-09 15:02:17 +0530
committerYannick Tivisse <yti@odoo.com>2017-08-09 15:11:53 +0200
commit2adc9d50321d58fa7f33a0231a5594d338a3ffa4 (patch)
tree5ed676b1be7eb33ce50120825550dd091e087ae9 /addons/web
parentdd427405e2f1683a1b46d5fba1d271e77ea34e93 (diff)
[IMP] account: Add a new boolean_favorite widget
Purpose ======= Projects and soon sales teams have a manual "favorite" button that calls a toggle_favorite method It should actually be a widget on a boolean field Specifications ============== Develop a favorite widget for kanban view that allows to toggle a boolean field Probably update boolean field to allow inverse method on it Functionally nothing changed.
Diffstat (limited to 'addons/web')
-rw-r--r--addons/web/static/src/js/fields/basic_fields.js53
-rw-r--r--addons/web/static/src/js/fields/field_registry.js1
-rw-r--r--addons/web/static/src/less/fields.less16
-rw-r--r--addons/web/static/tests/fields/basic_fields_tests.js123
4 files changed, 193 insertions, 0 deletions
diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js
index 2de5a73802d..2d18a0ddb56 100644
--- a/addons/web/static/src/js/fields/basic_fields.js
+++ b/addons/web/static/src/js/fields/basic_fields.js
@@ -1449,6 +1449,58 @@ var StateSelectionWidget = AbstractField.extend({
},
});
+var FavoriteWidget = AbstractField.extend({
+ className: 'o_favorite',
+ events: {
+ 'click': '_setFavorite'
+ },
+ supportedFieldTypes: ['boolean'],
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * A boolean field is always set since false is a valid value.
+ *
+ * @override
+ */
+ isSet: function () {
+ return true;
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * Render favorite icon based on state
+ *
+ * @override
+ * @private
+ */
+ _render: function () {
+ var template = this.attrs.nolabel ? '<a href="#"><i class="fa %s" title="%s"></i></a>' : '<a href="#"><i class="fa %s"></i> %s</a>';
+ this.$el.empty().append(_.str.sprintf(template, this.value ? 'fa-star' : 'fa-star-o', this.value ? _t('Remove from Favorites') : _t('Add to Favorites')));
+ },
+
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * Toggle favorite state
+ *
+ * @private
+ * @param {MouseEvent} event
+ */
+ _setFavorite: function (event) {
+ event.preventDefault();
+ event.stopPropagation();
+ this._setValue(!this.value);
+ },
+});
+
var LabelSelection = AbstractField.extend({
supportedFieldTypes: ['selection'],
@@ -2350,6 +2402,7 @@ return {
AttachmentImage: AttachmentImage,
LabelSelection: LabelSelection,
StateSelectionWidget: StateSelectionWidget,
+ FavoriteWidget: FavoriteWidget,
PriorityWidget: PriorityWidget,
StatInfo: StatInfo,
UrlWidget: UrlWidget,
diff --git a/addons/web/static/src/js/fields/field_registry.js b/addons/web/static/src/js/fields/field_registry.js
index 91bba587117..019a98e51f1 100644
--- a/addons/web/static/src/js/fields/field_registry.js
+++ b/addons/web/static/src/js/fields/field_registry.js
@@ -40,6 +40,7 @@ registry
.add('label_selection', basic_fields.LabelSelection)
.add('state_selection', basic_fields.StateSelectionWidget)
.add('kanban_state_selection', basic_fields.StateSelectionWidget)
+ .add('boolean_favorite', basic_fields.FavoriteWidget)
.add('boolean_button', basic_fields.FieldBooleanButton)
.add('boolean_toggle', basic_fields.BooleanToggle)
.add('statinfo', basic_fields.StatInfo)
diff --git a/addons/web/static/src/less/fields.less b/addons/web/static/src/less/fields.less
index 02885ef3514..8463dd312cc 100644
--- a/addons/web/static/src/less/fields.less
+++ b/addons/web/static/src/less/fields.less
@@ -180,6 +180,22 @@
}
}
+ // Favorite
+ &.o_favorite {
+ i.fa {
+ font-size: 16px;
+ }
+ i.fa-star-o {
+ color: @odoo-main-color-muted;
+ &:hover {
+ color: gold;
+ }
+ }
+ i.fa-star {
+ color: gold;
+ }
+ }
+
// Specific success color for toggle_button widget
.fa.o_toggle_button_success {
color: @brand-success;
diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js
index 508e86d9cd3..c2ab8121c16 100644
--- a/addons/web/static/tests/fields/basic_fields_tests.js
+++ b/addons/web/static/tests/fields/basic_fields_tests.js
@@ -2997,6 +2997,129 @@ QUnit.module('basic_fields', {
});
+ QUnit.module('FavoriteWidget');
+
+ QUnit.test('favorite widget in kanban view', function (assert) {
+ assert.expect(4);
+
+ var kanban = createView({
+ View: KanbanView,
+ model: 'partner',
+ data: this.data,
+ arch: '<kanban class="o_kanban_test">' +
+ '<templates>' +
+ '<t t-name="kanban-box">' +
+ '<div>' +
+ '<field name="bar" widget="boolean_favorite" />' +
+ '</div>' +
+ '</t>' +
+ '</templates>' +
+ '</kanban>',
+ domain: [['id', '=', 1]],
+ });
+
+ assert.strictEqual(kanban.$('.o_kanban_record .o_field_widget.o_favorite > a i.fa.fa-star').length, 1,
+ 'should be favorite');
+ assert.strictEqual(kanban.$('.o_kanban_record .o_field_widget.o_favorite > a').text(), ' Remove from Favorites',
+ 'the label should say "Remove from Favorites"');
+
+ // click on favorite
+ kanban.$('.o_field_widget.o_favorite').click();
+ assert.strictEqual(kanban.$('.o_kanban_record .o_field_widget.o_favorite > a i.fa.fa-star').length, 0,
+ 'should not be favorite');
+ assert.strictEqual(kanban.$('.o_kanban_record .o_field_widget.o_favorite > a').text(), ' Add to Favorites',
+ 'the label should say "Add to Favorites"');
+
+ kanban.destroy();
+ });
+
+ QUnit.test('favorite widget in form view', function (assert) {
+ assert.expect(10);
+
+ var form = createView({
+ View: FormView,
+ model: 'partner',
+ data: this.data,
+ arch: '<form string="Partners">' +
+ '<sheet>' +
+ '<group>' +
+ '<field name="bar" widget="boolean_favorite" />' +
+ '</group>' +
+ '</sheet>' +
+ '</form>',
+ res_id: 1,
+ });
+
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a i.fa.fa-star').length, 1,
+ 'should be favorite');
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a').text(), ' Remove from Favorites',
+ 'the label should say "Remove from Favorites"');
+
+ // click on favorite
+ form.$('.o_field_widget.o_favorite').click();
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a i.fa.fa-star').length, 0,
+ 'should not be favorite');
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a').text(), ' Add to Favorites',
+ 'the label should say "Add to Favorites"');
+
+ // switch to edit mode
+ form.$buttons.find('.o_form_button_edit').click();
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a i.fa.fa-star-o').length, 1,
+ 'should not be favorite');
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a').text(), ' Add to Favorites',
+ 'the label should say "Add to Favorites"');
+
+ // click on favorite
+ form.$('.o_field_widget.o_favorite').click();
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a i.fa.fa-star').length, 1,
+ 'should be favorite');
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a').text(), ' Remove from Favorites',
+ 'the label should say "Remove from Favorites"');
+
+ // save
+ form.$buttons.find('.o_form_button_save').click();
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a i.fa.fa-star').length, 1,
+ 'should be favorite');
+ assert.strictEqual(form.$('.o_field_widget.o_favorite > a').text(), ' Remove from Favorites',
+ 'the label should say "Remove from Favorites"');
+
+ form.destroy();
+ });
+
+ QUnit.test('favorite widget in editable list view without label', function (assert) {
+ assert.expect(4);
+
+ var list = createView({
+ View: ListView,
+ model: 'partner',
+ data: this.data,
+ arch: '<tree editable="bottom">' +
+ '<field name="bar" widget="boolean_favorite" nolabel="1" />' +
+ '</tree>',
+ });
+
+ assert.strictEqual(list.$('.o_data_row:first .o_field_widget.o_favorite > a i.fa.fa-star').length, 1,
+ 'should be favorite');
+
+ // switch to edit mode
+ list.$('tbody td:not(.o_list_record_selector)').first().click();
+ assert.strictEqual(list.$('.o_data_row:first .o_field_widget.o_favorite > a i.fa.fa-star').length, 1,
+ 'should be favorite');
+
+ // click on favorite
+ list.$('.o_data_row:first .o_field_widget.o_favorite').click();
+ assert.strictEqual(list.$('.o_data_row:first .o_field_widget.o_favorite > a i.fa.fa-star').length, 0,
+ 'should not be favorite');
+
+ // save
+ list.$buttons.find('.o_list_button_save').click();
+ assert.strictEqual(list.$('.o_data_row:first .o_field_widget.o_favorite > a i.fa.fa-star-o').length, 1,
+ 'should not be favorite');
+
+ list.destroy();
+ });
+
+
QUnit.module('LabelSelectionWidget');
QUnit.test('label_selection widget in form view', function (assert) {