| 64 | | model: Widget |
| | 64 | model: Widget, |
| | 65 | |
| | 66 | // Controls searching on the current widget collection |
| | 67 | // and triggers an update event |
| | 68 | doSearch: function( value ) { |
| | 69 | |
| | 70 | // Don't do anything if we've already done this search |
| | 71 | // Useful because the search handler fires multiple times per keystroke |
| | 72 | if ( this.terms === value ) { |
| | 73 | return; |
| | 74 | } |
| | 75 | |
| | 76 | // Updates terms with the value passed |
| | 77 | this.terms = value; |
| | 78 | |
| | 79 | // If we have terms, run a search... |
| | 80 | if ( this.terms.length > 0 ) { |
| | 81 | this.search( this.terms ); |
| | 82 | } |
| | 83 | |
| | 84 | // If search is blank, show all themes |
| | 85 | // Useful for resetting the views when you clean the input |
| | 86 | if ( this.terms === '' ) { |
| | 87 | this.reset( WidgetCustomizer_exports.available_widgets ); |
| | 88 | } |
| | 89 | |
| | 90 | // Trigger an 'update' event |
| | 91 | this.trigger( 'update' ); |
| | 92 | }, |
| | 93 | |
| | 94 | // Performs a search within the collection |
| | 95 | // @uses RegExp |
| | 96 | search: function( term ) { |
| | 97 | var match, results, haystack; |
| | 98 | |
| | 99 | // Start with a full collection |
| | 100 | this.reset( WidgetCustomizer_exports.available_widgets, { silent: true } ); |
| | 101 | |
| | 102 | // The RegExp object to match |
| | 103 | // |
| | 104 | // Consider spaces as word delimiters and match the whole string |
| | 105 | // so matching terms can be combined |
| | 106 | term = term.replace( ' ', ')(?=.*' ); |
| | 107 | match = new RegExp( '^(?=.*' + term + ').+', 'i' ); |
| | 108 | |
| | 109 | results = this.filter( function( data ) { |
| | 110 | haystack = _.union( data.get( 'name' ), data.get( 'id' ), data.get( 'description' ) ); |
| | 111 | |
| | 112 | return match.test( haystack ); |
| | 113 | }); |
| | 114 | |
| | 115 | this.reset( results ); |
| | 116 | } |
| 1560 | | update_available_widgets_list = function () { |
| 1561 | | self.available_widgets.each( function ( widget ) { |
| 1562 | | var widget_tpl = $( '#widget-tpl-' + widget.id ); |
| 1563 | | widget_tpl.toggle( ! widget.get( 'is_disabled' ) ); |
| 1564 | | if ( widget.get( 'is_disabled' ) && widget_tpl.is( panel.selected_widget_tpl ) ) { |
| 1565 | | panel.selected_widget_tpl = null; |
| 1566 | | } |
| 1567 | | } ); |
| 1568 | | }; |
| | 1612 | self.available_widgets.on( 'change update', panel.update_available_widgets_list ); |
| | 1613 | panel.update_available_widgets_list(); |
| 1614 | | // If a widget was selected but the filter value has been cleared out, clear selection |
| 1615 | | if ( panel.selected_widget_tpl && ! filter_val ) { |
| 1616 | | panel.selected_widget_tpl.removeClass( 'selected' ); |
| 1617 | | panel.selected_widget_tpl = null; |
| 1618 | | } |
| | 1646 | // Remove a widget from being selected if it is no longer visible |
| | 1647 | if ( panel.selected_widget_tpl && ! panel.selected_widget_tpl.is( ':visible' ) ) { |
| | 1648 | panel.selected_widget_tpl.removeClass( 'selected' ); |
| | 1649 | panel.selected_widget_tpl = null; |
| | 1650 | } |
| 1620 | | // If a filter has been entered and a widget hasn't been selected, select the first one shown |
| 1621 | | if ( ! panel.selected_widget_tpl && filter_val ) { |
| 1622 | | first_visible_widget = panel.container.find( '> .widget-tpl:visible:first' ); |
| 1623 | | if ( first_visible_widget.length ) { |
| 1624 | | panel.select( first_visible_widget ); |
| 1625 | | } |
| 1626 | | } |
| | 1652 | // If a widget was selected but the filter value has been cleared out, clear selection |
| | 1653 | if ( panel.selected_widget_tpl && ! event.target.value ) { |
| | 1654 | panel.selected_widget_tpl.removeClass( 'selected' ); |
| | 1655 | panel.selected_widget_tpl = null; |
| | 1656 | } |
| | 1719 | * Updates widgets list. |
| | 1720 | */ |
| | 1721 | update_available_widgets_list: function() { |
| | 1722 | var panel = self.availableWidgetsPanel; |
| | 1723 | |
| | 1724 | // First hide all widgets... |
| | 1725 | panel.container.find( '.widget-tpl' ).hide(); |
| | 1726 | |
| | 1727 | // ..and then show only available widgets which could be filtered |
| | 1728 | self.available_widgets.each( function ( widget ) { |
| | 1729 | var widget_tpl = $( '#widget-tpl-' + widget.id ); |
| | 1730 | widget_tpl.toggle( ! widget.get( 'is_disabled' ) ); |
| | 1731 | if ( widget.get( 'is_disabled' ) && widget_tpl.is( panel.selected_widget_tpl ) ) { |
| | 1732 | panel.selected_widget_tpl = null; |
| | 1733 | } |
| | 1734 | } ); |
| | 1735 | }, |
| | 1736 | |
| | 1737 | /** |
| 1783 | | |
| 1784 | | /* @todo remove this dependency */ |
| 1785 | | /* |
| 1786 | | * jQuery.liveFilter |
| 1787 | | * |
| 1788 | | * Copyright (c) 2009 Mike Merritt |
| 1789 | | * |
| 1790 | | * Forked by Lim Chee Aun (cheeaun.com) |
| 1791 | | * |
| 1792 | | */ |
| 1793 | | |
| 1794 | | (function($){ |
| 1795 | | $.fn.liveFilter = function(inputEl, filterEl, options){ |
| 1796 | | var el, filter, defaults = { |
| 1797 | | filterChildSelector: null, |
| 1798 | | filter: function(el, val){ |
| 1799 | | return $(el).text().toUpperCase().indexOf(val.toUpperCase()) >= 0; |
| 1800 | | }, |
| 1801 | | before: function(){}, |
| 1802 | | after: function(){} |
| 1803 | | }; |
| 1804 | | options = $.extend(defaults, options); |
| 1805 | | |
| 1806 | | el = $(this).find(filterEl); |
| 1807 | | if (options.filterChildSelector) { |
| 1808 | | el = el.find(options.filterChildSelector); |
| 1809 | | } |
| 1810 | | |
| 1811 | | filter = options.filter; |
| 1812 | | $(inputEl).keyup(function(){ |
| 1813 | | var val = $(this).val(), contains, containsNot; |
| 1814 | | |
| 1815 | | contains = el.filter(function(){ |
| 1816 | | return filter(this, val); |
| 1817 | | }); |
| 1818 | | containsNot = el.not(contains); |
| 1819 | | if (options.filterChildSelector){ |
| 1820 | | contains = contains.parents(filterEl); |
| 1821 | | containsNot = containsNot.parents(filterEl).hide(); |
| 1822 | | } |
| 1823 | | |
| 1824 | | options.before.call(this, contains, containsNot); |
| 1825 | | |
| 1826 | | contains.show(); |
| 1827 | | containsNot.hide(); |
| 1828 | | |
| 1829 | | if (val === '') { |
| 1830 | | contains.show(); |
| 1831 | | containsNot.show(); |
| 1832 | | } |
| 1833 | | |
| 1834 | | options.after.call(this, contains, containsNot); |
| 1835 | | }); |
| 1836 | | }; |
| 1837 | | })(jQuery); |