"].join(""));
return container;
},
// single
- opening: function () {
- this.search.show();
- this.parent.opening.apply(this, arguments);
- this.dropdown.removeClass("select2-offscreen");
+ enableInterface: function() {
+ if (this.parent.enableInterface.apply(this, arguments)) {
+ this.focusser.prop("disabled", !this.isInterfaceEnabled());
+ }
},
// single
- close: function () {
+ opening: function () {
+ var el, range, len;
+
+ if (this.opts.minimumResultsForSearch >= 0) {
+ this.showSearch(true);
+ }
+
+ this.parent.opening.apply(this, arguments);
+
+ if (this.showSearchInput !== false) {
+ // IE appends focusser.val() at the end of field :/ so we manually insert it at the beginning using a range
+ // all other browsers handle this just fine
+
+ this.search.val(this.focusser.val());
+ }
+ this.search.focus();
+ // move the cursor to the end after focussing, otherwise it will be at the beginning and
+ // new text will appear *before* focusser.val()
+ el = this.search.get(0);
+ if (el.createTextRange) {
+ range = el.createTextRange();
+ range.collapse(false);
+ range.select();
+ } else if (el.setSelectionRange) {
+ len = this.search.val().length;
+ el.setSelectionRange(len, len);
+ }
+
+ // initializes search's value with nextSearchTerm (if defined by user)
+ // ignore nextSearchTerm if the dropdown is opened by the user pressing a letter
+ if(this.search.val() === "") {
+ if(this.nextSearchTerm != undefined){
+ this.search.val(this.nextSearchTerm);
+ this.search.select();
+ }
+ }
+
+ this.focusser.prop("disabled", true).val("");
+ this.updateResults(true);
+ this.opts.element.trigger($.Event("select2-open"));
+ },
+
+ // single
+ close: function (params) {
if (!this.opened()) return;
this.parent.close.apply(this, arguments);
- this.dropdown.removeAttr("style").addClass("select2-offscreen").insertAfter(this.selection).show();
+
+ params = params || {focus: true};
+ this.focusser.removeAttr("disabled");
+
+ if (params.focus) {
+ this.focusser.focus();
+ }
},
// single
focus: function () {
- this.close();
- this.selection.focus();
+ if (this.opened()) {
+ this.close();
+ } else {
+ this.focusser.removeAttr("disabled");
+ this.focusser.focus();
+ }
},
// single
isFocused: function () {
- return this.selection[0] === document.activeElement;
+ return this.container.hasClass("select2-container-active");
},
// single
cancel: function () {
this.parent.cancel.apply(this, arguments);
- this.selection.focus();
+ this.focusser.removeAttr("disabled");
+ this.focusser.focus();
+ },
+
+ // single
+ destroy: function() {
+ $("label[for='" + this.focusser.attr('id') + "']")
+ .attr('for', this.opts.element.attr("id"));
+ this.parent.destroy.apply(this, arguments);
},
// single
@@ -1427,13 +1919,28 @@
var selection,
container = this.container,
- dropdown = this.dropdown,
- clickingInside = false;
+ dropdown = this.dropdown;
+
+ if (this.opts.minimumResultsForSearch < 0) {
+ this.showSearch(false);
+ } else {
+ this.showSearch(true);
+ }
this.selection = selection = container.find(".select2-choice");
- this.search.bind("keydown", this.bind(function (e) {
- if (!this.enabled) return;
+ this.focusser = container.find(".select2-focusser");
+
+ // rewrite labels from original element to focusser
+ this.focusser.attr("id", "s2id_autogen"+nextUid());
+
+ $("label[for='" + this.opts.element.attr("id") + "']")
+ .attr('for', this.focusser.attr('id'));
+
+ this.focusser.attr("tabindex", this.elementTabIndex);
+
+ this.search.on("keydown", this.bind(function (e) {
+ if (!this.isInterfaceEnabled()) return;
if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
// prevent the page from scrolling
@@ -1441,154 +1948,150 @@
return;
}
- if (this.opened()) {
- switch (e.which) {
- case KEY.UP:
- case KEY.DOWN:
- this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
- killEvent(e);
- return;
- case KEY.TAB:
- case KEY.ENTER:
- this.selectHighlighted();
- killEvent(e);
- return;
- case KEY.ESC:
- this.cancel(e);
- killEvent(e);
- return;
- }
- } else {
-
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
+ switch (e.which) {
+ case KEY.UP:
+ case KEY.DOWN:
+ this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
+ killEvent(e);
return;
- }
-
- if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
+ case KEY.ENTER:
+ this.selectHighlighted();
+ killEvent(e);
return;
- }
-
- this.open();
-
- if (e.which === KEY.ENTER) {
- // do not propagate the event otherwise we open, and propagate enter which closes
+ case KEY.TAB:
+ this.selectHighlighted({noFocus: true});
+ return;
+ case KEY.ESC:
+ this.cancel(e);
+ killEvent(e);
return;
- }
}
}));
- this.search.bind("focus", this.bind(function() {
- this.selection.attr("tabIndex", "-1");
- }));
- this.search.bind("blur", this.bind(function() {
- if (!this.opened()) this.container.removeClass("select2-container-active");
- window.setTimeout(this.bind(function() { this.selection.attr("tabIndex", this.opts.element.attr("tabIndex")); }), 10);
- }));
-
- selection.bind("mousedown", this.bind(function (e) {
- clickingInside = true;
-
- if (this.opened()) {
- this.close();
- this.selection.focus();
- } else if (this.enabled) {
- this.open();
+ this.search.on("blur", this.bind(function(e) {
+ // a workaround for chrome to keep the search field focussed when the scroll bar is used to scroll the dropdown.
+ // without this the search field loses focus which is annoying
+ if (document.activeElement === this.body().get(0)) {
+ window.setTimeout(this.bind(function() {
+ this.search.focus();
+ }), 0);
}
-
- clickingInside = false;
}));
- dropdown.bind("mousedown", this.bind(function() { this.search.focus(); }));
+ this.focusser.on("keydown", this.bind(function (e) {
+ if (!this.isInterfaceEnabled()) return;
- selection.bind("focus", this.bind(function() {
- this.container.addClass("select2-container-active");
- // hide the search so the tab key does not focus on it
- this.search.attr("tabIndex", "-1");
- }));
-
- selection.bind("blur", this.bind(function() {
- if (!this.opened()) {
- this.container.removeClass("select2-container-active");
- }
- window.setTimeout(this.bind(function() { this.search.attr("tabIndex", this.opts.element.attr("tabIndex")); }), 10);
- }));
-
- selection.bind("keydown", this.bind(function(e) {
- if (!this.enabled) return;
-
- if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
- // prevent the page from scrolling
- killEvent(e);
- return;
- }
-
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e)
- || e.which === KEY.ESC) {
+ if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
return;
}
if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
+ killEvent(e);
return;
}
- if (e.which == KEY.DELETE) {
+ if (e.which == KEY.DOWN || e.which == KEY.UP
+ || (e.which == KEY.ENTER && this.opts.openOnEnter)) {
+
+ if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) return;
+
+ this.open();
+ killEvent(e);
+ return;
+ }
+
+ if (e.which == KEY.DELETE || e.which == KEY.BACKSPACE) {
if (this.opts.allowClear) {
this.clear();
}
- return;
- }
-
- this.open();
-
- if (e.which === KEY.ENTER) {
- // do not propagate the event otherwise we open, and propagate enter which closes
killEvent(e);
return;
}
-
- // do not set the search input value for non-alpha-numeric keys
- // otherwise pressing down results in a '(' being set in the search field
- if (e.which < 48 ) { // '0' == 48
- killEvent(e);
- return;
- }
-
- var keyWritten = String.fromCharCode(e.which).toLowerCase();
-
- if (e.shiftKey) {
- keyWritten = keyWritten.toUpperCase();
- }
-
- // focus the field before calling val so the cursor ends up after the value instead of before
- this.search.focus();
- this.search.val(keyWritten);
-
- // prevent event propagation so it doesnt replay on the now focussed search field and result in double key entry
- killEvent(e);
}));
- selection.delegate("abbr", "mousedown", this.bind(function (e) {
- if (!this.enabled) return;
+
+ installKeyUpChangeEvent(this.focusser);
+ this.focusser.on("keyup-change input", this.bind(function(e) {
+ if (this.opts.minimumResultsForSearch >= 0) {
+ e.stopPropagation();
+ if (this.opened()) return;
+ this.open();
+ }
+ }));
+
+ selection.on("mousedown", "abbr", this.bind(function (e) {
+ if (!this.isInterfaceEnabled()) return;
this.clear();
- killEvent(e);
+ killEventImmediately(e);
this.close();
- this.triggerChange();
this.selection.focus();
}));
- this.setPlaceholder();
+ selection.on("mousedown", this.bind(function (e) {
- this.search.bind("focus", this.bind(function() {
+ if (!this.container.hasClass("select2-container-active")) {
+ this.opts.element.trigger($.Event("select2-focus"));
+ }
+
+ if (this.opened()) {
+ this.close();
+ } else if (this.isInterfaceEnabled()) {
+ this.open();
+ }
+
+ killEvent(e);
+ }));
+
+ dropdown.on("mousedown", this.bind(function() { this.search.focus(); }));
+
+ selection.on("focus", this.bind(function(e) {
+ killEvent(e);
+ }));
+
+ this.focusser.on("focus", this.bind(function(){
+ if (!this.container.hasClass("select2-container-active")) {
+ this.opts.element.trigger($.Event("select2-focus"));
+ }
+ this.container.addClass("select2-container-active");
+ })).on("blur", this.bind(function() {
+ if (!this.opened()) {
+ this.container.removeClass("select2-container-active");
+ this.opts.element.trigger($.Event("select2-blur"));
+ }
+ }));
+ this.search.on("focus", this.bind(function(){
+ if (!this.container.hasClass("select2-container-active")) {
+ this.opts.element.trigger($.Event("select2-focus"));
+ }
this.container.addClass("select2-container-active");
}));
+
+ this.initContainerWidth();
+ this.opts.element.addClass("select2-offscreen");
+ this.setPlaceholder();
+
},
// single
- clear: function() {
- this.opts.element.val("");
- this.selection.find("span").empty();
- this.selection.removeData("select2-data");
- this.setPlaceholder();
+ clear: function(triggerChange) {
+ var data=this.selection.data("select2-data");
+ if (data) { // guard against queued quick consecutive clicks
+ var evt = $.Event("select2-clearing");
+ this.opts.element.trigger(evt);
+ if (evt.isDefaultPrevented()) {
+ return;
+ }
+ var placeholderOption = this.getPlaceholderOption();
+ this.opts.element.val(placeholderOption ? placeholderOption.val() : "");
+ this.selection.find(".select2-chosen").empty();
+ this.selection.removeData("select2-data");
+ this.setPlaceholder();
+
+ if (triggerChange !== false){
+ this.opts.element.trigger({ type: "select2-removed", val: this.id(data), choice: data });
+ this.triggerChange({removed:data});
+ }
+ }
},
/**
@@ -1597,7 +2100,8 @@
// single
initSelection: function () {
var selected;
- if (this.opts.element.val() === "") {
+ if (this.isPlaceholderOptionSelected()) {
+ this.updateSelection(null);
this.close();
this.setPlaceholder();
} else {
@@ -1612,47 +2116,87 @@
}
},
+ isPlaceholderOptionSelected: function() {
+ var placeholderOption;
+ if (!this.getPlaceholder()) return false; // no placeholder specified so no option should be considered
+ return ((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.prop("selected"))
+ || (this.opts.element.val() === "")
+ || (this.opts.element.val() === undefined)
+ || (this.opts.element.val() === null);
+ },
+
// single
prepareOpts: function () {
- var opts = this.parent.prepareOpts.apply(this, arguments);
+ var opts = this.parent.prepareOpts.apply(this, arguments),
+ self=this;
if (opts.element.get(0).tagName.toLowerCase() === "select") {
// install the selection initializer
opts.initSelection = function (element, callback) {
- var selected = element.find(":selected");
+ var selected = element.find("option").filter(function() { return this.selected });
// a single select box always has a value, no need to null check 'selected'
- if ($.isFunction(callback))
- callback({id: selected.attr("value"), text: selected.text()});
+ callback(self.optionToData(selected));
+ };
+ } else if ("data" in opts) {
+ // install default initSelection when applied to hidden input and data is local
+ opts.initSelection = opts.initSelection || function (element, callback) {
+ var id = element.val();
+ //search in data by id, storing the actual matching item
+ var match = null;
+ opts.query({
+ matcher: function(term, text, el){
+ var is_match = equal(id, opts.id(el));
+ if (is_match) {
+ match = el;
+ }
+ return is_match;
+ },
+ callback: !$.isFunction(callback) ? $.noop : function() {
+ callback(match);
+ }
+ });
};
}
return opts;
},
+ // single
+ getPlaceholder: function() {
+ // if a placeholder is specified on a single select without a valid placeholder option ignore it
+ if (this.select) {
+ if (this.getPlaceholderOption() === undefined) {
+ return undefined;
+ }
+ }
+
+ return this.parent.getPlaceholder.apply(this, arguments);
+ },
+
// single
setPlaceholder: function () {
var placeholder = this.getPlaceholder();
- if (this.opts.element.val() === "" && placeholder !== undefined) {
+ if (this.isPlaceholderOptionSelected() && placeholder !== undefined) {
- // check for a first blank option if attached to a select
- if (this.select && this.select.find("option:first").text() !== "") return;
+ // check for a placeholder option if attached to a select
+ if (this.select && this.getPlaceholderOption() === undefined) return;
- this.selection.find("span").html(this.opts.escapeMarkup(placeholder));
+ this.selection.find(".select2-chosen").html(this.opts.escapeMarkup(placeholder));
this.selection.addClass("select2-default");
- this.selection.find("abbr").hide();
+ this.container.removeClass("select2-allowclear");
}
},
// single
- postprocessResults: function (data, initial) {
+ postprocessResults: function (data, initial, noHighlightUpdate) {
var selected = 0, self = this, showSearchInput = true;
// find the selected element in the result list
- this.results.find(".select2-result-selectable").each2(function (i, elm) {
+ this.findHighlightableChoices().each2(function (i, elm) {
if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) {
selected = i;
return false;
@@ -1660,56 +2204,91 @@
});
// and highlight it
-
- this.highlight(selected);
-
- // hide the search box if this is the first we got the results and there are a few of them
-
- if (initial === true) {
- showSearchInput = this.showSearchInput = countResults(data.results) >= this.opts.minimumResultsForSearch;
- this.dropdown.find(".select2-search")[showSearchInput ? "removeClass" : "addClass"]("select2-search-hidden");
-
- //add "select2-with-searchbox" to the container if search box is shown
- $(this.dropdown, this.container)[showSearchInput ? "addClass" : "removeClass"]("select2-with-searchbox");
+ if (noHighlightUpdate !== false) {
+ if (initial === true && selected >= 0) {
+ this.highlight(selected);
+ } else {
+ this.highlight(0);
+ }
}
+ // hide the search box if this is the first we got the results and there are enough of them for search
+
+ if (initial === true) {
+ var min = this.opts.minimumResultsForSearch;
+ if (min >= 0) {
+ this.showSearch(countResults(data.results) >= min);
+ }
+ }
},
// single
- onSelect: function (data) {
- var old = this.opts.element.val();
+ showSearch: function(showSearchInput) {
+ if (this.showSearchInput === showSearchInput) return;
+
+ this.showSearchInput = showSearchInput;
+
+ this.dropdown.find(".select2-search").toggleClass("select2-search-hidden", !showSearchInput);
+ this.dropdown.find(".select2-search").toggleClass("select2-offscreen", !showSearchInput);
+ //add "select2-with-searchbox" to the container if search box is shown
+ $(this.dropdown, this.container).toggleClass("select2-with-searchbox", showSearchInput);
+ },
+
+ // single
+ onSelect: function (data, options) {
+
+ if (!this.triggerSelect(data)) { return; }
+
+ var old = this.opts.element.val(),
+ oldData = this.data();
this.opts.element.val(this.id(data));
this.updateSelection(data);
- this.close();
- this.selection.focus();
- if (!equal(old, this.id(data))) { this.triggerChange(); }
+ this.opts.element.trigger({ type: "select2-selected", val: this.id(data), choice: data });
+
+ this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val());
+ this.close();
+
+ if (!options || !options.noFocus)
+ this.focusser.focus();
+
+ if (!equal(old, this.id(data))) { this.triggerChange({added:data,removed:oldData}); }
},
// single
updateSelection: function (data) {
- var container=this.selection.find("span"), formatted;
+ var container=this.selection.find(".select2-chosen"), formatted, cssClass;
this.selection.data("select2-data", data);
container.empty();
- formatted=this.opts.formatSelection(data, container);
+ if (data !== null) {
+ formatted=this.opts.formatSelection(data, container, this.opts.escapeMarkup);
+ }
if (formatted !== undefined) {
- container.append(this.opts.escapeMarkup(formatted));
+ container.append(formatted);
+ }
+ cssClass=this.opts.formatSelectionCssClass(data, container);
+ if (cssClass !== undefined) {
+ container.addClass(cssClass);
}
this.selection.removeClass("select2-default");
if (this.opts.allowClear && this.getPlaceholder() !== undefined) {
- this.selection.find("abbr").show();
+ this.container.addClass("select2-allowclear");
}
},
// single
val: function () {
- var val, data = null, self = this;
+ var val,
+ triggerChange = false,
+ data = null,
+ self = this,
+ oldData = this.data();
if (arguments.length === 0) {
return this.opts.element.val();
@@ -1717,29 +2296,39 @@
val = arguments[0];
+ if (arguments.length > 1) {
+ triggerChange = arguments[1];
+ }
+
if (this.select) {
this.select
.val(val)
- .find(":selected").each2(function (i, elm) {
- data = {id: elm.attr("value"), text: elm.text()};
+ .find("option").filter(function() { return this.selected }).each2(function (i, elm) {
+ data = self.optionToData(elm);
return false;
});
this.updateSelection(data);
this.setPlaceholder();
+ if (triggerChange) {
+ this.triggerChange({added: data, removed:oldData});
+ }
} else {
+ // val is an id. !val is true for [undefined,null,'',0] - 0 is legal
+ if (!val && val !== 0) {
+ this.clear(triggerChange);
+ return;
+ }
if (this.opts.initSelection === undefined) {
throw new Error("cannot call val() if initSelection() is not defined");
}
- // val is an id. !val is true for [undefined,null,'']
- if (!val) {
- this.clear();
- return;
- }
this.opts.element.val(val);
this.opts.initSelection(this.opts.element, function(data){
self.opts.element.val(!data ? "" : self.id(data));
self.updateSelection(data);
self.setPlaceholder();
+ if (triggerChange) {
+ self.triggerChange({added: data, removed:oldData});
+ }
});
}
},
@@ -1747,22 +2336,31 @@
// single
clearSearch: function () {
this.search.val("");
+ this.focusser.val("");
},
// single
data: function(value) {
- var data;
+ var data,
+ triggerChange = false;
if (arguments.length === 0) {
data = this.selection.data("select2-data");
if (data == undefined) data = null;
return data;
} else {
- if (!value || value === "") {
- this.clear();
+ if (arguments.length > 1) {
+ triggerChange = arguments[1];
+ }
+ if (!value) {
+ this.clear(triggerChange);
} else {
+ data = this.data();
this.opts.element.val(!value ? "" : this.id(value));
this.updateSelection(value);
+ if (triggerChange) {
+ this.triggerChange({added: value, removed:data});
+ }
}
}
}
@@ -1772,45 +2370,105 @@
// multi
createContainer: function () {
- var container = $("
", {
+ var container = $(document.createElement("div")).attr({
"class": "select2-container select2-container-multi"
}).html([
- "
" ,
- "
" ,
+ "
",
+ "
"].join(""));
- return container;
+ return container;
},
// multi
prepareOpts: function () {
- var opts = this.parent.prepareOpts.apply(this, arguments);
+ var opts = this.parent.prepareOpts.apply(this, arguments),
+ self=this;
// TODO validate placeholder is a string if specified
if (opts.element.get(0).tagName.toLowerCase() === "select") {
// install sthe selection initializer
- opts.initSelection = function (element,callback) {
+ opts.initSelection = function (element, callback) {
var data = [];
- element.find(":selected").each2(function (i, elm) {
- data.push({id: elm.attr("value"), text: elm.text()});
- });
- if ($.isFunction(callback))
- callback(data);
+ element.find("option").filter(function() { return this.selected }).each2(function (i, elm) {
+ data.push(self.optionToData(elm));
+ });
+ callback(data);
+ };
+ } else if ("data" in opts) {
+ // install default initSelection when applied to hidden input and data is local
+ opts.initSelection = opts.initSelection || function (element, callback) {
+ var ids = splitVal(element.val(), opts.separator);
+ //search in data by array of ids, storing matching items in a list
+ var matches = [];
+ opts.query({
+ matcher: function(term, text, el){
+ var is_match = $.grep(ids, function(id) {
+ return equal(id, opts.id(el));
+ }).length;
+ if (is_match) {
+ matches.push(el);
+ }
+ return is_match;
+ },
+ callback: !$.isFunction(callback) ? $.noop : function() {
+ // reorder matches based on the order they appear in the ids array because right now
+ // they are in the order in which they appear in data array
+ var ordered = [];
+ for (var i = 0; i < ids.length; i++) {
+ var id = ids[i];
+ for (var j = 0; j < matches.length; j++) {
+ var match = matches[j];
+ if (equal(id, opts.id(match))) {
+ ordered.push(match);
+ matches.splice(j, 1);
+ break;
+ }
+ }
+ }
+ callback(ordered);
+ }
+ });
};
}
return opts;
},
+ // multi
+ selectChoice: function (choice) {
+
+ var selected = this.container.find(".select2-search-choice-focus");
+ if (selected.length && choice && choice[0] == selected[0]) {
+
+ } else {
+ if (selected.length) {
+ this.opts.element.trigger("choice-deselected", selected);
+ }
+ selected.removeClass("select2-search-choice-focus");
+ if (choice && choice.length) {
+ this.close();
+ choice.addClass("select2-search-choice-focus");
+ this.opts.element.trigger("choice-selected", choice);
+ }
+ }
+ },
+
+ // multi
+ destroy: function() {
+ $("label[for='" + this.search.attr('id') + "']")
+ .attr('for', this.opts.element.attr("id"));
+ this.parent.destroy.apply(this, arguments);
+ },
+
// multi
initContainer: function () {
@@ -1819,27 +2477,72 @@
this.searchContainer = this.container.find(".select2-search-field");
this.selection = selection = this.container.find(selector);
- this.search.bind("keydown", this.bind(function (e) {
- if (!this.enabled) return;
+ var _this = this;
+ this.selection.on("click", ".select2-search-choice:not(.select2-locked)", function (e) {
+ //killEvent(e);
+ _this.search[0].focus();
+ _this.selectChoice($(this));
+ });
- if (e.which === KEY.BACKSPACE && this.search.val() === "") {
- this.close();
+ // rewrite labels from original element to focusser
+ this.search.attr("id", "s2id_autogen"+nextUid());
+ $("label[for='" + this.opts.element.attr("id") + "']")
+ .attr('for', this.search.attr('id'));
- var choices,
- selected = selection.find(".select2-search-choice-focus");
- if (selected.length > 0) {
+ this.search.on("input paste", this.bind(function() {
+ if (!this.isInterfaceEnabled()) return;
+ if (!this.opened()) {
+ this.open();
+ }
+ }));
+
+ this.search.attr("tabindex", this.elementTabIndex);
+
+ this.keydowns = 0;
+ this.search.on("keydown", this.bind(function (e) {
+ if (!this.isInterfaceEnabled()) return;
+
+ ++this.keydowns;
+ var selected = selection.find(".select2-search-choice-focus");
+ var prev = selected.prev(".select2-search-choice:not(.select2-locked)");
+ var next = selected.next(".select2-search-choice:not(.select2-locked)");
+ var pos = getCursorInfo(this.search);
+
+ if (selected.length &&
+ (e.which == KEY.LEFT || e.which == KEY.RIGHT || e.which == KEY.BACKSPACE || e.which == KEY.DELETE || e.which == KEY.ENTER)) {
+ var selectedChoice = selected;
+ if (e.which == KEY.LEFT && prev.length) {
+ selectedChoice = prev;
+ }
+ else if (e.which == KEY.RIGHT) {
+ selectedChoice = next.length ? next : null;
+ }
+ else if (e.which === KEY.BACKSPACE) {
this.unselect(selected.first());
this.search.width(10);
- killEvent(e);
- return;
+ selectedChoice = prev.length ? prev : next;
+ } else if (e.which == KEY.DELETE) {
+ this.unselect(selected.first());
+ this.search.width(10);
+ selectedChoice = next.length ? next : null;
+ } else if (e.which == KEY.ENTER) {
+ selectedChoice = null;
}
- choices = selection.find(".select2-search-choice");
- if (choices.length > 0) {
- choices.last().addClass("select2-search-choice-focus");
+ this.selectChoice(selectedChoice);
+ killEvent(e);
+ if (!selectedChoice || !selectedChoice.length) {
+ this.open();
}
+ return;
+ } else if (((e.which === KEY.BACKSPACE && this.keydowns == 1)
+ || e.which == KEY.LEFT) && (pos.offset == 0 && !pos.length)) {
+
+ this.selectChoice(selection.find(".select2-search-choice:not(.select2-locked)").last());
+ killEvent(e);
+ return;
} else {
- selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
+ this.selectChoice(null);
}
if (this.opened()) {
@@ -1850,10 +2553,13 @@
killEvent(e);
return;
case KEY.ENTER:
- case KEY.TAB:
this.selectHighlighted();
killEvent(e);
return;
+ case KEY.TAB:
+ this.selectHighlighted({noFocus:true});
+ this.close();
+ return;
case KEY.ESC:
this.cancel(e);
killEvent(e);
@@ -1866,8 +2572,12 @@
return;
}
- if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
- return;
+ if (e.which === KEY.ENTER) {
+ if (this.opts.openOnEnter === false) {
+ return;
+ } else if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
+ return;
+ }
}
this.open();
@@ -1876,62 +2586,73 @@
// prevent the page from scrolling
killEvent(e);
}
+
+ if (e.which === KEY.ENTER) {
+ // prevent form from being submitted
+ killEvent(e);
+ }
+
}));
- this.search.bind("keyup", this.bind(this.resizeSearch));
+ this.search.on("keyup", this.bind(function (e) {
+ this.keydowns = 0;
+ this.resizeSearch();
+ })
+ );
- this.search.bind("blur", this.bind(function(e) {
+ this.search.on("blur", this.bind(function(e) {
this.container.removeClass("select2-container-active");
this.search.removeClass("select2-focused");
- this.clearSearch();
+ this.selectChoice(null);
+ if (!this.opened()) this.clearSearch();
e.stopImmediatePropagation();
+ this.opts.element.trigger($.Event("select2-blur"));
}));
- this.container.delegate(selector, "mousedown", this.bind(function (e) {
- if (!this.enabled) return;
+ this.container.on("click", selector, this.bind(function (e) {
+ if (!this.isInterfaceEnabled()) return;
if ($(e.target).closest(".select2-search-choice").length > 0) {
// clicked inside a select2 search choice, do not open
return;
}
+ this.selectChoice(null);
this.clearPlaceholder();
+ if (!this.container.hasClass("select2-container-active")) {
+ this.opts.element.trigger($.Event("select2-focus"));
+ }
this.open();
this.focusSearch();
e.preventDefault();
}));
- this.container.delegate(selector, "focus", this.bind(function () {
- if (!this.enabled) return;
+ this.container.on("focus", selector, this.bind(function () {
+ if (!this.isInterfaceEnabled()) return;
+ if (!this.container.hasClass("select2-container-active")) {
+ this.opts.element.trigger($.Event("select2-focus"));
+ }
this.container.addClass("select2-container-active");
this.dropdown.addClass("select2-drop-active");
this.clearPlaceholder();
}));
+ this.initContainerWidth();
+ this.opts.element.addClass("select2-offscreen");
+
// set the placeholder if necessary
this.clearSearch();
},
// multi
- enable: function() {
- if (this.enabled) return;
-
- this.parent.enable.apply(this, arguments);
-
- this.search.removeAttr("disabled");
- },
-
- // multi
- disable: function() {
- if (!this.enabled) return;
-
- this.parent.disable.apply(this, arguments);
-
- this.search.attr("disabled", true);
+ enableInterface: function() {
+ if (this.parent.enableInterface.apply(this, arguments)) {
+ this.search.prop("disabled", !this.isInterfaceEnabled());
+ }
},
// multi
initSelection: function () {
var data;
- if (this.opts.element.val() === "") {
+ if (this.opts.element.val() === "" && this.opts.element.text() === "") {
this.updateSelection([]);
this.close();
// set the placeholder if necessary
@@ -1952,16 +2673,16 @@
// multi
clearSearch: function () {
- var placeholder = this.getPlaceholder();
+ var placeholder = this.getPlaceholder(),
+ maxWidth = this.getMaxSearchWidth();
if (placeholder !== undefined && this.getVal().length === 0 && this.search.hasClass("select2-focused") === false) {
this.search.val(placeholder).addClass("select2-default");
// stretch the search box to full width of the container so as much of the placeholder is visible as possible
- this.resizeSearch();
+ // we could call this.resizeSearch(), but we do not because that requires a sizer and we do not want to create one so early because of a firefox bug, see #944
+ this.search.width(maxWidth > 0 ? maxWidth : this.container.css("width"));
} else {
- // we set this to " " instead of "" and later clear it on focus() because there is a firefox bug
- // that does not properly render the caret when the field starts out blank
- this.search.val(" ").width(10);
+ this.search.val("").width(10);
}
},
@@ -1969,19 +2690,21 @@
clearPlaceholder: function () {
if (this.search.hasClass("select2-default")) {
this.search.val("").removeClass("select2-default");
- } else {
- // work around for the space character we set to avoid firefox caret bug
- if (this.search.val() === " ") this.search.val("");
}
},
// multi
opening: function () {
+ this.clearPlaceholder(); // should be done before super so placeholder is not used to search
+ this.resizeSearch();
+
this.parent.opening.apply(this, arguments);
- this.clearPlaceholder();
- this.resizeSearch();
this.focusSearch();
+
+ this.updateResults(true);
+ this.search.focus();
+ this.opts.element.trigger($.Event("select2-open"));
},
// multi
@@ -2021,9 +2744,10 @@
self.postprocessResults();
},
+ // multi
tokenize: function() {
var input = this.search.val();
- input = this.opts.tokenizer(input, this.data(), this.bind(this.onSelect), this.opts);
+ input = this.opts.tokenizer.call(this, input, this.data(), this.bind(this.onSelect), this.opts);
if (input != null && input != undefined) {
this.search.val(input);
if (input.length > 0) {
@@ -2034,9 +2758,15 @@
},
// multi
- onSelect: function (data) {
+ onSelect: function (data, options) {
+
+ if (!this.triggerSelect(data)) { return; }
+
this.addSelectedChoice(data);
- if (this.select) { this.postprocessResults(); }
+
+ this.opts.element.trigger({ type: "selected", val: this.id(data), choice: data });
+
+ if (this.select || !this.opts.closeOnSelect) this.postprocessResults(data, false, this.opts.closeOnSelect===true);
if (this.opts.closeOnSelect) {
this.close();
@@ -2045,10 +2775,16 @@
if (this.countSelectableResults()>0) {
this.search.width(10);
this.resizeSearch();
+ if (this.getMaximumSelectionSize() > 0 && this.val().length >= this.getMaximumSelectionSize()) {
+ // if we reached max selection size repaint the results so choices
+ // are replaced with the max selection reached message
+ this.updateResults(true);
+ }
this.positionDropdown();
} else {
// if nothing left to select close
this.close();
+ this.search.width(10);
}
}
@@ -2056,7 +2792,8 @@
// added we do not need to check if this is a new element before firing change
this.triggerChange({ added: data });
- this.focusSearch();
+ if (!options || !options.noFocus)
+ this.focusSearch();
},
// multi
@@ -2065,36 +2802,51 @@
this.focusSearch();
},
- // multi
addSelectedChoice: function (data) {
- var choice=$(
+ var enableChoice = !data.locked,
+ enabledItem = $(
"
" +
" " +
" " +
""),
+ disabledItem = $(
+ "
" +
+ "" +
+ "");
+ var choice = enableChoice ? enabledItem : disabledItem,
id = this.id(data),
val = this.getVal(),
- formatted;
+ formatted,
+ cssClass;
- formatted=this.opts.formatSelection(data, choice);
- choice.find("div").replaceWith("
"+this.opts.escapeMarkup(formatted)+"
");
- choice.find(".select2-search-choice-close")
- .bind("mousedown", killEvent)
- .bind("click dblclick", this.bind(function (e) {
- if (!this.enabled) return;
+ formatted=this.opts.formatSelection(data, choice.find("div"), this.opts.escapeMarkup);
+ if (formatted != undefined) {
+ choice.find("div").replaceWith("
"+formatted+"
");
+ }
+ cssClass=this.opts.formatSelectionCssClass(data, choice.find("div"));
+ if (cssClass != undefined) {
+ choice.addClass(cssClass);
+ }
- $(e.target).closest(".select2-search-choice").fadeOut('fast', this.bind(function(){
- this.unselect($(e.target));
- this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
- this.close();
- this.focusSearch();
- })).dequeue();
- killEvent(e);
- })).bind("focus", this.bind(function () {
- if (!this.enabled) return;
- this.container.addClass("select2-container-active");
- this.dropdown.addClass("select2-drop-active");
- }));
+ if(enableChoice){
+ choice.find(".select2-search-choice-close")
+ .on("mousedown", killEvent)
+ .on("click dblclick", this.bind(function (e) {
+ if (!this.isInterfaceEnabled()) return;
+
+ $(e.target).closest(".select2-search-choice").fadeOut('fast', this.bind(function(){
+ this.unselect($(e.target));
+ this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
+ this.close();
+ this.focusSearch();
+ })).dequeue();
+ killEvent(e);
+ })).on("focus", this.bind(function () {
+ if (!this.isInterfaceEnabled()) return;
+ this.container.addClass("select2-container-active");
+ this.dropdown.addClass("select2-drop-active");
+ }));
+ }
choice.data("select2-data", data);
choice.insertBefore(this.searchContainer);
@@ -2108,7 +2860,6 @@
var val = this.getVal(),
data,
index;
-
selected = selected.closest(".select2-search-choice");
if (selected.length === 0) {
@@ -2117,55 +2868,81 @@
data = selected.data("select2-data");
- index = indexOf(this.id(data), val);
+ if (!data) {
+ // prevent a race condition when the 'x' is clicked really fast repeatedly the event can be queued
+ // and invoked on an element already removed
+ return;
+ }
- if (index >= 0) {
+ while((index = indexOf(this.id(data), val)) >= 0) {
val.splice(index, 1);
this.setVal(val);
if (this.select) this.postprocessResults();
}
+
+ var evt = $.Event("select2-removing");
+ evt.val = this.id(data);
+ evt.choice = data;
+ this.opts.element.trigger(evt);
+
+ if (evt.isDefaultPrevented()) {
+ return;
+ }
+
selected.remove();
+
+ this.opts.element.trigger({ type: "select2-removed", val: this.id(data), choice: data });
this.triggerChange({ removed: data });
},
// multi
- postprocessResults: function () {
+ postprocessResults: function (data, initial, noHighlightUpdate) {
var val = this.getVal(),
- choices = this.results.find(".select2-result-selectable"),
+ choices = this.results.find(".select2-result"),
compound = this.results.find(".select2-result-with-children"),
self = this;
choices.each2(function (i, choice) {
var id = self.id(choice.data("select2-data"));
if (indexOf(id, val) >= 0) {
- choice.addClass("select2-disabled").removeClass("select2-result-selectable");
- } else {
- choice.removeClass("select2-disabled").addClass("select2-result-selectable");
+ choice.addClass("select2-selected");
+ // mark all children of the selected parent as selected
+ choice.find(".select2-result-selectable").addClass("select2-selected");
}
});
- compound.each2(function(i, e) {
- if (e.find(".select2-result-selectable").length==0) {
- e.addClass("select2-disabled");
- } else {
- e.removeClass("select2-disabled");
+ compound.each2(function(i, choice) {
+ // hide an optgroup if it doesnt have any selectable children
+ if (!choice.is('.select2-result-selectable')
+ && choice.find(".select2-result-selectable:not(.select2-selected)").length === 0) {
+ choice.addClass("select2-selected");
}
});
- choices.each2(function (i, choice) {
- if (!choice.hasClass("select2-disabled") && choice.hasClass("select2-result-selectable")) {
- self.highlight(0);
- return false;
+ if (this.highlight() == -1 && noHighlightUpdate !== false){
+ self.highlight(0);
+ }
+
+ //If all results are chosen render formatNoMAtches
+ if(!this.opts.createSearchChoice && !choices.filter('.select2-result:not(.select2-selected)').length > 0){
+ if(!data || data && !data.more && this.results.find(".select2-no-results").length === 0) {
+ if (checkFormatter(self.opts.formatNoMatches, "formatNoMatches")) {
+ this.results.append("
" + self.opts.formatNoMatches(self.search.val()) + "");
+ }
}
- });
+ }
},
// multi
- resizeSearch: function () {
+ getMaxSearchWidth: function() {
+ return this.selection.width() - getSideBorderPadding(this.search);
+ },
+ // multi
+ resizeSearch: function () {
var minimumWidth, left, maxWidth, containerLeft, searchWidth,
- sideBorderPadding = getSideBorderPadding(this.search);
+ sideBorderPadding = getSideBorderPadding(this.search);
minimumWidth = measureTextWidth(this.search) + 10;
@@ -2175,6 +2952,7 @@
containerLeft = this.selection.offset().left;
searchWidth = maxWidth - (left - containerLeft) - sideBorderPadding;
+
if (searchWidth < minimumWidth) {
searchWidth = maxWidth - sideBorderPadding;
}
@@ -2182,7 +2960,12 @@
if (searchWidth < 40) {
searchWidth = maxWidth - sideBorderPadding;
}
- this.search.width(searchWidth);
+
+ if (searchWidth <= 0) {
+ searchWidth = minimumWidth;
+ }
+
+ this.search.width(Math.floor(searchWidth));
},
// multi
@@ -2213,19 +2996,47 @@
},
// multi
- val: function () {
- var val, data = [], self=this;
+ buildChangeDetails: function (old, current) {
+ var current = current.slice(0),
+ old = old.slice(0);
+
+ // remove intersection from each array
+ for (var i = 0; i < current.length; i++) {
+ for (var j = 0; j < old.length; j++) {
+ if (equal(this.opts.id(current[i]), this.opts.id(old[j]))) {
+ current.splice(i, 1);
+ if(i>0){
+ i--;
+ }
+ old.splice(j, 1);
+ j--;
+ }
+ }
+ }
+
+ return {added: current, removed: old};
+ },
+
+
+ // multi
+ val: function (val, triggerChange) {
+ var oldData, self=this;
if (arguments.length === 0) {
return this.getVal();
}
- val = arguments[0];
+ oldData=this.data();
+ if (!oldData.length) oldData=[];
- if (!val) {
+ // val is an id. !val is true for [undefined,null,'',0] - 0 is legal
+ if (!val && val !== 0) {
this.opts.element.val("");
this.updateSelection([]);
this.clearSearch();
+ if (triggerChange) {
+ this.triggerChange({added: this.data(), removed: oldData});
+ }
return;
}
@@ -2233,20 +3044,23 @@
this.setVal(val);
if (this.select) {
- this.select.find(":selected").each(function () {
- data.push({id: $(this).attr("value"), text: $(this).text()});
- });
- this.updateSelection(data);
+ this.opts.initSelection(this.select, this.bind(this.updateSelection));
+ if (triggerChange) {
+ this.triggerChange(this.buildChangeDetails(oldData, this.data()));
+ }
} else {
if (this.opts.initSelection === undefined) {
- throw new Error("val() cannot be called if initSelection() is not defined")
+ throw new Error("val() cannot be called if initSelection() is not defined");
}
this.opts.initSelection(this.opts.element, function(data){
- var ids=$(data).map(self.id);
+ var ids=$.map(data, self.id);
self.setVal(ids);
self.updateSelection(data);
self.clearSearch();
+ if (triggerChange) {
+ self.triggerChange(self.buildChangeDetails(oldData, self.data()));
+ }
});
}
this.clearSearch();
@@ -2277,7 +3091,6 @@
this.resizeSearch();
// update selection
-
this.selection.find(".select2-search-choice").each(function() {
val.push(self.opts.id($(this).data("select2-data")));
});
@@ -2286,19 +3099,23 @@
},
// multi
- data: function(values) {
- var self=this, ids;
+ data: function(values, triggerChange) {
+ var self=this, ids, old;
if (arguments.length === 0) {
return this.selection
.find(".select2-search-choice")
.map(function() { return $(this).data("select2-data"); })
.get();
} else {
+ old = this.data();
if (!values) { values = []; }
- ids = $.map(values, function(e) { return self.opts.id(e)});
+ ids = $.map(values, function(e) { return self.opts.id(e); });
this.setVal(ids);
this.updateSelection(values);
this.clearSearch();
+ if (triggerChange) {
+ this.triggerChange(this.buildChangeDetails(old, this.data()));
+ }
}
}
});
@@ -2308,7 +3125,11 @@
var args = Array.prototype.slice.call(arguments, 0),
opts,
select2,
- value, multiple, allowedMethods = ["val", "destroy", "opened", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown", "data"];
+ method, value, multiple,
+ allowedMethods = ["val", "destroy", "opened", "open", "close", "focus", "isFocused", "container", "dropdown", "onSortStart", "onSortEnd", "enable", "disable", "readonly", "positionDropdown", "data", "search"],
+ valueMethods = ["opened", "isFocused", "container", "dropdown"],
+ propertyMethods = ["val", "data"],
+ methodsMap = { search: "externalSearch" };
this.each(function () {
if (args.length === 0 || typeof(args[0]) === "object") {
@@ -2316,7 +3137,7 @@
opts.element = $(this);
if (opts.element.get(0).tagName.toLowerCase() === "select") {
- multiple = opts.element.attr("multiple");
+ multiple = opts.element.prop("multiple");
} else {
multiple = opts.multiple || false;
if ("tags" in opts) {opts.multiple = multiple = true;}
@@ -2333,12 +3154,22 @@
value = undefined;
select2 = $(this).data("select2");
if (select2 === undefined) return;
- if (args[0] === "container") {
- value=select2.container;
+
+ method=args[0];
+
+ if (method === "container") {
+ value = select2.container;
+ } else if (method === "dropdown") {
+ value = select2.dropdown;
} else {
- value = select2[args[0]].apply(select2, args.slice(1));
+ if (methodsMap[method]) method = methodsMap[method];
+
+ value = select2[method].apply(select2, args.slice(1));
+ }
+ if (indexOf(args[0], valueMethods) >= 0
+ || (indexOf(args[0], propertyMethods) && args.length == 1)) {
+ return false; // abort the iteration, ready to return first matched value
}
- if (value !== undefined) {return false;}
} else {
throw "Invalid arguments to select2 plugin: " + args;
}
@@ -2349,43 +3180,58 @@
// plugin defaults, accessible to users
$.fn.select2.defaults = {
width: "copy",
+ loadMorePadding: 0,
closeOnSelect: true,
openOnEnter: true,
containerCss: {},
dropdownCss: {},
containerCssClass: "",
dropdownCssClass: "",
- formatResult: function(result, container, query) {
+ formatResult: function(result, container, query, escapeMarkup) {
var markup=[];
- markMatch(result.text, query.term, markup);
+ markMatch(result.text, query.term, markup, escapeMarkup);
return markup.join("");
},
- formatSelection: function (data, container) {
- return data.text;
+ formatSelection: function (data, container, escapeMarkup) {
+ return data ? escapeMarkup(data.text) : undefined;
+ },
+ sortResults: function (results, container, query) {
+ return results;
},
formatResultCssClass: function(data) {return undefined;},
+ formatSelectionCssClass: function(data, container) {return undefined;},
formatNoMatches: function () { return "No matches found"; },
- formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
+ formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " more character" + (n == 1? "" : "s"); },
+ formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1? "" : "s"); },
formatSelectionTooBig: function (limit) { return "You can only select " + limit + " item" + (limit == 1 ? "" : "s"); },
formatLoadMore: function (pageNumber) { return "Loading more results..."; },
formatSearching: function () { return "Searching..."; },
minimumResultsForSearch: 0,
minimumInputLength: 0,
+ maximumInputLength: null,
maximumSelectionSize: 0,
id: function (e) { return e.id; },
matcher: function(term, text) {
- return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;
+ return stripDiacritics(''+text).toUpperCase().indexOf(stripDiacritics(''+term).toUpperCase()) >= 0;
},
separator: ",",
tokenSeparators: [],
tokenizer: defaultTokenizer,
- escapeMarkup: function (markup) {
- if (markup && typeof(markup) === "string") {
- return markup.replace(/&/g, "&");
- }
- return markup;
- },
- blurOnChange: false
+ escapeMarkup: defaultEscapeMarkup,
+ blurOnChange: false,
+ selectOnBlur: false,
+ adaptContainerCssClass: function(c) { return c; },
+ adaptDropdownCssClass: function(c) { return null; },
+ nextSearchTerm: function(selectedObject, currentSearchTerm) { return undefined; }
+ };
+
+ $.fn.select2.ajaxDefaults = {
+ transport: $.ajax,
+ params: {
+ type: "GET",
+ cache: false,
+ dataType: "json"
+ }
};
// exports
@@ -2396,7 +3242,9 @@
tags: tags
}, util: {
debounce: debounce,
- markMatch: markMatch
+ markMatch: markMatch,
+ escapeMarkup: defaultEscapeMarkup,
+ stripDiacritics: stripDiacritics
}, "class": {
"abstract": AbstractSelect2,
"single": SingleSelect2,
diff --git a/addons/base_import/static/src/js/import.js b/addons/base_import/static/src/js/import.js
index 1238bc9570f..91dcc713301 100644
--- a/addons/base_import/static/src/js/import.js
+++ b/addons/base_import/static/src/js/import.js
@@ -482,5 +482,13 @@ openerp.base_import = function (instance) {
{ name: 'import_succeeded', from: 'importing', to: 'imported'},
{ name: 'import_failed', from: 'importing', to: 'results' }
]
- })
+ });
+
+ $.extend($.fn.select2.defaults, {
+ formatNoMatches: function () { return _t("No matches found"); },
+ formatLoadMore: function (pageNumber) { return _t("Loading more results..."); },
+ formatSearching: function () { return _t("Searching..."); }
+ });
+
};
+
diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py
index a7450cfb1a6..2ef6e7d6ef1 100644
--- a/addons/crm/crm_lead.py
+++ b/addons/crm/crm_lead.py
@@ -406,7 +406,7 @@ class crm_lead(format_address, osv.osv):
'probability = 0 %, select "Change Probability Automatically".\n'
'Create a specific stage or edit an existing one by editing columns of your opportunity pipe.'))
for stage_id, lead_ids in stages_leads.items():
- self.write(cr, uid, lead_ids, {'stage_id': stage_id}, context=context)
+ self.write(cr, uid, lead_ids, {'stage_id': stage_id, 'date_closed': fields.datetime.now()}, context=context)
return True
def case_mark_won(self, cr, uid, ids, context=None):
@@ -427,7 +427,7 @@ class crm_lead(format_address, osv.osv):
'probability = 100 % and select "Change Probability Automatically".\n'
'Create a specific stage or edit an existing one by editing columns of your opportunity pipe.'))
for stage_id, lead_ids in stages_leads.items():
- self.write(cr, uid, lead_ids, {'stage_id': stage_id}, context=context)
+ self.write(cr, uid, lead_ids, {'stage_id': stage_id, 'date_closed': fields.datetime.now()}, context=context)
return True
def case_escalate(self, cr, uid, ids, context=None):
diff --git a/addons/crm/crm_phonecall_menu.xml b/addons/crm/crm_phonecall_menu.xml
index f6f94547173..8f934a9638d 100644
--- a/addons/crm/crm_phonecall_menu.xml
+++ b/addons/crm/crm_phonecall_menu.xml
@@ -58,7 +58,7 @@
tree,calendar
[]
-
{'default_state': 'done'}
+
{'search_default_state': 'done', 'default_state': 'done'}
diff --git a/addons/crm/crm_phonecall_view.xml b/addons/crm/crm_phonecall_view.xml
index a027a910520..5f5930cd0e9 100644
--- a/addons/crm/crm_phonecall_view.xml
+++ b/addons/crm/crm_phonecall_view.xml
@@ -167,6 +167,7 @@
+
diff --git a/addons/event/report/report_event_registration.py b/addons/event/report/report_event_registration.py
index 0e33e892432..797a009237d 100644
--- a/addons/event/report/report_event_registration.py
+++ b/addons/event/report/report_event_registration.py
@@ -58,7 +58,7 @@ class report_event_registration(osv.osv):
# TOFIX this request won't select events that have no registration
cr.execute(""" CREATE VIEW report_event_registration AS (
SELECT
- e.id::char || '/' || coalesce(r.id::char,'') AS id,
+ e.id::varchar || '/' || coalesce(r.id::varchar,'') AS id,
e.id AS event_id,
e.user_id AS user_id,
r.user_id AS user_id_registration,
diff --git a/addons/hr_expense/hr_expense.py b/addons/hr_expense/hr_expense.py
index e25b5ac0894..f57b142cae9 100644
--- a/addons/hr_expense/hr_expense.py
+++ b/addons/hr_expense/hr_expense.py
@@ -263,6 +263,10 @@ class hr_expense_expense(osv.osv):
#convert eml into an osv-valid format
lines = map(lambda x:(0,0,self.line_get_convert(cr, uid, x, exp.employee_id.address_home_id, exp.date_confirm, context=context)), eml)
+ journal_id = move_obj.browse(cr, uid, move_id, context).journal_id
+ # post the journal entry if 'Skip 'Draft' State for Manual Entries' is checked
+ if journal_id.entry_posted:
+ move_obj.button_validate(cr, uid, [move_id], context)
move_obj.write(cr, uid, [move_id], {'line_id': lines}, context=context)
self.write(cr, uid, ids, {'account_move_id': move_id, 'state': 'done'}, context=context)
return True
diff --git a/addons/hr_payroll_account/hr_payroll_account.py b/addons/hr_payroll_account/hr_payroll_account.py
index ccab06c0dc1..ad8527fce43 100644
--- a/addons/hr_payroll_account/hr_payroll_account.py
+++ b/addons/hr_payroll_account/hr_payroll_account.py
@@ -23,7 +23,7 @@ import time
from datetime import date, datetime, timedelta
from openerp.osv import fields, osv
-from openerp.tools import config
+from openerp.tools import config, float_compare
from openerp.tools.translate import _
class hr_payslip(osv.osv):
@@ -86,6 +86,7 @@ class hr_payslip(osv.osv):
def process_sheet(self, cr, uid, ids, context=None):
move_pool = self.pool.get('account.move')
period_pool = self.pool.get('account.period')
+ precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Payroll')
timenow = time.strftime('%Y-%m-%d')
for slip in self.browse(cr, uid, ids, context=context):
@@ -149,7 +150,7 @@ class hr_payslip(osv.osv):
line_ids.append(credit_line)
credit_sum += credit_line[2]['credit'] - credit_line[2]['debit']
- if debit_sum > credit_sum:
+ if float_compare(credit_sum, debit_sum, precision_digits=precision) == -1:
acc_id = slip.journal_id.default_credit_account_id.id
if not acc_id:
raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Credit Account!')%(slip.journal_id.name))
@@ -165,7 +166,7 @@ class hr_payslip(osv.osv):
})
line_ids.append(adjust_credit)
- elif debit_sum < credit_sum:
+ elif float_compare(debit_sum, credit_sum, precision_digits=precision) == -1:
acc_id = slip.journal_id.default_debit_account_id.id
if not acc_id:
raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Debit Account!')%(slip.journal_id.name))
diff --git a/addons/l10n_multilang/l10n_multilang.py b/addons/l10n_multilang/l10n_multilang.py
index 937ba332694..4d70d2f40e7 100644
--- a/addons/l10n_multilang/l10n_multilang.py
+++ b/addons/l10n_multilang/l10n_multilang.py
@@ -138,7 +138,7 @@ class wizard_multi_charts_accounts(osv.osv_memory):
def _process_taxes_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
obj_tax_template = self.pool.get('account.tax.template')
obj_tax = self.pool.get('account.tax')
- in_ids = sorted([x.id for x in obj_multi.chart_template_id.tax_template_ids])
+ in_ids = [x.id for x in obj_multi.chart_template_id.tax_template_ids]
out_ids = obj_tax.search(cr, uid, [('company_id', '=', company_id)], order='id')
return self.process_translations(cr, uid, langs, obj_tax_template, field, in_ids, obj_tax, out_ids, context=context)
diff --git a/addons/membership/membership_demo.xml b/addons/membership/membership_demo.xml
index 49863bb1f4c..8d82f12ffd6 100644
--- a/addons/membership/membership_demo.xml
+++ b/addons/membership/membership_demo.xml
@@ -6,7 +6,7 @@
True
-
+
Golden Membership
180
@@ -16,7 +16,7 @@
True
-
+
Silver Membership
80
@@ -26,7 +26,7 @@
True
-
+
Basic Membership
40
diff --git a/addons/mrp/mrp.py b/addons/mrp/mrp.py
index e3271f3f947..6b629a16bc6 100644
--- a/addons/mrp/mrp.py
+++ b/addons/mrp/mrp.py
@@ -29,6 +29,7 @@ from openerp.tools import float_compare
from openerp.tools.translate import _
from openerp import tools, SUPERUSER_ID
from openerp import SUPERUSER_ID
+from openerp.addons.product import _common
#----------------------------------------------------------
# Work Centers
@@ -320,7 +321,7 @@ class mrp_bom(osv.osv):
"""
routing_obj = self.pool.get('mrp.routing')
factor = factor / (bom.product_efficiency or 1.0)
- factor = rounding(factor, bom.product_rounding)
+ factor = _common.ceiling(factor, bom.product_rounding)
if factor < bom.product_rounding:
factor = bom.product_rounding
result = []
@@ -376,6 +377,8 @@ class mrp_bom(osv.osv):
def rounding(f, r):
+ # TODO for trunk: log deprecation warning
+ # _logger.warning("Deprecated rounding method, please use tools.float_round to round floats.")
import math
if not r:
return f
diff --git a/addons/mrp_repair/__openerp__.py b/addons/mrp_repair/__openerp__.py
index a274cbc3ef4..ad20feb39b0 100644
--- a/addons/mrp_repair/__openerp__.py
+++ b/addons/mrp_repair/__openerp__.py
@@ -57,7 +57,8 @@ The following topics should be covered by this module:
'test/test_mrp_repair_b4inv.yml',
'test/test_mrp_repair_afterinv.yml',
'test/test_mrp_repair_cancel.yml',
- 'test/mrp_repair_report.yml'
+ 'test/mrp_repair_report.yml',
+ 'test/test_mrp_repair_fee.yml',
],
'installable': True,
'auto_install': False,
diff --git a/addons/mrp_repair/mrp_repair.py b/addons/mrp_repair/mrp_repair.py
index f04aa68ca6a..b1aec60a4f4 100644
--- a/addons/mrp_repair/mrp_repair.py
+++ b/addons/mrp_repair/mrp_repair.py
@@ -108,10 +108,12 @@ class mrp_repair(osv.osv):
return res
def _get_lines(self, cr, uid, ids, context=None):
- result = {}
- for line in self.pool.get('mrp.repair.line').browse(cr, uid, ids, context=context):
- result[line.repair_id.id] = True
- return result.keys()
+ return self.pool['mrp.repair'].search(
+ cr, uid, [('operations', 'in', ids)], context=context)
+
+ def _get_fee_lines(self, cr, uid, ids, context=None):
+ return self.pool['mrp.repair'].search(
+ cr, uid, [('fees_lines', 'in', ids)], context=context)
_columns = {
'name': fields.char('Repair Reference',size=24, required=True, states={'confirmed':[('readonly',True)]}),
@@ -160,18 +162,21 @@ class mrp_repair(osv.osv):
'repaired': fields.boolean('Repaired', readonly=True),
'amount_untaxed': fields.function(_amount_untaxed, string='Untaxed Amount',
store={
- 'mrp.repair': (lambda self, cr, uid, ids, c={}: ids, ['operations'], 10),
+ 'mrp.repair': (lambda self, cr, uid, ids, c={}: ids, ['operations', 'fees_lines'], 10),
'mrp.repair.line': (_get_lines, ['price_unit', 'price_subtotal', 'product_id', 'tax_id', 'product_uom_qty', 'product_uom'], 10),
+ 'mrp.repair.fee': (_get_fee_lines, ['price_unit', 'price_subtotal', 'product_id', 'tax_id', 'product_uom_qty', 'product_uom'], 10),
}),
'amount_tax': fields.function(_amount_tax, string='Taxes',
store={
- 'mrp.repair': (lambda self, cr, uid, ids, c={}: ids, ['operations'], 10),
+ 'mrp.repair': (lambda self, cr, uid, ids, c={}: ids, ['operations', 'fees_lines'], 10),
'mrp.repair.line': (_get_lines, ['price_unit', 'price_subtotal', 'product_id', 'tax_id', 'product_uom_qty', 'product_uom'], 10),
+ 'mrp.repair.fee': (_get_fee_lines, ['price_unit', 'price_subtotal', 'product_id', 'tax_id', 'product_uom_qty', 'product_uom'], 10),
}),
'amount_total': fields.function(_amount_total, string='Total',
store={
- 'mrp.repair': (lambda self, cr, uid, ids, c={}: ids, ['operations'], 10),
+ 'mrp.repair': (lambda self, cr, uid, ids, c={}: ids, ['operations', 'fees_lines'], 10),
'mrp.repair.line': (_get_lines, ['price_unit', 'price_subtotal', 'product_id', 'tax_id', 'product_uom_qty', 'product_uom'], 10),
+ 'mrp.repair.fee': (_get_fee_lines, ['price_unit', 'price_subtotal', 'product_id', 'tax_id', 'product_uom_qty', 'product_uom'], 10),
}),
}
diff --git a/addons/mrp_repair/test/test_mrp_repair_fee.yml b/addons/mrp_repair/test/test_mrp_repair_fee.yml
new file mode 100644
index 00000000000..6aac605d0d9
--- /dev/null
+++ b/addons/mrp_repair/test/test_mrp_repair_fee.yml
@@ -0,0 +1,23 @@
+-
+ Testing total amount update function
+-
+ I check the total amount of mrp_repair_rmrp1 is 100
+-
+ !assert {model: mrp.repair, id: mrp_repair_rmrp1, string=amount_total should be 100}:
+ - amount_total == 100
+-
+ I add a new fee line
+-
+ !record {model: mrp.repair, id: mrp_repair_rmrp1}:
+ fees_lines:
+ - name: 'Assembly Service Cost'
+ product_id: product.product_assembly
+ product_uom_qty: 1.0
+ product_uom: product.product_uom_hour
+ price_unit: 12.0
+ to_invoice: True
+-
+ I check the total amount of mrp_repair_rmrp1 is now 112
+-
+ !assert {model: mrp.repair, id: mrp_repair_rmrp1, string=amount_total should be 112}:
+ - amount_total == 112
diff --git a/addons/product/__openerp__.py b/addons/product/__openerp__.py
index fdadda2e4d4..d81a352087e 100644
--- a/addons/product/__openerp__.py
+++ b/addons/product/__openerp__.py
@@ -61,7 +61,6 @@ Print product labels with barcode.
],
'test': [
'product_pricelist_demo.yml',
- 'test/product_uom.yml',
'test/product_pricelist.yml',
],
'installable': True,
diff --git a/addons/product/_common.py b/addons/product/_common.py
index f5f53fe8689..c05dcee66a2 100644
--- a/addons/product/_common.py
+++ b/addons/product/_common.py
@@ -18,12 +18,18 @@
# along with this program. If not, see .
#
##############################################################################
+from openerp import tools
+
+import math
+
+
def rounding(f, r):
+ # TODO for trunk: log deprecation warning
+ # _logger.warning("Deprecated rounding method, please use tools.float_round to round floats.")
+ return tools.float_round(f, precision_rounding=r)
+
+# TODO for trunk: add rounding method parameter to tools.float_round and use this method as hook
+def ceiling(f, r):
if not r:
return f
- return round(f / r) * r
-
-
-
-# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
-
+ return math.ceil(f / r) * r
diff --git a/addons/product/pricelist.py b/addons/product/pricelist.py
index aaac5a9a3de..3b9a454f954 100644
--- a/addons/product/pricelist.py
+++ b/addons/product/pricelist.py
@@ -21,8 +21,7 @@
import time
-from _common import rounding
-
+from openerp import tools
from openerp.osv import fields, osv
from openerp.tools.translate import _
@@ -294,7 +293,8 @@ class product_pricelist(osv.osv):
if price is not False:
price_limit = price
price = price * (1.0+(res['price_discount'] or 0.0))
- price = rounding(price, res['price_round']) #TOFIX: rounding with tools.float_rouding
+ if res['price_round']:
+ price = tools.float_round(price, precision_rounding=res['price_round'])
price += (res['price_surcharge'] or 0.0)
if res['price_min_margin']:
price = max(price, price_limit+res['price_min_margin'])
diff --git a/addons/product/product.py b/addons/product/product.py
index 71f1eeffaf3..31c41018570 100644
--- a/addons/product/product.py
+++ b/addons/product/product.py
@@ -22,7 +22,7 @@
import math
import re
-from _common import rounding
+from _common import ceiling
from openerp import tools
from openerp.osv import osv, fields
@@ -177,7 +177,7 @@ class product_uom(osv.osv):
return qty
amount = qty / from_unit.factor
if to_unit:
- amount = rounding(amount * to_unit.factor, to_unit.rounding)
+ amount = ceiling(amount * to_unit.factor, to_unit.rounding)
return amount
def _compute_price(self, cr, uid, from_uom_id, price, to_uom_id=False):
diff --git a/addons/product/product_view.xml b/addons/product/product_view.xml
index 889e7eb5d13..0e2de43f3a6 100644
--- a/addons/product/product_view.xml
+++ b/addons/product/product_view.xml
@@ -178,7 +178,6 @@
-
diff --git a/addons/product/test/product_uom.yml b/addons/product/test/product_uom.yml
deleted file mode 100644
index 04928d43d87..00000000000
--- a/addons/product/test/product_uom.yml
+++ /dev/null
@@ -1,15 +0,0 @@
--
- In order to test conversation of UOM,
--
- I convert Grams into TON with price.
--
- !python {model: product.uom}: |
- from_uom_id = ref("product_uom_gram")
- to_uom_id = ref("product_uom_ton")
- price = 2
- qty = 1020000
- price = self._compute_price(cr, uid, from_uom_id, price, to_uom_id)
- qty = self._compute_qty(cr, uid, from_uom_id, qty, to_uom_id)
- assert qty == 1.02, "Qty is not correspond."
- assert price == 2000000.0, "Price is not correspond."
-
diff --git a/addons/product/tests/__init__.py b/addons/product/tests/__init__.py
new file mode 100644
index 00000000000..850189ac0f4
--- /dev/null
+++ b/addons/product/tests/__init__.py
@@ -0,0 +1,5 @@
+from . import test_uom
+
+fast_suite = [
+ test_uom,
+]
diff --git a/addons/product/tests/test_uom.py b/addons/product/tests/test_uom.py
new file mode 100644
index 00000000000..3d3ba04b689
--- /dev/null
+++ b/addons/product/tests/test_uom.py
@@ -0,0 +1,37 @@
+from openerp.tests.common import TransactionCase
+
+class TestUom(TransactionCase):
+ """Tests for unit of measure conversion"""
+
+ def setUp(self):
+ super(TestUom, self).setUp()
+ self.product = self.registry('product.product')
+ self.uom = self.registry('product.uom')
+ self.imd = self.registry('ir.model.data')
+
+ def test_10_conversion(self):
+ cr, uid = self.cr, self.uid
+ gram_id = self.imd.get_object_reference(cr, uid, 'product', 'product_uom_gram')[1]
+ tonne_id = self.imd.get_object_reference(cr, uid, 'product', 'product_uom_ton')[1]
+
+ qty = self.uom._compute_qty(cr, uid, gram_id, 1020000, tonne_id)
+ self.assertEquals(qty, 1.02, "Converted quantity does not correspond.")
+
+ price = self.uom._compute_price(cr, uid, gram_id, 2, tonne_id)
+ self.assertEquals(price, 2000000.0, "Converted price does not correspond.")
+
+ def test_20_rounding(self):
+ cr, uid = self.cr, self.uid
+ unit_id = self.imd.get_object_reference(cr, uid, 'product', 'product_uom_unit')[1]
+ categ_unit_id = self.imd.get_object_reference(cr, uid, 'product', 'product_uom_categ_unit')[1]
+
+ score_id = self.uom.create(cr, uid, {
+ 'name': 'Score',
+ 'factor_inv': 20,
+ 'uom_type': 'bigger',
+ 'rounding': 1.0,
+ 'category_id': categ_unit_id
+ })
+
+ qty = self.uom._compute_qty(cr, uid, unit_id, 2, score_id)
+ self.assertEquals(qty, 1, "Converted quantity should be rounded up.")
diff --git a/addons/product_margin/product_margin.py b/addons/product_margin/product_margin.py
index 1bbfe8c285b..c344abb3410 100644
--- a/addons/product_margin/product_margin.py
+++ b/addons/product_margin/product_margin.py
@@ -53,9 +53,9 @@ class product_product(osv.osv):
states = ('draft', 'open', 'paid')
sqlstr="""select
- sum(l.price_unit * l.quantity)/sum(l.quantity) as avg_unit_price,
+ sum(l.price_unit * l.quantity)/sum(nullif(l.quantity,0)) as avg_unit_price,
sum(l.quantity) as num_qty,
- sum(l.quantity * (l.price_subtotal/l.quantity)) as total,
+ sum(l.quantity * (l.price_subtotal/(nullif(l.quantity,0)))) as total,
sum(l.quantity * pt.list_price) as sale_expected,
sum(l.quantity * pt.standard_price) as normal_cost
from account_invoice_line l
diff --git a/addons/sale/sale.py b/addons/sale/sale.py
index 71e9ed16a07..3512d44f7c7 100644
--- a/addons/sale/sale.py
+++ b/addons/sale/sale.py
@@ -180,8 +180,8 @@ class sale_order(osv.osv):
'date_confirm': fields.date('Confirmation Date', readonly=True, select=True, help="Date on which sales order is confirmed."),
'user_id': fields.many2one('res.users', 'Salesperson', states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, select=True, track_visibility='onchange'),
'partner_id': fields.many2one('res.partner', 'Customer', readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, required=True, change_default=True, select=True, track_visibility='always'),
- 'partner_invoice_id': fields.many2one('res.partner', 'Invoice Address', readonly=True, required=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, help="Invoice address for current sales order."),
- 'partner_shipping_id': fields.many2one('res.partner', 'Delivery Address', readonly=True, required=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, help="Delivery address for current sales order."),
+ 'partner_invoice_id': fields.many2one('res.partner', 'Invoice Address', domain="[('parent_id','=',partner_id)]", readonly=True, required=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, help="Invoice address for current sales order."),
+ 'partner_shipping_id': fields.many2one('res.partner', 'Delivery Address', domain="[('parent_id','=',partner_id)]", readonly=True, required=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, help="Delivery address for current sales order."),
'order_policy': fields.selection([
('manual', 'On Demand'),
], 'Create Invoice', required=True, readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},