📄 cmssearchwidgetdialog.java
字号:
super.defineWidgets();
// first block "Query for search index..."
addWidget(new CmsWidgetDialogParameter(m_searchParams, "index", "", PAGES[0], new CmsDisplayWidget(), 1, 1));
addWidget(new CmsWidgetDialogParameter(m_searchParams, "query", "", PAGES[0], new CmsInputWidget(), 1, 1));
addWidget(new CmsWidgetDialogParameter(m_searchParams, "sortName", "", PAGES[0], new CmsSelectWidget(
getSortWidgetConfiguration()), 0, 1));
addWidget(new CmsWidgetDialogParameter(
m_searchParams.getRoots(),
"roots",
"/",
PAGES[0],
new CmsVfsFileWidget(),
1,
10));
addWidget(new CmsWidgetDialogParameter(
m_searchParams.getCategories(),
"categories",
"",
PAGES[0],
new CmsInputWidget(),
0,
6));
addWidget(new CmsWidgetDialogParameter(m_searchParams, "calculateCategories", new CmsCheckboxWidget()));
// 2nd block "fields to search in"
addWidget(new CmsWidgetDialogParameter(m_searchParams, "searchFieldContent", new CmsCheckboxWidget()));
addWidget(new CmsWidgetDialogParameter(m_searchParams, "searchFieldMeta", new CmsCheckboxWidget()));
addWidget(new CmsWidgetDialogParameter(m_searchParams, "searchFieldTitle", new CmsCheckboxWidget()));
addWidget(new CmsWidgetDialogParameter(m_searchParams, "searchFieldKeywords", new CmsCheckboxWidget()));
addWidget(new CmsWidgetDialogParameter(m_searchParams, "searchFieldDescription", new CmsCheckboxWidget()));
}
/**
* Overridden to additionally get a hold on the widget object of type
* <code>{@link CmsSearchParameters}</code>.<p>
*
* @see org.opencms.workplace.tools.searchindex.A_CmsEditSearchIndexDialog#initUserObject()
*/
protected void initUserObject() {
super.initUserObject();
Object o = getDialogObject();
if (o == null) {
m_searchParams = new CmsSearchParameters();
// implant a hook upon modifications of the list
// this will set the search page to 1 if a restriction to the set of categories is performed
m_searchParams.setCategories(new CmsHookListSearchCategory(m_searchParams, m_searchParams.getCategories()));
m_search = new CmsSearch();
} else {
Map dialogObject = (Map)o;
m_searchParams = (CmsSearchParameters)dialogObject.get(PARAM_SEARCH_PARAMS);
if (m_searchParams == null) {
m_searchParams = new CmsSearchParameters();
}
m_search = (CmsSearch)dialogObject.get(PARAM_SEARCH_OBJECT);
if (m_search == null) {
m_search = new CmsSearch();
}
}
m_searchParams.setSearchIndex(m_index);
}
/**
* Additionally saves <code>{@link #PARAM_SEARCH_PARAMS}</code> to the dialog object map.<p>
*
* @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
*/
protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
super.initWorkplaceRequestValues(settings, request);
Map dialogMap = (Map)getDialogObject();
if (dialogMap != null) {
dialogMap.put(PARAM_SEARCH_PARAMS, m_searchParams);
dialogMap.put(PARAM_SEARCH_OBJECT, m_search);
}
}
/**
* Returns the hmtl for the category search results.<p>
*
* Note that a valid search (<code>{@link CmsSearch#getSearchResult()}</code> with
* correct settings and inited) has to be triggered before this call or an empty
* String will be returned. <p>
*
* @param search the preconfigured search bean
*
* @return the hmtl for the category search results
*/
private String createCategorySearchResultHtml() {
StringBuffer result = new StringBuffer();
if (m_searchParams.isCalculateCategories()) {
// trigger calculation of categories, even if we don't need search results
// this is cached unless more set operation on CmsSearch are performed
Map categoryMap = m_search.getSearchResultCategories();
if (categoryMap != null) {
result.append(dialogContentStart(null));
result.append(result.append(createWidgetTableStart()));
// first block "Query for index...."
result.append(dialogBlockStart(key(
Messages.GUI_LABEL_SEARCHINDEX_BLOCK_SEARCH_CATEGORIES_1,
new Object[] {m_searchParams.getQuery()})));
result.append(createWidgetTableStart());
// categories:
result.append("\n<p>\n");
Map.Entry entry;
Iterator it = categoryMap.entrySet().iterator();
while (it.hasNext()) {
entry = (Map.Entry)it.next();
result.append(" ").append("<a class=\"searchcategory\" href=\"#\" onClick=\"filterCategory('");
result.append(entry.getKey()).append("')\")>");
result.append(entry.getKey());
result.append("</a> : ");
result.append(entry.getValue()).append("<br>\n");
}
result.append("</p>\n");
result.append(createWidgetTableEnd());
result.append(dialogBlockEnd());
result.append(createWidgetTableEnd());
result.append(dialogContentEnd());
}
}
return result.toString();
}
private String createSearchResults() {
String query = m_searchParams.getQuery();
StringBuffer result = new StringBuffer();
if (!CmsStringUtil.isEmptyOrWhitespaceOnly(query) && query.length() > 3) {
CmsSearchResultView resultView = new CmsSearchResultView(getJsp());
// proprietary workplace admin link for pagelinks of search:
resultView.setSearchRessourceUrl(getJsp().link(
"/system/workplace/views/admin/admin-main.jsp?path=/searchindex/singleindex/search&indexname="
+ m_index.getName()));
m_search.init(getCms());
// custom parameters (non-widget controlled)
// these are from generated search page links
String page = getJsp().getRequest().getParameter("searchPage");
if (!CmsStringUtil.isEmptyOrWhitespaceOnly(page)) {
m_searchParams.setSearchPage(Integer.parseInt(page));
}
String categories = getJsp().getRequest().getParameter("searchCategories");
if (!CmsStringUtil.isEmptyOrWhitespaceOnly(categories)) {
m_searchParams.setCategories(CmsStringUtil.splitAsList(categories, ','));
}
String searchRoots = getJsp().getRequest().getParameter("searchRoots");
if (!CmsStringUtil.isEmptyOrWhitespaceOnly(searchRoots)) {
m_searchParams.setSearchRoots(searchRoots);
}
m_search.setParameters(m_searchParams);
// set to root site to allow searching every index in the administration view
// regardless of the site selection. This behaviour is not wanted online!
CmsRequestContext context = getJsp().getRequestContext();
String siteRoot = context.getSiteRoot();
context.setSiteRoot("");
result.append("<div style=\"padding:12px;\">\n").append(resultView.displaySearchResult(m_search)).append(
"\n</div>\n");
context.setSiteRoot(siteRoot);
} else {
// Just don't perform search
}
return result.toString();
}
private String filterCategoryJS() {
StringBuffer result = new StringBuffer();
// fool the widget with a hidden categories input field
result.append("<input name=\"dummysearchcategory\" id=\"dummysearchcategory\" type=\"hidden\" value=\"\">\n");
result.append("<input name=\"dummysearchpage\" id=\"dummysearchpage\" type=\"hidden\" value=\"\">\n");
// delete all other chosen "cateogries.x" values and put a value here
result.append("<script type=\"text/javascript\">\n");
result.append(" function filterCategory(category) {\n");
result.append(" var searchform = document.forms['EDITOR'];\n");
result.append(" var inputFields = searchform.elements;\n");
result.append(" for (var i=0; i<inputFields.length; i++) {\n");
result.append(" if(inputFields[i].name.indexOf('categories') != -1) {\n");
result.append(" inputFields[i].value='';\n");
result.append(" inputFields[i].name='invalidsearchcategory';\n");
result.append(" }\n");
result.append(" }\n");
// if we found a category field use it, if not, set the name of our
// fooling field to "categories.0" or the widget filling will fail
result.append(" var categoryField = inputFields['dummysearchcategory'];\n");
result.append(" categoryField.name = 'categories.0';\n");
result.append(" categoryField.value = category;\n");
// additionally set searchpage to zero because filtered results may / will be smaller
result.append(" inputFields['dummysearchpage'].value = '0';\n");
result.append(" inputFields['dummysearchpage'].name = 'searchpage.0';\n");
result.append(" validateQuery();\n");
result.append(" searchform.submit();\n");
result.append(" }\n");
result.append("</script>\n");
return result.toString();
}
/**
* Returns the different select options for sort search result criteria. <p>
*
* @return the different select options for sort search result criteria
*/
private List getSortWidgetConfiguration() {
List result = new LinkedList();
CmsMessages messages = Messages.get().getBundle(getLocale());
result.add(new CmsSelectWidgetOption(
CmsSearchParameters.SORT_NAMES[0],
true,
messages.key(Messages.GUI_SELECT_LABEL_SEARCH_SORT_SCORE_0)));
result.add(new CmsSelectWidgetOption(
CmsSearchParameters.SORT_NAMES[1],
false,
messages.key(Messages.GUI_SELECT_LABEL_SEARCH_SORT_DATE_CREATED_0)));
result.add(new CmsSelectWidgetOption(
CmsSearchParameters.SORT_NAMES[2],
false,
messages.key(Messages.GUI_SELECT_LABEL_SEARCH_SORT_DATE_LAST_MODIFIED_0)));
result.add(new CmsSelectWidgetOption(
CmsSearchParameters.SORT_NAMES[3],
false,
messages.key(Messages.GUI_SELECT_LABEL_SEARCH_SORT_TITLE_0)));
return result;
}
private String normalizePreviousQueryJS() {
StringBuffer result = new StringBuffer();
result.append("<script type=\"text/javascript\">\n");
result.append(" function normalizeQueryValue() {\n");
result.append(" var searchform = document.forms['EDITOR'];\n");
result.append(" var query = searchform.elements['query.0'].value;\n");
result.append(" query = decodeURI(query);\n");
result.append(" searchform.elements['query.0'].value = query;\n");
result.append(" return true;\n");
result.append(" }\n");
result.append(" normalizeQueryValue();\n");
result.append("</script>\n");
return result.toString();
}
private String submitJS() {
StringBuffer result = new StringBuffer();
result.append(" function validateQuery() {\n");
result.append(" var searchform = document.forms['EDITOR'];\n");
result.append(" var query = searchform.elements['query.0'].value;\n");
result.append(" searchform.elements['query.0'].value = query;\n");
result.append(" return true;\n");
result.append(" }\n");
result.append(" window.captureEvents(Event.SUBMIT);\n");
result.append(" window.onsubmit = validateQuery();\n");
return result.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -