org.silex.core.Utils

In Utils class we put all the usefull functions used to deal with medias and standard user interactions. in the repository : /trunk/core/Utils.as

Version

1

Author

Alexandre Hoyau

Summary
org.silex.core.UtilsIn Utils class we put all the usefull functions used to deal with medias and standard user interactions.
silex_ptrReference to silex main Api object (org.silex.core.Api).
UtilsConstructor.
Date and time accessorsto display today date as 08/02/14 : ((<<silex.utils.YY>>/<<silex.utils.MM>>/<<silex.utils.DD>>)) to display today date as monday 14 february : ((<<silex.utils.DAY>> <<silex.utils.DD>> <<silex.utils.MONTH>>))
YYthe year on 2 digits (eg 08 for 2008)
MMthe month number on 2 digits (eg 01 for january)
DDthe day number on 2 digits (eg 08 for 8th day of the month)
HHthe hour (from 0 to 23) on 2 digits
HH12the hour (from 0 to 12) on 2 digits
MMINthe minutes (from 0 to 59) on 2 digits
YEARthe year on 4 digits (eg 2008)
DAYdependent on the language config
MONTHdependent on the language config
HOURthe hour (from 0 to 23)
HOUR12the hour (from 0 to 12)
AMPMAM or PM
MINUTthe minutes (from 0 to 59)
Conversions
getRawTextFromHtmlConvert an html formated text into a raw text (removes all tags).
cleanIDRemove unwanted chars from ID.
addslashesAdd slashes to a string before characters that need to be quoted in database queries etc.
formatNumberWithNDigitsConvert num into string and adds 0s before it so that it has nDigits numbers.
User interaction windows
lastOpenedAlertSimpleNameName of the last opened alertSimple dialog box.
alertDisplay a message in a dialog box.
alertSimpleDisplay a message without a dialog box.
confirmDisplay a question in a dialog box with “ok” and “cancel” buttons.
promptAsk the user to write a string.
promptPasswordAsk the user to login
swf related functions
getRootUrlTakes the url from which _root was downloaded, remove the file name and return the url of the folder from which _root was downloaded.
getTargetretrieve an object from its path, starting in a given MovieClip
media functions
loadingMediasArray which contains all the files and streams being downloaded.
resizeMediaResize the given movie clip so that it fits the rectangle and it keeps the same width/height ratio.
centerMediaCenter a movie clip in a specifyed zone or container.
loadMediaLoads a media into a container.
traceCustom trace function.
dumpDump an object recursively.
obj2jsonSerialize an object recursively into a json string
strings utilities
replaceReplace a string by another string in the source string.
deaccentReplace accented UTF-8 characters by unaccented ASCII-7 equivalents.
splitTagsUse the accessors delimiters paterns to split the input string into an array.
revealAccessorsReplaces accessors by their values in a String.
revealWikiSyntaxReveal wiki tags.
processWikiImageTagReveal images.
processWikiLinkTagReveal links.
processWikiLinkTagCallbackCallback function executed when the user clicks on a link in a text.
revealTagsReveal all wiki tags or accessors contained in a string.
validators
checkValidMailMail validation.
arrays utilities
isPartOfcheck if an array is part of another array
effects
applyTypeWriterEffectTypewriter effect.
removeTypeWriterEffectRemove the effect from the TextFiled
createDelegateCreates a functions wrapper for the original function so that it runs in the provided context.<br/> You can pass parameters to the function @parameter obj Context in which to run the function.

silex_ptr

private var silex_ptr: org.silex.core.Api

Reference to silex main Api object (org.silex.core.Api).

Utils

function Utils(api: org.silex.core.Api)

Constructor.

Parameters

param api Reference to silex main Api

Date and time accessors

to display today date as 08/02/14 : ((<<silex.utils.YY>>/<<silex.utils.MM>>/<<silex.utils.DD>>)) to display today date as monday 14 february : ((<<silex.utils.DAY>> <<silex.utils.DD>> <<silex.utils.MONTH>>))

YY

function get YY():String

Returns

the year on 2 digits (eg 08 for 2008)

