<?xml version="1.0" encoding="UTF-8"?>
<unload unload_date="2024-08-16 12:19:48">
<sp_widget action="INSERT_OR_UPDATE">
<category/>
<client_script><![CDATA[function ($filter, $location, glideUserSession) {

	var c = this;
	c.options.glyph = c.options.glyph || 'search';
	c.options.title = c.options.title || c.data.searchMsg;

	c.onSelect = function ($item, $model, $label) {
		$label = '';
		$item.label = ''; // This blanks the search box when a user clicks on something
		$(this).val('');
		if ($item.type == "sc")
			$location.search({ id: 'sc_cat_item', sys_id: $item.sys_id });
		if ($item.type == "sc_content") {
			if ($item.content_type == "kb")
				$location.search({ id: 'kb_article', sys_id: $item.kb_article });
			else if ($item.content_type == "external")
				window.open($item.url, '_blank');
			else
				$location.search({ id: 'sc_cat_item', sys_id: $item.sys_id });
		}
		if ($item.type == "sc_guide")
			$location.search({ id: 'sc_cat_item_guide', sys_id: $item.sys_id });
		if ($item.type == "kb")
			$location.search({ id: 'uob_kb_article', sys_id: $item.sys_id });
		if ($item.type == "qa")
			$location.search({ id: 'kb_social_qa_question', question_id: $item.sys_id });
		if ($item.type == "rec")
			$location.search({ id: $item.page, sys_id: $item.sys_id, table: $item.table });
	};

	c.valchanged = function (ele) {
		console.log(ele.val());
	}

	c.getResults = function (query) {
		return c.server.get({ q: query }).then(function (response) {
			var a = $filter('orderBy')(response.data.results, '-score');
			a = $filter('limitTo')(a, c.data.limit + 3);

			var helpItems = [{ "name": "<span class='typeahead-help-group-contents'><a href='/itportal?id=uob_report_a_fault_p1'>Contact us to Report a Fault</a></span>" }];

			if (helpItems.length > 0) {
				for (var o in helpItems) {
					var item = {};
					item.type = "";
					item.grpname = "Can't find what you need? If you're not logged in you won't see all the search results.";
					item.label = helpItems[o].name;
					item.score = 33;
					a.push(item);
				}
			}

			var results = _(a)
				.groupBy('grpname')
				.map(function (g) {
					g[0].firstInGroup = true;  // the first item in each group
					return g;
				})
				.flatten()
				.value();
			//		console.log(results);
			return results;
		});
	}
}]]></client_script>
<controller_as>c</controller_as>
<css>ul.dropdown-menu {
  min-width: 100%;
  border-radius: 0px 0px 4px 4px;
  margin: 0px;
  padding-bottom: 10px;

  &gt;li&gt;a {
    white-space: normal;
    padding: 5px 30px;
    font-size: medium;
  }
}

ul.dropdown-menu a.ta-item {
  line-height: 20px;
}

ul.dropdown-menu i.ta-icon,
i.ta-img {
  width: 20px;
  height: 20px;
  background-size: contain;
  display: inline-block;
  background-repeat: no-repeat;
  background-position: center center;
  text-align: center;
  line-height: 20px;
  vertical-align: bottom;
  margin-right: 8px;
}

input[name="q"] {
  color: black;
}


.aisearch {
  --classicsponlydonotuse--rem-multipy: 1.4;
}

button[name="search"]:focus {
  outline: 2px solid $input-border-focus !important;
  outline-offset: -4px !important;
}

input[name="q"] {
  color: black;
}

.typeahead-group-header {
  padding: 8px;
  background-color: #f9f9fd;
  font-weight: bold;
  font-size: medium;
}

.typeahead-help-group {
  padding: 8px 10px;
  background-color: #efeff5;
  font-weight: bold;
  font-size: medium;
}

.typeahead-help-group-contents {
  padding: 5px 30px;
  font-size: medium;
}

.gethelplinks {
  float: left;
}

