~opencrea/+junk/aprobio

« back to all changes in this revision

Viewing changes to web_widget_color/static/src/js/widget.js

  • Committer: joannes
  • Date: 2017-05-17 09:40:42 UTC
  • Revision ID: joannes@debian-20170517094042-47q3j6on72w2h1il
community module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
odoo.define('web.web_widget_color', function(require) {
 
2
    "use strict";
 
3
 
 
4
    var core = require('web.core');
 
5
    var widget = require('web.form_widgets');
 
6
    var FormView = require('web.FormView');
 
7
 
 
8
    var QWeb = core.qweb;
 
9
    var _lt = core._lt;
 
10
 
 
11
    var _super_getDir = jscolor.getDir.prototype;
 
12
    jscolor.getDir = function () {
 
13
        var dir = _super_getDir.constructor();
 
14
        if (dir.indexOf('web_widget_color') === -1) {
 
15
            jscolor.dir = 'web_widget_color/static/lib/jscolor/';
 
16
        }
 
17
        return jscolor.dir;
 
18
    };
 
19
 
 
20
    var FieldColor = widget.FieldChar.extend({
 
21
        template: 'FieldColor',
 
22
        widget_class: 'oe_form_field_color',
 
23
        is_syntax_valid: function () {
 
24
            var $input = this.$('input');
 
25
            if (!this.get("effective_readonly") && $input.size() > 0) {
 
26
                var val = $input.val();
 
27
                var isOk = /^#[0-9A-F]{6}$/i.test(val);
 
28
                if (!isOk) {
 
29
                    return false;
 
30
                }
 
31
                try {
 
32
                    this.parse_value(this.$('input').val(), '');
 
33
                    return true;
 
34
                } catch (e) {
 
35
                    return false;
 
36
                }
 
37
            }
 
38
            return true;
 
39
        },
 
40
        store_dom_value: function() {
 
41
            if (!this.silent) {
 
42
                if (!this.get('effective_readonly') &&
 
43
                    this.$('input').val() !== '' &&
 
44
                    this.is_syntax_valid()) {
 
45
                    // We use internal_set_value because we were called by
 
46
                    // ``.commit_value()`` which is called by a ``.set_value()``
 
47
                    // itself called because of a ``onchange`` event
 
48
                    this.internal_set_value(
 
49
                        this.parse_value(
 
50
                            this.$('input').val())
 
51
                        );
 
52
                }
 
53
            }
 
54
            },
 
55
        render_value: function () {
 
56
            var show_value = this.format_value(this.get('value'), '');
 
57
            if (!this.get("effective_readonly")) {
 
58
                var $input = this.$el.find('input');
 
59
                $input.val(show_value);
 
60
                $input.css("background-color", show_value);
 
61
                jscolor.init(this.$el[0]);
 
62
            } else {
 
63
                this.$(".oe_form_char_content").text(show_value);
 
64
                this.$('span').css("background-color", show_value);
 
65
            }
 
66
        }
 
67
    });
 
68
 
 
69
    core.form_widget_registry.add('color', FieldColor);
 
70
 
 
71
    /*
 
72
     * Init jscolor for each editable mode on view form
 
73
     */
 
74
    FormView.include({
 
75
        on_button_edit: function () {
 
76
            this._super();
 
77
            jscolor.init(this.$el[0]);
 
78
        },
 
79
        on_button_create: function () {
 
80
            this._super();
 
81
            jscolor.init(this.$el[0]);
 
82
        }
 
83
    });
 
84
 
 
85
    return {
 
86
        FieldColor: FieldColor
 
87
    };
 
88
});