MM

function get MM():String

Returns

the month number on 2 digits (eg 01 for january)

DD

function get DD():String

Returns

the day number on 2 digits (eg 08 for 8th day of the month)

HH

function get HH():String

Returns

the hour (from 0 to 23) on 2 digits

HH12

function get HH12():String

Returns

the hour (from 0 to 12) on 2 digits

MMIN

function get MMIN():String

Returns

the minutes (from 0 to 59) on 2 digits

YEAR

function get YEAR():String

Returns

the year on 4 digits (eg 2008)

DAY

function get DAY():String

dependent on the language config

Returns

the day of today (eg Monday)

MONTH

function get MONTH():String

dependent on the language config

Returns

the month (eg February)

HOUR

function get HOUR():String

Returns

the hour (from 0 to 23)

HOUR12

function get HOUR12():String

Returns

the hour (from 0 to 12)

AMPM

function get AMPM():String

Returns

AM or PM

MINUT

function get MINUT():String

Returns

the minutes (from 0 to 59)

Conversions

getRawTextFromHtml

public function getRawTextFromHtml(html_str):String

Convert an html formated text into a raw text (removes all tags).

Parameters

html_strthe html formated text

Returns

the corresponding raw text

cleanID

function cleanID(id: String):String

Remove unwanted chars from ID.  Cleans a given ID to only use allowed characters.  Accented characters are converted to unaccented ones.  Comes from dokuwiki inc/pageutils.php.  @example filterSectionName(“my Section.  NAME”) returns “my_section._name”

Parameters

param raw_id The pageid to clean

addslashes

function addslashes(_str: String):String

Add slashes to a string before characters that need to be quoted in database queries etc.  These characters are single quote (‘), double quote (“), backslash (\).

Parameters

param _str the string to which to add the slashes

Returns

a string with backslashes before characters that need to be quoted in database queries etc. 

formatNumberWithNDigits

function formatNumberWithNDigits(num: Number,
nDigits: Number):String

Convert num into string and adds 0s before it so that it has nDigits numbers.

Parameters

param num the number to convert
param nDigits the number of digits which the output string will have

Returns

a string with nDigits digit and which represents the input number

User interaction windows

lastOpenedAlertSimpleName

var lastOpenedAlertSimpleName: String

Name of the last opened alertSimple dialog box.

alert

public function alert(text_str: String,
cbkFunction: Function):MovieClip

Display a message in a dialog box. when the user closes the window (click on “ok” button or on “close” button), the desired callback function is called.

Parameters

text_strthe text to be displayed (the message)
cbkFunctionthe callback function to be called when the user closes the window

Returns

the dialog box (use it to force the dialog box to close for example)

alertSimple

public function alertSimple(text_str: String,
time_num: Number,
cbkFunction: Function):MovieClip

Display a message without a dialog box.

Parameters

text_strthe text to be displayed (the message)
time_numduration in milliseconds for the message to be displayed
cbkFunctionthe callback function to be called when the time has elapsed

Returns

the dialog box (use it to force the dialog box to close for example)

confirm

public function confirm(text_str: String,
cbkFunction: Function):MovieClip

Display a question in a dialog box with “ok” and “cancel” buttons.  When the user closes the window (click on “ok” button or on “close” button or “cancel” button), the desired callback function is called.

Parameters

text_strthe text to be displayed (the message)
cbkFunctionthe callback function to be called when the user closes the window

Returns

the dialog box (use it to force the dialog box to close for example)

prompt

public function prompt(text_str: String,
default_str: String,
cbkFunction: Function):MovieClip

Ask the user to write a string. when the user closes the window (click on “ok” button or on “close” button or “cancel” button), the desired callback function is called.

Parameters

text_strthe text to be displayed (the message)
default_strthe default string to be displayed before the user modify it
cbkFunctionthe callback function to be called when the user closes the window

Returns

the dialog box (use it to force the dialog box to close for example)

promptPassword

public function promptPassword(text_str: String,
cbkFunction: Function):MovieClip

Ask the user to login

Parameters

