From d2bffc0117b196afbb2c831e4b32a7e755482b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27czesiek=27=20Czy=C5=BCewski?= Date: Fri, 22 Mar 2013 22:01:12 +0100 Subject: [PATCH] JS and srv.py refactoring --- srv.py | 53 +++++++++++++++++++++++++-------------------- static/js/penis.js | 42 +++++++++++++++++++++++++---------- templates/home.html | 6 ++--- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/srv.py b/srv.py index 874dfb0..e102fc3 100644 --- a/srv.py +++ b/srv.py @@ -15,33 +15,38 @@ app.jinja_env.filters['jsonify'] = jsonify @app.route('///') @app.route('/') def home(first_id=None, second_id=None, third_id=None): - ''' - print len(array) - while first_id == None: - rand_int = randint(0, len(array)-1) - value = array[rand_int] - if len(value) > 0: - first_id = rand_int - while second_id == None: - rand_int = randint(0, len(array)-1) - value = array[rand_int] - if len(value) > 0: - second_id = rand_int - ''' - def get_words(words): - ret = [] - for word_id in words: - word = array[word_id] if word_id != None and word_id < len(array) else random.choice(array) - ret.append(word.decode('utf-8')) - - return ret - - words = get_words([first_id, second_id, third_id]) - return render_template('home.html', words=words) + return render_template('home.html') @app.route('/api/words') def api_words(): - return json.dumps(['frirst', '2nd', 'th33rd']) + # NOTE: below is old code from route('/') up for a review + # + #''' + # print len(array) + #while first_id == None: + # rand_int = randint(0, len(array)-1) + # value = array[rand_int] + # if len(value) > 0: + # first_id = rand_int + #while second_id == None: + # rand_int = randint(0, len(array)-1) + # value = array[rand_int] + # if len(value) > 0: + # second_id = rand_int + #''' + # + #def get_words(words): + # ret = [] + # for word_id in words: + # word = array[word_id] if word_id != None and word_id < len(array) else random.choice(array) + # ret.append(word.decode('utf-8')) + # return ret + + word1 = random.choice(array); + word2 = random.choice(array); + word3 = random.choice(array); + + return json.dumps([word1, word2, word3]) @app.route('/api/save', methods=['POST']) def api_save(): diff --git a/static/js/penis.js b/static/js/penis.js index 018fc44..658d314 100644 --- a/static/js/penis.js +++ b/static/js/penis.js @@ -1,16 +1,34 @@ -$(function() { - $.getJSON('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&imgsz=medium|large|xlarge|xxlarge&rsz=8&as_rights=(cc_publicdomain|cc_attribute|cc_sharealike|cc_nonderived).-(cc_noncommertial)&as_filetype=jpg|png|gif&safe=moderate&callback=?', {'q':words.join(' || ')}, function(data) { - results = data.responseData.results; - //for(var i in results) - { - var i = 0; // hardcore +function reload() { + $.getJSON('/api/words', function(words) { + $.getJSON('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&imgsz=medium|large|xlarge|xxlarge&rsz=8&as_rights=(cc_publicdomain|cc_attribute|cc_sharealike|cc_nonderived).-(cc_noncommertial)&as_filetype=jpg|png|gif&safe=moderate&callback=?', {'q': words.join(' || ')}, function(data) { + results = data.responseData.results; + + // replace words + $('#word1').text(words[0]); + $('#word2').text(words[1]); + + // TODO: insert it w/out jQuery, would be faster $img = $(''); - $img.attr('src', results[i].url); - $img.appendTo($('#image_wrapper')); - return; - } - }) + $img.attr('src', results[0].url); + $('#image_wrapper').html($img); + }); + }); +} + +function prefetch_images() { + // check how much images are there + var qty_max = 3; + var qty = qty_max - 1; // testing + + // fetch and add images + for(var i = 0; i < qty; i++) { + } +} + +$(function() { + reload(); + $(document).click(function(e) { - window.location.reload() + reload(); }); }); diff --git a/templates/home.html b/templates/home.html index f5e4158..3b957c0 100644 --- a/templates/home.html +++ b/templates/home.html @@ -52,10 +52,10 @@ - - {{ words[0]|e }} +
- {{ words[1]|e }}
+ +
Copyright © RiddleKiller Ltd.
Features Kelson Sans font by FontFabric.com/