Class: CommonUtils

mmir. CommonUtils

mmir.CommonUtils

A Utility class to support various functions.
Example
var isList = mmir.CommonUtils.isArray(list);

Requires

  • module:Resources
  • module:mmir.SemanticInterpreter
  • module:util/isArray
  • module:util/deferred
  • module:util/loadFile

Members

regexHTMLCommentString RegExp

Regular Expression for matching HTML comments.
This RegExp also matches multi-line comments. Note upon using the RegExp that it does not consider if a HTML comment is specified within a String or data-definition (i.e. the comment is matched regardless were its defined).
Example

Methods

checkNetworkConnection(){Boolean}

This function is used check whether a network connection is enabled.
This version of checking the network connection is based on the cordova 2.3.0 API. TODO implement with HTML5 functions (in addition to / instead of cordova)?
Returns:
Type Description
Boolean true if a network connection is enabled

concatArray(){string}

extracts all the strings from a String-Array into a single string
Returns:
Type Description
string text

getCompiledGrammarPath(generatedGrammarsPath, grammarId, isFileNameOnly){String}

Get the file path/name for a compiled grammar (executable JavaScript grammars).
Name Type Description
generatedGrammarsPath String Path of the grammars which should be loaded, e.g. gen/grammar/
grammarId String the ID (e.g. language code) for the grammar
isFileNameOnly Boolean optional OPTIONAL if TRUE then only the file name will be returned, otherwise the full path is returned
Returns:
Type Description
String file path / name for the compiled grammar (returns an empty string, if there is no compile grammar for the specified grammar ID)

getCompiledResourcesIds(compiledResourcesPath){Array.<String>}

Get the IDs of compiled resources
Name Type Description
compiledResourcesPath String Path for the resources, e.g. gen/grammar/
Returns:
Type Description
Array.<String> list of IDs for the compiled resources

getDirectoryStructure(){Object}

Returns:
Type Description
Object Directory structure as json object

asyncgetLocalScript(scriptUrl, success, fail)

Similar to the jQuery.getScript() function - appending a <script> element for javascript-source to the header of the main document. The success-callback is invoked if the script was successfully loaded, otherwise the fail-callback.
Name Type Description
scriptUrl String source of javascript-file
success function success callback function
fail function fail callback function

getPartialsPrefix(){String}

Returns:
Type Description
String The Prefix for the file names of partial-files
Checks if an object is an Array.

