<commit_before>/*
Copyright (c) 2010, Geomatics and Cartographic Research Centre, Carleton 
University
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

 - Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.
 - Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
 - Neither the name of the Geomatics and Cartographic Research Centre, 
   Carleton University nor the names of its contributors may be used to 
   endorse or promote products derived from this software without specific 
   prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.

$Id: n2.schema.js 8461 2012-08-29 18:54:28Z jpfiset $
*/

// @requires n2.core.js
// @requires n2.utils.js
// @requires n2.class.js

;(function($,$n2){
"use strict";

// Localization
var _loc = function(str,args){ return $n2.loc(str,'nunaliit2',args); };


var defaultErrorFn = function(err){ $n2.reportError(err); };
var defaultSuccessFn = function(){};

var typeClassStringPrefix = 'n2schema_type_';

var HTML = ':html';
var INPUT = ':input';
var FIELD = ':field';
var ITERATE = ':iterate';
var EMPTY = ':empty';
var CONTEXT = ':context';
var PARENT = ':parent';
var SELECT = ':selector';
var LOCALIZE = ':localize';
var ARRAY = ':array';

//============================================================
// Object

function getDataFromObjectSelector(o, selectors) {
	if( typeof(selectors) === 'string' ) {
		selectors = selectors.split('.');
	}
	return findDataFromObject(o, selectors, 0);
};

function findDataFromObject(o, selectors, selectorIndex) {
	if( selectorIndex >= selectors.length ) {
		return o;
	};
	
	// This is an error. There are more
	// selectors in the array but we have
	// a scalar. Return an error.
	if( null == o
	 || typeof(o) === 'number' 
	 || typeof(o) === 'string'
	 || typeof(o) === 'undefined'
	 || typeof(o) === 'function' ) {
		return null;
	};
	
	if( $n2.isArray(o) ) {
		var index = 1 * selectors[selectorIndex];
		if( index >= o.length ) {
			return null;
		};
		return findDataFromObject(o[index], selectors, (selectorIndex+1));
	}

	if( typeof(o) === 'object' ) {
		var key = selectors[selectorIndex];
		var value = o[key];
		if( value === undefined ) {
			return null;
		};
		return findDataFromObject(value, selectors, (selectorIndex+1));
	};
	
	// Should not get here. Error. Return null.
	return null;
};

//============================================================
// Context

function _localizeString() {
	var args = [];
	args.push.apply(args,arguments);
	var options = args.pop();

	// Gets the text between start and end tags
	var text = options.fn(this);

	// Syntax is: <selector>(,<option>)*
	var splits = text.split(',');
	var key = splits[0];

	// <option> is one of:
	// - optionName
	// - optionName=optionValue
	// - optionsName=value1+value2+...
	var opts = {};
	for(var i=1,e=splits.length;i<e;++i){
		var optStr = splits[i];
		var optSplit = optStr.split('=');
		if( optSplit.length > 1 ){
			var valSplits = optSplit[1].split('+');
			if( valSplits.length > 1 ) {
				opts[optSplit[0]]=valSplits;
			} else {
				opts[optSplit[0]]=[optSplit[1]];
			};
		} else {
			opts[optSplit[0]]=[];
		};
	};
	
	// Get data from key
	if( '.' === key ) {
		var s = this;
	} else {
		s = getDataFromObjectSelector(this, key);
	};

	if( s
	 && typeof(s) === 'object' 
	 && s.nunaliit_type === 'localized') {
		var lang = 'en';
		if( $n2.l10n && $n2.l10n.getLocale ){
			lang = $n2.l10n.getLocale().lang;
		};
		
		if( s[lang] ) {
			if( opts.html ) {
				return s[lang];
			};
			
			var escaped = $n2.utils.escapeHtml(s[lang]);
			return escaped;
			
		} else {
			// Find a language to fall back on
			var fbLang = 'en';
			if( !s[fbLang] ) {
				fbLang = null;
				for(var l in s){
					if( l.length > 0 && l[0] === ':' ){
						// ignore
					} else if( l === 'nunaliit_type' ) {
						// ignore
					} else if( s[l] ) { // ignore empty string
						fbLang = l;
						break;
					};
				};
			};
			
			if( fbLang ){
				var result = [];
				result.push('<span class="n2_localized_string n2_localize_fallback">');
				result.push('<span class="n2_localize_fallback_lang">(');
				result.push(fbLang);
				result.push(')</span>');
				if( s[fbLang] ){
					if( opts.html ) {
						result.push(''+s[fbLang]);
					} else {
						var escaped = $n2.utils.escapeHtml(s[fbLang]);
						result.push(escaped);
					};
				};
				result.push('</span>');
				return result.join('');
				
			} else {
				return '';
			};
		};
		
	} else if( typeof(s) === 'undefined' ) {
		return '';

	} else if( s === null ) {
		return '';
		
	} else {
		// Must be string, number, boolean, float, ...
		// From Handlebars, we do not get a real "string". Instead,
		// "this" is an object that reacts like string. ''+s becomes
		// a real javascript string.
		if( opts.html ) {
			return ''+s;
		};
		
		var escaped = $n2.utils.escapeHtml(''+s);
		return escaped;
		
	};
};

function _formSingleField(r,obj,sels,options){
	
	// option: textarea
	if( options.textarea ){
		r.push('<textarea class="');
	} else if( options.checkbox ){
		r.push('<input type="checkbox" class="');
	} else {
		r.push('<input type="text" class="');
	};
	
	r.push('n2schema_input');
	
	var selector = obj[SELECT];
	if( selector ) {
		var completeSelector = selector.slice(0);
		completeSelector.push.apply(completeSelector,sels);
		var selClass = createClassStringFromSelector(completeSelector);

		r.push(' '+selClass);
	};
	
	if( options.date ){
		r.push(' ' + typeClassStringPrefix + 'date');
		
	} else if( options.numeric ){
		r.push(' ' + typeClassStringPrefix + 'numeric');
		
	} else if( options.layers ){
		r.push(' ' + typeClassStringPrefix + 'layers');
		
	} else if( options.localized ){
		r.push(' ' + typeClassStringPrefix + 'localized');
	};

	if( options.textarea ){
		r.push('"></textarea>');
	} else if( options.checkbox ){
		r.push('"/>');
	} else {
		r.push('"/>');
	};

};

function _formField() {
	// The arguments to handlebars block expression functions are:
	// ([obj,]options)
	// obj is not provided in this case, since we do not expect any arguments
	// to {{:field}}
	// The options hash contains a "fn" attribute, which is a function to
	// render inner content.
	// this points to the current object
	var args = [];
	args.push.apply(args,arguments);
	var options = args.pop();
	
	// Gets the text between start and end tags
	var text = options.fn(this);

	// Syntax is: <selector>(,<option>)*
	var obj,sels;
	var splits = text.split(',');
	var identifier = splits[0];
	if( '.' === identifier ){
		obj = this;
		sels = [];
	} else {
		sels = identifier.split('.');
		obj = getDataFromObjectSelector(this, sels);
	};
	
	if( obj
	 && !obj[SELECT]
	 && options
	 && options.data
	 && options.data.n2_selector
	 ){
		obj[SELECT] = options.data.n2_selector;
	};
	
	// <option> is one of:
	// - optionName
	// - optionName=optionValue
	// - optionsName=value1+value2+...
	var opts = {};
	for(var i=1,e=splits.length;i<e;++i){<commit_msg>nunaliit2-js: Fix issue with bringing up the user form.<commit_after>/*
Copyright (c) 2010, Geomatics and Cartographic Research Centre, Carleton 
University
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

 - Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.
 - Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
 - Neither the name of the Geomatics and Cartographic Research Centre, 
   Carleton University nor the names of its contributors may be used to 
   endorse or promote products derived from this software without specific 
   prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.

$Id: n2.schema.js 8461 2012-08-29 18:54:28Z jpfiset $
*/

// @requires n2.core.js
// @requires n2.utils.js
// @requires n2.class.js

;(function($,$n2){
"use strict";

// Localization
var _loc = function(str,args){ return $n2.loc(str,'nunaliit2',args); };


var defaultErrorFn = function(err){ $n2.reportError(err); };
var defaultSuccessFn = function(){};

var typeClassStringPrefix = 'n2schema_type_';

var HTML = ':html';
var INPUT = ':input';
var FIELD = ':field';
var ITERATE = ':iterate';
var EMPTY = ':empty';
var CONTEXT = ':context';
var PARENT = ':parent';
var SELECT = ':selector';
var LOCALIZE = ':localize';
var ARRAY = ':array';

//============================================================
// Object

function getDataFromObjectSelector(o, selectors) {
	if( typeof(selectors) === 'string' ) {
		selectors = selectors.split('.');
	}
	return findDataFromObject(o, selectors, 0);
};

function findDataFromObject(o, selectors, selectorIndex) {
	if( selectorIndex >= selectors.length ) {
		return o;
	};
	
	// This is an error. There are more
	// selectors in the array but we have
	// a scalar. Return an error.
	if( null == o
	 || typeof(o) === 'number' 
	 || typeof(o) === 'string'
	 || typeof(o) === 'undefined'
	 || typeof(o) === 'function' ) {
		return null;
	};
	
	if( $n2.isArray(o) ) {
		var index = 1 * selectors[selectorIndex];
		if( index >= o.length ) {
			return null;
		};
		return findDataFromObject(o[index], selectors, (selectorIndex+1));
	}

	if( typeof(o) === 'object' ) {
		var key = selectors[selectorIndex];
		var value = o[key];
		if( value === undefined ) {
			return null;
		};
		return findDataFromObject(value, selectors, (selectorIndex+1));
	};
	
	// Should not get here. Error. Return null.
	return null;
};

//============================================================
// Context

function _localizeString() {
	var args = [];
	args.push.apply(args,arguments);
	var options = args.pop();

	// Gets the text between start and end tags
	var text = options.fn(this);

	// Syntax is: <selector>(,<option>)*
	var splits = text.split(',');
	var key = splits[0];

	// <option> is one of:
	// - optionName
	// - optionName=optionValue
	// - optionsName=value1+value2+...
	var opts = {};
	for(var i=1,e=splits.length;i<e;++i){
		var optStr = splits[i];
		var optSplit = optStr.split('=');
		if( optSplit.length > 1 ){
			var valSplits = optSplit[1].split('+');
			if( valSplits.length > 1 ) {
				opts[optSplit[0]]=valSplits;
			} else {
				opts[optSplit[0]]=[optSplit[1]];
			};
		} else {
			opts[optSplit[0]]=[];
		};
	};
	
	// Get data from key
	if( '.' === key ) {
		var s = this;
	} else {
		s = getDataFromObjectSelector(this, key);
	};

	if( s
	 && typeof(s) === 'object' 
	 && s.nunaliit_type === 'localized') {
		var lang = 'en';
		if( $n2.l10n && $n2.l10n.getLocale ){
			lang = $n2.l10n.getLocale().lang;
		};
		
		if( s[lang] ) {
			if( opts.html ) {
				return s[lang];
			};
			
			var escaped = $n2.utils.escapeHtml(s[lang]);
			return escaped;
			
		} else {
			// Find a language to fall back on
			var fbLang = 'en';
			if( !s[fbLang] ) {
				fbLang = null;
				for(var l in s){
					if( l.length > 0 && l[0] === ':' ){
						// ignore
					} else if( l === 'nunaliit_type' ) {
						// ignore
					} else if( s[l] ) { // ignore empty string
						fbLang = l;
						break;
					};
				};
			};
			
			if( fbLang ){
				var result = [];
				result.push('<span class="n2_localized_string n2_localize_fallback">');
				result.push('<span class="n2_localize_fallback_lang">(');
				result.push(fbLang);
				result.push(')</span>');
				if( s[fbLang] ){
					if( opts.html ) {
						result.push(''+s[fbLang]);
					} else {
						var escaped = $n2.utils.escapeHtml(s[fbLang]);
						result.push(escaped);
					};
				};
				result.push('</span>');
				return result.join('');
				
			} else {
				return '';
			};
		};
		
	} else if( typeof(s) === 'undefined' ) {
		return '';

	} else if( s === null ) {
		return '';
		
	} else {
		// Must be string, number, boolean, float, ...
		// From Handlebars, we do not get a real "string". Instead,
		// "this" is an object that reacts like string. ''+s becomes
		// a real javascript string.
		if( opts.html ) {
			return ''+s;
		};
		
		var escaped = $n2.utils.escapeHtml(''+s);
		return escaped;
		
	};
};

function _formSingleField(r,obj,sels,options){
	
	// option: textarea
	if( options.textarea ){
		r.push('<textarea class="');
	} else if( options.checkbox ){
		r.push('<input type="checkbox" class="');
	} else {
		r.push('<input type="text" class="');
	};
	
	r.push('n2schema_input');
	
	var selector = obj[SELECT];
	if( selector ) {
		var completeSelector = selector.slice(0);
		completeSelector.push.apply(completeSelector,sels);
		var selClass = createClassStringFromSelector(completeSelector);

		r.push(' '+selClass);
	};
	
	if( options.date ){
		r.push(' ' + typeClassStringPrefix + 'date');
		
	} else if( options.numeric ){
		r.push(' ' + typeClassStringPrefix + 'numeric');
		
	} else if( options.layers ){
		r.push(' ' + typeClassStringPrefix + 'layers');
		
	} else if( options.localized ){
		r.push(' ' + typeClassStringPrefix + 'localized');
	};

	if( options.textarea ){
		r.push('"></textarea>');
	} else if( options.checkbox ){
		r.push('"/>');
	} else {
		r.push('"/>');
	};

};

function _formField() {
	// The arguments to handlebars block expression functions are:
	// ([obj,]options)
	// obj is not provided in this case, since we do not expect any arguments
	// to {{:field}}
	// The options hash contains a "fn" attribute, which is a function to
	// render inner content.
	// this points to the current object
	var args = [];
	args.push.apply(args,arguments);
	var options = args.pop();
	
	// Gets the text between start and end tags
	var text = options.fn(this);

	// Syntax is: <selector>(,<option>)*
	var obj,sels;
	var splits = text.split(',');
	var identifier = splits[0];
	if( '.' === identifier ){
		obj = this;
		sels = [];
	} else {
		sels = identifier.split('.');
		obj = getDataFromObjectSelector(this, sels);
	};
	
	if( typeof obj === 'object'
	 && !obj[SELECT]
	 && options
	 && options.data
	 && options.data.n2_selector
	 ){
		obj[SELECT] = options.data.n2_selector;
	};
	
	// <option> is one of:
	// - optionName
	// - optionName=optionValue
	// - optionsName=value1+value2+...
	var opts = {};
	for(var i=1,e=splits.length;i<e;++i){
{'fraction_non_alphanumeric': 0.1669990029910269, 'fraction_numerical': 0.009035393818544367, 'mean_word_length': 3.4066447007138936, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1554157', 'n_tokens_mistral': 6001, 'n_tokens_neox': 5200, 'n_words': 1553}