<commit_before>/* ============================================================================

 >4SESz.,     _,     ,gSEE2zx.,_        .azx                    ,w.
@Sh. "QEE3.  JEE3.  &ss  `*4EEEEE2x.,_  "EEV  ,aS5^;dEEEE2x.,   VEEF  _
\E2`_,gF4EEEx.?jF   EE5L       `*4EEEEE2zpn..ZEEF ;sz.  `*EEESzw.w* '|EE.
  ,dEEF   `"?j]  _,_   ,_  _,     _,.     L.EEEF  !EEF  _,,  `"``    EE7   ,,_
 :EEE7 ,ws,`|EEL`JEEL`JEE)`JEEL zE5` E3. / [EE3  ,w.  zE2` Ek .zE5^JZE3.,6EF [3
 {EEE. VEE7.EE2  AE3. EEV  ZEEL{EEL ws. ;  [EE1  EEEt{E3. JEELEE3. JE5LJEEF ,w,
  \EEk,,>^ JEEL,@EEF ZE5L,ZE5^ "Q3. V2`.    \EEk,,J' "Q[ yE2^ VE[_zEE[,"QEL V5F
          ,ss  :EE7 ;EEF               L,szzw..            ,.,            ``
          \E5.,E5F  EE1.              /; ``*4EEEZhw._      EEEL
            ````     ``              JEEE.     `"45EEEhw.,,,]

From 2010 till ∞
typecode-js v 0.1
*/

// Dependencies:
// NI.field

