Plugin Directory

Changeset 768400


Ignore:
Timestamp:
09/07/2013 09:14:19 PM (13 years ago)
Author:
marcelioleal
Message:

refactoring js and add expression in js

Location:
siteapps/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • siteapps/trunk/classes/SiteAppsPages.php

    r768326 r768400  
    8080        global $current_user;
    8181        get_currentuserinfo();
    82 
     82       
    8383        $saId               = $options['id'];
    8484        $userKey            = $options['user_key'];
  • siteapps/trunk/classes/SiteAppsPlugin.php

    r759664 r768400  
    121121    function prepare_widget($buffer)
    122122    {
     123        //hide widgets preconfigured
    123124        $widget_id = $this->current_widget_id;
    124125
     
    169170            }
    170171        }
     172       
     173       
     174        $widgetsToShow = json_encode($show_list);
     175        $widgetsToHide = json_encode($hide_list);
     176       
    171177        require_once(SITEAPPS_VIEW_DIR . 'widgets/onload.php');
    172178    }
  • siteapps/trunk/views/admin/settings.php

    r768326 r768400  
    126126               
    127127                <div class="inputitem" style="position: relative; top: 20px">
    128                     <span><input type="checkbox" name="sa_enable_smart_widgets" <?php print $smartWidgetCheck; ?>> Enable Segments on Widgets: <a href="" class="question" title="You can show to different user segments your widgets.">?</a></span>
     128                    <span><input type="checkbox" name="sa_enable_smart_widgets" <?php print $smartWidgetCheck; ?> value="1"> Enable Segments on Widgets: <a href="" class="question" title="You can show to different user segments your widgets.">?</a></span>
    129129                </div>
    130130               
  • siteapps/trunk/views/widgets/onload.php

    r696744 r768400  
    11<script type="text/javascript" language="Javascript">
    2     var wpbtb_hide_list = new Array();
    3     <?php foreach ($hide_list as $bucket => $list): ?>
    4         <?php if (count($list) > 0): ?>
    5             wpbtb_hide_list["<?php echo $bucket ?>"] = ["<?php echo implode('","', $list) ?>"];
    6         <?php else: ?>
    7             wpbtb_hide_list["<?php echo $bucket ?>"] = new Array();
    8         <?php endif; ?>
    9     <?php endforeach; ?>
    10 
    11     var wpbtb_show_list = new Array();
    12     <?php foreach ($show_list as $bucket => $list): ?>
    13         <?php if (count($list) > 0): ?>
    14             wpbtb_show_list["<?php echo $bucket ?>"] = ["<?php echo implode('","', $list) ?>"];
    15         <?php else: ?>
    16             wpbtb_show_list["<?php echo $bucket ?>"] = new Array();
    17         <?php endif; ?>
    18     <?php endforeach; ?>
    19 
     2   
     3    //Function to debug SiteApps Widget
    204    function consoleWrite(msg, value)
    215    {
    22         <?php
    23         if(isset($debug) && $debug) :
    24         ?>
    25             try {
    26                console.log(msg, value);
    27             } catch(e) {
    28             }
    29         <?php
    30         endif;
    31         ?>
     6<?php if(isset($debug) && $debug) : ?>
     7        try {
     8           console.log(msg, value);
     9        } catch(e) {
     10        }
     11<?php endif; ?>
     12    }
     13       
     14    function toArray(obj) {
     15        var arr = [];
     16        for (var key in obj) {
     17            arr[key] = obj[key];
     18        }
     19        consoleWrite('config: ' , arr);
     20        return arr;
     21    }       
     22   
     23    function mustRunWidget (segments, expression) {
     24        // expression must be a logical expression of bucket values. eg: (A or B) and C
     25        var userBuckets = segments
     26          , expressionForEval = expression.replace(/\s+and\s+/ig, '&&').replace(/\s+or\s+/ig, '||')
     27          , regexString = []
     28          , regex = null
     29          , i = 0;
     30
     31        for(i in userBuckets) {
     32            regexString.push(userBuckets);
     33        }
     34        regexString = regexString.join('|');
     35        regex = new RegExp(regexString, 'ig');
     36
     37        expressionForEval = expressionForEval.replace(regex, 'true');
     38        try {
     39            eval(expressionForEval);
     40        } catch (err) {
     41            return false;
     42        }
     43
     44        return true;
    3245    }
    3346   
    34     function applyingSegmentsConf(user_buckets) {
    35         consoleWrite('user_buckets: ', user_buckets);
    36         for (var b in user_buckets) {
    37             consoleWrite('bucket: ', user_buckets[b]);
    38             if (user_buckets[b] in wpbtb_hide_list) {
    39                 var current_bucket  = user_buckets[b];
    40                 var widgets_to_hide = wpbtb_hide_list[current_bucket];
    41                 consoleWrite('widgets_to_hide: ', widgets_to_hide);
    42                 for (var i in widgets_to_hide) {
    43                     var w_id = widgets_to_hide[i];
    44                     var e;
    45                     while (e = document.getElementById(w_id)) {
    46                         e.style.display = "none";
    47                         e.id = w_id + '-hidden';
    48                     }
    49                 }
     47    var widgetsToShow = toArray(<?php print $widgetsToShow; ?>),
     48    widgetsToHide = toArray(<?php print $widgetsToHide; ?>),
     49    expressionsToShow = toArray({"recent-posts-2": "default_new_visitor", "recent-comments-2": "mobile or default_new_visitor"});
     50   
     51
     52    function changeDisplay(widgets, display) {
     53        consoleWrite('widgets display to ' + display + ': ' , widgets);
     54        for (var i in widgets) {
     55            var widgetId = widgets[i];
     56            var e;
     57            while (e = document.getElementById(widgetId)) {
     58                e.style.display = display;
     59                e.id = widgetId + '-modified';               
    5060            }
    51             if (user_buckets[b] in wpbtb_show_list) {
    52                 var current_bucket = user_buckets[b];
    53                 var widgets_to_show = wpbtb_show_list[current_bucket];
    54                 consoleWrite('widgets_to_show: ', widgets_to_show);
    55                 for (var i in widgets_to_show) {
    56                     var w_id = widgets_to_show[i];
    57                     var e;
    58                     while (e = document.getElementById(w_id)) {
    59                         e.style.display = "block";
    60                         e.id = w_id + '-shown';
    61                     }
    62                 }
     61        }
     62    }
     63
     64    function applyingExpressions(segments) {
     65        for (var ex in expressionsToShow) {
     66            var expression = expressionsToShow[ex];
     67            consoleWrite('expression: ', expression);
     68            consoleWrite('widget: ', ex);
     69            var show = mustRunWidget(segments, expression);
     70            consoleWrite('show', show);
     71            if (show) {
     72                changeDisplay([ex], "block");
    6373            }
    6474        }
    6575    }
    6676   
    67     function getBuckets(times) {
    68         consoleWrite('times: ', times);
    69         if(times < 100) {
    70             setTimeout(function (times){
    71                 try {
    72                     user_buckets = $SA.getAllUserBuckets();
    73                     if(user_buckets.length == 0) {
    74                         getBuckets(times + 1);
    75                     }
    76                     applyingSegmentsConf(user_buckets);
    77                 } catch (e) {
    78                   getBuckets(times + 1);
    79                 }
    80             }, 200, times);
     77
     78    function applyingSegmentsConf(segments) {
     79        for (var b in segments) {
     80            var segment = segments[b];
     81            consoleWrite('segment: ', segment);
     82            if (segment in widgetsToHide) {
     83                changeDisplay(widgetsToHide[segment], 'none');
     84            }
     85            if (segment in widgetsToShow) {
     86                changeDisplay(widgetsToShow[segment], 'block');
     87            }
    8188        }
    8289    }
    8390   
    84     getBuckets(1, wpbtb_hide_list, wpbtb_show_list);
     91    function getBuckets () {
     92        var times = 50;
     93        var interval = setInterval(function () {
     94            if (times === 0) {
     95                clearInterval(interval);
     96                return;
     97            }   
     98            consoleWrite('times: ', times);
     99            try{
     100                var segments = $SA.getAllUserBuckets();
     101                if(segments.length === 0) {
     102                    return;
     103                }
     104                clearInterval(interval);
     105                consoleWrite('segments: ', segments);
     106                applyingSegmentsConf(segments);
     107                applyingExpressions(segments);
     108            } catch(e) {
     109                times -= 1;   
     110            }
     111        }, 200);
     112    }
     113   
     114    getBuckets();
    85115</script>
Note: See TracChangeset for help on using the changeset viewer.