1 // ========================================================================== 2 // Papercube.SearchController 3 // 4 // License: PaperCube is open source software released under 5 // the MIT License (see license.js) 6 // ========================================================================== 7 8 require('core'); 9 10 /** @class 11 12 Controls the search of data. Interfaces with the server. 13 14 @extends SC.Object 15 @author Peter Bergstrom 16 @version 1.0 17 @copyright 2008-2009 Peter Bergström. 18 @static 19 */ 20 Papercube.searchController = SC.Object.create( 21 /** @scope Papercube.searchController */ { 22 23 /** 24 The current viewing mode is specified in the viewController. 25 26 @property {String} 27 @binding "Papercube.viewController.viewingMode" 28 */ 29 viewingModeBinding: "Papercube.viewController.viewingMode", 30 31 /** 32 boolean that is bound to show the request spinner. 33 34 @property {Boolean} 35 @default NO 36 */ 37 showRequestSpinner: NO, 38 39 /** 40 These are the valid search headings. 41 42 Depending on the view mode, you switch the heading. 43 44 Values 45 46 { 47 "Authors": "Search for authors", 48 "Papers": "Search for papers" 49 } 50 51 @property {Object} 52 */ 53 _searchHeadings: 54 { 55 "Authors": "Search for authors", 56 "Papers": "Search for papers" 57 }, 58 59 /** 60 Currently selected search heading. 61 62 Defaults to Papers. 63 64 @property {String} 65 @default '' 66 */ 67 searchHeading: "", 68 69 /** 70 These are the search keys. 71 72 Values 73 [ 74 {order: 1, name: "Paper Title", key: "title"}, 75 {order: 2, name: "Paper Abstract", key: "abstract"}, 76 {order: 3, name: "Paper Publication Year", key: "year"}, 77 {order: 4, name: "Paper Publisher", key: "publisher"}, 78 {order: 5, name: "Author Name", key: "name"}, 79 {order: 6, name: "Author Affiliation", key: "affiliation"}, 80 {order: 7, name: "Author Address", key: "address"} 81 ] 82 83 @property {Array} 84 */ 85 searchKeys: 86 [ 87 {order: 1, name: "Paper Title", key: "title"}, 88 {order: 2, name: "Paper Abstract", key: "abstract"}, 89 {order: 2, name: "Paper Subject", key: "subject"}, 90 {order: 3, name: "Paper Publication Year", key: "pubyear"}, 91 {order: 4, name: "Author Name", key: "name"}, 92 ], 93 94 /** 95 Internal search count for synchronization. 96 97 @property {Integer} 98 @default 0 99 */ 100 _sc: 0, 101 102 /** 103 Start for paging. 104 105 @property {Integer} 106 @default 0 107 */ 108 _searchStart: 0, 109 110 /** 111 Limit for paging. 112 113 @property {Integer} 114 @default 50 115 */ 116 _searchLimit: 50, 117 118 /** 119 Show more button visiblity. Only visible when there are results. 120 121 @property {Boolean} 122 @default NO 123 */ 124 showMoreAllowed: NO, 125 126 /** 127 The current searchKey. 128 129 @property {String} 130 @default '' 131 */ 132 searchKey: '', 133 134 /** 135 The current searchValue. 136 137 @property {String} 138 @default '' 139 */ 140 searchValue: '', 141 142 /** 143 The search count is a string that is displayed at the bottom of the search window. 144 145 @property {String} 146 @default '' 147 */ 148 searchCount: '', 149 150 /** 151 The data retrieved from a search. This property is used by the searchResultsCollectionView. 152 153 @property {String} 154 */ 155 searchResults: null, 156 157 /** 158 Observes the viewingMode from the viewingController and switches search options based on it. 159 160 @observes viewingMode 161 */ 162 viewingModeDidChange: function() 163 { 164 var viewingMode = this.get('viewingMode'); 165 166 // Set the desired search heading. 167 switch(viewingMode) 168 { 169 case "Authors": 170 this.set('searchHeading', this._searchHeadings["Authors"]); 171 break; 172 default: 173 this.set('searchHeading', this._searchHeadings["Papers"]); 174 break; 175 } 176 177 this.set("searchResults", []); 178 this.set("searchCounnt", 0); 179 this.performSearch(); 180 181 }.observes('viewingMode'), 182 183 /** 184 Observe the selection of the search results. Call the viewController when it changes. 185 186 @observes selection 187 */ 188 selectionDidChange: function() 189 { 190 var sel = this.get("selection"); 191 if(sel && sel[0]) 192 { 193 Papercube.viewController.setContentToView(sel[0]); 194 } 195 }.observes('selection'), 196 197 /** 198 Perform the search operation. 199 */ 200 performSearch: function() 201 { 202 203 // Get the searchKey and searchValue specified in the search form. 204 var searchKey = this.get("searchKey"); 205 var searchValue = this.get("searchValue"); 206 207 // Bail if there is no value. 208 if(searchValue == '') return; 209 210 // Hide the fan and show the request spinner. 211 Papercube.canvasController.hideFan(); 212 Papercube.searchController.set('showRequestSpinner', YES); 213 214 // Increment the search count to allow for easy search result synchronization. 215 this._sc++; 216 217 // Set the viewController's content to null since a new search is being processed. 218 Papercube.viewController.setContentToView(null); 219 220 // Make sure that there is a search key. 221 if(!searchKey) 222 { 223 searchKey = 'title'; 224 } 225 this._searchStart = 0; 226 227 // Branch the search on the viewingMode. 228 if(Papercube.viewController.get('viewingMode') == 'Papers') 229 { 230 Papercube.adaptor.searchPapers(searchKey, searchValue, this._sc, 0, this._searchLimit); 231 } 232 else 233 { 234 Papercube.adaptor.searchAuthors(searchKey, searchValue, this._sc, 0, this._searchLimit); 235 } 236 }, 237 238 /** 239 Without starting a new search, increment the start index and get more search results. 240 */ 241 showMoreFromSearch: function() 242 { 243 Papercube.canvasController.hideFan(); 244 245 Papercube.searchController.set('showRequestSpinner', YES); 246 247 // Get the searchKey and searchValue specified in the search form. 248 var searchKey = this.get("searchKey"); 249 var searchValue = this.get("searchValue"); 250 251 // Make sure that there is a search key. 252 if(!searchKey) 253 { 254 searchKey = 'title'; 255 } 256 257 this._searchStart += this._searchLimit; 258 259 // Branch the search on the viewingMode. 260 if(Papercube.viewController.get('viewingMode') == 'Papers') 261 { 262 Papercube.adaptor.searchPapers(searchKey, searchValue, this._sc, this._searchStart, this._searchLimit); 263 } 264 else 265 { 266 Papercube.adaptor.searchAuthors(searchKey, searchValue, this._sc, this._searchStart, this._searchLimit); 267 } 268 }, 269 270 /** 271 Display the results from the server. 272 */ 273 displayResults: function() 274 { 275 // Set the number of items returned in the search. 276 if(Papercube.viewController.get('viewingMode') == 'Papers') 277 { 278 var results = Papercube.Paper.findAll({sc: this._sc}); 279 this.set("searchCount", Papercube.pluralizeString(' Paper', results.length)); 280 } 281 else 282 { 283 var results = Papercube.Author.findAll({sc: this._sc}); 284 this.set("searchCount", Papercube.pluralizeString(' Author', results.length)); 285 } 286 287 // Set the results to the searchResults property. 288 this.set('showMoreAllowed', (results.length != 0)); 289 this.set("searchResults", results); 290 Papercube.searchController.set('showRequestSpinner', NO); 291 }, 292 293 294 295 /** 296 Initialiation function. 297 */ 298 init: function() 299 { 300 sc_super(); 301 302 // Set papers as the default heading. 303 this.set('searchHeading', this._searchHeadings["Papers"]); 304 } 305 306 }) ; 307