kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
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.
204 lines
7.3 KiB
204 lines
7.3 KiB
12 years ago
|
//-- copyright
|
||
|
// OpenProject is a project management system.
|
||
11 years ago
|
// Copyright (C) 2012-2013 the OpenProject Foundation (OPF)
|
||
12 years ago
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License version 3.
|
||
|
//
|
||
11 years ago
|
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
// Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
// Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License
|
||
|
// as published by the Free Software Foundation; either version 2
|
||
|
// of the License, or (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
//
|
||
12 years ago
|
// See doc/COPYRIGHT.rdoc for more details.
|
||
|
//++
|
||
|
|
||
12 years ago
|
|
||
|
// Boilerplate code for remote autocompletion/infinite scrolling.
|
||
|
// Borrows graciously from select2
|
||
|
|
||
|
(function ($, undefined) {
|
||
|
"use strict";
|
||
|
|
||
11 years ago
|
function Autocompleter (object, args) {
|
||
12 years ago
|
this.element = object;
|
||
|
this.opts = null;
|
||
|
this.fakeInput = null;
|
||
|
this.initOptions(args);
|
||
|
this.setupInput();
|
||
|
this.initSelect2();
|
||
|
}
|
||
|
|
||
11 years ago
|
Autocompleter.prototype = $.extend(Autocompleter.prototype, {
|
||
12 years ago
|
initOptions: function (args) {
|
||
|
var self = this;
|
||
11 years ago
|
this.opts = $.extend(true, {}, $.fn.autocomplete.defaults);
|
||
12 years ago
|
this.opts = $.extend(true, this.opts, args[0]);
|
||
|
if (!(this.element.attr("data-ajaxURL") === "" || this.element.attr("data-ajaxURL") === null || this.element.attr("data-ajaxURL") === undefined)) {
|
||
|
this.opts.ajax.url = this.element.attr("data-ajaxURL");
|
||
|
}
|
||
11 years ago
|
if (!($(this.element).attr("data-values") === "" || $(this.element).attr("data-values") === null || $(this.element).attr("data-values") === undefined)) {
|
||
11 years ago
|
this.opts.data.results = JSON.parse($(this.element).attr('data-values')).map(function (e) {
|
||
|
e.text = e.text || e.name;
|
||
|
return e;
|
||
|
});
|
||
11 years ago
|
delete this.opts.ajax;
|
||
|
}
|
||
12 years ago
|
},
|
||
|
|
||
|
setupInput: function () {
|
||
|
var attrs_to_copy = {}, currentName, select2id, values = [];
|
||
|
|
||
11 years ago
|
if ($(this.element).is("input")) {
|
||
|
this.fakeInput = $(this.element);
|
||
|
} else {
|
||
|
$("input[name='" + $(this.element).attr("name")+"']").remove();
|
||
12 years ago
|
|
||
11 years ago
|
for(var i = 0; i < $(this.element).get(0).attributes.length; i++) {
|
||
|
currentName = $(this.element).get(0).attributes[i].name;
|
||
|
if(currentName.indexOf("data-") === 0 || $.inArray(currentName, this.opts.allowedAttributes) !== -1) { //only ones starting with data-
|
||
|
attrs_to_copy[currentName] = $(this.element).attr(currentName);
|
||
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
select2id = $(this.element).attr("id");
|
||
|
this.fakeInput = $(this.element).after("<input type='hidden' id='" + select2id + "'></input>").siblings(":input#" + select2id);
|
||
|
this.fakeInput.attr(attrs_to_copy);
|
||
|
if (!($(this.element).attr("data-selected") === "" || $(this.element).attr("data-selected") === null || $(this.element).attr("data-selected") === undefined)) {
|
||
|
JSON.parse($(this.element).attr('data-selected')).each(function (elem) {
|
||
|
values.push(elem[1]);
|
||
|
});
|
||
|
this.fakeInput.val(values);
|
||
|
}
|
||
|
$(this.element).remove();
|
||
12 years ago
|
}
|
||
|
},
|
||
|
|
||
|
initSelect2: function () {
|
||
|
$(this.fakeInput).select2(this.opts);
|
||
12 years ago
|
|
||
|
if (this.opts.sortable) {
|
||
|
var input = $(this.fakeInput);
|
||
|
input.select2("container").find("ul.select2-choices").sortable({
|
||
|
containment: 'parent',
|
||
|
start: function() {
|
||
|
input.select2("onSortStart");
|
||
|
},
|
||
|
update: function() {
|
||
|
input.select2("onSortEnd");
|
||
|
}
|
||
|
});
|
||
|
}
|
||
12 years ago
|
}
|
||
|
});
|
||
|
|
||
11 years ago
|
$.fn.autocomplete = function () {
|
||
12 years ago
|
var args = Array.prototype.slice.call(arguments, 0),
|
||
|
autocompleter;
|
||
|
|
||
|
$(this).each(function () {
|
||
11 years ago
|
autocompleter = new Autocompleter($(this), args);
|
||
12 years ago
|
});
|
||
11 years ago
|
};
|
||
12 years ago
|
|
||
11 years ago
|
$.fn.autocomplete.defaults = {
|
||
|
multiple: false,
|
||
11 years ago
|
data: {},
|
||
12 years ago
|
allowedAttributes: ["title", "placeholder", "id", "name"],
|
||
|
minimumInputLength: 0,
|
||
|
ajax: {
|
||
|
null_element: null,
|
||
|
dataType: 'json',
|
||
|
quietMillis: 500,
|
||
|
contentType: "application/json",
|
||
|
data: function (term, page) {
|
||
|
return {
|
||
|
q: term, //search term
|
||
|
page_limit: 10, // page size
|
||
|
page: page // current page number
|
||
|
};
|
||
|
},
|
||
|
results: function (data, page) {
|
||
|
var active_items = [];
|
||
|
data.results.items.each(function (e) {
|
||
|
active_items.push(e);
|
||
|
});
|
||
|
active_items = this.add_null_element(active_items, page);
|
||
|
return {'results': active_items, 'more': data.results.more};
|
||
|
},
|
||
|
add_null_element: function (results, page) {
|
||
|
if (this.null_element === null || this.null_element === undefined || page !== 1) {
|
||
|
return results;
|
||
|
}
|
||
|
return [this.null_element].concat(results);
|
||
|
}
|
||
|
},
|
||
|
formatResult: function (item, container, query) {
|
||
|
var match = item.name.toUpperCase().indexOf(query.term.toUpperCase()),
|
||
|
tl = query.term.length,
|
||
|
markup = [];
|
||
|
|
||
|
if (match < 0) {
|
||
12 years ago
|
return "<span data-value='" + item.id + "'>" +
|
||
|
OpenProject.Helpers.markupEscape(item.name) + "</span>";
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
markup.push(OpenProject.Helpers.markupEscape(
|
||
|
item.name.substring(0, match)));
|
||
12 years ago
|
markup.push("<span class='select2-match' data-value='" + item.id + "'>");
|
||
12 years ago
|
markup.push(OpenProject.Helpers.markupEscape(
|
||
|
item.name.substring(match, match + tl)));
|
||
12 years ago
|
markup.push("</span>");
|
||
12 years ago
|
markup.push(OpenProject.Helpers.markupEscape(
|
||
|
item.name.substring(match + tl, item.name.length)));
|
||
|
return markup.join("");
|
||
12 years ago
|
},
|
||
|
formatSelection: function (item) {
|
||
11 years ago
|
return item.name;
|
||
12 years ago
|
},
|
||
|
initSelection: function (element, callback) {
|
||
11 years ago
|
var data,
|
||
|
multiple = $(element).attr("multiple");
|
||
|
if (multiple !== null && multiple !== undefined && multiple) {
|
||
|
data = []
|
||
|
};
|
||
12 years ago
|
if (!($(element).attr("data-selected") === "" || $(element).attr("data-selected") === null || $(element).attr("data-selected") === undefined)) {
|
||
|
JSON.parse($(element).attr('data-selected')).each(function (elem) {
|
||
11 years ago
|
if (multiple) {
|
||
|
data.push({id: elem[1], name: elem[0]});
|
||
|
} else {
|
||
|
data = {id: elem[1], name: elem[0]};
|
||
|
}
|
||
12 years ago
|
});
|
||
11 years ago
|
} else if (element.is("input") && !(element.attr("data-values") === "" || element.attr("data-values") === null || element.attr("data-values") === undefined)) {
|
||
|
var possible = JSON.parse(element.attr('data-values'));
|
||
|
var vals = element.val().split(",");
|
||
|
var byID = {};
|
||
|
|
||
|
var i;
|
||
|
for (i = 0; i < possible.length; i += 1) {
|
||
|
byID[possible[i].id] = possible[i];
|
||
|
}
|
||
|
|
||
|
for (i = 0; i < vals.length; i += 1) {
|
||
|
data.push(byID[vals[i]]);
|
||
|
}
|
||
12 years ago
|
}
|
||
11 years ago
|
|
||
|
callback(data);
|
||
12 years ago
|
}
|
||
11 years ago
|
};
|
||
12 years ago
|
}(jQuery));
|