1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-03 17:59:21 +02:00
ampache/lib/javascript/search.js
Lawrence Brown 73f35cd4aa change copyright years to 2015
sorry for taking two commits to do this!
2015-01-06 17:34:39 +00:00

172 lines
5.7 KiB
JavaScript

// vim:set softtabstop=4 shiftwidth=4 expandtab:
//
// Copyright 2010 - 2015 Ampache.org
// All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License v2
// as published by the Free Software Foundation.
//
//
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
var rowIter = 1;
var rowCount = 0;
var SearchRow = {
add: function(ruleType, operator, input) {
if (typeof(ruleType) != 'string') {
ruleType = 0;
}
else {
jQuery.each(types, function(i) {
if (types[i].name == ruleType) {
ruleType = i;
return false;
}
});
}
if (typeof(operator) != 'string') {
operator = 0;
}
else {
if (ruleType != null) {
var opts = basetypes[types[ruleType].type];
jQuery.each(opts, function(i) {
if (opts[i].name == operator) {
operator = i;
return false;
}
});
}
}
var row = document.createElement('tr');
var cells = new Array();
for (var i = 0 ; i < 5 ; i++) {
cells[i] = document.createElement('td');
}
cells[0].appendChild(SearchRow.constructOptions(ruleType, rowIter));
cells[1].appendChild(SearchRow.constructOperators(ruleType, rowIter, operator));
cells[2].appendChild(SearchRow.constructInput(ruleType, rowIter, input));
cells[3].innerHTML = removeIcon;
jQuery.each(cells, function(i) {
row.appendChild(cells[i]);
});
$('#searchtable').append(row);
rowCount++;
$(cells[3]).on('click', function(){if(rowCount > 1) { this.parentNode.remove(); rowCount--; }});
rowIter++;
},
constructInput: function(ruleType, ruleNumber, input) {
if (input === null || input === undefined) {
input = '';
}
widget = types[ruleType].widget;
var inputNode = document.createElement(widget['0']);
inputNode.id = 'rule_' + ruleNumber + '_input';
inputNode.name = 'rule_' + ruleNumber + '_input';
switch(widget['0']) {
case 'input':
inputNode.setAttribute('type', widget['1']);
inputNode.setAttribute('value', input);
break;
case 'select':
jQuery.each(widget['1'], function(i) {
var option = document.createElement('option');
if ( isNaN(parseInt(widget['1'][i])) ) {
realvalue = i;
}
else {
realvalue = parseInt(widget['1'][i]);
}
if ( input == realvalue ) {
option.selected = true;
}
option.value = realvalue;
option.innerHTML = widget['1'][i];
inputNode.appendChild(option);
});
break;
}
return inputNode;
},
constructOptions: function(ruleType, ruleNumber) {
var optionsNode = document.createElement('select');
optionsNode.id = 'rule_' + ruleNumber;
optionsNode.name = 'rule_' + ruleNumber;
jQuery.each(types, function(i) {
var option = document.createElement('option');
option.innerHTML = types[i].label;
option.value = types[i].name;
if ( i == ruleType ) {
option.selected = true;
}
optionsNode.appendChild(option);
});
$(optionsNode).change(SearchRow.update);
return optionsNode;
},
constructOperators: function(ruleType, ruleNumber, operator) {
var operatorNode = document.createElement('select');
operatorNode.id = 'rule_' + ruleNumber + '_operator';
operatorNode.name = 'rule_' + ruleNumber + '_operator';
basetype = types[ruleType].type;
operatorNode.className = 'operator' + basetype;
var opts = basetypes[basetype];
jQuery.each(opts, function(i) {
var option = document.createElement('option');
option.innerHTML = opts[i].description;
option.value = i;
if (i == operator) {
option.selected = true;
}
operatorNode.appendChild(option);
});
return operatorNode;
},
update: function() {
var r_findID = /rule_(\d+)/;
var targetID = r_findID.exec(this.id)[1];
var operator = $('#rule_' + targetID + '_operator');
if (operator.className != 'operator' + types[this.selectedIndex].type) {
var operator_cell = operator.parent();
operator.remove();
operator_cell.append(SearchRow.constructOperators(this.selectedIndex, targetID));
}
var input = $('#rule_' + targetID + '_input');
if (input.type == 'text') {
var oldinput = input.value;
}
var input_cell = input.parent();
input.remove();
input_cell.append(SearchRow.constructInput(this.selectedIndex, targetID, oldinput));
}
};