iedarbīgas zāles ([info]bez4pieci) rakstīja [info]koderi kopienā,
@ 2009-09-10 11:56:00

Previous Entry  Add to memories!  Tell a Friend!  Next Entry
meklēju kādu, kas man varētu palīdzēt pārrakstīt vienu pavisam nelielu klasi (~50 rindiņas) no Mootools uz JQuery. tā kā jquery pasaulē esmu pilnīgi zaļš — vienīgais, ko zinu, ir, ka jquery nav tādu klašu kā tādu — tad, iedomājos, varbūt kādam gribas pavingrināties un reizē palīdzēt man...


(Lasīt komentārus) - (Ierakstīt jaunu komentāru)


[info]bez4pieci
2009-09-10 14:47 (saite)
īsumā: ir kaut kāda gara simbolu virkne, kas tiek iepeistota input laukā, un no kuras jāizdabū simbolu virkne, kas jāatstāj tajā laukā, bet pārējais viss garbage vienkārši jāizmet.
vajadzētu sagalabāt re-usability..

var CardInputCheck = new Class({
	
	Implements: [ Options, Events ],
	
	element: null,
	options: {
		readerOutputLength: 117,		// number of characters in the card reader output
		readerOutputRE: /^\%B\d{16}\^\s+\^\d+\?;\d+=\d+\?$/,	// regexp for the card reader output
		
		focusOnSuccess: null,	// element to focus on success						
		onSuccess: $empty	// function to execute on success
	},
	
	initialize: function(inputFieldElement, options) {
		this.setOptions(options);
		this.element = $(inputFieldElement);
		
		// focus the input field in question, so when the card is swiped, the output is written in this input field
		if(this.element && $type(this.element) == 'element') {
			this.element.addEvent('keyup', this.elementOnKeyUp.bindWithEvent(this));
			this.focusElement();				// for firefox (the delayed focus does not work for some reason)
			this.focusElement.delay(200, this);	// for all other browsers, who are not ready for immediate focusing
		}
	},
	
	elementOnKeyUp: function(event) {
		var v = this.element.get('value').trim();
		if(v.length == this.options.readerOutputLength && v.test(this.options.readerOutputRE)) {
			this.element.set('value', v.substr(8, 10)); // extract the actual card number from the reader's output
			this.success();
		}
	},
	
	success: function() {
		if(this.options.focusOnSuccess && this.options.focusOnSuccess.focus) {
			try {
				this.options.focusOnSuccess.focus();
			} catch(e) {
			}
		}
		
		this.fireEvent('success');
	},
	
	focusElement: function() {
		try {
			// focus method for some reason sometimes throws errors...
			this.element.focus();
		} catch(e) { 
		}
	}

});

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)


[info]misame
2009-09-10 15:53 (saite)
Puslīdz pāveidoju. Skaisti tas, šķiet, nav, bet dara apmēram to pašu, ko oriģināls. function CardInputCheck(inputFieldElement, opOptions) { if (opOptions!=null) this.options = opOptions; this.element = $(inputFieldElement); var self = this; // focus the input field in question, so when the card is swiped, the output is written in this input field if(this.element) { // && $type(this.element) == 'element') { this.element.bind('keyup', function(event) { var v = $(self.element).val(); if(v.length == self.options.readerOutputLength && v.match(self.options.readerOutputRE)) { $(self.element).val(v.substr(8, 10)); // extract the actual card number from the reader's output self.success(); } }); this.focusElement(); // for firefox (the delayed focus does not work for some reason) //this.focusElement.delay(200, this); // for all other browsers, who are not ready for immediate focusing } } CardInputCheck.prototype.options = { readerOutputLength: 117, // number of characters in the card reader output readerOutputRE: /^\%B\d{16}\^\s+\^\d+\?;\d+=\d+\?$/, // regexp for the card reader output focusOnSuccess: null, // element to focus on success onSuccess: null // function to execute on success }; CardInputCheck.prototype.success = function() { if(this.options.focusOnSuccess && this.options.focusOnSuccess.focus) { try { this.options.focusOnSuccess.focus(); } catch(e) { } } if(this.options.onSuccess!=null){ this.options.onSuccess(); } } CardInputCheck.prototype.focusElement = function() { try { // focus method for some reason sometimes throws errors... this.element.focus(); } catch(e) { } } /*#########################################################################################################################*/ //pieliekam jQuery modelim metodi "CardInputCheck" $.fn.CardInputCheck = function(options) { return this.each( function() { var oCheck = new CardInputCheck($(this), options); }) }; //dokumentam onload ar selektoru atrodam vajadzīgos elementusu un tiem izveicam CardInputCheck $(document).ready( function(){ $("#kuu").CardInputCheck( {readerOutputLength:13, readerOutputRE:/.*/ , focusOnSuccess:null, onSuccess: null } ); } );

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)