text_strthe text to be displayed (the message)
cbkFunctionthe callback function to be called when the user closes the window

Returns

the dialog box (use it to force the dialog box to close for example)

swf related functions

getRootUrl

public function getRootUrl():String

Takes the url from which _root was downloaded, remove the file name and return the url of the folder from which _root was downloaded.  Also used in Api rootUrl getter.

Returns

the url of the folder from which _root was downloaded

getTarget

public function getTarget(source_mc/*: Object*/,
path_str: String,
separator: String):Object

retrieve an object from its path, starting in a given MovieClip

Parameters

param source_mc the object in which to start looking for the target
param path_str the target path starting in the source MovieClip
param separator the separator used for inclusion in the notation (path.to.a.variable or path/to/a/variable) - default is “.”

Returns

the targeted object

media functions

loadingMedias

public var loadingMedias: Array

Array which contains all the files and streams being downloaded.

resizeMedia

public function resizeMedia(target_mc: MovieClip,
w: Number,
h: Number):Number

Resize the given movie clip so that it fits the rectangle and it keeps the same width/height ratio.

Parameters

target_mcthe movie clip to be resized
wthe maximum width that the media can have
hthe maximum height that the media can have

Returns

the ratio by which the media has been resized

centerMedia

public function centerMedia(target_mc, 
w: Number,
h: Number,
delta_x: Number,
delta_y: Number)

Center a movie clip in a specifyed zone or container.

Parameters

target_mcmovie clip to be centered
wcontainer’s width
hcontainer’s height
delta_xoptional - container’s upper left corner x-coordinate
delta_yoptional - container’s upper left corner y-coordinate

loadMedia

public function loadMedia(url_str: String,
target_mc: MovieClip,
resObj: Object,
container_rect: Object,
resize_bool: Boolean,
center_bool: Boolean)

Loads a media into a container. the media can be of any type (jpg, swf, mp3, flv, and other once the frame development will be done).  While downloading, the media will be referenced in an object stored in loadingMedias.  TO DO : USE IT IN org.silex.ui.players.Image

Parameters

url_strurl of the media to be loaded
target_mcthe MovieClip in which to load the media
resObjoptional - the listener object
container_rectoptional - object representing the rectangle used to center or resize the media once loaded, contains left, top, right and bottom attributes
resize_booloptional - if true, the media is resized to fit container_rect
center_booloptional - if true, the media is centered with container_rect’s center

trace

public function trace(_obj: Object,
level_str: String)

Custom trace function.  Use XRAY debug console if it is available.

Parameters

param _obj the object to trace
param level_str a debug level

dump

function dump(_obj: Object,
tab: String):String

Dump an object recursively.  Trace all its properties and dump its objects

Parameters

param _obj the object to dump
param tab tab characters to be added at the beginning of a line for formatting

Returns

a string which can be printed or traced

obj2json

function obj2json(_obj):String

Serialize an object recursively into a json string

Parameters

param _obj the object, array or value to serialize

Returns

a string which can be printed or traced

strings utilities

replace

function replace(chaine_str: String,
a_remplacer_str: String,
remplacement_str: String):String

Replace a string by another string in the source string.

Parameters

param chaine_str
param a_remplacer_str
param remplacement_str

Returns

the input with all occurences replaced

deaccent

function deaccent(_str: String):String

Replace accented UTF-8 characters by unaccented ASCII-7 equivalents.  From dokuwiki inc/utf8.php.

Parameters

param _str the input string

Returns

the de-accented string

splitTags

function splitTags(input_str: String,
leftTag: String,
rightTag: String):Array

Use the accessors delimiters paterns to split the input string into an array.

Parameters

param input_str a string containing accessors
param leftTag [[optional]]left delimiter, (( by default
param rightTag [[optional]]right delimiter, )) by default

Returns

the array of strings containing accessors

revealAccessors

function revealAccessors(input_str/*: String*/,
source_mc: Object,
separator: String)//:String