.tagline-color {
  color: #FFFFFF;
}</css>
<data_table>sp_instance</data_table>
<demo_data/>
<description/>
<docs display_value=""/>
<field_list>color,size,glyph,title,short_description</field_list>
<has_preview>false</has_preview>
<id>grouped-homepage-search</id>
<internal>false</internal>
<link/>
<name>Grouped Homepage Search</name>
<option_schema>[{"name":"title","label":"Title","type":"string"},{"name":"color","label":"Bootstrap Color","type":"string"},{"name":"glyph","label":"Glyph","type":"string"},{"name":"size","label":"Bootstrap Search Box Size","type":"string"},{"hint":"Number of typeahead search results returned (default 15)","name":"limit","label":"Search Item Limit","type":"integer"}]</option_schema>
<public>true</public>
<roles/>
<script><![CDATA[(function () {
	data.searchType = $sp.getParameter("t");
	data.results = [];
	data.searchMsg = gs.getMessage("Search");
	data.limit = options.limit || 15;
	var textQuery = '123TEXTQUERY321';

	if (!input)
		return;

	data.q = input.q;
	getKnowledge();
	if (gs.isLoggedIn())
		getCatalogItems();
	data.sqandaEnabled = gs.getProperty('glide.sp.socialqa.enabled', false) === 'true';
	if (data.sqandaEnabled)
		getQuestions();

	// add in additional search tables from sp_search_groups
	var portalGR = $sp.getPortalRecord();
	var portalID = portalGR.getDisplayValue('sys_id');
	var sg = GlideRecord('sp_search_group');
	sg.addQuery('sp_portal', portalID);
	sg.addQuery('active', true);
	sg.orderBy('order');
	sg.query();
	while (sg.next())
		addSearchTable(sg);

	// typeahead search generates multiple "Your text query contained only
	// common words..." msgs, we don't want them
	gs.flushMessages();

	function addSearchTable(sg) {
		var table = sg.getValue('name');
		var condition = sg.getValue('condition');
		var label = sg.getValue('label');
		var gr = GlideRecord(table);
		if (condition)
			gr.addEncodedQuery(condition);
		//		gr.addQuery(textQuery, data.q);
		gr.addQuery('number', 'CONTAINS', data.q);
		gr.query();
		var searchTableCount = 0;
		while (gr.next() && searchTableCount < data.limit) {
			var rec = {};
			rec.type = "rec";
			rec.grpname = label || "Additional Resources";
			rec.table = table;
			rec.sys_id = gr.getDisplayValue('sys_id');
			rec.page = sg.getDisplayValue('sp_page');
			if (!rec.page)
				rec.page = "form";
			rec.label = gr.getDisplayValue();
			if (gr.isValidField('short_description') && gr.getDisplayValue('short_description') != '')
				rec.label += (' - ' + gr.getDisplayValue('short_description'));
			rec.score = parseInt(gr.ir_query_score.getDisplayValue());
			data.results.push(rec);
			searchTableCount++;
		}
	}

	function getKnowledge() {

		var kbs = new global.GlideQuery('m2m_sp_portal_knowledge_base')
			.where('sp_portal', $sp.getPortalRecord().getUniqueValue()).orderBy('order')
			.select('kb_knowledge_base').toArray(100)
			.map(function (elm) { return elm.kb_knowledge_base; });

		var kb = new GlideRecord('kb_knowledge');
		kb.addQuery('workflow_state', 'published');
		kb.addQuery('valid_to', '>=', (new GlideDate()).getLocalDate().getValue());
		kb.addQuery(textQuery, data.q);

		if (kbs != null && kbs.length > 0) {
			gqkb = kb.addQuery('kb_knowledge_base', 'CONTAINS', kbs[0]);
			kbs.slice(1).forEach(function (sys_id) {
				gqkb.addOrCondition('kb_knowledge_base', 'CONTAINS', sys_id);
			});
		}
		kb.query();
		var kbCount = 0;
		while (kb.next() && kbCount < data.limit) {
			if (!$sp.canReadRecord(kb))
				continue;

			var article = {};
			article.type = "kb";
			article.grpname = "Knowledge Base Articles";
			$sp.getRecordDisplayValues(article, kb, 'sys_id,number,short_description,published,text');
			if (!article.text)
				article.text = "";
			article.text = $sp.stripHTML(article.text);
			article.text = article.text.substring(0, 200);
			article.score = parseInt(kb.ir_query_score.getDisplayValue());
			article.label = article.short_description;
			data.results.push(article);
			kbCount++;
		}
	}

	function getCatalogItems() {
		var catalogs = new global.GlideQuery('m2m_sp_portal_catalog')
			.where('sp_portal', $sp.getPortalRecord().getUniqueValue()).orderBy('order')
			.select('sc_catalog').toArray(100)
			.map(function (elm) { return elm.sc_catalog; });

		var sc = new GlideRecord('sc_cat_item');
		sc.addQuery(textQuery, data.q + "*");
		sc.addQuery('active', true);
		sc.addQuery('no_search', '!=', true);
		sc.addQuery('visible_standalone', true);
		sc.addQuery('sys_class_name', 'NOT IN', 'sc_cat_item_wizard');
		sc.addNotNullQuery('category');
		if (catalogs != null && catalogs.length > 0) {
			gq = sc.addQuery('sc_catalogs', 'CONTAINS', catalogs[0]);
			catalogs.slice(1).forEach(function (sys_id) {
				gq.addOrCondition('sc_catalogs', 'CONTAINS', sys_id);
			});
		}
		sc.query();
		var catCount = 0;
		while (sc.next() && catCount < data.limit) {
			if (!$sp.canReadRecord(sc))
				continue;

			var item = {};
			if (sc.getRecordClassName() == "sc_cat_item_guide") {
				item.type = "sc_guide";
				item.grpname = "Order Guides";
			} else if (sc.getRecordClassName() == "sc_cat_item_content") {
				var gr = new GlideRecord('sc_cat_item_content');
				gr.get(sc.getUniqueValue());
				$sp.getRecordValues(item, gr, 'url,content_type,kb_article');
				item.type = "sc_content";
				// item.grpname = "Content Items";
				// MW - Changes this so content items show with the rest of the SC items
				item.grpname = "Service Catalog Items";
			}
			else {
				item.type = "sc";
				item.grpname = "Service Catalog Items";
			}
			$sp.getRecordDisplayValues(item, sc, 'name,short_description,picture,price,sys_id');
			item.score = parseInt(sc.ir_query_score.getDisplayValue());
			item.label = item.name;
			data.results.push(item);
			catCount++;
		}
	}

	function getQuestions() {
		var questionGR = new GlideRecord("kb_social_qa_question");
		questionGR.addActiveQuery();
		questionGR.addQuery(textQuery, data.q);
		questionGR.query();
		var qCount = 0;
		while (questionGR.next() && qCount < data.limit) {
			if (!$sp.canReadRecord(questionGR))
				continue;

			var question = {};
			question.type = "qa";
			question.grpname = "Question-Answers";
			$sp.getRecordDisplayValues(question, questionGR, 'question,question_details,sys_created_on,sys_id');
			question.text = (question.question_details) ? $sp.stripHTML(question.question_details) : "";
			question.text = question.text.substring(0, 200);
			question.label = question.question;
			question.score = 0;
			data.results.push(question);
			qCount++;
		}
	}

})();]]></script>
<servicenow>false</servicenow>
<sys_class_name>sp_widget</sys_class_name>
<sys_created_by>mwarwick</sys_created_by>
<sys_created_on>2017-01-09 21:37:23</sys_created_on>
<sys_customer_update>false</sys_customer_update>
<sys_id>2e92161c4f54fe008147fc828110c712</sys_id>
<sys_mod_count>408</sys_mod_count>
<sys_name>Grouped Homepage Search</sys_name>
<sys_package display_value="Global" source="global">global</sys_package>
<sys_policy/>
<sys_replace_on_upgrade>false</sys_replace_on_upgrade>
<sys_scope display_value="Global">global</sys_scope>
<sys_update_name>sp_widget_2e92161c4f54fe008147fc828110c712</sys_update_name>
<sys_updated_by>mwarwick</sys_updated_by>
<sys_updated_on>2024-08-14 16:23:11</sys_updated_on>
<template><![CDATA[<div id="homepage-search" class="hidden-xs wrapper-xl">
  <div class="wrapper-xl">
    <h2 class="text-center text-4x m-b-lg tagline-color" ng-bind="options.title"></h2>
    <div ng-if="options.short_description" class="text-center h4 m-b-lg tagline-color"
      ng-bind="options.short_description"></div>
    <form method="get" action="?">
      <input type="hidden" name="id" value="search" />
      <!--   <input type="hidden" name="t" value="{{data.searchType}}"/> -->
      <div class="input-group input-group-{{::c.options.size}}">
        <!-- uses ui.bootstrap.typeahead -->
        <input name="q" id="typeahead" type="text" placeholder="Search" aria-label="Seach Box"
          ng-model="c.selectedState" ng-model-options="{debounce: 250}" autocomplete="off"
          uib-typeahead="item as item.label for item in c.getResults($viewValue)" typeahead-focus-first="false"
          typeahead-on-select="c.onSelect($item, $model, $label)" ng-change="c.valchanged(this)"
          typeahead-template-url="sp-typeahead-uob.html" class="form-control input-typeahead" role="textbox"
          tabindex="0">
        <span class="input-group-btn">
          <button name="search" type="submit" aria-label="Search" class="btn btn-{{::c.options.color}}">
            <i ng-if="::c.options.glyph" class="fa fa-{{::c.options.glyph}}"></i>
          </button>
        </span>
      </div>
    </form>
  </div>
</div>



<script type="text/ng-template" id="sp-typeahead-uob.html">
  <div class="typeahead-group-header" ng-if="match.model.firstInGroup">    
    &nbsp;
    <i class="fa fa-book" aria-hidden="true" ng-if="match.model.grpname == 'Knowledge Base Articles'"></i>
    <i class="fa fa-shopping-cart" aria-hidden="true" ng-if="match.model.grpname == 'Service Catalog Items'"></i>
    <i class="fa fa-external-link" aria-hidden="true" ng-if="match.model.grpname == 'Content Items'"></i>
    <i class="fa fa-question-circle" aria-hidden="true" ng-if="match.model.grpname.toLowerCase().indexOf('find what you need?')>=0"></i>
    &nbsp;{{match.model.grpname}}
  </div>
  
  <a ng-if="match.model.grpname.toLowerCase().indexOf('find what you need?')<0">
    <span ng-bind-html="match.label | uibTypeaheadHighlight:query" aria-label="{{match.label}}"></span>
  </a>
  
  <div ng-if="match.model.grpname.toLowerCase().indexOf('find what you need?')>=0">
    <div ng-bind-html="match.label"></div>
  </div>
</script>

<script type="text/x-custom-template" id="gethelplinks">
  <p>TEST</p>
</script>]]></template>
</sp_widget>
</unload>
