2011-03-15 22:16:01 +01:00
|
|
|
(function($) {
|
|
|
|
$.entwine('ss', function($){
|
2011-08-05 05:46:57 +02:00
|
|
|
/**
|
|
|
|
* On resize of any close the open treedropdownfields
|
|
|
|
* as we'll need to redo with widths
|
|
|
|
*/
|
|
|
|
$(window).resize(function() {
|
|
|
|
$('.TreeDropdownField').closePanel();
|
|
|
|
});
|
2011-03-15 22:16:01 +01:00
|
|
|
|
2011-03-07 20:29:09 +01:00
|
|
|
var strings = {
|
|
|
|
'openlink': 'Open',
|
2011-03-10 03:35:33 +01:00
|
|
|
'fieldTitle': '(choose)',
|
2011-03-07 20:29:09 +01:00
|
|
|
'searchFieldTitle': '(choose or search)'
|
|
|
|
};
|
2012-01-03 14:42:56 +01:00
|
|
|
|
|
|
|
var _clickTestFn = function(e) {
|
|
|
|
// If the click target is not a child of the current field, close the panel automatically.
|
2012-06-15 00:04:36 +02:00
|
|
|
if(!$(e.target).parents('.TreeDropdownField').length) $('.TreeDropdownField').closePanel();
|
2012-01-03 14:42:56 +01:00
|
|
|
};
|
|
|
|
|
2011-03-15 22:16:01 +01:00
|
|
|
/**
|
2011-03-07 20:29:09 +01:00
|
|
|
* @todo Error display
|
|
|
|
* @todo No results display for search
|
|
|
|
* @todo Automatic expansion of ajax children when multiselect is triggered
|
|
|
|
* @todo Automatic panel positioning based on available space (top/bottom)
|
|
|
|
* @todo forceValue
|
|
|
|
* @todo Automatic width
|
|
|
|
* @todo Expand title height to fit all elements
|
2011-03-15 22:16:01 +01:00
|
|
|
*/
|
|
|
|
$('.TreeDropdownField').entwine({
|
2012-07-12 04:46:23 +02:00
|
|
|
onadd: function() {
|
2011-03-07 20:29:09 +01:00
|
|
|
this.append(
|
2011-08-09 07:43:40 +02:00
|
|
|
'<span class="treedropdownfield-title"></span>' +
|
|
|
|
'<div class="treedropdownfield-toggle-panel-link"><a href="#" class="ui-icon ui-icon-triangle-1-s"></a></div>' +
|
|
|
|
'<div class="treedropdownfield-panel"><div class="tree-holder"></div></div>'
|
2011-03-07 20:29:09 +01:00
|
|
|
);
|
2011-08-05 05:46:57 +02:00
|
|
|
|
|
|
|
var linkTitle = strings.openLink;
|
2011-08-09 07:43:40 +02:00
|
|
|
if(linkTitle) this.find("treedropdownfield-toggle-panel-link a").attr('title', linkTitle);
|
2012-06-13 16:04:14 +02:00
|
|
|
if(this.data('title')) this.setTitle(decodeURIComponent(this.data('title')));
|
2011-03-07 20:29:09 +01:00
|
|
|
|
2011-08-01 04:27:52 +02:00
|
|
|
this.getPanel().hide();
|
2011-03-07 20:29:09 +01:00
|
|
|
this._super();
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
getPanel: function() {
|
2011-08-09 07:43:40 +02:00
|
|
|
return this.find('.treedropdownfield-panel');
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
openPanel: function() {
|
2011-08-09 07:43:40 +02:00
|
|
|
// close all other panels
|
|
|
|
$('.TreeDropdownField').closePanel();
|
2012-01-03 14:42:56 +01:00
|
|
|
|
|
|
|
// Listen for clicks outside of the field to auto-close it
|
|
|
|
$('body').bind('click', _clickTestFn);
|
2011-08-09 07:43:40 +02:00
|
|
|
|
2011-03-07 20:29:09 +01:00
|
|
|
var panel = this.getPanel(), tree = this.find('.tree-holder');
|
2012-10-02 12:02:42 +02:00
|
|
|
var top = this.position().top + this.height();
|
2011-08-09 07:43:40 +02:00
|
|
|
|
|
|
|
panel.css('top', top);
|
2011-08-05 05:46:57 +02:00
|
|
|
panel.css('width', this.width());
|
|
|
|
|
2011-03-15 22:16:01 +01:00
|
|
|
panel.show();
|
2011-08-01 04:27:52 +02:00
|
|
|
|
|
|
|
// swap the down arrow with an up arrow
|
2011-08-09 07:43:40 +02:00
|
|
|
var toggle = this.find(".treedropdownfield-toggle-panel-link");
|
|
|
|
toggle.addClass('treedropdownfield-open-tree');
|
|
|
|
this.addClass("treedropdownfield-open-tree");
|
2011-08-05 05:46:57 +02:00
|
|
|
|
|
|
|
toggle.find("a")
|
2011-08-01 04:27:52 +02:00
|
|
|
.removeClass('ui-icon-triangle-1-s')
|
|
|
|
.addClass('ui-icon-triangle-1-n');
|
|
|
|
|
2011-03-07 20:29:09 +01:00
|
|
|
if(tree.is(':empty')) this.loadTree();
|
2012-08-20 03:28:03 +02:00
|
|
|
this.trigger('panelshow');
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
closePanel: function() {
|
2012-01-03 14:42:56 +01:00
|
|
|
jQuery('body').unbind('click', _clickTestFn);
|
|
|
|
|
2011-08-01 04:27:52 +02:00
|
|
|
// swap the up arrow with a down arrow
|
2011-08-09 07:43:40 +02:00
|
|
|
var toggle = this.find(".treedropdownfield-toggle-panel-link");
|
|
|
|
toggle.removeClass('treedropdownfield-open-tree');
|
|
|
|
this.removeClass('treedropdownfield-open-tree');
|
|
|
|
|
2011-08-05 05:46:57 +02:00
|
|
|
toggle.find("a")
|
2011-08-01 04:27:52 +02:00
|
|
|
.removeClass('ui-icon-triangle-1-n')
|
|
|
|
.addClass('ui-icon-triangle-1-s');
|
|
|
|
|
2011-08-09 07:43:40 +02:00
|
|
|
|
2011-03-15 22:16:01 +01:00
|
|
|
this.getPanel().hide();
|
2012-08-20 03:28:03 +02:00
|
|
|
this.trigger('panelhide');
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
togglePanel: function() {
|
|
|
|
this[this.getPanel().is(':visible') ? 'closePanel' : 'openPanel']();
|
|
|
|
},
|
|
|
|
setTitle: function(title) {
|
2011-03-10 03:35:33 +01:00
|
|
|
if(!title) title = strings.fieldTitle;
|
|
|
|
|
2012-06-13 16:04:14 +02:00
|
|
|
this.find('.treedropdownfield-title').html(title);
|
|
|
|
this.data('title', encodeURIComponent(title)); // separate view from storage (important for search cancellation)
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
getTitle: function() {
|
2011-08-09 07:43:40 +02:00
|
|
|
return this.find('.treedropdownfield-title').text();
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
2012-01-04 21:19:30 +01:00
|
|
|
/**
|
|
|
|
* Update title from tree node value
|
|
|
|
*/
|
|
|
|
updateTitle: function() {
|
2012-02-22 17:27:02 +01:00
|
|
|
var self = this, tree = self.find('.tree-holder'), val = this.getValue();
|
2012-01-04 21:19:30 +01:00
|
|
|
var updateFn = function() {
|
|
|
|
var val = self.getValue();
|
|
|
|
if(val) {
|
2012-05-07 05:43:25 +02:00
|
|
|
|
2012-01-04 21:19:30 +01:00
|
|
|
var node = tree.find('*[data-id="' + val + '"]'),
|
2012-05-07 05:43:25 +02:00
|
|
|
title = node.children('a').find("span.jstree_pageicon")?node.children('a').find("span.item").html():null;
|
|
|
|
if(!title) title=(node) ? tree.jstree('get_text', node[0]) : null;
|
|
|
|
|
2012-01-04 21:19:30 +01:00
|
|
|
if(title) self.setTitle(title);
|
|
|
|
if(node) tree.jstree('select_node', node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load the tree if its not already present
|
2012-02-22 17:27:02 +01:00
|
|
|
if(jQuery.jstree._reference(tree) || !val) updateFn();
|
2012-01-04 21:19:30 +01:00
|
|
|
else this.loadTree(null, updateFn);
|
|
|
|
},
|
2011-03-15 22:16:01 +01:00
|
|
|
setValue: function(val) {
|
2012-05-11 12:40:17 +02:00
|
|
|
this.find(':input:hidden').val(val).trigger('change');
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
getValue: function() {
|
2011-03-07 20:30:08 +01:00
|
|
|
return this.find(':input:hidden').val();
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
2011-03-07 20:30:08 +01:00
|
|
|
loadTree: function(params, callback) {
|
|
|
|
var self = this, panel = this.getPanel(), treeHolder = $(panel).find('.tree-holder');
|
2012-01-02 22:38:13 +01:00
|
|
|
var params = (params) ? $.extend({}, this.getRequestParams(), params) : this.getRequestParams();
|
2011-03-07 20:30:08 +01:00
|
|
|
panel.addClass('loading');
|
2012-02-23 16:07:38 +01:00
|
|
|
treeHolder.load(this.data('urlTree'), params, function(html, status, xhr) {
|
2011-03-07 20:30:08 +01:00
|
|
|
var firstLoad = true;
|
2011-03-15 22:16:01 +01:00
|
|
|
if(status == 'success') {
|
|
|
|
$(this)
|
2012-01-02 23:35:17 +01:00
|
|
|
.jstree('destroy')
|
2011-03-15 22:16:01 +01:00
|
|
|
.bind('loaded.jstree', function(e, data) {
|
2012-01-05 23:16:12 +01:00
|
|
|
var val = self.getValue(), selectNode = treeHolder.find('*[data-id="' + val + '"]'),
|
|
|
|
currentNode = data.inst.get_selected();
|
|
|
|
if(val && selectNode != currentNode) data.inst.select_node(selectNode);
|
2011-03-07 20:30:08 +01:00
|
|
|
firstLoad = false;
|
|
|
|
if(callback) callback.apply(self);
|
2011-03-15 22:16:01 +01:00
|
|
|
})
|
|
|
|
.jstree(self.getTreeConfig())
|
|
|
|
.bind('select_node.jstree', function(e, data) {
|
2011-03-10 03:35:33 +01:00
|
|
|
var node = data.rslt.obj, id = $(node).data('id');
|
2012-09-12 23:27:09 +02:00
|
|
|
if(!firstLoad && !self.getValue() === id) {
|
2012-01-05 23:16:12 +01:00
|
|
|
// Value is already selected, unselect it (for lack of a better UI to do this)
|
2011-05-08 13:36:13 +02:00
|
|
|
self.data('metadata', null);
|
2011-03-10 03:35:33 +01:00
|
|
|
self.setTitle(null);
|
2011-05-08 13:36:13 +02:00
|
|
|
self.setValue(null);
|
2012-01-05 23:16:12 +01:00
|
|
|
data.inst.deselect_node(node);
|
2011-03-10 03:35:33 +01:00
|
|
|
} else {
|
2012-01-05 23:16:12 +01:00
|
|
|
self.data('metadata', $.extend({id: id}, $(node).getMetaData()));
|
2011-03-10 03:35:33 +01:00
|
|
|
self.setTitle(data.inst.get_text(node));
|
2011-05-08 13:36:13 +02:00
|
|
|
self.setValue(id);
|
2011-03-10 03:35:33 +01:00
|
|
|
}
|
|
|
|
|
2011-03-07 20:30:08 +01:00
|
|
|
// Avoid auto-closing panel on first load
|
|
|
|
if(!firstLoad) self.closePanel();
|
2012-01-02 23:35:17 +01:00
|
|
|
firstLoad=false
|
2011-03-15 22:16:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-07 20:30:08 +01:00
|
|
|
panel.removeClass('loading');
|
2011-03-15 22:16:01 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
getTreeConfig: function() {
|
2011-03-10 03:04:29 +01:00
|
|
|
var self = this;
|
2011-03-15 22:16:01 +01:00
|
|
|
return {
|
|
|
|
'core': {
|
2012-01-02 23:35:17 +01:00
|
|
|
// 'initially_open': ['record-0'],
|
2011-03-15 22:16:01 +01:00
|
|
|
'animation': 0
|
|
|
|
},
|
|
|
|
'html_data': {
|
|
|
|
// TODO Hack to avoid ajax load on init, see http://code.google.com/p/jstree/issues/detail?id=911
|
|
|
|
'data': this.getPanel().find('.tree-holder').html(),
|
|
|
|
'ajax': {
|
2012-08-01 09:58:11 +02:00
|
|
|
'url': function(node) {
|
2012-08-09 00:18:33 +02:00
|
|
|
var url = $.path.parseUrl(self.data('urlTree')).hrefNoSearch;
|
|
|
|
return url + '/' + ($(node).data("id") ? $(node).data("id") : 0);
|
2012-08-01 09:58:11 +02:00
|
|
|
},
|
2011-03-15 22:16:01 +01:00
|
|
|
'data': function(node) {
|
2012-08-09 00:18:33 +02:00
|
|
|
var query = $.query.load(self.data('urlTree')).keys;
|
2012-08-01 09:58:11 +02:00
|
|
|
var params = self.getRequestParams();
|
2012-08-09 00:18:33 +02:00
|
|
|
params = $.extend({}, query, params, {ajax: 1});
|
2011-03-10 03:04:29 +01:00
|
|
|
return params;
|
2011-03-15 22:16:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'ui': {
|
|
|
|
"select_limit" : 1,
|
|
|
|
'initially_select': [this.getPanel().find('.current').attr('id')]
|
|
|
|
},
|
2011-03-02 20:38:02 +01:00
|
|
|
'themes': {
|
|
|
|
'theme': 'apple'
|
|
|
|
},
|
2011-03-15 22:16:01 +01:00
|
|
|
'plugins': ['html_data', 'ui', 'themes']
|
|
|
|
};
|
2011-03-10 03:04:29 +01:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* If the field is contained in a form, submit all form parameters by default.
|
|
|
|
* This is useful to keep state like locale values which are typically
|
|
|
|
* encoded in hidden fields through the form.
|
|
|
|
*
|
2012-01-02 22:38:13 +01:00
|
|
|
* @return {object}
|
2011-03-10 03:04:29 +01:00
|
|
|
*/
|
|
|
|
getRequestParams: function() {
|
2012-01-02 22:38:13 +01:00
|
|
|
return {};
|
2011-03-15 22:16:01 +01:00
|
|
|
}
|
|
|
|
});
|
2011-08-09 07:43:40 +02:00
|
|
|
|
2011-05-08 13:36:13 +02:00
|
|
|
$('.TreeDropdownField .tree-holder li').entwine({
|
|
|
|
/**
|
|
|
|
* Overload to return more data. The same data should be set on initial
|
|
|
|
* value through PHP as well (see TreeDropdownField->Field()).
|
|
|
|
*
|
|
|
|
* @return {object}
|
|
|
|
*/
|
|
|
|
getMetaData: function() {
|
|
|
|
var matches = this.attr('class').match(/class-([^\s]*)/i);
|
|
|
|
var klass = matches ? matches[1] : '';
|
|
|
|
return {ClassName: klass};
|
|
|
|
}
|
|
|
|
});
|
2011-08-09 07:43:40 +02:00
|
|
|
|
2011-03-15 22:16:01 +01:00
|
|
|
$('.TreeDropdownField *').entwine({
|
|
|
|
getField: function() {
|
|
|
|
return this.parents('.TreeDropdownField:first');
|
|
|
|
}
|
|
|
|
});
|
2011-08-09 07:43:40 +02:00
|
|
|
|
2012-03-23 23:30:13 +01:00
|
|
|
$('.TreeDropdownField').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
this.togglePanel();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.TreeDropdownField .treedropdownfield-panel').entwine({
|
2011-03-15 22:16:01 +01:00
|
|
|
onclick: function(e) {
|
2011-03-07 20:30:08 +01:00
|
|
|
return false;
|
2011-03-15 22:16:01 +01:00
|
|
|
}
|
|
|
|
});
|
2011-03-07 20:30:08 +01:00
|
|
|
|
|
|
|
$('.TreeDropdownField.searchable').entwine({
|
2012-07-12 04:46:23 +02:00
|
|
|
onadd: function() {
|
2011-03-07 20:30:08 +01:00
|
|
|
this._super();
|
|
|
|
|
2012-08-06 23:04:42 +02:00
|
|
|
var title = decodeURIComponent(this.data('title'));
|
2012-01-02 22:38:13 +01:00
|
|
|
this.find('.treedropdownfield-title').replaceWith(
|
2011-08-09 07:43:40 +02:00
|
|
|
$('<input type="text" class="treedropdownfield-title search" />')
|
2011-03-07 20:30:08 +01:00
|
|
|
);
|
2011-08-09 07:43:40 +02:00
|
|
|
|
2011-03-07 20:30:08 +01:00
|
|
|
this.setTitle(title ? title : strings.searchFieldTitle);
|
|
|
|
},
|
|
|
|
setTitle: function(title) {
|
2012-01-02 23:35:17 +01:00
|
|
|
if(!title && title !== '') title = strings.fieldTitle;
|
2011-03-10 03:35:33 +01:00
|
|
|
|
2011-08-09 07:43:40 +02:00
|
|
|
this.find('.treedropdownfield-title').val(title);
|
2011-03-07 20:30:08 +01:00
|
|
|
},
|
|
|
|
getTitle: function() {
|
2011-08-09 07:43:40 +02:00
|
|
|
return this.find('.treedropdownfield-title').val();
|
2011-03-07 20:30:08 +01:00
|
|
|
},
|
|
|
|
search: function(str, callback) {
|
|
|
|
this.openPanel();
|
|
|
|
this.loadTree({search: str}, callback);
|
|
|
|
},
|
|
|
|
cancelSearch: function() {
|
|
|
|
this.closePanel();
|
|
|
|
this.loadTree();
|
|
|
|
this.setTitle(this.data('title'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.TreeDropdownField.searchable input.search').entwine({
|
2012-01-02 23:35:17 +01:00
|
|
|
onfocusin: function(e) {
|
|
|
|
var field = this.getField();
|
|
|
|
field.setTitle('');
|
|
|
|
},
|
|
|
|
onfocusout: function(e) {
|
|
|
|
var field = this.getField();
|
|
|
|
if(!field.getTitle()) field.setTitle(false);
|
|
|
|
},
|
2011-03-07 20:30:08 +01:00
|
|
|
onkeydown: function(e) {
|
|
|
|
var field = this.getField();
|
|
|
|
if(e.keyCode == 13) {
|
|
|
|
// trigger search on ENTER key
|
|
|
|
field.search(this.val());
|
|
|
|
return false;
|
|
|
|
} else if(e.keyCode == 27) {
|
|
|
|
// cancel search on ESC key
|
|
|
|
field.cancelSearch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.TreeDropdownField.multiple').entwine({
|
|
|
|
getTreeConfig: function() {
|
|
|
|
var cfg = this._super();
|
2012-03-08 01:27:25 +01:00
|
|
|
cfg.checkbox = {override_ui: true, two_state: true};
|
2011-03-07 20:30:08 +01:00
|
|
|
cfg.plugins.push('checkbox');
|
|
|
|
cfg.ui.select_limit = -1;
|
|
|
|
return cfg;
|
|
|
|
},
|
|
|
|
loadTree: function(params, callback) {
|
|
|
|
var self = this, panel = this.getPanel(), treeHolder = $(panel).find('.tree-holder');
|
2012-01-02 22:38:13 +01:00
|
|
|
var params = (params) ? $.extend({}, this.getRequestParams(), params) : this.getRequestParams();
|
2011-03-07 20:30:08 +01:00
|
|
|
panel.addClass('loading');
|
2012-02-23 16:07:38 +01:00
|
|
|
treeHolder.load(this.data('urlTree'), params, function(html, status, xhr) {
|
2011-03-07 20:30:08 +01:00
|
|
|
var firstLoad = true;
|
|
|
|
if(status == 'success') {
|
|
|
|
$(this)
|
2012-01-02 23:35:17 +01:00
|
|
|
.jstree('destroy')
|
2011-03-07 20:30:08 +01:00
|
|
|
.bind('loaded.jstree', function(e, data) {
|
|
|
|
$.each(self.getValue(), function(i, val) {
|
|
|
|
data.inst.check_node(treeHolder.find('*[data-id=' + val + ']'));
|
|
|
|
});
|
|
|
|
firstLoad = false;
|
|
|
|
if(callback) callback.apply(self);
|
|
|
|
})
|
|
|
|
.jstree(self.getTreeConfig())
|
|
|
|
.bind('uncheck_node.jstree check_node.jstree', function(e, data) {
|
|
|
|
var nodes = data.inst.get_checked(null, true);
|
|
|
|
self.setValue($.map(nodes, function(el, i) {
|
|
|
|
return $(el).data('id');
|
|
|
|
}));
|
|
|
|
self.setTitle($.map(nodes, function(el, i) {
|
|
|
|
return data.inst.get_text(el);
|
|
|
|
}));
|
2011-05-08 13:36:13 +02:00
|
|
|
self.data('metadata', $.map(nodes, function(el, i) {
|
|
|
|
return {id: $(el).data('id'), metadata: $(el).getMetaData()};
|
|
|
|
}));
|
2011-03-07 20:30:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
panel.removeClass('loading');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getValue: function() {
|
|
|
|
var val = this._super();
|
|
|
|
return val.split(/ *, */);
|
|
|
|
},
|
|
|
|
setValue: function(val) {
|
|
|
|
this._super($.isArray(val) ? val.join(',') : val);
|
|
|
|
},
|
|
|
|
setTitle: function(title) {
|
2011-03-09 06:55:24 +01:00
|
|
|
this._super($.isArray(title) ? title.join(', ') : title);
|
2012-01-04 21:19:30 +01:00
|
|
|
},
|
|
|
|
updateTitle: function() {
|
|
|
|
// TODO Not supported due to multiple values/titles yet
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.TreeDropdownField input[type=hidden]').entwine({
|
2012-07-12 04:46:23 +02:00
|
|
|
onadd: function() {
|
|
|
|
this.bind('change.TreeDropdownField', function() {
|
|
|
|
$(this).getField().updateTitle();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onremove: function() {
|
|
|
|
this.unbind('.TreeDropdownField');
|
2011-03-07 20:30:08 +01:00
|
|
|
}
|
2011-03-09 06:55:24 +01:00
|
|
|
});
|
2011-03-15 22:16:01 +01:00
|
|
|
});
|
2012-05-11 05:31:12 +02:00
|
|
|
}(jQuery));
|