2011-03-15 22:16:01 +01:00
|
|
|
(function($) {
|
|
|
|
$.entwine('ss', function($){
|
|
|
|
|
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)'
|
|
|
|
};
|
|
|
|
|
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({
|
|
|
|
onmatch: function() {
|
2011-03-07 20:29:09 +01:00
|
|
|
this.append(
|
|
|
|
'<span class="title"></span>' +
|
|
|
|
'<a href="#" title="' + strings.openLink + '" class="toggle-panel-link"></a>' +
|
|
|
|
'<div class="panel"><div class="tree-holder"></div></div>'
|
|
|
|
);
|
|
|
|
if(this.data('title')) this.setTitle(this.data('title'));
|
|
|
|
this.getPanel().hide();
|
|
|
|
|
|
|
|
this._super();
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
getPanel: function() {
|
|
|
|
return this.find('.panel');
|
|
|
|
},
|
|
|
|
openPanel: function() {
|
2011-03-07 20:29:09 +01:00
|
|
|
var panel = this.getPanel(), tree = this.find('.tree-holder');
|
2011-03-15 22:16:01 +01:00
|
|
|
panel.show();
|
2011-03-07 20:29:09 +01:00
|
|
|
if(tree.is(':empty')) this.loadTree();
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
closePanel: function() {
|
|
|
|
this.getPanel().hide();
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
|
2011-03-15 22:16:01 +01:00
|
|
|
this.find('.title').text(title);
|
2011-03-10 03:35:33 +01:00
|
|
|
this.data('title', title); // separate view from storage (important for search cancellation)
|
2011-03-15 22:16:01 +01:00
|
|
|
},
|
|
|
|
getTitle: function() {
|
|
|
|
return this.find('.title').text();
|
|
|
|
},
|
|
|
|
setValue: function(val) {
|
2011-03-07 20:30:08 +01:00
|
|
|
this.find(':input:hidden').val(val);
|
2011-04-05 05:35:49 +02:00
|
|
|
|
|
|
|
this.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');
|
2011-03-10 03:04:29 +01:00
|
|
|
var params = (params) ? this.getRequestParams().concat(params) : this.getRequestParams();
|
2011-03-07 20:30:08 +01:00
|
|
|
panel.addClass('loading');
|
2011-03-10 04:12:46 +01:00
|
|
|
treeHolder.load(this.data('url-tree'), 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)
|
|
|
|
.bind('loaded.jstree', function(e, data) {
|
|
|
|
var val = self.getValue();
|
|
|
|
if(val) data.inst.select_node(treeHolder.find('*[data-id=' + val + ']'));
|
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');
|
|
|
|
if(self.getValue() == id) {
|
|
|
|
self.setValue(null);
|
|
|
|
self.setTitle(null);
|
|
|
|
} else {
|
|
|
|
self.setValue(id);
|
|
|
|
self.setTitle(data.inst.get_text(node));
|
|
|
|
}
|
|
|
|
|
2011-03-07 20:30:08 +01:00
|
|
|
// Avoid auto-closing panel on first load
|
|
|
|
if(!firstLoad) self.closePanel();
|
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': {
|
|
|
|
'initially_open': ['record-0'],
|
|
|
|
'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': {
|
2011-03-10 04:12:46 +01:00
|
|
|
'url': this.data('url-tree'),
|
2011-03-15 22:16:01 +01:00
|
|
|
'data': function(node) {
|
2011-03-10 03:04:29 +01:00
|
|
|
var id = $(node).data("id") ? $(node).data("id") : 0, params = self.getRequestParams();
|
|
|
|
params = params.concat([{name: 'ID', value: id}, {name: 'ajax', value: 1}]);
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @return {array}
|
|
|
|
*/
|
|
|
|
getRequestParams: function() {
|
|
|
|
return [];
|
2011-03-15 22:16:01 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$('.TreeDropdownField *').entwine({
|
|
|
|
getField: function() {
|
|
|
|
return this.parents('.TreeDropdownField:first');
|
|
|
|
}
|
|
|
|
});
|
2011-03-07 20:30:08 +01:00
|
|
|
$('.TreeDropdownField .toggle-panel-link, .TreeDropdownField span.title').entwine({
|
2011-03-15 22:16:01 +01:00
|
|
|
onclick: function(e) {
|
|
|
|
this.getField().togglePanel();
|
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({
|
|
|
|
onmatch: function() {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
var title = this.data('title');
|
|
|
|
this.find('.title').replaceWith(
|
|
|
|
$('<input type="text" class="title search" />')
|
|
|
|
);
|
|
|
|
this.setTitle(title ? title : strings.searchFieldTitle);
|
|
|
|
},
|
|
|
|
setTitle: function(title) {
|
2011-03-10 03:35:33 +01:00
|
|
|
if(!title) title = strings.fieldTitle;
|
|
|
|
|
2011-03-07 20:30:08 +01:00
|
|
|
this.find('.title').val(title);
|
|
|
|
},
|
|
|
|
getTitle: function() {
|
|
|
|
return this.find('.title').val();
|
|
|
|
},
|
|
|
|
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({
|
|
|
|
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();
|
|
|
|
cfg.checkbox = {override_ui: true};
|
|
|
|
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');
|
2011-03-10 03:04:29 +01:00
|
|
|
var params = (params) ? this.getRequestParams().concat(params) : this.getRequestParams();
|
2011-03-07 20:30:08 +01:00
|
|
|
panel.addClass('loading');
|
2011-03-10 04:12:46 +01:00
|
|
|
treeHolder.load(this.data('url-tree'), params, function(html, status, xhr) {
|
2011-03-07 20:30:08 +01:00
|
|
|
var firstLoad = true;
|
|
|
|
if(status == 'success') {
|
|
|
|
$(this)
|
|
|
|
.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);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2011-03-07 20:30:08 +01:00
|
|
|
}
|
2011-03-09 06:55:24 +01:00
|
|
|
});
|
2011-03-15 22:16:01 +01:00
|
|
|
});
|
|
|
|
}(jQuery));
|