grrr, auto format...
[info]misame
2009-09-10 15:54 (saite)
function CardInputCheck(inputFieldElement, opOptions) {
	if (opOptions!=null) this.options = opOptions;
	this.element = $(inputFieldElement);
	
	var self = this;
	
	// focus the input field in question, so when the card is swiped, the output is written in this input field
	if(this.element) { // && $type(this.element) == 'element') {
		this.element.bind('keyup', function(event) {
										var v = $(self.element).val();
										if(v.length == self.options.readerOutputLength && v.match(self.options.readerOutputRE)) {
											$(self.element).val(v.substr(8, 10)); // extract the actual card number from the reader's output
											self.success();
										}
									});
		this.focusElement();				// for firefox (the delayed focus does not work for some reason)
		//this.focusElement.delay(200, this);	// for all other browsers, who are not ready for immediate focusing
	}
} 
CardInputCheck.prototype.options = {
		readerOutputLength: 117,		// number of characters in the card reader output
		readerOutputRE: /^\%B\d{16}\^\s+\^\d+\?;\d+=\d+\?$/,	// regexp for the card reader output
		focusOnSuccess: null,	// element to focus on success						
		onSuccess: null	// function to execute on success
	};

CardInputCheck.prototype.success = function() {
	if(this.options.focusOnSuccess && this.options.focusOnSuccess.focus) {
		try {
			this.options.focusOnSuccess.focus();
		} catch(e) {
		}
	}
	
	if(this.options.onSuccess!=null){
		this.options.onSuccess();
	}
}

CardInputCheck.prototype.focusElement = function() {
	try {
		// focus method for some reason sometimes throws errors...
		this.element.focus();
	} catch(e) { 
	}
}

/*#########################################################################################################################*/
//pieliekam jQuery modelim metodi "CardInputCheck"
$.fn.CardInputCheck = function(options) {
    return this.each( function() {
		var oCheck = new CardInputCheck($(this), options);
	})
};

//dokumentam onload ar selektoru atrodam vajadzīgos elementusu un tiem izveicam CardInputCheck
$(document).ready(
 function(){
  $("#kuu").CardInputCheck(
	{readerOutputLength:13, readerOutputRE:/.*/ , focusOnSuccess:null, onSuccess: null }
	);
 }
);	

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)

Re: grrr, auto format...
[info]bez4pieci
2009-09-11 02:48 (saite)
o! paldies! izskatās tīri tīkami un saprotami.
nezinu kāpēc jquery man vienmēr licies tāds kā bubulis - neērts, smags utt, bet nu vienmēr patīkami mainīt viedokli ; )

esmu tavs parādnieks!

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)

Re: grrr, auto format...
[info]misame
2009-09-11 15:59 (saite)

ņem par labu!
Šeit gan lielākā daļa koda sanāk "plain JavaScript" un tikai pašās beigās parādās kaut kas no jquery :-)

(Atbildēt uz šo) (Iepriekšējais)


(Lasīt komentārus) -

Neesi iežurnalējies. Iežurnalēties?