]>
git.proxmox.com Git - sencha-touch.git/blob - src/examples/touchflickr/app/controller/Search.js
2 * This controller is the main, and only controller for this application. It handles all the views and functionality
5 Ext
.define('Flickr.controller.Search', {
6 extend
: 'Ext.app.Controller',
11 selector
: 'mainview',
15 searchBar
: 'searchbar',
16 searchList
: 'searchlist',
17 flickrList
: 'flickrlist',
18 searchField
: 'searchbar > searchfield'
27 itemtap
: 'onFlickrTap'
31 select
: 'onSearchSelect',
32 itemswipe
: 'onSearchSwipe'
35 'searchlist searchlistitem button': {
42 Ext
.getStore('Searches').load({
43 callback
: this.onSearchesStoreLoad
,
49 * Called when the searchesStore has been loaded from localStorage. If it is NOT a phone, it will select one of the searches
50 * from the list, now that it is loaded.
51 * We don't want to select a search when it is loaded on a phone, as it would trigger the flickrList view to display.
53 onSearchesStoreLoad: function() {
54 var search
= Ext
.getStore('Searches').getAt(0);
57 this.doSearch("senchacon");
62 * Called when a search is selected from the searchList. It sets the store of the flickrList to the flickrimages() store of the selected
65 onSearchSelect: function(list
, search
) {
66 var store
= search
.flickrimages();
68 this.getFlickrList().setStore(store
);
73 * Called when an item in the searchList is swiped. It will show the delete button in the swiped item.
75 onSearchSwipe: function(dataview
, index
, target
) {
76 if (Ext
.getStore('Searches').getCount() < 2) {
80 //set the currentDeleteButton so we know what is it to hide it in the listener below
81 this.currentDeleteButton
= target
.getDeleteButton();
82 this.currentDeleteButton
.show();
84 //add a listener to the body, so we can hide the button if the user taps anywhere but the button.
85 Ext
.getBody().on('tap', this.onBodyTap
, this);
89 * Called when the user taps on the body. Hides the delete button and removes the listener from the body.
91 onBodyTap: function(e
) {
92 if (this.currentDeleteButton
) {
93 this.currentDeleteButton
.hide();
97 Ext
.getBody().un('tap', this.onBodyTap
, this);
101 * Called when a user taps on an item in the flickrList.
103 onFlickrTap: function(list
, index
, target
, record
, e
) {
107 me
.popUp
= Ext
.create("Ext.Panel", {
117 src
: record
.get("image_url")
121 img
= me
.popUp
.down("image");
123 me
.popUp
.on("hide", function(){
127 img
.on("load", function() {
128 me
.popUp
.setMasked(false);
131 img
= me
.popUp
.down("image");
133 img
.setSrc(record
.get("image_url"));
136 me
.popUp
.setMasked({xtype
:'loadmask'});
141 * Called when a use taps the delete button on a searchList item
143 onSearchDelete: function(button
, e
) {
144 var item
= button
.getParent(),
145 search
= item
.getRecord();
147 this.fireAction('destroy', [search
, button
], 'doDestroy');
151 * Removes a specified search record from the searches store. The tablet controller subclass has some additional
152 * logic to select the nearest saved search
154 doDestroy: function(search
, button
) {
155 var store
= Ext
.getStore('Searches');
157 store
.remove(search
);
163 * Called on the keyup event of the search field. If the enter/return key was pressed, it will fire the search action.
165 onSearch: function(field
, e
) {
166 var keyCode
= e
.event
.keyCode
,
167 searchField
= this.getSearchField();
169 //the return keyCode is 13.
171 //fire the search action with the current value of the searchField
172 this.fireAction('search', [searchField
.getValue()], 'doSearch');
177 * Called with the search action above. Searches twitter for a specified search term or record
179 doSearch: function(search
) {
180 var model
= Flickr
.model
.Search
,
181 flickrList
= this.getFlickrList(),
182 searchList
= this.getSearchList(),
183 searchesStore
= Ext
.getStore('Searches'),
184 searchField
= this.getSearchField(),
187 // ensure there is a search...
192 //ensure the flickrlist is visible
195 //check if ths search already exists in the searchesStore
196 index
= searchesStore
.find('query', search
);
198 //it exists, so lets just select it
199 search
= searchesStore
.getAt(index
);
201 searchList
.select(search
);
203 //empty the field and blur it so it looses focus
204 searchField
.setValue('');
210 //if the passed argument is not an instance of a Search mode, create a new instance
211 if (!(search
instanceof Flickr
.model
.Search
)) {
212 query
= search
.replace("%20", " ");
218 //add the new search instance to the searchsStore
219 searchesStore
.add(search
);
220 searchesStore
.sync();
222 // select the new record in the list
223 searchList
.select(search
);
225 //empty the field and remove focus from it
226 searchField
.setValue('');