Replaces accessors by their values in a String. for example “my text is for <<dbdata.name1>>!!! not for <<dbdata.name2>> ((nor for <<dbdata.name3>>!!!  ))” becomes - “my text is for value name1!!! not for value name2 ((nor for value name3!!!  ))” if all name variables exist - “my text is for !!! not for <<dbdata.name2>>” if only dbdata.name2 exists

Parameters

input_stra string containing accessors (“((...<<path.to.a.variable>>...))”)
source_mcthe movie clip from which to start when looking for the variable (see org.silex.Utils.getTarget)
separatorthe separator used for inclusion in the notation (path.to.a.variable or path/to/a/variable) - default is “.”

Returns

the resulting string [or object if only 1 accessor in the string, or an array of objects if only accessors and no other chars]

revealWikiSyntax

function revealWikiSyntax(input_str: String):String

Reveal wiki tags.  Images and links, see bellow

Parameters

param input_str a string which contains wiki tags

Returns

the resulting string

processWikiImageTag

function processWikiImageTag(tag_str: String):String

Reveal images.

Parameters

param tag_str the string between {{ and }}

Returns

the equivalent html code

processWikiLinkTag

function processWikiLinkTag(tag_str: String):String

Reveal links.  If you use this, you have to declare a function like this one in the same movie than the text field which displays the text: function processWikiLinkTagCallback (escapedCommands_str:String) { silexInstance.utils.processWikiLinkTagCallback(escapedCommands_str); }

Parameters

param tag_str the string between {{ and }}

Returns

the equivalent html code

processWikiLinkTagCallback

function processWikiLinkTagCallback (escapedCommands_str: String)

Callback function executed when the user clicks on a link in a text.  Execute the commands associated with this link.

Parameters

escapedCommands_strescaped string containing SILEX commands separated by a pipe character (“|”)

revealTags

function revealTags(input_str: String,
openTag_str: String,
closeTag_str: String,
processTagFunction: Function):String

Reveal all wiki tags or accessors contained in a string.

Parameters

param input_str string containing wiki tags or accessors
param openTag_str accessor left patern
param closeTag_str accessor right patern
param processTagFunction callback function to be called for each tag found

Returns

the resulting string

validators

checkValidMail

function checkValidMail(mail_str):Boolean

Mail validation.

Parameters

param mail_str a string which is suposed to be an email

Returns

true if it is a valid email adress

arrays utilities

isPartOf

function isPartOf(localContext_array: Array,
globalContext_array: Array):Boolean

check if an array is part of another array

Parameters

param localContext_array the context that may be contained in the other context
param globalContext_array the context that may contain the other context

Returns

true if localContext is included in globalContext

effects

applyTypeWriterEffect

function applyTypeWriterEffect(_txt: TextField,
text_str: String,
speen_num: Number,
callback: Function)

Typewriter effect.  @example _global.getSilex().utils.applyTypeWriterEffect(_txt,”My display text”, 40);

Parameters

param _txt TextField to which to apply the effect
param text_str the text to display
param speen_num the speed of the effect
param callback callback called at the end of the anomation

removeTypeWriterEffect

function removeTypeWriterEffect(_txt: TextField)

Remove the effect from the TextFiled

Parameters

param _txt TextFiled which has a typewriter effect

createDelegate

static function createDelegate(obj: Object,
func: Function):Function

Creates a functions wrapper for the original function so that it runs in the provided context.<br/> You can pass parameters to the function @parameter obj Context in which to run the function.  @paramater func Function to run.  @paramater ... parameters to pass to the delegated function

