Add pagination to OCD search results, a different URL for oauth, and fixes
- Add pagination to OCD search results.
- Change server code to have a different endpoint (redirect URI of swtstore)
to process the OAuth authentication.
- Fix bug when entering input which is not a URL, the app would throw 500;
now it handles that error.
- Fix overlay not showing for the entire page.
| | | | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
---|
2 | | 2 | |
---|
3 | import flask | | import flask |
---|
4 | from flask import session | | from flask import session |
---|
| | 3 | from flask import Flask, session, request, make_response, url_for, redirect,\ | | | 4 | render_template, jsonify, abort |
---|
5 | import lxml.html | 5 | import lxml.html |
---|
6 | import requests | 6 | import requests |
---|
7 | import json | 7 | import json |
---|
… | | … | |
---|
12 | import config | 12 | import config |
---|
13 | | 13 | |
---|
14 | | 14 | |
---|
15 | app = flask.Flask(__name__) | | app = flask.Flask(__name__) |
---|
| | 15 | app = Flask(__name__) | 16 | app.config['SECRET_KEY'] = config.secret_key | 16 | app.config['SECRET_KEY'] = config.secret_key |
---|
17 | | 17 | |
---|
18 | | 18 | |
---|
19 | @app.route('/', methods=['GET']) | 19 | @app.route('/', methods=['GET']) |
---|
20 | def index(): | 20 | def index(): |
---|
21 | auth_tok = None | | auth_tok = None |
---|
22 | # check if ?code param is there | | # check if ?code param is there |
---|
23 | if flask.request.args.get('code'): | | if flask.request.args.get('code'): |
---|
24 | # prepare the payload | | # prepare the payload |
---|
25 | payload = { | | payload = { |
---|
26 | 'scopes': 'email sweet', | | 'scopes': 'email sweet', |
---|
27 | 'client_secret': config.app_secret, | | 'client_secret': config.app_secret, |
---|
28 | 'code': flask.request.args.get('code'), | | 'code': flask.request.args.get('code'), |
---|
29 | 'redirect_uri': config.redirect_uri, | | 'redirect_uri': config.redirect_uri, |
---|
30 | 'grant_type': 'authorization_code', | | 'grant_type': 'authorization_code', |
---|
31 | 'client_id': config.app_id | | 'client_id': config.app_id |
---|
32 | } | | } |
---|
33 | # token exchange endpoint | | # token exchange endpoint |
---|
34 | oauth_token_x_endpoint = config.swtstoreURL + '/oauth/token' | | oauth_token_x_endpoint = config.swtstoreURL + '/oauth/token' |
---|
35 | resp = requests.post(oauth_token_x_endpoint, data=payload) | | resp = requests.post(oauth_token_x_endpoint, data=payload) |
---|
36 | auth_tok = json.loads(resp.text) | | auth_tok = json.loads(resp.text) |
---|
37 | print 'recvd auth token from swtstore' | | print 'recvd auth token from swtstore' |
---|
38 | print auth_tok | | print auth_tok |
---|
39 | | 21 | |
---|
40 | if 'error' in auth_tok: | | if 'error' in auth_tok: |
---|
41 | print auth_tok['error'] | | print auth_tok['error'] |
---|
42 | return flask.make_response(auth_tok['error'], 200) | | return flask.make_response(auth_tok['error'], 200) |
---|
43 | | | |
---|
44 | # set sessions et al | | # set sessions et al |
---|
45 | session['auth_tok'] = auth_tok | | session['auth_tok'] = auth_tok |
---|
46 | session['auth_tok']['issued'] = datetime.utcnow() | | session['auth_tok']['issued'] = datetime.utcnow() |
---|
47 | | | |
---|
48 | # else if session is already existing.. | | # else if session is already existing.. |
---|
| | 22 | # if auth_tok is in session already.. | 49 | if 'auth_tok' in session: | 23 | if 'auth_tok' in session: |
---|
50 | auth_tok = session['auth_tok'] | 24 | auth_tok = session['auth_tok'] |
---|
| | 25 | |
---|
51 | # check if it has expired | 26 | # check if it has expired |
---|
52 | oauth_token_expires_in_endpoint = config.swtstoreURL +\ | 27 | oauth_token_expires_in_endpoint = config.swtstoreURL +\ |
---|
53 | '/oauth/token-expires-in' | 28 | '/oauth/token-expires-in' |
---|
54 | resp = requests.get(oauth_token_expires_in_endpoint) | 29 | resp = requests.get(oauth_token_expires_in_endpoint) |
---|
55 | expires_in = json.loads(resp.text)['expires_in'] | 30 | expires_in = json.loads(resp.text)['expires_in'] |
---|
| | 31 | |
---|
56 | # added for backwared compatibility. previous session stores did not | 32 | # added for backwared compatibility. previous session stores did not |
---|
57 | # have issued key | 33 | # have issued key |
---|
58 | try: | 34 | try: |
---|
… | | … | |
---|
50 | | 50 | |
---|
51 | #print 'existing tokens' | 51 | #print 'existing tokens' |
---|
52 | #print auth_tok | 52 | #print auth_tok |
---|
53 | return flask.render_template('index.html', | | return flask.render_template('index.html', |
---|
54 | access_token=auth_tok['access_token'], | | access_token=auth_tok['access_token'], |
---|
55 | refresh_token=auth_tok['refresh_token'], | | refresh_token=auth_tok['refresh_token'], |
---|
56 | config=config, | | config=config, |
---|
57 | url=flask.request.args.get('where')) | | url=flask.request.args.get('where')) |
---|
| | 53 | return render_template('index.html', | | | 54 | access_token=auth_tok['access_token'], |
---|
| | 55 | refresh_token=auth_tok['refresh_token'], |
---|
| | 56 | config=config, |
---|
| | 57 | url=request.args.get('where')) |
---|
58 | | 58 | |
---|
59 | | 59 | |
---|
| | 60 | @app.route('/authenticate', methods=['GET']) |
---|
| | 61 | def authenticateWithOAuth(): |
---|
| | 62 | auth_tok = None |
---|
| | 63 | code = request.args.get('code') |
---|
| | 64 | |
---|
| | 65 | # prepare the payload |
---|
| | 66 | payload = { |
---|
| | 67 | 'scopes': 'email sweet', |
---|
| | 68 | 'client_secret': config.app_secret, |
---|
| | 69 | 'code': code, |
---|
| | 70 | 'redirect_uri': config.redirect_uri, |
---|
| | 71 | 'grant_type': 'authorization_code', |
---|
| | 72 | 'client_id': config.app_id |
---|
| | 73 | } |
---|
| | 74 | |
---|
| | 75 | # token exchange endpoint |
---|
| | 76 | oauth_token_x_endpoint = config.swtstoreURL + '/oauth/token' |
---|
| | 77 | resp = requests.post(oauth_token_x_endpoint, data=payload) |
---|
| | 78 | auth_tok = json.loads(resp.text) |
---|
| | 79 | print 'recvd auth token from swtstore' |
---|
| | 80 | print auth_tok |
---|
| | 81 | |
---|
| | 82 | if 'error' in auth_tok: |
---|
| | 83 | print auth_tok['error'] |
---|
| | 84 | return make_response(auth_tok['error'], 200) |
---|
| | 85 | |
---|
| | 86 | # set sessions et al |
---|
| | 87 | session['auth_tok'] = auth_tok |
---|
| | 88 | session['auth_tok']['issued'] = datetime.utcnow() |
---|
| | 89 | return redirect(url_for('index')) |
---|
| | 90 | |
---|
| | 91 | |
---|
60 | # endpoint to search the Open Cuultur Data APIs | 92 | # endpoint to search the Open Cuultur Data APIs |
---|
61 | # takes in `query`, `size`, and `from` parameters in query string | 93 | # takes in `query`, `size`, and `from` parameters in query string |
---|
62 | # returns a JSON response | 94 | # returns a JSON response |
---|
63 | @app.route('/search/ocd', methods=['GET']) | 95 | @app.route('/search/ocd', methods=['GET']) |
---|
64 | def searchOCD(): | 96 | def searchOCD(): |
---|
65 | query = flask.request.args.get('query') | | query = flask.request.args.get('query') |
---|
| | 97 | query = request.args.get('query') | 66 | #collection = flask.request.args.get('collection') | 98 | #collection = flask.request.args.get('collection') |
---|
67 | size = flask.request.args.get('size') or 10 | | size = flask.request.args.get('size') or 10 |
---|
68 | offset = flask.request.args.get('from') or 0 | | offset = flask.request.args.get('from') or 0 |
---|
| | 99 | size = request.args.get('size') or 9 | | | 100 | offset = request.args.get('from') or 0 |
---|
69 | | 101 | |
---|
70 | # if query parameter is not passed, return bad request. | 102 | # if query parameter is not passed, return bad request. |
---|
71 | if not query: | 103 | if not query: |
---|
72 | flask.abort(400) | | flask.abort(400) |
---|
| | 104 | abort(400) | 73 | | 105 | |
---|
74 | payload = { | 106 | payload = { |
---|
75 | 'query': query, | 107 | 'query': query, |
---|
… | | … | |
---|
118 | resp = requests.post('http://api.opencultuurdata.nl/v0/search', | 118 | resp = requests.post('http://api.opencultuurdata.nl/v0/search', |
---|
119 | data=json.dumps(payload)) | 119 | data=json.dumps(payload)) |
---|
120 | | 120 | |
---|
121 | response = flask.make_response() | | response = flask.make_response() |
---|
| | 121 | response = make_response() | 122 | response.data = json.dumps(resp.json()) | 122 | response.data = json.dumps(resp.json()) |
---|
123 | response.headers['Content-type'] = 'application/json' | 123 | response.headers['Content-type'] = 'application/json' |
---|
124 | return response | 124 | return response |
---|
… | | … | |
---|
128 | @app.route('/resolve-ocd-media', methods=['GET']) | 128 | @app.route('/resolve-ocd-media', methods=['GET']) |
---|
129 | def resolveOCDMediaURLs(): | 129 | def resolveOCDMediaURLs(): |
---|
130 | | 130 | |
---|
131 | media_hash = flask.request.args.get('hash') or None | | media_hash = flask.request.args.get('hash') or None |
---|
| | 131 | media_hash = request.args.get('hash') or None | 132 | | 132 | |
---|
133 | if not media_hash: | 133 | if not media_hash: |
---|
134 | flask.abort(400) | | flask.abort(400) |
---|
| | 134 | abort(400) | 135 | | 135 | |
---|
136 | resp = requests.get('http://api.opencultuurdata.nl/v0/resolve/' + | 136 | resp = requests.get('http://api.opencultuurdata.nl/v0/resolve/' + |
---|
137 | media_hash) | 137 | media_hash) |
---|
138 | | 138 | |
---|
139 | return flask.jsonify(url=resp.url) | | return flask.jsonify(url=resp.url) |
---|
| | 139 | return jsonify(url=resp.url) | 140 | | 140 | |
---|
141 | | 141 | |
---|
142 | @app.route('/media-type', methods=['GET']) | 142 | @app.route('/media-type', methods=['GET']) |
---|
143 | def getMediaType(): | 143 | def getMediaType(): |
---|
144 | | 144 | |
---|
145 | where = flask.request.args.get('where') or None | | where = flask.request.args.get('where') or None |
---|
| | 145 | where = request.args.get('where') or None | 146 | | 146 | |
---|
147 | if not where: | 147 | if not where: |
---|
148 | flask.abort(400) | | flask.abort(400) |
---|
| | 148 | abort(400) | 149 | | 149 | |
---|
150 | resp = requests.get(where) | 150 | resp = requests.get(where) |
---|
151 | content = resp.text | 151 | content = resp.text |
---|
152 | | 152 | |
---|
153 | if imghdr.what('ignore', content) is None: | 153 | if imghdr.what('ignore', content) is None: |
---|
154 | return flask.jsonify({'type': 'html'}) | | return flask.jsonify({'type': 'html'}) |
---|
| | 154 | return jsonify({'type': 'html'}) | 155 | else: | 155 | else: |
---|
156 | return flask.jsonify({'type': 'image'}) | | return flask.jsonify({'type': 'image'}) |
---|
| | 156 | return jsonify({'type': 'image'}) | 157 | | 157 | |
---|
158 | | 158 | |
---|
159 | @app.route('/annotate', methods=['GET']) | 159 | @app.route('/annotate', methods=['GET']) |
---|
160 | def annotate(): | 160 | def annotate(): |
---|
161 | # img = urllib2.urlopen(flask.request.args['where']).read() | 161 | # img = urllib2.urlopen(flask.request.args['where']).read() |
---|
162 | request = requests.get(flask.request.args['where']) | | request = requests.get(flask.request.args['where']) |
---|
163 | content = request.text | | content = request.text |
---|
| | 162 | where = request.args.get('where') | | | 163 | response = requests.get(where) |
---|
| | 164 | content = response.text |
---|
164 | if imghdr.what('ignore', content) is None: | 165 | if imghdr.what('ignore', content) is None: |
---|
165 | root = lxml.html.parse(StringIO.StringIO(content)).getroot() | 166 | root = lxml.html.parse(StringIO.StringIO(content)).getroot() |
---|
166 | root.make_links_absolute(flask.request.args['where'], | | root.make_links_absolute(flask.request.args['where'], |
---|
| | 167 | root.make_links_absolute(where, | 167 | resolve_base_href=True) | 168 | resolve_base_href=True) |
---|
168 | | 169 | |
---|
169 | addScript("//code.jquery.com/jquery-1.11.0.min.js", root) | 170 | addScript("//code.jquery.com/jquery-1.11.0.min.js", root) |
---|
170 | addScript(flask.url_for('static', filename="js/annotator-full.min.js"), | | addScript(flask.url_for('static', filename="js/annotator-full.min.js"), |
---|
| | 171 | addScript(url_for('static', filename="js/annotator-full.min.js"), | 171 | root) | 172 | root) |
---|
172 | | 173 | |
---|
173 | addCSS(flask.url_for('static', filename='css/annotator.min.css'), root) | | addCSS(flask.url_for('static', filename='css/annotator.min.css'), root) |
---|
174 | addCSS(flask.url_for('static', filename='css/swtmaker.css'), root) | | addCSS(flask.url_for('static', filename='css/swtmaker.css'), root) |
---|
175 | addCSS(flask.url_for('static', filename='css/bootstrap.min.css'), root) | | addCSS(flask.url_for('static', filename='css/bootstrap.min.css'), root) |
---|
176 | addScript(flask.url_for('static', | | addScript(flask.url_for('static', |
---|
177 | filename="js/lib/underscore-1.5.2.min.js"), | | filename="js/lib/underscore-1.5.2.min.js"), |
---|
| | 174 | addCSS(url_for('static', filename='css/annotator.min.css'), root) | | | 175 | addCSS(url_for('static', filename='css/swtmaker.css'), root) |
---|
| | 176 | addCSS(url_for('static', filename='css/bootstrap.min.css'), root) |
---|
| | 177 | addScript(url_for('static', |
---|
| | 178 | filename="js/lib/underscore-1.5.2.min.js"), |
---|
178 | root) | 179 | root) |
---|
179 | addScript(flask.url_for('static', | | addScript(flask.url_for('static', |
---|
180 | filename="js/lib/backbone-1.0.0.min.js"), | | filename="js/lib/backbone-1.0.0.min.js"), |
---|
| | 180 | addScript(url_for('static', | | | 181 | filename="js/lib/backbone-1.0.0.min.js"), |
---|
181 | root) | 182 | root) |
---|
182 | addCSS(flask.url_for('static', filename='css/annotorious.css'), root) | | addCSS(flask.url_for('static', filename='css/annotorious.css'), root) |
---|
183 | addScript(flask.url_for('static', | | addScript(flask.url_for('static', |
---|
184 | filename="js/annotorious.okfn.0.3.js"), | | filename="js/annotorious.okfn.0.3.js"), |
---|
| | 183 | addCSS(url_for('static', filename='css/annotorious.css'), root) | | | 184 | addScript(url_for('static', |
---|
| | 185 | filename="js/annotorious.okfn.0.3.js"), |
---|
185 | root) | 186 | root) |
---|
186 | | 187 | |
---|
187 | if 'auth_tok' in session: | 188 | if 'auth_tok' in session: |
---|
… | | … | |
---|
213 | config.redirect_uri) | 213 | config.redirect_uri) |
---|
214 | configScript.set("type", "text/javascript") | 214 | configScript.set("type", "text/javascript") |
---|
215 | | 215 | |
---|
216 | addScript(flask.url_for('static', filename="js/oauth.js"), root) | | addScript(flask.url_for('static', filename="js/oauth.js"), root) |
---|
| | 216 | addScript(url_for('static', filename="js/oauth.js"), root) | 217 | | 217 | |
---|
218 | addScript(flask.url_for('static', filename="js/app.js"), root) | | addScript(flask.url_for('static', filename="js/app.js"), root) |
---|
| | 218 | addScript(url_for('static', filename="js/app.js"), root) | 219 | | 219 | |
---|
220 | response = flask.make_response() | | response = flask.make_response() |
---|
| | 220 | response = make_response() | 221 | response.data = lxml.html.tostring(root) | 221 | response.data = lxml.html.tostring(root) |
---|
222 | return response | 222 | return response |
---|
223 | | 223 | |
---|
… | | … | |
---|
227 | else: | 227 | else: |
---|
228 | auth_tok = {'access_token': '', 'refresh_token': ''} | 228 | auth_tok = {'access_token': '', 'refresh_token': ''} |
---|
229 | | 229 | |
---|
230 | return flask.render_template('index.html', | | return flask.render_template('index.html', |
---|
231 | access_token=auth_tok['access_token'], | | access_token=auth_tok['access_token'], |
---|
232 | refresh_token=auth_tok['refresh_token'], | | refresh_token=auth_tok['refresh_token'], |
---|
233 | config=config, | | config=config, |
---|
234 | url=flask.request.args.get('where')) | | url=flask.request.args.get('where')) |
---|
| | 230 | return render_template('index.html', | | | 231 | access_token=auth_tok['access_token'], |
---|
| | 232 | refresh_token=auth_tok['refresh_token'], |
---|
| | 233 | config=config, |
---|
| | 234 | url=request.args.get('where')) |
---|
235 | | 235 | |
---|
236 | | 236 | |
---|
237 | def addScript(src, el): | 237 | def addScript(src, el): |
---|
| | | | 66 | throw Error('"where" option must be passed to get sweets of a URI'); | 66 | throw Error('"where" option must be passed to get sweets of a URI'); |
---|
67 | return false; | 67 | return false; |
---|
68 | } | 68 | } |
---|
69 | /*if(!swtr.access_token) { | | /*if(!swtr.access_token) { |
---|
70 | throw new Error('Access Token required to get query that API'); | | throw new Error('Access Token required to get query that API'); |
---|
71 | }*/ | | }*/ |
---|
72 | // setting up params | 69 | // setting up params |
---|
73 | var where = options.where, | 70 | var where = options.where, |
---|
74 | who = options.who || null; | 71 | who = options.who || null; |
---|
… | | … | |
---|
230 | cleanUp: function() { | 230 | cleanUp: function() { |
---|
231 | //console.log('cleaning up'); | 231 | //console.log('cleaning up'); |
---|
232 | $(this.el).hide(); | 232 | $(this.el).hide(); |
---|
233 | if(swtr.appView.source === 'ocd') { | | if(swtr.appView.source === 'ocd') { |
---|
234 | $('#img-annotation-wrapper').hide(); | | $('#img-annotation-wrapper').hide(); |
---|
235 | $('#ocd-results').show(); | | $('#ocd-results').show(); |
---|
236 | } | | } |
---|
237 | } | 233 | } |
---|
238 | }); | 234 | }); |
---|
239 | | 235 | |
---|
… | | … | |
---|
299 | this.loadURL(input); | 299 | this.loadURL(input); |
---|
300 | } | 300 | } |
---|
301 | }, | 301 | }, |
---|
| | 302 | // load a URL for annotation (can be of image or html resource for now) |
---|
302 | loadURL: function(url, type) { | 303 | loadURL: function(url, type) { |
---|
303 | console.log('loadURL()'); | | console.log('loadURL()'); |
---|
| | 304 | //console.log('loadURL()'); | 304 | if(this.source !== 'ocd') { | 305 | if(this.source !== 'ocd') { |
---|
305 | $('#ocd-results').hide(); | 306 | $('#ocd-results').hide(); |
---|
306 | } | 307 | } |
---|
307 | $('#img-annotation-wrapper').show(); | 308 | $('#img-annotation-wrapper').show(); |
---|
308 | if(!url) { | | if(!url) { |
---|
| | 309 | if(!url || !url.match(/http/)) { | | | 310 | this.helpview.step(13); |
---|
309 | return false; | 311 | return false; |
---|
310 | } | 312 | } |
---|
311 | // if type is given explicitly; we load it as such. | 313 | // if type is given explicitly; we load it as such. |
---|
… | | … | |
---|
605 | var self = this; | 605 | var self = this; |
---|
606 | $('#img-annotation-wrapper').hide(); | 606 | $('#img-annotation-wrapper').hide(); |
---|
607 | $('#ocd-results').show(); | 607 | $('#ocd-results').show(); |
---|
608 | $('#ocd-results').append('Loading..'); | | $('#ocd-results').append('Loading..'); |
---|
| | 608 | $('#ocd-results').html('<h4 style="text-align: center;">Loading..</h4>'); | 609 | $.ajax({ | 609 | $.ajax({ |
---|
610 | type: 'GET', | 610 | type: 'GET', |
---|
611 | url: '/search/ocd', | 611 | url: '/search/ocd', |
---|
612 | data: {query: input}, | 612 | data: {query: input}, |
---|
613 | success: function(data) { | 613 | success: function(data) { |
---|
614 | self.ocdView = new OCDView({model: data.hits.hits}); | | self.ocdView = new OCDView({model: data.hits.hits}); |
---|
| | 614 | self.ocdView = new OCDView({ | | | 615 | query: input, |
---|
| | 616 | data: data, |
---|
| | 617 | model: data.hits.hits |
---|
| | 618 | }); |
---|
615 | } | 619 | } |
---|
616 | }); | 620 | }); |
---|
617 | } | 621 | } |
---|
618 | }); | 622 | }); |
---|
619 | | 623 | |
---|
620 | var OCDView = Backbone.View.extend({ | 624 | var OCDView = Backbone.View.extend({ |
---|
621 | el: $('#ocd-results'), | | el: $('#ocd-results'), |
---|
| | 625 | el: $('#ocd-view'), | 622 | events: { | 626 | events: { |
---|
623 | 'click .ocd-item a': 'onClickImg' | | 'click .ocd-item a': 'onClickImg' |
---|
| | 627 | 'click .ocd-item a': 'onImgClick', | | | 628 | 'click .pager li': 'onPagerClick' |
---|
624 | }, | 629 | }, |
---|
625 | initialize: function() { | | initialize: function() { |
---|
| | 630 | initialize: function(opts) { | | | 631 | this.data = opts.data || {}; |
---|
| | 632 | this.query = opts.query || ''; |
---|
| | 633 | this.size = 9; // num of items per page |
---|
| | 634 | this.page = 0; |
---|
626 | this.item_template = _.template($('#ocd-item-template').html()); | 635 | this.item_template = _.template($('#ocd-item-template').html()); |
---|
| | 636 | this.base_template = _.template($('#ocd-view-base-template').html()); |
---|
627 | this.render(); | 637 | this.render(); |
---|
628 | }, | 638 | }, |
---|
629 | render: function() { | 639 | render: function() { |
---|
630 | var $row_el; | 640 | var $row_el; |
---|
631 | this.$el.html(''); | 641 | this.$el.html(''); |
---|
| | 642 | if(!this.model.length) { |
---|
| | 643 | this.$el.html('No results could be found from your query.'); |
---|
| | 644 | return; |
---|
| | 645 | } |
---|
| | 646 | this.$el.html(this.base_template()); |
---|
| | 647 | var $el = $('#ocd-results'); |
---|
632 | _.each(this.model, function(item, idx) { | 648 | _.each(this.model, function(item, idx) { |
---|
633 | // put every 3 items in a row | 649 | // put every 3 items in a row |
---|
634 | if(idx % 3 === 0) { | 650 | if(idx % 3 === 0) { |
---|
635 | $row_el = $('<div class="row"></div>'); | 651 | $row_el = $('<div class="row"></div>'); |
---|
636 | this.$el.append($row_el); | | this.$el.append($row_el); |
---|
| | 652 | $el.append($row_el); | 637 | } | 653 | } |
---|
638 | $row_el.append(this.item_template({ | 654 | $row_el.append(this.item_template({ |
---|
639 | title: item._source.title, | 655 | title: item._source.title, |
---|
… | | … | |
---|
658 | })); | 658 | })); |
---|
659 | }, this); | 659 | }, this); |
---|
660 | this.resolveOCDURLs(); | 660 | this.resolveOCDURLs(); |
---|
| | 661 | this.appendTotal(); |
---|
661 | }, | 662 | }, |
---|
| | 663 | appendTotal: function() { |
---|
| | 664 | $('#ocd-total-results').html(this.data.hits.total + ' results found.'); |
---|
| | 665 | }, |
---|
662 | // resolve the OCD media URLs | 666 | // resolve the OCD media URLs |
---|
663 | resolveOCDURLs: function() { | 667 | resolveOCDURLs: function() { |
---|
664 | var self = this; | 668 | var self = this; |
---|
… | | … | |
---|
674 | }); | 674 | }); |
---|
675 | }); | 675 | }); |
---|
676 | }, | 676 | }, |
---|
677 | onClickImg: function(event) { | | onClickImg: function(event) { |
---|
| | 677 | rerender: function(data) { | | | 678 | this.data = data; |
---|
| | 679 | this.model = data.hits.hits; |
---|
| | 680 | this.render(); |
---|
| | 681 | }, |
---|
| | 682 | onPagerClick: function(event) { |
---|
678 | event.preventDefault(); | 683 | event.preventDefault(); |
---|
| | 684 | var elem = $(event.currentTarget); |
---|
| | 685 | var self = this; |
---|
| | 686 | if(elem.hasClass('next')) { |
---|
| | 687 | if((this.page + 1) * this.size >= this.data.hits.total) { |
---|
| | 688 | console.log('no next page to go to'); |
---|
| | 689 | return false; |
---|
| | 690 | } |
---|
| | 691 | console.log('clicked next'); |
---|
| | 692 | this.search({ |
---|
| | 693 | query: this.query, |
---|
| | 694 | from: (this.page + 1) * this.size |
---|
| | 695 | }, function(resp) { |
---|
| | 696 | console.log('reached next page'); |
---|
| | 697 | self.page = self.page + 1; |
---|
| | 698 | self.rerender(resp); |
---|
| | 699 | }); |
---|
| | 700 | } |
---|
| | 701 | else if (elem.hasClass('previous')) { |
---|
| | 702 | if(this.page <= 0) { |
---|
| | 703 | console.log('no prev page to go to'); |
---|
| | 704 | return false; |
---|
| | 705 | } |
---|
| | 706 | console.log('clicked prev'); |
---|
| | 707 | this.search({ |
---|
| | 708 | query: this.query, |
---|
| | 709 | from: (this.page - 1) * this.size |
---|
| | 710 | }, function(resp) { |
---|
| | 711 | console.log('reached prev page'); |
---|
| | 712 | self.page = self.page - 1; |
---|
| | 713 | self.rerender(resp); |
---|
| | 714 | }); |
---|
| | 715 | } |
---|
| | 716 | return false; |
---|
| | 717 | }, |
---|
| | 718 | onImgClick: function(event) { |
---|
| | 719 | event.preventDefault(); |
---|
679 | // TODO: init the image anno | 720 | // TODO: init the image anno |
---|
680 | var url = $(event.currentTarget).find('img').attr('src'); | 721 | var url = $(event.currentTarget).find('img').attr('src'); |
---|
681 | swtr.appView.loadURL(url, 'image'); | 722 | swtr.appView.loadURL(url, 'image'); |
---|
682 | return false; | 723 | return false; |
---|
| | 724 | }, |
---|
| | 725 | search: function(data, cb) { |
---|
| | 726 | swtr.appView.$overlay.show(); |
---|
| | 727 | var self = this; |
---|
| | 728 | $.ajax({ |
---|
| | 729 | type: 'GET', |
---|
| | 730 | url: '/search/ocd', |
---|
| | 731 | data: data, |
---|
| | 732 | success: function(resp) { |
---|
| | 733 | swtr.appView.$overlay.hide(); |
---|
| | 734 | cb(resp); |
---|
| | 735 | } |
---|
| | 736 | }); |
---|
683 | } | 737 | } |
---|
684 | }); | 738 | }); |
---|
685 | | 739 | |
---|
… | | … | |
---|
752 | switch (n) { | 752 | switch (n) { |
---|
753 | case 0 : text = 'Getting annotations..'; | 753 | case 0 : text = 'Getting annotations..'; |
---|
754 | break; | 754 | break; |
---|
755 | case 1: text = 'Enter the URL of an image or web page below, and start annotating!'; | | case 1: text = 'Enter the URL of an image or web page below, and start annotating!'; |
---|
| | 755 | case 1: text = 'Enter URL of an image or web page below, and start annotating!'; | 756 | break; | 756 | break; |
---|
757 | case 2: text = 'Annotate the image, or see other annotations'; | 757 | case 2: text = 'Annotate the image, or see other annotations'; |
---|
758 | break; | 758 | break; |
---|
… | | … | |
---|
775 | case 11: text = 'Search in <a href="http://www.opencultuurdata.nl/">Open Cuultur Data API</a>'; | 775 | case 11: text = 'Search in <a href="http://www.opencultuurdata.nl/">Open Cuultur Data API</a>'; |
---|
776 | break; | 776 | break; |
---|
777 | case 12: text = 'Analyzing the resource type..'; | 777 | case 12: text = 'Analyzing the resource type..'; |
---|
| | 778 | break; |
---|
| | 779 | case 13: text = 'This does not seem to be a URL. Please enter a valid URL.'; |
---|
778 | break; | 780 | break; |
---|
779 | } | 781 | } |
---|
780 | $(this.el).html(text); | 782 | $(this.el).html(text); |
---|