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

Hi! I cleaned up your code for you! #1

Open
wants to merge 7 commits into
base: master
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
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc

# Logs and databases #
######################
*.log

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
105 changes: 53 additions & 52 deletions Demo/GrowingInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,64 @@ Script: GrowingInput.js
Guillermo Rauch
*/

(function(){
(function($){

GrowingInput = new Class({
Implements: [Options, Events],
options: {
$.GrowingInput = function(element, options){

var value, lastValue, calc;

options = $.extend({
min: 0,
max: null,
startWidth: 2,
startWidth: 15,
correction: 15
},

initialize: function(element, options){
this.setOptions(options);
this.element = $(element).store('growing', this).set('autocomplete', 'off');
this.calc = new Element('span', {
'styles': {
'float': 'left',
'display': 'inline-block',
'position': 'absolute',
'left': -1000
}
}).inject(this.element, 'after');
['font-size', 'font-family', 'padding-left', 'padding-top', 'padding-bottom',
'padding-right', 'border-left', 'border-right', 'border-top', 'border-bottom',
'word-spacing', 'letter-spacing', 'text-indent', 'text-transform'].each(function(p){
this.calc.setStyle(p, this.element.getStyle(p));
}, this);
this.resize();
var resize = this.resize.bind(this);
this.element.addEvents({blur: resize, keyup: resize, keydown: resize, keypress: resize});
},

calculate: function(chars){
this.calc.set('html', chars);
var width = this.calc.getStyle('width').toInt();
return (width ? width : this.options.startWidth) + this.options.correction;
},

resize: function(){
this.lastvalue = this.value;
this.value = this.element.value;
var value = this.value;
if($chk(this.options.min) && this.value.length < this.options.min){
if($chk(this.lastvalue) && (this.lastvalue.length <= this.options.min)) return;
value = str_pad(this.value, this.options.min, '-');
} else if($chk(this.options.max) && this.value.length > this.options.max){
if($chk(this.lastvalue) && (this.lastvalue.length >= this.options.max)) return;
value = this.value.substr(0, this.options.max);
}, options);

element = $(element).data('growing', this);

var self = this;
var init = function(){
calc = $('<span></span>').css({
'float': 'left',
'display': 'inline-block',
'position': 'absolute',
'left': -1000
}).insertAfter(element);
$.each(['font-size', 'font-family', 'padding-left', 'padding-top', 'padding-bottom',
'padding-right', 'border-left', 'border-right', 'border-top', 'border-bottom',
'word-spacing', 'letter-spacing', 'text-indent', 'text-transform'], function(i, p){
calc.css(p, element.css(p));
});
element.blur(resize).keyup(resize).keydown(resize).keypress(resize);
resize();
};

var calculate = function(chars){
calc.text(chars);
var width = calc.width();
return (width ? width : options.startWidth) + options.correction;
};

var resize = function(){
lastValue = value;
value = element.val();
var retValue = value;
if(chk(options.min) && value.length < options.min){
if(chk(lastValue) && (lastValue.length <= options.min)) return;
retValue = str_pad(value, options.min, '-');
} else if(chk(options.max) && value.length > options.max){
if(chk(lastValue) && (lastValue.length >= options.max)) return;
retValue = value.substr(0, options.max);
}
this.element.setStyle('width', this.calculate(value));
return this;
}

});
element.width(calculate(retValue));
return self;
};

this.resize = resize;
init();
};

var chk = function(v){ return !!(v || v === 0); };
var str_repeat = function(str, times){ return new Array(times + 1).join(str); };
var str_pad = function(self, length, str, dir){
if (self.length >= length) return this;
Expand All @@ -76,4 +77,4 @@ var str_pad = function(self, length, str, dir){
return pad.substr(0, (pad.length / 2).floor()) + self + pad.substr(0, (pad.length / 2).ceil());
};

})();
})(jQuery);
75 changes: 75 additions & 0 deletions Demo/SuggestInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
(function($){

$.SuggestInput = function(bitEditable, textboxlist, autocomplete){

var value, lastValue, suggestInput;

bitEditableInput = bitEditable.getInput().data('suggest', this);

var self = this;
var init = function(){
suggestInput = $('<input type="text" class="'+ textboxlist.getOptions().prefix +'-autocomplete-suggest ' + bitEditableInput.attr('class') +'" disabled="disabled" />')
.insertBefore(bitEditableInput);

lastValue = bitEditableInput.val();
bitEditableInput.keypress(updateSuggest).keyup(delKey).blur(clearSuggest);
};

var updateSuggest = function(ev) {
if(ev.which == 8) return;
new_search = bitEditableInput.val() + String.fromCharCode(ev.charCode);
if (!strStartsWith(lastValue, new_search)) clearSuggest();
};

var delKey = function(ev) {
if(ev.which != 8) return;
if(bitEditableInput.val().length < autocomplete.getOptions().minLength) {
clearSuggest();
}
}

var suggest = function(resultValue) {
if(resultValue == lastValue) return;
suggestInput.val(compose(bitEditableInput.val(), resultValue));
lastValue = resultValue;
};

var clearSuggest = function() {
suggestInput.val('');
lastValue = '';
};

this.suggest = suggest;
this.clearSuggest = clearSuggest;

init();
};

var strStartsWith = function(str, prefix){
return sanitize(str.toLowerCase()).indexOf(sanitize(prefix.toLowerCase())) === 0;
}
var compose = function(base, extended){ return base + extended.substring(base.length) }
var sanitize = function(str) {
var r = '';

for(var i = 0, l = str.length; i < l; i++) {
var s = str.charAt(i);
switch(s) {
case 'à': case 'á':
r += 'a'; break;
case 'è': case 'é':
r += 'e'; break;
case 'í':
r += 'i'; break;
case 'ò': case 'ó':
r += 'o'; break;
case 'ú':
r += 'u'; break;
default:
r += s;
}
}
return r;
}

})(jQuery);
2 changes: 1 addition & 1 deletion Demo/autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// TextboxList Autocomplete sample data for queryRemote: false (names are fetched all at once when TextboxList is initialized)

// get names (eg: database)
// the format is:
// the format is:
// id, searchable plain text, html (for the textboxlist item, if empty the plain is used), html (for the autocomplete dropdown)

$response = array();
Expand Down
2 changes: 1 addition & 1 deletion Demo/autocomplete2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// TextboxList Autocomplete sample data for queryRemote: true (server is queried as user types)

// get names (eg: database)
// the format is:
// the format is:
// id, searchable plain text, html (for the textboxlist item, if empty the plain is used), html (for the autocomplete dropdown)

$response = array();
Expand Down
Loading