Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

wordSelect.as

Name: wordSelect() - (F5 or MX) click a word in a textField to select it and peform an action.
Author: senocular: www.senocular.com
Date: 1899-12-31T00:01:29.600
Documentation:
WORDSELECT: allows you to click within a textfield, have the click 
word selected and passed to a function.

wordSelect itself is run using two functions, the continuous runWordSelect (for an enterFrame)
and testWordSelect which would be most likely used on a mouseUp event.

runWordSelect selects and runs a function passing in the selected text if the passed text var
was considered selected by the testWordSelect function. 

The reason these are split up is because clicking and selecting a span in Flash all at the
same time doesnt seem to work as well as one might hope it would...

testWordSelect
- sets a variable in this object (_wordSelected) which determines if there is selected text
or not... so its really not a good idea to use this for more than one textfield on any given
object.
Arguments:
- textVar: textfield to test selection (clicked word) on. *if using Flash 5, this needs
to be the textfield var, if MX, use the textfield instance name.

runWordSelect
Arguments:
- textVar: textfield to set selection (clicked word) on. in both Flash 5 and MX this is
the textfield var (or instancename.text).
- action: a function to have run when a word has been selected. It gets passed, in this
order:
(string) the text var, (string) the word selected, (int) start index of the word in the text and
the (int) end index of the word in the text. ex:
action = function(text, word, wordStart, wordEnd){};

Returns:
- returns 1 if a word was selected, 0 if there was a test yielding in no (or empty) word and
-1 if no test was intiated (the value returned everyframe if testWordSelect isnt run)

Example:
// For a movieclip (Flash 5) containing the dynamic text field with var txt
onClipEvent(load){
	// txt is inner textField (field1_txt) text var name
	this.txt = "Include in the beginning of the body text of your website, words or phrases that are associated with identifying a website like yours.";
	this.display = function(text, word, wordStart, wordEnd){ // run when word is selected
		_root.selected = word; // textField in _root given the selected word as a value
	}
}
onClipEvent(enterFrame){
	// always pass textField VAR name
	runWordSelect(this.txt, display);
}
onClipEvent(mouseUp){
	// in Flash 5, pass textField VAR name, in MX pass textField INSTANCE name
	testWordSelect(this.txt);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
String.prototype.isAlphaNumeric = function(){
	var c = this.charCodeAt(0);
	if (47 < c && c < 58) return true;
	if (64 < c && c < 91) return true;
	if (96 < c && c < 123) return true;
	return false;
}
String.prototype.getWordEnd = function(index, dir){
	var r;
	if (arguments.length < 2) var dir = 1;
	do{
		if (dir < 0){ if (index <= 0) return 0;
		}else if (index >= this.length) return this.length;
		r = this.charAt(index);
		if (!r.isAlphaNumeric()) return index;
	}while(index += dir);
	return 0;
}
Object.prototype.runWordSelect = function(text, action){
	if (this._wordSelected && this._wordSelected++ > 1){
		this._wordSelected = 0;
		var i = Selection.getCaretIndex();
		var span = [text.getWordEnd(i,-1), text.getWordEnd(i,1)];
		if (span[0] != span[1]){
			if (span[0]) span[0]++;
			Selection.setSelection(span[0],span[1]);
			action(text, text.slice(span[0],span[1]), span[0], span[1]);
			return 1;
		}
		return 0;
	}
	return -1;
	
}
Object.prototype.testWordSelect = function(textVar){ // textVar: textField VAR in F5, textField INSTANCE in MX
	if (eval(Selection.getFocus()) == textVar){
		if(!this._wordSelected) this._wordSelected = 1;
	}
}