diff --git a/build-min/visualsearch.js b/build-min/visualsearch.js index 33bd88c..8a95f17 100644 --- a/build-min/visualsearch.js +++ b/build-min/visualsearch.js @@ -1,11 +1,11 @@ (function(){var $=jQuery;if(!window.VS)window.VS={};if(!VS.app)VS.app={};if(!VS.ui)VS.ui={};if(!VS.model)VS.model={};if(!VS.utils)VS.utils={};VS.VERSION='0.5.0';VS.VisualSearch=function(options){var defaults={container:'',query:'',autosearch:true,unquotable:[],remainder:'text',showFacets:true,readOnly:false,callbacks:{search:$.noop,focus:$.noop,blur:$.noop,facetMatches:$.noop,valueMatches:$.noop,clearSearch:$.noop,removedFacet:$.noop}};this.options=_.extend({},defaults,options);this.options.callbacks=_.extend({},defaults.callbacks,options.callbacks);VS.app.hotkeys.initialize();this.searchQuery=new VS.model.SearchQuery();this.searchBox=new VS.ui.SearchBox({app:this,showFacets:this.options.showFacets});if(options.container){var searchBox=this.searchBox.render().el;$(this.options.container).html(searchBox);} this.searchBox.value(this.options.query||'');$(window).bind('unload',function(e){});return this;};VS.init=function(options){return new VS.VisualSearch(options);};})();(function(){var $=jQuery;VS.ui.SearchBox=Backbone.View.extend({id:'search',events:{'click .VS-cancel-search-box':'clearSearch','mousedown .VS-search-box':'maybeFocusSearch','dblclick .VS-search-box':'highlightSearch','click .VS-search-box':'maybeTripleClick'},initialize:function(options){this.options=_.extend({},this.options,options);this.app=this.options.app;this.flags={allSelected:false};this.facetViews=[];this.inputViews=[];_.bindAll(this,'renderFacets','_maybeDisableFacets','disableFacets','deselectAllFacets','addedFacet','removedFacet','changedFacet');this.app.searchQuery.bind('reset',this.renderFacets).bind('add',this.addedFacet).bind('remove',this.removedFacet).bind('change',this.changedFacet);$(document).bind('keydown',this._maybeDisableFacets);},render:function(){$(this.el).append(JST['search_box']({readOnly:this.app.options.readOnly}));$(document.body).setMode('no','search');return this;},value:function(query){if(query==null)return this.serialize();return this.setQuery(query);},serialize:function(){var query=[];var inputViewsCount=this.inputViews.length;this.app.searchQuery.each(_.bind(function(facet,i){query.push(this.inputViews[i].value());query.push(facet.serialize());},this));if(inputViewsCount){query.push(this.inputViews[inputViewsCount-1].value());} -return _.compact(query).join(' ');},selected:function(){return _.select(this.facetViews,function(view){return view.modes.editing=='is'||view.modes.selected=='is';});},selectedModels:function(){return _.pluck(this.selected(),'model');},setQuery:function(query){this.currentQuery=query;VS.app.SearchParser.parse(this.app,query);},viewPosition:function(view){var views=view.type=='facet'?this.facetViews:this.inputViews;var position=_.indexOf(views,view);if(position==-1)position=0;return position;},searchEvent:function(e){var query=this.value();this.focusSearch(e);this.value(query);this.app.options.callbacks.search(query,this.app.searchQuery);},addFacet:function(category,initialQuery,position){category=VS.utils.inflector.trim(category);initialQuery=VS.utils.inflector.trim(initialQuery||'');if(!category)return;var model=new VS.model.SearchFacet({category:category,value:initialQuery||'',app:this.app});this.app.searchQuery.add(model,{at:position});},addedFacet:function(model){this.renderFacets();var facetView=_.detect(this.facetViews,function(view){if(view.model==model)return true;});_.defer(function(){facetView.enableEdit();});},changedFacet:function(){this.renderFacets();},removedFacet:function(facet,query,options){this.app.options.callbacks.removedFacet(facet,query,options);},renderFacets:function(){this.facetViews=[];this.inputViews=[];this.$('.VS-search-inner').empty();this.app.searchQuery.each(_.bind(this.renderFacet,this));this.renderSearchInput();this.renderPlaceholder();},renderFacet:function(facet,position){var view=new VS.ui.SearchFacet({app:this.app,model:facet,order:position});this.renderSearchInput();this.facetViews.push(view);this.$('.VS-search-inner').children().eq(position*2).after(view.render().el);view.calculateSize();_.defer(_.bind(view.calculateSize,view));return view;},renderSearchInput:function(){var input=new VS.ui.SearchInput({position:this.inputViews.length,app:this.app,showFacets:this.options.showFacets});this.$('.VS-search-inner').append(input.render().el);this.inputViews.push(input);},renderPlaceholder:function(){var $placeholder=this.$('.VS-placeholder');if(this.app.searchQuery.length){$placeholder.addClass("VS-hidden");}else{$placeholder.removeClass("VS-hidden").text(this.app.options.placeholder);}},clearSearch:function(e){if(this.app.options.readOnly)return;var actualClearSearch=_.bind(function(){this.disableFacets();this.value('');this.flags.allSelected=false;this.searchEvent(e);this.focusSearch(e);},this);if(this.app.options.callbacks.clearSearch!=$.noop){this.app.options.callbacks.clearSearch(actualClearSearch);}else{actualClearSearch();}},selectAllFacets:function(){this.flags.allSelected=true;$(document).one('click.selectAllFacets',this.deselectAllFacets);_.each(this.facetViews,function(facetView,i){facetView.selectFacet();});_.each(this.inputViews,function(inputView,i){inputView.selectText();});},allSelected:function(deselect){if(deselect)this.flags.allSelected=false;return this.flags.allSelected;},deselectAllFacets:function(e){this.disableFacets();if(this.$(e.target).is('.category,input')){var el=$(e.target).closest('.search_facet,.search_input');var view=_.detect(this.facetViews.concat(this.inputViews),function(v){return v.el==el[0];});if(view.type=='facet'){view.selectFacet();}else if(view.type=='input'){_.defer(function(){view.enableEdit(true);});}}},disableFacets:function(keepView){_.each(this.inputViews,function(view){if(view&&view!=keepView&&(view.modes.editing=='is'||view.modes.selected=='is')){view.disableEdit();}});_.each(this.facetViews,function(view){if(view&&view!=keepView&&(view.modes.editing=='is'||view.modes.selected=='is')){view.disableEdit();view.deselectFacet();}});this.flags.allSelected=false;this.removeFocus();$(document).unbind('click.selectAllFacets');},resizeFacets:function(view){_.each(this.facetViews,function(facetView,i){if(!view||facetView==view){facetView.resize();}});},_maybeDisableFacets:function(e){if(this.flags.allSelected&&VS.app.hotkeys.key(e)=='backspace'){e.preventDefault();this.clearSearch(e);return false;}else if(this.flags.allSelected&&VS.app.hotkeys.printable(e)){this.clearSearch(e);}},focusNextFacet:function(currentView,direction,options){options=options||{};var viewCount=this.facetViews.length;var viewPosition=options.viewPosition||this.viewPosition(currentView);if(!options.skipToFacet){if(currentView.type=='text'&&direction>0)direction-=1;if(currentView.type=='facet'&&direction<0)direction+=1;}else if(options.skipToFacet&¤tView.type=='text'&&viewCount==viewPosition&&direction>=0){return false;} +return _.compact(query).join(' ');},selected:function(){return _.select(this.facetViews,function(view){return view.modes.editing=='is'||view.modes.selected=='is';});},selectedModels:function(){return _.pluck(this.selected(),'model');},setQuery:function(query){this.currentQuery=query;VS.app.SearchParser.parse(this.app,query);},viewPosition:function(view){var views=view.type=='facet'?this.facetViews:this.inputViews;var position=_.indexOf(views,view);if(position==-1)position=0;return position;},searchEvent:function(e){var query=this.value();this.focusSearch(e);this.value(query);this.app.options.callbacks.search(query,this.app.searchQuery);},addFacet:function(category,initialQuery,position){category=VS.utils.inflector.trim(category);initialQuery=VS.utils.inflector.trim(initialQuery||'');if(!category)return;var model=new VS.model.SearchFacet({category:category,value:initialQuery||'',app:this.app});this.app.searchQuery.add(model,{at:position});},addedFacet:function(model){this.renderFacets();var facetView=_.detect(this.facetViews,function(view){if(view.model==model)return true;});_.defer(function(){facetView.enableEdit();});},changedFacet:function(){this.renderFacets();},removedFacet:function(facet,query,options){this.app.options.callbacks.removedFacet(facet,query,options);},renderFacets:function(){this.facetViews=[];this.inputViews=[];this.$('.VS-search-inner').empty();this.app.searchQuery.each(_.bind(this.renderFacet,this));this.renderSearchInput();this.renderPlaceholder();},renderFacet:function(facet,position){var view=new VS.ui.SearchFacet({app:this.app,model:facet,order:position});this.renderSearchInput();this.facetViews.push(view);this.$('.VS-search-inner').children().eq(position*2).after(view.render().el);view.calculateSize();_.defer(_.bind(view.calculateSize,view));return view;},renderSearchInput:function(){var input=new VS.ui.SearchInput({position:this.inputViews.length,app:this.app,showFacets:this.options.showFacets});this.$('.VS-search-inner').append(input.render().el);this.inputViews.push(input);},renderPlaceholder:function(){var $placeholder=this.$('.VS-placeholder');if(this.app.searchQuery.length||_.any(this.inputViews,function(view){return view.value()!="";})){$placeholder.addClass("VS-hidden");}else{$placeholder.removeClass("VS-hidden").text(this.app.options.placeholder);}},clearSearch:function(e){if(this.app.options.readOnly)return;var actualClearSearch=_.bind(function(){this.disableFacets();this.value('');this.flags.allSelected=false;this.searchEvent(e);this.focusSearch(e);},this);if(this.app.options.callbacks.clearSearch!=$.noop){this.app.options.callbacks.clearSearch(actualClearSearch);}else{actualClearSearch();}},selectAllFacets:function(){this.flags.allSelected=true;$(document).one('click.selectAllFacets',this.deselectAllFacets);_.each(this.facetViews,function(facetView,i){facetView.selectFacet();});_.each(this.inputViews,function(inputView,i){inputView.selectText();});},allSelected:function(deselect){if(deselect)this.flags.allSelected=false;return this.flags.allSelected;},deselectAllFacets:function(e){this.disableFacets();if(this.$(e.target).is('.category,input')){var el=$(e.target).closest('.search_facet,.search_input');var view=_.detect(this.facetViews.concat(this.inputViews),function(v){return v.el==el[0];});if(view.type=='facet'){view.selectFacet();}else if(view.type=='input'){_.defer(function(){view.enableEdit(true);});}}},disableFacets:function(keepView){_.each(this.inputViews,function(view){if(view&&view!=keepView&&(view.modes.editing=='is'||view.modes.selected=='is')){view.disableEdit();}});_.each(this.facetViews,function(view){if(view&&view!=keepView&&(view.modes.editing=='is'||view.modes.selected=='is')){view.disableEdit();view.deselectFacet();}});this.flags.allSelected=false;this.removeFocus();$(document).unbind('click.selectAllFacets');},resizeFacets:function(view){_.each(this.facetViews,function(facetView,i){if(!view||facetView==view){facetView.resize();}});},_maybeDisableFacets:function(e){if(this.flags.allSelected&&VS.app.hotkeys.key(e)=='backspace'){e.preventDefault();this.clearSearch(e);return false;}else if(this.flags.allSelected&&VS.app.hotkeys.printable(e)){this.clearSearch(e);}},focusNextFacet:function(currentView,direction,options){options=options||{};var viewCount=this.facetViews.length;var viewPosition=options.viewPosition||this.viewPosition(currentView);if(!options.skipToFacet){if(currentView.type=='text'&&direction>0)direction-=1;if(currentView.type=='facet'&&direction<0)direction+=1;}else if(options.skipToFacet&¤tView.type=='text'&&viewCount==viewPosition&&direction>=0){return false;} var view,next=Math.min(viewCount,viewPosition+direction);if(currentView.type=='text'){if(next>=0&&next=viewCount||next<0){view=_.last(this.inputViews);view.enableEdit();}else{view=this.facetViews[next];view.enableEdit();view.setCursorAtEnd(direction||options.startAtEnd);}}else{view=this.inputViews[next];view.enableEdit();}} if(options.selectText)view.selectText();this.resizeFacets();return true;},maybeFocusSearch:function(e){if(this.app.options.readOnly)return;if($(e.target).is('.VS-search-box')||$(e.target).is('.VS-search-inner')||e.type=='keydown'){this.focusSearch(e);}},focusSearch:function(e,selectText){if(this.app.options.readOnly)return;var view=this.inputViews[this.inputViews.length-1];view.enableEdit(selectText);if(!selectText)view.setCursorAtEnd(-1);if(e.type=='keydown'){view.keydown(e);view.box.trigger('keydown');} -_.defer(_.bind(function(){if(!this.$('input:focus').length){view.enableEdit(selectText);}},this));},highlightSearch:function(e){if(this.app.options.readOnly)return;if($(e.target).is('.VS-search-box')||$(e.target).is('.VS-search-inner')||e.type=='keydown'){var lastinput=this.inputViews[this.inputViews.length-1];lastinput.startTripleClickTimer();this.focusSearch(e,true);}},maybeTripleClick:function(e){var lastinput=this.inputViews[this.inputViews.length-1];return lastinput.maybeTripleClick(e);},addFocus:function(){if(this.app.options.readOnly)return;this.app.options.callbacks.focus();this.$('.VS-search-box').addClass('VS-focus');},removeFocus:function(){this.app.options.callbacks.blur();var focus=_.any(this.facetViews.concat(this.inputViews),function(view){return view.isFocused();});if(!focus)this.$('.VS-search-box').removeClass('VS-focus');},showFacetCategoryMenu:function(e){e.preventDefault();e.stopPropagation();if(this.facetCategoryMenu&&this.facetCategoryMenu.modes.open=='is'){return this.facetCategoryMenu.close();} +_.defer(_.bind(function(){if(!this.$('input:focus').length){view.enableEdit(selectText);}},this));},highlightSearch:function(e){if(this.app.options.readOnly)return;if($(e.target).is('.VS-search-box')||$(e.target).is('.VS-search-inner')||e.type=='keydown'){var lastinput=this.inputViews[this.inputViews.length-1];lastinput.startTripleClickTimer();this.focusSearch(e,true);}},maybeTripleClick:function(e){var lastinput=this.inputViews[this.inputViews.length-1];return lastinput.maybeTripleClick(e);},addFocus:function(){if(this.app.options.readOnly)return;this.app.options.callbacks.focus();this.$('.VS-search-box').addClass('VS-focus');},removeFocus:function(){this.app.options.callbacks.blur();var focus=_.any(this.facetViews.concat(this.inputViews),function(view){return view.isFocused();});if(!focus)this.$('.VS-search-box').removeClass('VS-focus');this.renderPlaceholder();},showFacetCategoryMenu:function(e){e.preventDefault();e.stopPropagation();if(this.facetCategoryMenu&&this.facetCategoryMenu.modes.open=='is'){return this.facetCategoryMenu.close();} var items=[{title:'Account',onClick:_.bind(this.addFacet,this,'account','')},{title:'Project',onClick:_.bind(this.addFacet,this,'project','')},{title:'Filter',onClick:_.bind(this.addFacet,this,'filter','')},{title:'Access',onClick:_.bind(this.addFacet,this,'access','')}];var menu=this.facetCategoryMenu||(this.facetCategoryMenu=new dc.ui.Menu({items:items,standalone:true}));this.$('.VS-icon-search').after(menu.render().open().content);return false;}});})();(function(){var $=jQuery;VS.ui.SearchFacet=Backbone.View.extend({type:'facet',className:'search_facet',events:{'click .category':'selectFacet','keydown input':'keydown','mousedown input':'enableEdit','mouseover .VS-icon-cancel':'showDelete','mouseout .VS-icon-cancel':'hideDelete','click .VS-icon-cancel':'remove'},initialize:function(options){this.options=_.extend({},this.options,options);this.flags={canClose:false};_.bindAll(this,'set','keydown','deselectFacet','deferDisableEdit');this.app=this.options.app;},render:function(){$(this.el).html(JST['search_facet']({model:this.model,readOnly:this.app.options.readOnly}));this.setMode('not','editing');this.setMode('not','selected');this.box=this.$('input');this.box.val(this.model.label());this.box.bind('blur',this.deferDisableEdit);this.box.bind('input propertychange',this.keydown);this.setupAutocomplete();return this;},calculateSize:function(){this.box.autoGrowInput();this.box.unbind('updated.autogrow');this.box.bind('updated.autogrow',_.bind(this.moveAutocomplete,this));},resize:function(e){this.box.trigger('resize.autogrow',e);},setupAutocomplete:function(){this.box.autocomplete({source:_.bind(this.autocompleteValues,this),minLength:0,delay:0,autoFocus:true,position:{offset:"0 5"},create:_.bind(function(e,ui){$(this.el).find('.ui-autocomplete-input').css('z-index','auto');},this),select:_.bind(function(e,ui){e.preventDefault();var originalValue=this.model.get('value');this.set(ui.item.value);if(originalValue!=ui.item.value||this.box.val()!=ui.item.value){if(this.app.options.autosearch){this.search(e);}else{this.app.searchBox.renderFacets();this.app.searchBox.focusNextFacet(this,1,{viewPosition:this.options.order});}} return false;},this),open:_.bind(function(e,ui){var box=this.box;this.box.autocomplete('widget').find('.ui-menu-item').each(function(){var $value=$(this),autoCompleteData=$value.data('item.autocomplete')||$value.data('ui-autocomplete-item');if(autoCompleteData['value']==box.val()&&box.data('ui-autocomplete').menu.activate){box.data('ui-autocomplete').menu.activate(new $.Event("mouseover"),$value);}});},this)});this.box.autocomplete('widget').addClass('VS-interface');},moveAutocomplete:function(){var autocomplete=this.box.data('ui-autocomplete');if(autocomplete){autocomplete.menu.element.position({my:"left top",at:"left bottom",of:this.box.data('ui-autocomplete').element,collision:"flip",offset:"0 5"});}},searchAutocomplete:function(e){var autocomplete=this.box.data('ui-autocomplete');if(autocomplete){var menu=autocomplete.menu.element;autocomplete.search();menu.outerWidth(Math.max(menu.width('').outerWidth(),autocomplete.element.outerWidth()));}},closeAutocomplete:function(){var autocomplete=this.box.data('ui-autocomplete');if(autocomplete)autocomplete.close();},autocompleteValues:function(req,resp){var category=this.model.get('category');var value=this.model.get('value');var searchTerm=req.term;this.app.options.callbacks.valueMatches(category,searchTerm,function(matches,options){options=options||{};matches=matches||[];if(searchTerm&&value!=searchTerm){if(options.preserveMatches){resp(matches);}else{var re=VS.utils.inflector.escapeRegExp(searchTerm||'');var matcher=new RegExp('\\b'+re,'i');matches=$.grep(matches,function(item){return matcher.test(item)||matcher.test(item.value)||matcher.test(item.label);});}} if(options.preserveOrder){resp(matches);}else{resp(_.sortBy(matches,function(match){if(match==value||match.value==value)return'';else return match;}));}});},set:function(value){if(!value)return;this.model.set({'value':value});},search:function(e,direction){if(!direction)direction=1;this.closeAutocomplete();this.app.searchBox.searchEvent(e);_.defer(_.bind(function(){this.app.searchBox.focusNextFacet(this,direction,{viewPosition:this.options.order});},this));},enableEdit:function(){if(this.app.options.readOnly)return;if(this.modes.editing!='is'){this.setMode('is','editing');this.deselectFacet();if(this.box.val()==''){this.box.val(this.model.get('value'));}} diff --git a/build-min/visualsearch.js.gz b/build-min/visualsearch.js.gz index 8b0339a..34bb68c 100644 Binary files a/build-min/visualsearch.js.gz and b/build-min/visualsearch.js.gz differ diff --git a/build/visualsearch.js b/build/visualsearch.js index 6d03e18..8d51a5f 100644 --- a/build/visualsearch.js +++ b/build/visualsearch.js @@ -278,7 +278,7 @@ VS.ui.SearchBox = Backbone.View.extend({ // Handles showing/hiding the placeholder text renderPlaceholder : function() { var $placeholder = this.$('.VS-placeholder'); - if (this.app.searchQuery.length) { + if (this.app.searchQuery.length || _.any(this.inputViews, function(view) { return view.value() != ""; })) { $placeholder.addClass("VS-hidden"); } else { $placeholder.removeClass("VS-hidden") @@ -508,6 +508,7 @@ VS.ui.SearchBox = Backbone.View.extend({ return view.isFocused(); }); if (!focus) this.$('.VS-search-box').removeClass('VS-focus'); + this.renderPlaceholder(); }, // Show a menu which adds pre-defined facets to the search box. This is unused for now. diff --git a/lib/js/views/search_box.js b/lib/js/views/search_box.js index 2af7213..8d0f7ba 100644 --- a/lib/js/views/search_box.js +++ b/lib/js/views/search_box.js @@ -197,7 +197,7 @@ VS.ui.SearchBox = Backbone.View.extend({ // Handles showing/hiding the placeholder text renderPlaceholder : function() { var $placeholder = this.$('.VS-placeholder'); - if (this.app.searchQuery.length) { + if (this.app.searchQuery.length || _.any(this.inputViews, function(view) { return view.value() != ""; })) { $placeholder.addClass("VS-hidden"); } else { $placeholder.removeClass("VS-hidden") @@ -427,6 +427,7 @@ VS.ui.SearchBox = Backbone.View.extend({ return view.isFocused(); }); if (!focus) this.$('.VS-search-box').removeClass('VS-focus'); + this.renderPlaceholder(); }, // Show a menu which adds pre-defined facets to the search box. This is unused for now.