Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to read only state. #848

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ QueryBuilder.inputs = [
*/
QueryBuilder.modifiable_options = [
'display_errors',
'read_only',
'allow_groups',
'allow_empty',
'default_condition',
Expand Down Expand Up @@ -128,6 +129,7 @@ QueryBuilder.DEFAULTS = {

sort_filters: false,
display_errors: true,
read_only: false,
allow_groups: -1,
allow_empty: false,
conditions: ['AND', 'OR'],
Expand Down
5 changes: 5 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ var QueryBuilder = function($el, options) {
this.operators = this.checkOperators(this.operators);
this.bindEvents();
this.initPlugins();

// if read only, disable interative elements
if (this.settings.read_only) {
setTimeout(function () { $el.find(':input').prop('disabled', true)});
}
};

$.extend(QueryBuilder.prototype, /** @lends QueryBuilder.prototype */ {
Expand Down
20 changes: 20 additions & 0 deletions src/plugins/sortable/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ QueryBuilder.define('sortable', function(options) {
.draggable({
allowFrom: QueryBuilder.selectors.drag_handle,
onstart: function(event) {
// ignore when readonly
if (e.builder.settings.read_only) {
return;
}

moved = false;

// get model of dragged element
Expand All @@ -72,11 +77,21 @@ QueryBuilder.define('sortable', function(options) {
src.$el.hide();
},
onmove: function(event) {
// ignore when readonly
if (e.builder.settings.read_only) {
return;
}

// make the ghost follow the cursor
ghost[0].style.top = event.clientY - 15 + 'px';
ghost[0].style.left = event.clientX - 15 + 'px';
},
onend: function(event) {
// ignore when readonly
if (e.builder.settings.read_only) {
return;
}

// starting from Interact 1.3.3, onend is called before ondrop
if (event.dropzone) {
moveSortableToTarget(src, $(event.relatedTarget), self);
Expand Down Expand Up @@ -204,6 +219,11 @@ QueryBuilder.defaults({
* @private
*/
function moveSortableToTarget(node, target, builder) {
// ignore when readonly
if (builder.settings.read_only) {
return;
}

var parent, method;
var Selectors = QueryBuilder.selectors;

Expand Down
9 changes: 9 additions & 0 deletions src/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ QueryBuilder.prototype.reset = function() {

this.addRule(this.model.root);

// if read only, disable interative elements
var $el = this.$el;
if (this.settings.read_only) {
setTimeout(function () { $el.find(':input').prop('disabled', true)});
}

/**
* After the {@link QueryBuilder#reset} method
* @event afterReset
Expand Down Expand Up @@ -105,6 +111,9 @@ QueryBuilder.prototype.setOptions = function(options) {
$.each(options, function(opt, value) {
if (QueryBuilder.modifiable_options.indexOf(opt) !== -1) {
this.settings[opt] = value;
if (opt === 'read_only') {
this.$el.find(':input').prop('disabled', value);
}
}
}.bind(this));
};
Expand Down
16 changes: 8 additions & 8 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ QueryBuilder.templates.group = '\
<div id="{{= it.group_id }}" class="rules-group-container"> \
<div class="rules-group-header"> \
<div class="btn-group pull-right group-actions"> \
<button type="button" class="btn btn-xs btn-success" data-add="rule"> \
<button type="button" class="btn btn-xs btn-success" data-add="rule"{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> \
<i class="{{= it.icons.add_rule }}"></i> {{= it.translate("add_rule") }} \
</button> \
{{? it.settings.allow_groups===-1 || it.settings.allow_groups>=it.level }} \
<button type="button" class="btn btn-xs btn-success" data-add="group"> \
<button type="button" class="btn btn-xs btn-success" data-add="group"{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> \
<i class="{{= it.icons.add_group }}"></i> {{= it.translate("add_group") }} \
</button> \
{{?}} \
{{? it.level>1 }} \
<button type="button" class="btn btn-xs btn-danger" data-delete="group"> \
<button type="button" class="btn btn-xs btn-danger" data-delete="group"{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> \
<i class="{{= it.icons.remove_group }}"></i> {{= it.translate("delete_group") }} \
</button> \
{{?}} \
</div> \
<div class="btn-group group-conditions"> \
{{~ it.conditions: condition }} \
<label class="btn btn-xs btn-primary"> \
<input type="radio" name="{{= it.group_id }}_cond" value="{{= condition }}"> {{= it.translate("conditions", condition) }} \
<input type="radio" name="{{= it.group_id }}_cond" value="{{= condition }}"{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> {{= it.translate("conditions", condition) }} \
</label> \
{{~}} \
</div> \
Expand All @@ -36,7 +36,7 @@ QueryBuilder.templates.rule = '\
<div id="{{= it.rule_id }}" class="rule-container"> \
<div class="rule-header"> \
<div class="btn-group pull-right rule-actions"> \
<button type="button" class="btn btn-xs btn-danger" data-delete="rule"> \
<button type="button" class="btn btn-xs btn-danger" data-delete="rule"{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> \
<i class="{{= it.icons.remove_rule }}"></i> {{= it.translate("delete_rule") }} \
</button> \
</div> \
Expand All @@ -51,7 +51,7 @@ QueryBuilder.templates.rule = '\

QueryBuilder.templates.filterSelect = '\
{{ var optgroup = null; }} \
<select class="form-control" name="{{= it.rule.id }}_filter"> \
<select class="form-control" name="{{= it.rule.id }}_filter"{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> \
{{? it.settings.display_empty_filter }} \
<option value="-1">{{= it.settings.select_placeholder }}</option> \
{{?}} \
Expand All @@ -74,7 +74,7 @@ QueryBuilder.templates.operatorSelect = '\
</span> \
{{?}} \
{{ var optgroup = null; }} \
<select class="form-control {{? it.operators.length === 1 }}hide{{?}}" name="{{= it.rule.id }}_operator"> \
<select class="form-control {{? it.operators.length === 1 }}hide{{?}}" name="{{= it.rule.id }}_operator"{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> \
{{~ it.operators: operator }} \
{{? optgroup !== operator.optgroup }} \
{{? optgroup !== null }}</optgroup>{{?}} \
Expand All @@ -89,7 +89,7 @@ QueryBuilder.templates.operatorSelect = '\

QueryBuilder.templates.ruleValueSelect = '\
{{ var optgroup = null; }} \
<select class="form-control" name="{{= it.name }}" {{? it.rule.filter.multiple }}multiple{{?}}> \
<select class="form-control" name="{{= it.name }}"{{? it.rule.filter.multiple }} multiple{{?}}{{? it.settings.read_only!=false}} disabled="disabled"{{?}}> \
{{? it.rule.filter.placeholder }} \
<option value="{{= it.rule.filter.placeholder_value }}" disabled selected>{{= it.rule.filter.placeholder }}</option> \
{{?}} \
Expand Down