This function can be safely run in arbitrary contexts, e.g.

 var checkArray = mmir.CommonUtils.isArray;
 if( checkArray(someObject) ){
  ...
Name Type Description
object Object the Object for checking if it is an Array
Returns:
Type Description
Boolean true if object is an Array, otherwise false.

isRunningOnAndroid(){Boolean}

Detects via the user-agent-string if the application is running on Android.
Returns:
Type Description
Boolean True if application is running on Android, False otherwise

isRunningOnSmartphone(){Boolean}

Should detect - via the user-agent-string - if the application is running on Android, Symbian or iOS; in other words: on a smartphone.
Returns:
Type Description
Boolean True if application is running on smartphone, False otherwise

listDir(pathname, filter){Array}

This function returns an array of strings (file names) with the contents of the directory pathname. The pathname must be one of the directories (or sub-directories) of the framework's parsed folders, see directoriesToParse. If a filter is use, only files which's names match the filter are included in the returned list.
Name Type Description
pathname String Path of the directory which's contents should be returned
filter RegExp | function optional Filter for file-names: if String the file-name or part of the file-name (no wild cards!) (comparison is not case-sensitive), e.g.: someFile.js, .js or myview.ehtml if RegExp the file-name must match the regular expression, e.g.: /.*\.js/ig or /^.*\.ehtml$/ig if Function the file-name is included, if the function returns true, where the function signature is function(fileName: String) : Boolean, note that argument fileName will have been transformed to lower-case characters
Returns:
Type Description
Array Array of Strings which contains the contents of the directory. Or null, if pathname is not one of the framework's parsed folders.

asyncloadCompiledGrammars(generatedGrammarsPath, cbFunction, ignoreGrammarIds){Promise}

Load all compiled grammars (executable JavaScript grammars).
Name Type Description
generatedGrammarsPath String Path of the grammars which should be loaded, e.g. gen/grammar/
cbFunction function The function that should be executed after the plugins are loaded. If the execution of following functions is dependent on the presence of the grammars, they should be triggered from inside the callback-function.
ignoreGrammarIds Array.<String> optional OPTIONAL grammar IDs that should be ignored, i.e. not loaded, even if there is a file available
Returns:
Type Description
Promise a deferred promise (see loadImpl())

asyncloadDirectoryStructure(success, errorFunc)

Parses the directory structure and stores the result in the class-property mmir.CommonUtils-directoryStructure
Name Type Description
success function optional The function that should be executed after the diretories are parsed - it's best to include all following functions inside the callback-function.
errorFunc function optional callback function that is invoked if an error occured during initialization.

asyncloadImpl(librariesPath, isSerial, completedCallback, checkIsAlreadyLoadedFunc, statusCallback){Promise}

Load implementation files (i.e. JavaScript files) from a directory (if librariesPath is a String) or or a list of files-names (if librariesPath is an Array of Strings).
Name Type Description
librariesPath String | Array.<String> Path (or list of of the plugins which should be loaded, e.g. mmirf/plugins/ NOTE: The (String) path must be an entry in directories.json! (directories.json is used to generate/"query" the file-list for the path)
isSerial Boolean Set true if the libraries should be loaded serially, i.e. synchronously, that is "one after the other" (later ones may depend on earlier ones). set false if libraries should be loaded in parallel, i.e. "asychronously" (NOTE in this case, the libraries must not depend on each other). NOTE: The loading process as a hole is asynchronous (regardless of parameter isSerial), i.e. loading is completed when completedCallback() is invoked, NOT when this function returns!
completedCallback function optional The function that should be executed after the libraries are loaded. If the execution of following functions is dependent on the presence of the libraries, they should be capsuled inside this callback-function.
checkIsAlreadyLoadedFunc function optional If provided, this function checks (based on the file-name), if the library is already loaded. The signature for the callback is checkIsAlreadyLoadedFunc(String fileName) return [true|false], i.e. the function may check - based on the file-name - if the library is already loaded. If the function returns true, the library will not be loaded, and loading continues with the next library-file. NOTE: if isSerial is false, libraries with lower indices in the list may still be loading, when later entries are checked with this callback. In consequence, the "is already loaded"-check may not be accurate, in case parallel loading is used and the library-list contains "duplicate" entries.
statusCallback function optional If provided, this function is invoked, when a library was loaded loaded (INFO) or an error occurs. The signature for the callback is statusCallback(String statusLevel, String fileName, String message, [result]) where statusLevel is one of info, warning, error, fileName is the file-name for the library that this status message concerns, and message is a message text with details concerning the status, and OPTIONALLY result is the result (may be undefined if no result is returned; the actual value may depend on the execution environment)
Returns:
Type Description
Promise a deferred promise that will be fulfilled when loadImpl() has finished.

asyncloadScript(uri, success, fail)

Checks uri for "require://" protocol: If require-protocol, does require() the resource
"require://controller/application" -> require('controller/application')
Otherwise loads as URL via getLocalScript.
Name Type Description
uri String the URI for the script: either require-recouse or file path/URL
success function success callback function
fail function fail callback function
See:

parseParamsToDictionary(urlParamsPartStrings){Object}

IMPORTED FROM paramsParseFunc.js

Convert parameter-part of an URL to a "dictionary", containing the parameter keys and values

?id=5&name=heinz&name=kunz

	{
	  id: "5",
	  name: ["heinz", "kunz"],

	  //utility functions
	  has: function(key) : Boolean,
	  isMultiple: function(key) : Boolean,// is entry an Array of values
	  getKeys: function() : String[],     // get list of all keys
	}
	

The returned "dictionary" has the following functions:

  • has(String key): returns true if the dictionary contains an entry for key
  • isMultiple(String key): returns true if the entry for key is an Array (i.e. the URL contained multiple values for this key)
  • getKeys(): returns an Array with the keys (String) for all entries
Name Type Description
urlParamsPartStrings String the parameter-part of the URL, i.e. &...
Returns:
Type Description
Object a "dictionary" for the parameters