Commits

Maciej Wiśniowski committed b2c1626

changed js structure - allow multiple slots of same type

Comments (0)

Files changed (4)

dj_banner/models.py

     def get_banner_set(self):
         today = date.today()
         qs = self.bannerweight_set.filter(weight__gt=0)
-        qs = qs.filter(models.Q(display_from__gte=today)|models.Q(display_from__isnull=True),
-                    models.Q(display_from__lte=today)|models.Q(display_from__isnull=True))
+        qs = qs.filter(models.Q(display_from__lte=today)|models.Q(display_from__isnull=True),
+                    models.Q(display_to__gte=today)|models.Q(display_to__isnull=True))
         return qs
 
     def get_banner_count(self):

dj_banner/static/dj_banner.js

-var banner_slots = [];
+var banner_slots = {};
 
-function register_slot(name){
-    if (banner_slots.indexOf(name) == -1){
-        banner_slots.push(name);
+function setup_slot(slot){
+    var slug = slot.getAttribute('data-slug');
+    var next = slot.getAttribute('data-next')
+
+    // unique idnetifier for this slot - it's possible to have multiple slots with same slug (eg. if using tabs)
+    banner_slots[slug] = banner_slots[slug] ? banner_slots[slug] + 1 : 1
+    var uid = 'banner_' + slug + '_' + banner_slots[slug];
+    slot.setAttribute('id', uid)
+
+    function load_next(resp, txt, xhr){
+        /* load next banner into slot after banner spefific interval*/
+        // time
+        var tm = $(".display_time_" + slug, slot).html();
+        var itm = parseInt(tm) * 1000;
+        if (isNaN(itm)) itm=20000;
+        setTimeout(load_banner, itm);
     }
+
+    function load_banner(){
+        /* load banner and initialize loading of next one into this slot */
+        $('#' + uid).load(next, load_next)
+    }
+    load_banner();
 }
 
 jQuery(window).load(function(){
     // find all banners
     $('.banner_slot').each(function(){
-        var slug = $(this).id.split('_')[2];
-        register_slot(slug);
+        setup_slot(this)
     })
-    var l = banner_slots.length;
-    for (var i=0; i<l; i++){
-        var slug = banner_slots[i];
-        load_banner(slug);
-    }
-});
 
+    $(".banner_slot").delegate("div.banner_clicker a", "click", function(){
+        var parent = $(this).parents('div.banner_clicker')[0];
 
-function load_banner(slug){
-    // next url
-    var next = jQuery("#banner_next_" + slug);
-    var next_href = next.attr('href');
-
-    // time
-    var tm = $("#display_time_" + slug).html();
-    var itm = parseInt(tm) * 1000;
-    if (isNaN(itm)) itm=20000;
-
-    jQuery("#banner_slot_" + slug).load(next_href, function(resp, txt, xhr){setTimeout("load_banner('" + slug + "')", itm);});
-}
+        var base_url = parent.getAttribute('data-baseurl');
+        var is_new_window = 1 * parent.getAttribute('data-new_window');
+console.log('baseurl: ' + base_url)
+        var url = base_url + '?destination=' + $(this).attr('href');
+        $(this).attr('href', url);
+        if (is_new_window){
+            $(this).attr('target', '_blank');
+        }
+        return false;
+    });
+});

dj_banner/templates/dj_banner/banner_tag.html

 {% if active %}
-<script type="text/javascript">
-    jQuery(window).load(function(){
-       load_banner_{{slug}}();
-    });
-
-    function load_next_{{slug}}(responseText, textStatus, XMLHttpRequest){
-        var tm = $("#display_time_{{slug}}").html();
-        var itm = parseInt(tm) * 1000;
-        if (isNaN(itm)) itm=20000;
-        setTimeout("load_banner_{{slug}}()", itm);
-    }
-
-    function load_banner_{{slug}}(){
-        var next = jQuery("#banner_next_{{slug}}");
-        if(next == null){
-            next_href = "{% url dj_banner.views.banner slug=slug %}"
-        }else{
-            var next_href = next.attr('href');
-            if(!next_href) {
-                next_href = "{% url dj_banner.views.banner slug=slug %}"
-            }
-        }
-        jQuery("#banner_slot_{{slug}}").load(next_href, load_next_{{slug}});
-    }
-</script>
-<div id="banner_slot_{{slug}}" class="banner_slot" style="width:{{width}}px;height:{{height}}px;">
-</div>
+  <div class="banner_slot" data-slug="{{ slug }}" data-next="{% url dj_banner.views.banner slug=slug %}"
+     style="width:{{width}}px;height:{{height}}px;">
+  </div>
 {% endif %}
 {% if message %}
-<p>{{message}}</p>
+  <p>{{message}}</p>
 {% endif %}

dj_banner/views.py

 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.core.urlresolvers import reverse
+from django.views.decorators.cache import never_cache
 
 from dj_banner.models import Baner, BannerSet
 
 def test_page(request):
     return render_to_response('test.html')
 
-
+@never_cache
 def banner(request, slug, current_banner_id=None):
     try:
         b_set = BannerSet.objects.get(slug=slug)