private var silex_ptr: org.silex.core.Api
Reference to silex main Api object (org.silex.core.Api).
function Utils(api: org.silex.core.Api)
Constructor.
function get YY():String
the year on 2 digits (eg 08 for 2008)
function get MM():String
the month number on 2 digits (eg 01 for january)
function get DD():String
the day number on 2 digits (eg 08 for 8th day of the month)
function get HH():String
the hour (from 0 to 23) on 2 digits
function get HH12():String
the hour (from 0 to 12) on 2 digits
function get MMIN():String
the minutes (from 0 to 59) on 2 digits
function get YEAR():String
the year on 4 digits (eg 2008)
function get DAY():String
dependent on the language config
function get MONTH():String
dependent on the language config
function get HOUR():String
the hour (from 0 to 23)
function get HOUR12():String
the hour (from 0 to 12)
function get AMPM():String
AM or PM
function get MINUT():String
the minutes (from 0 to 59)
public function getRawTextFromHtml(html_str):String
Convert an html formated text into a raw text (removes all tags).
function cleanID(id: String):String
Remove unwanted chars from ID.
function addslashes(_str: String):String
Add slashes to a string before characters that need to be quoted in database queries etc.
function formatNumberWithNDigits(num: Number,
nDigits: Number):String
Convert num into string and adds 0s before it so that it has nDigits numbers.
var lastOpenedAlertSimpleName: String
Name of the last opened alertSimple dialog box.
public function alert(text_str: String,
cbkFunction: Function):MovieClip
Display a message in a dialog box.
public function alertSimple(text_str: String,
time_num: Number,
cbkFunction: Function):MovieClip
Display a message without a dialog box.
public function confirm(text_str: String,
cbkFunction: Function):MovieClip
Display a question in a dialog box with “ok” and “cancel” buttons.
public function prompt(text_str: String,
default_str: String,
cbkFunction: Function):MovieClip
Ask the user to write a string.
public function promptPassword(text_str: String,
cbkFunction: Function):MovieClip
Ask the user to login
public function getRootUrl():String
Takes the url from which _root was downloaded, remove the file name and return the url of the folder from which _root was downloaded.
public function getTarget(source_mc/*: Object*/,
path_str: String,
separator: String):Object
retrieve an object from its path, starting in a given MovieClip
public var loadingMedias: Array
Array which contains all the files and streams being downloaded.
public function resizeMedia(target_mc: MovieClip,
w: Number,
h: Number):Number
Resize the given movie clip so that it fits the rectangle and it keeps the same width/height ratio.
public function centerMedia(target_mc, 
w: Number,
h: Number,
delta_x: Number,
delta_y: Number)
Center a movie clip in a specifyed zone or container.
public function loadMedia(url_str: String,
target_mc: MovieClip,
resObj: Object,
container_rect: Object,
resize_bool: Boolean,
center_bool: Boolean)
Loads a media into a container.
public function trace(_obj: Object,
level_str: String)
Custom trace function.
function dump(_obj: Object,
tab: String):String
Dump an object recursively.
function obj2json(_obj):String
Serialize an object recursively into a json string
function replace(chaine_str: String,
a_remplacer_str: String,
remplacement_str: String):String
Replace a string by another string in the source string.
function deaccent(_str: String):String
Replace accented UTF-8 characters by unaccented ASCII-7 equivalents.
function splitTags(input_str: String,
leftTag: String,
rightTag: String):Array
Use the accessors delimiters paterns to split the input string into an array.
function revealAccessors(input_str/*: String*/,
source_mc: Object,
separator: String)//:String
Replaces accessors by their values in a String.
function revealWikiSyntax(input_str: String):String
Reveal wiki tags.
function processWikiImageTag(tag_str: String):String
Reveal images.
function processWikiLinkTag(tag_str: String):String
Reveal links.
function processWikiLinkTagCallback (escapedCommands_str: String)
Callback function executed when the user clicks on a link in a text.
function revealTags(input_str: String,
openTag_str: String,
closeTag_str: String,
processTagFunction: Function):String
Reveal all wiki tags or accessors contained in a string.
function checkValidMail(mail_str):Boolean
Mail validation.
function isPartOf(localContext_array: Array,
globalContext_array: Array):Boolean
check if an array is part of another array
function applyTypeWriterEffect(_txt: TextField,
text_str: String,
speen_num: Number,
callback: Function)
Typewriter effect.
function removeTypeWriterEffect(_txt: TextField)
Remove the effect from the TextFiled
static function createDelegate(obj: Object,
func: Function):Function
Creates a functions wrapper for the original function so that it runs in the provided context.<br/> You can pass parameters to the function @parameter obj Context in which to run the function.