define(['jquery', 'NIseed', 'NIfield'], function($) {
	
	var window = this,
	NI = window.NI,
	field = NI.field;
	
	field.synthesize({
		name: "Dropdown",
		
		props: {
			CONTAINER_CLASS: "tc-dropdown",
			DISPLAY_CLASS: "display",
			TRIGGER_ARROW_CLASS: "trigarr",
			MENU_CLASS: "menu",
			ITEM_CLASS: "item",

			morph_native_select: function($f) {
				var $e, buf;

				if(!$f[0]){
					return $f;
				}

				if ($f[0].nodeName.toLowerCase() != "select") {
					return $f;
				}

				$e = $("<div></div>")
						.addClass(field.co.BASE_CLASS+ " " +field.Dropdown.CONTAINER_CLASS)
						.html("<div class='"+ field.Dropdown.DISPLAY_CLASS +"'></div>"+
							"<div class='"+ field.Dropdown.MENU_CLASS +"'></div>");
				
				buf = "";
				$f.find("option").each(function() {
					var $option = $(this);
					buf += "<div data-value='"+ $option.attr("value") +"' class='"+ field.Dropdown.ITEM_CLASS +"'>"+
						field.get_display_text($option) +"</div>";
				});
				$e.find("."+ field.Dropdown.MENU_CLASS).html(buf);

				if (!NI.ex.isHeadless($f)) {
					$f.replaceWith($e);
				}

				return $e;
			}
			
		},
		
		proto: {
			
			options: {
				item_generator: null,
				item_handlers: {
					click: function(e) {
						e.data.me.set_val(e.data.$item.attr(field.co.VALUE)).close();
					}
				},
				
				empty_val: "-1",
				empty_text: "&nbsp;",
				
				circular: true,
				filtering: true,
				rolling: false,
				
				on_open: function(instance) {},
				on_close: function(instance) {},
				on_keydown: function(e) {},
				
				fx: {
					open: function($menu, callback) {
						$menu.slideDown(100, function() {
							if ($.isFunction(callback)) {
								callback();
							}
						});
					},
					close: function($menu) {
						$menu.slideUp(100);
					}
				}
			},
			
			_init: function() {
				
				this.events.click = function(e) {
					var me = e.data.me;
					
					if (me.$e[0] === e.target || $.contains(me.$e[0], e.target)) {
						// clicked somewhere on the dropdown
						
						if (me.elements.display[0] === e.target || 
						    $.contains(me.elements.display[0], e.target) || 
						    (me.elements.trigarr && me.elements.trigarr[0] === e.target)) {
							
							// clicked somewhere on the dropdown display area
							me[me.internal.open ? "close" : "open"]();
						}
						
					} else {
						// clicked somewhere off the dropdown
						if (me.internal.open) {
							me.close();
						}
					}
				};
				
				this.events.keydown = function(e) {
					var me, prevent_default, key;
					
					me = e.data.me;
					if ($.isFunction(me.internal.o.on_keydown)) {
						if (me.internal.o.on_keydown(e) === false) {
							return;
						}
					}
					prevent_default = true;
					key = e.keyCode || e.which;
					
					if (key >= 48 && key <= 90) {
						if (me.internal.o.filtering) {
							me.internal.filter_str += String.fromCharCode(key);
							me.filter(me.internal.filter_str);
						}
						
					} else if (key == NI.co.keyboard.BACKSPACE ||
						       key == NI.co.keyboard.DELETE) {
						if (me.internal.filter_str.length) {
							me.clear_filter();
						}
						
					} else {
						switch (key) {
							case NI.co.keyboard.LEFT:
							case NI.co.keyboard.UP:
								me.move_selection(NI.co.directions.UP);
								break;
						
							case NI.co.keyboard.RIGHT:
							case NI.co.keyboard.DOWN:
								me.move_selection(NI.co.directions.DOWN);
								break;
							
							case NI.co.keyboard.ENTER:
							case NI.co.keyboard.ESCAPE:
								me.close();
								break;
						
							default:
								prevent_default = false;
								break;
						}
					}
					if (prevent_default) {
						e.preventDefault();
					}
				};
				
				$(window.document).bind("click."+ this._name, {me: this}, this.events.click);
				
				this.internal.filter_str = "";
				this._update_display(this.get_active_item());
				this.close();
			},
			
			generate_from_dom: function(options) {
				var me, $items;
				me = this;
				field._Base.prototype.generate_from_dom.apply(this, arguments);
				this.$e.addClass(field.Dropdown.CONTAINER_CLASS);
				this._init_elements();
				$items = this.get_items(true);
				if (typeof(this.internal.o.item_handlers) === "object") {
					$.each(this.internal.o.item_handlers, function(type, fn) {
						$items.each(function() {
							var $item, empty_val;
							$item = $(this);
							if ($item.hasClass(field.co.EMPTY_CLASS)) {
								empty_val = $item.attr(field.co.VALUE);
								if (empty_val) {
									me.internal.o.empty_val = empty_val;
								}
								me.internal.o.empty_text = field.get_display_text($item);
								$item.remove();
								return true;
							}
							$item.bind(type, {me: me, $item: $item}, fn);
						});<commit_msg>drop down automatically processes a native select<commit_after>/* ============================================================================

 >4SESz.,     _,     ,gSEE2zx.,_        .azx                    ,w.
@Sh. "QEE3.  JEE3.  &ss  `*4EEEEE2x.,_  "EEV  ,aS5^;dEEEE2x.,   VEEF  _
\E2`_,gF4EEEx.?jF   EE5L       `*4EEEEE2zpn..ZEEF ;sz.  `*EEESzw.w* '|EE.
  ,dEEF   `"?j]  _,_   ,_  _,     _,.     L.EEEF  !EEF  _,,  `"``    EE7   ,,_
 :EEE7 ,ws,`|EEL`JEEL`JEE)`JEEL zE5` E3. / [EE3  ,w.  zE2` Ek .zE5^JZE3.,6EF [3
 {EEE. VEE7.EE2  AE3. EEV  ZEEL{EEL ws. ;  [EE1  EEEt{E3. JEELEE3. JE5LJEEF ,w,
  \EEk,,>^ JEEL,@EEF ZE5L,ZE5^ "Q3. V2`.    \EEk,,J' "Q[ yE2^ VE[_zEE[,"QEL V5F
          ,ss  :EE7 ;EEF               L,szzw..            ,.,            ``
          \E5.,E5F  EE1.              /; ``*4EEEZhw._      EEEL
            ````     ``              JEEE.     `"45EEEhw.,,,]

From 2010 till ∞
typecode-js v 0.1
*/

// Dependencies:
// NI.field

define(['jquery', 'NIseed', 'NIfield'], function($) {
	
	var window = this,
	NI = window.NI,
	field = NI.field;
	
	field.synthesize({
		name: "Dropdown",
		
		props: {
			CONTAINER_CLASS: "tc-dropdown",
			DISPLAY_CLASS: "display",
			TRIGGER_ARROW_CLASS: "trigarr",
			MENU_CLASS: "menu",
			ITEM_CLASS: "item",

			morph_native_select: function($f) {
				var $e, buf;

				if(!$f[0]){
					return $f;
				}

				if ($f[0].nodeName.toLowerCase() != "select") {
					return $f;
				}

				$e = $("<div></div>")
						.addClass(field.co.BASE_CLASS+ " " +field.Dropdown.CONTAINER_CLASS)
						.html("<div class='"+ field.Dropdown.DISPLAY_CLASS +"'></div>"+
							"<div class='"+ field.Dropdown.MENU_CLASS +"'></div>");
				
				buf = "";
				$f.find("option").each(function() {
					var $option = $(this);
					buf += "<div data-value='"+ $option.attr("value") +"' class='"+ field.Dropdown.ITEM_CLASS +"'>"+
						field.get_display_text($option) +"</div>";
				});
				$e.find("."+ field.Dropdown.MENU_CLASS).html(buf);

				if (!NI.ex.isHeadless($f)) {
					$f.replaceWith($e);
				}

				return $e;
			}
			
		},
		
		proto: {
			
			options: {
				item_generator: null,
				item_handlers: {
					click: function(e) {
						e.data.me.set_val(e.data.$item.attr(field.co.VALUE)).close();
					}
				},
				
				empty_val: "-1",
				empty_text: "&nbsp;",
				
				circular: true,
				filtering: true,
				rolling: false,
				
				on_open: function(instance) {},
				on_close: function(instance) {},
				on_keydown: function(e) {},
				
				fx: {
					open: function($menu, callback) {
						$menu.slideDown(100, function() {
							if ($.isFunction(callback)) {
								callback();
							}
						});
					},
					close: function($menu) {
						$menu.slideUp(100);
					}
				}
			},
			
			_init: function() {
				
				this.events.click = function(e) {
					var me = e.data.me;
					
					if (me.$e[0] === e.target || $.contains(me.$e[0], e.target)) {
						// clicked somewhere on the dropdown
						
						if (me.elements.display[0] === e.target || 
						    $.contains(me.elements.display[0], e.target) || 
						    (me.elements.trigarr && me.elements.trigarr[0] === e.target)) {
							
							// clicked somewhere on the dropdown display area
							me[me.internal.open ? "close" : "open"]();
						}
						
					} else {
						// clicked somewhere off the dropdown
						if (me.internal.open) {
							me.close();
						}
					}
				};
				
				this.events.keydown = function(e) {
					var me, prevent_default, key;
					
					me = e.data.me;
					if ($.isFunction(me.internal.o.on_keydown)) {
						if (me.internal.o.on_keydown(e) === false) {
							return;
						}
					}
					prevent_default = true;
					key = e.keyCode || e.which;
					
					if (key >= 48 && key <= 90) {
						if (me.internal.o.filtering) {
							me.internal.filter_str += String.fromCharCode(key);
							me.filter(me.internal.filter_str);
						}
						
					} else if (key == NI.co.keyboard.BACKSPACE ||
						       key == NI.co.keyboard.DELETE) {
						if (me.internal.filter_str.length) {
							me.clear_filter();
						}
						
					} else {
						switch (key) {
							case NI.co.keyboard.LEFT:
							case NI.co.keyboard.UP:
								me.move_selection(NI.co.directions.UP);
								break;
						
							case NI.co.keyboard.RIGHT:
							case NI.co.keyboard.DOWN:
								me.move_selection(NI.co.directions.DOWN);
								break;
							
							case NI.co.keyboard.ENTER:
							case NI.co.keyboard.ESCAPE:
								me.close();
								break;
						
							default:
								prevent_default = false;
								break;
						}
					}
					if (prevent_default) {
						e.preventDefault();
					}
				};
				
				$(window.document).bind("click."+ this._name, {me: this}, this.events.click);
				
				this.internal.filter_str = "";
				this._update_display(this.get_active_item());
				this.close();
			},
			
			generate_from_dom: function(options) {
				var me, $items;
				me = this;

				if (options.$e[0].nodeName.toLowerCase() == "select") {
					this.$e = field.Dropdown.morph_native_select(options.$e);
				} else {
					this.$e = options.$e;
				}

				this.$e.addClass(field.co.BASE_CLASS);
				this.$e.addClass(field.Dropdown.CONTAINER_CLASS);

				this._init_elements();
				$items = this.get_items(true);
				if (typeof(this.internal.o.item_handlers) === "object") {
					$.each(this.internal.o.item_handlers, function(type, fn) {
						$items.each(function() {
							var $item, empty_val;
							$item = $(this);
							if ($item.hasClass(field.co.EMPTY_CLASS)) {
								empty_val = $item.attr(field.co.VALUE);
								if (empty_val) {
									me.internal.o.empty_val = empty_val;
								}
								me.internal.o.empty_text = field.get_display_text($item);
								$item.remove();
								return true;
							}
							$item.bind(type, {me: me, $item: $item}, fn);
						});
{'fraction_non_alphanumeric': 0.1881818975298944, 'fraction_numerical': 0.01178318931657502, 'mean_word_length': 1.9893034176884947, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, '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': '6472461', 'n_tokens_mistral': 5871, 'n_tokens_neox': 4184, 'n_words': 697}