Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/rev/bf7095e67d38
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 00:09:12 2012
Кодировка: UTF-8
allpy: bf7095e67d38

allpy

changeset 46:bf7095e67d38

add class work-time
author boris <bnagaev@gmail.com>
date Wed, 15 Sep 2010 22:05:12 +0400
parents d0c6393ecc46
children d4296357acc1
files blocks3d-wt.pro files/js/.ajax.js files/js/.dollar.js files/js/countdown.js work-time.C work-time.h
diffstat 6 files changed, 532 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/blocks3d-wt.pro	Wed Sep 15 21:47:55 2010 +0400
     1.2 +++ b/blocks3d-wt.pro	Wed Sep 15 22:05:12 2010 +0400
     1.3 @@ -4,6 +4,7 @@
     1.4  SOURCES += salt.C
     1.5  SOURCES += blocks3d-wt.C
     1.6  SOURCES += blocks3d-wt-widget.C
     1.7 +SOURCES += work-time.C
     1.8  
     1.9  CONFIG += debug
    1.10  CONFIG += precompile_header
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/files/js/.ajax.js	Wed Sep 15 22:05:12 2010 +0400
     2.3 @@ -0,0 +1,393 @@
     2.4 +
     2.5 +
     2.6 +	//////////////////////////////////////////////
     2.7 +	//////////////////////////////////////////////
     2.8 +	////         AJAX functions
     2.9 +	//////////////////////////////////////////////
    2.10 +	//////////////////////////////////////////////
    2.11 +
    2.12 +
    2.13 +
    2.14 +/*
    2.15 +
    2.16 +usage:
    2.17 +
    2.18 +1. Execute this JavaScript file
    2.19 +
    2.20 +2. Main function is 
    2.21 + bajax(sURL, post_array, action, then)
    2.22 +
    2.23 +Arguments:
    2.24 +
    2.25 +    sURL - path to downloading file
    2.26 +        GET parameters should be written here
    2.27 +        example: "/php/info1.php?fu=123"
    2.28 +
    2.29 +    post_array - array with POST parameters and their values
    2.30 +    
    2.31 +        variant 1:
    2.32 +            example: ['fu','ttt', 'eee','rrr']
    2.33 +        variant 2:
    2.34 +            example: {'fu':'ttt', 'eee':'rrr'}
    2.35 +            
    2.36 +        In this example two parameters wiil be sent:
    2.37 +        fu = ttt, eee = rrr
    2.38 +
    2.39 +    action - a string with rules how to use downloaded text
    2.40 +        'return': function will be runned in not
    2.41 +            asynchronous type and return this text (not Asynchronous)
    2.42 +        
    2.43 +        'eval': downloaded text will 
    2.44 +            be runned as JavaScript code (Asynchronous)
    2.45 +        
    2.46 +        'eval_sync': same, but not Asynchronous
    2.47 +        
    2.48 +
    2.49 +        Other:
    2.50 +        downloaded text will be inserted in HTML element with id = action
    2.51 +        
    2.52 +        
    2.53 +
    2.54 +    then - this JavaScript code will be runned after using 
    2.55 +        downloaded text. It can be zero string. 
    2.56 +        It will not executed, if action = 'return'
    2.57 +
    2.58 +
    2.59 +-------------
    2.60 +
    2.61 +Other functions to work:
    2.62 +
    2.63 +setHTML(sURL, then, kuda)
    2.64 +    Set downloadedtext to element with id kuda and runns then
    2.65 +
    2.66 +myeval_post(sURL, then, what, value)
    2.67 +    Analog of myeval, but it adds POST parameter what with value value :)
    2.68 +
    2.69 +loadHTML(sURL)
    2.70 +    returns downloaded text
    2.71 +
    2.72 +
    2.73 +myeval(sURL, then)
    2.74 +    runns downloaded text and execute code from argument then
    2.75 +*/
    2.76 +
    2.77 +// Возвращает document.getElementById
    2.78 +function $(str)
    2.79 +{
    2.80 +    return document.getElementById(str);
    2.81 +}
    2.82 +
    2.83 +
    2.84 +
    2.85 +
    2.86 +
    2.87 +// показать надпись, что идет загрузка
    2.88 +function ajax_loading_show()
    2.89 +{
    2.90 +    if ($('ajax_loading'))
    2.91 +    {
    2.92 +        $('ajax_loading').style.visibility = '';
    2.93 +    }
    2.94 +}
    2.95 +
    2.96 +// спрятать надпись, что идет загрузка
    2.97 +function ajax_loading_hide()
    2.98 +{
    2.99 +    if ($('ajax_loading'))
   2.100 +    {
   2.101 +        $('ajax_loading').style.visibility = 'hidden';
   2.102 +    }
   2.103 +}
   2.104 +
   2.105 +ajax_loading_hide();
   2.106 +
   2.107 +
   2.108 +var bajax_killed = 0; // убит ли bajax
   2.109 +
   2.110 +// после выполнения этой функции bajax перестает работать
   2.111 +function bajax_kill()
   2.112 +{
   2.113 +    bajax_killed = 1;
   2.114 +    
   2.115 +    try
   2.116 +    {
   2.117 +        make_request_object = function(){return false;};
   2.118 +        bajax = function(){return false;};
   2.119 +    }
   2.120 +    catch(e)
   2.121 +    {
   2.122 +    }
   2.123 +}
   2.124 +
   2.125 +////////////////////////////////////////////////////////////
   2.126 +//     функция возвращает объект request
   2.127 +////////////////////////////////////////////////////////////
   2.128 +
   2.129 +function make_request_object()
   2.130 +{
   2.131 +    var i;
   2.132 +    var request = null;
   2.133 +
   2.134 +    var msxmlhttp =
   2.135 +    [
   2.136 +    //'Msxml2.XMLHTTP.5.0',
   2.137 +    //'Msxml2.XMLHTTP.4.0',
   2.138 +    //'Msxml2.XMLHTTP.3.0',
   2.139 +    'Msxml2.XMLHTTP',
   2.140 +    'Microsoft.XMLHTTP'
   2.141 +    ];
   2.142 +
   2.143 +    for (i = 0; i < msxmlhttp.length; i++)
   2.144 +    {
   2.145 +        try
   2.146 +        {
   2.147 +            request = new ActiveXObject(msxmlhttp[i]);
   2.148 +        }
   2.149 +        catch (e)
   2.150 +        {
   2.151 +            request = null;
   2.152 +        }
   2.153 +        
   2.154 +        if (request)
   2.155 +        {
   2.156 +            break;
   2.157 +        }
   2.158 +    }
   2.159 +
   2.160 +    if (request)
   2.161 +    {
   2.162 +        return request;
   2.163 +    }
   2.164 +
   2.165 +    try
   2.166 +    {
   2.167 +        request = new XMLHttpRequest();
   2.168 +    }
   2.169 +    catch (e)
   2.170 +    {
   2.171 +    }
   2.172 +
   2.173 +    return request;
   2.174 +}
   2.175 +
   2.176 +
   2.177 +
   2.178 +////////////////////////////////////////////////////////////
   2.179 +//             универсальная AJAX-функция
   2.180 +////////////////////////////////////////////////////////////
   2.181 +
   2.182 +function bajax(sURL, post_array, action, then)
   2.183 +{
   2.184 +    
   2.185 +    if (bajax_killed)
   2.186 +    {
   2.187 +        return '';
   2.188 +    }
   2.189 +    
   2.190 +    var i;
   2.191 +    var post = '';
   2.192 +    var request_type = 'GET';
   2.193 +    var sURL1 = sURL;
   2.194 +
   2.195 +    var request = make_request_object();
   2.196 +    var asynchronous = false;
   2.197 +
   2.198 +    if (!request)
   2.199 +    {
   2.200 +        return;
   2.201 +    }
   2.202 +
   2.203 +    if (sURL.split('?').length==1)
   2.204 +    {
   2.205 +        sURL1 += '?';
   2.206 +    }
   2.207 +    else
   2.208 +    {
   2.209 +        if (sURL[sURL.length - 1] != '&')
   2.210 +        {
   2.211 +            sURL1 += '&';
   2.212 +        }
   2.213 +    }
   2.214 +    
   2.215 +    sURL1 += 'ran='+Math.floor(Math.random() * 9999999);
   2.216 +
   2.217 +    if (action != 'return' && action != 'eval_sync')
   2.218 +    {
   2.219 +        asynchronous = true;
   2.220 +
   2.221 +        request.onreadystatechange = function()
   2.222 +        {
   2.223 +            if (request.readyState == 4)
   2.224 +            {
   2.225 +                //alert(sURL + "\n\n" + request.responseText);
   2.226 +                
   2.227 +                // hide 'loading...'
   2.228 +                ajax_loading_hide();
   2.229 +                
   2.230 +                try
   2.231 +                {
   2.232 +                    if (action == 'eval')
   2.233 +                    {
   2.234 +                        eval(request.responseText);
   2.235 +                    }
   2.236 +                    else
   2.237 +                    {
   2.238 +                        $(action).innerHTML = request.responseText;
   2.239 +                    }
   2.240 +                }
   2.241 +                catch (e)
   2.242 +                {
   2.243 +                }
   2.244 +
   2.245 +                if (then)
   2.246 +                {
   2.247 +                    try
   2.248 +                    {
   2.249 +                        eval(then);
   2.250 +                    }
   2.251 +                    catch(e)
   2.252 +                    {
   2.253 +                    }
   2.254 +                }
   2.255 +            }
   2.256 +        };
   2.257 +    }
   2.258 +
   2.259 +
   2.260 +
   2.261 +    // если есть POST-данные, присобачим их
   2.262 +    if (post_array)
   2.263 +    {
   2.264 +        var p, v;
   2.265 +        request_type = 'POST';
   2.266 +        if (post_array.length)
   2.267 +        {
   2.268 +            for (i = 0; i < post_array.length / 2; i++)
   2.269 +            {
   2.270 +                p = post_array[2 * i];
   2.271 +                v = post_array[2 * i + 1];
   2.272 +                if (!v)
   2.273 +                {
   2.274 +                    continue;
   2.275 +                }
   2.276 +                post += p + "=" + encodeURIComponent(v + '') + '&';
   2.277 +            }
   2.278 +        }
   2.279 +        else
   2.280 +        {
   2.281 +            for (p in post_array)
   2.282 +            {
   2.283 +                v = post_array[p];
   2.284 +                if (!v)
   2.285 +                {
   2.286 +                    continue;
   2.287 +                }
   2.288 +                post += p + "=" + encodeURIComponent(v + '') + '&';
   2.289 +            }
   2.290 +        }
   2.291 +    }
   2.292 +
   2.293 +    // loading...
   2.294 +    ajax_loading_show();
   2.295 +
   2.296 +    // делаем запрос
   2.297 +    request.open(request_type, sURL1, asynchronous);
   2.298 +
   2.299 +    if (post_array)
   2.300 +    {
   2.301 +        //alert(post);
   2.302 +        
   2.303 +        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   2.304 +        request.setRequestHeader("Content-Length", post.length);
   2.305 +        request.send(post);
   2.306 +    }
   2.307 +    else
   2.308 +    {
   2.309 +        request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
   2.310 +        request.send(null);
   2.311 +    }
   2.312 +
   2.313 +    if (action == 'return')
   2.314 +    {
   2.315 +        // hide 'loading...'
   2.316 +        ajax_loading_hide();
   2.317 +        return request.responseText;
   2.318 +    }
   2.319 +    
   2.320 +    if (action == 'eval_sync')
   2.321 +    {
   2.322 +        // hide 'loading...'
   2.323 +        ajax_loading_hide();
   2.324 +          
   2.325 +        try
   2.326 +        {
   2.327 +            eval(request.responseText);
   2.328 +        }
   2.329 +        catch (e)
   2.330 +        {
   2.331 +        }
   2.332 +        
   2.333 +        if (then)
   2.334 +        {
   2.335 +            try
   2.336 +            {
   2.337 +                eval(then);
   2.338 +            }
   2.339 +            catch(e)
   2.340 +            {
   2.341 +            }
   2.342 +        }
   2.343 +    }    
   2.344 +}
   2.345 +
   2.346 +
   2.347 +function gbajax(sURL, GET, POST, action, then)
   2.348 +{
   2.349 +    if (sURL.split('?').length==1)
   2.350 +    {
   2.351 +        sURL += '?';
   2.352 +    }
   2.353 +    else
   2.354 +    {
   2.355 +        sURL += '&';
   2.356 +    }
   2.357 +
   2.358 +    for (k in GET)
   2.359 +    {
   2.360 +        sURL += encodeURIComponent(k + '') + '=' + encodeURIComponent(GET[k] + '') + '&';
   2.361 +    }
   2.362 +    return bajax(sURL, POST, action, then);
   2.363 +}
   2.364 +
   2.365 +
   2.366 +////////////////////////////////////////////////////////////
   2.367 +// функция для выполнения содержимого файла из Интернет
   2.368 +////////////////////////////////////////////////////////////
   2.369 +function myeval(sURL, then)
   2.370 +{
   2.371 +    bajax(sURL, false, 'eval', then);
   2.372 +}
   2.373 +
   2.374 +////////////////////////////////////////////////////////////////
   2.375 +// функция присваивает элементу kuda значение файла из Интернет
   2.376 +////////////////////////////////////////////////////////////////
   2.377 +function setHTML(sURL, then, kuda)
   2.378 +{
   2.379 +    bajax(sURL, false, kuda, then);
   2.380 +}
   2.381 +
   2.382 +////////////////////////////////////////////////////////////////
   2.383 +// функция присваивает элементу kuda значение файла из Интернет
   2.384 +////////////////////////////////////////////////////////////////
   2.385 +
   2.386 +function loadHTML(sURL)
   2.387 +{
   2.388 +    return bajax(sURL, false, 'return', false);
   2.389 +}
   2.390 +
   2.391 +// функция для получения содержимого файла из Интернет
   2.392 +function myeval_post(sURL, then, what, value)
   2.393 +{
   2.394 +    bajax(sURL, [what, value], 'eval', then);
   2.395 +}
   2.396 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/files/js/.dollar.js	Wed Sep 15 22:05:12 2010 +0400
     3.3 @@ -0,0 +1,11 @@
     3.4 +function dollar(str)
     3.5 +{
     3.6 +    if (document.getElementById(str))
     3.7 +    {
     3.8 +        return document.getElementById(str);
     3.9 +    }
    3.10 +    else
    3.11 +    {
    3.12 +        return {};
    3.13 +    }
    3.14 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/files/js/countdown.js	Wed Sep 15 22:05:12 2010 +0400
     4.3 @@ -0,0 +1,73 @@
     4.4 +
     4.5 +function timer_div_id(id)
     4.6 +{
     4.7 +    return '#timer_' + id;
     4.8 +}
     4.9 +function timer_div(id)
    4.10 +{
    4.11 +    return $(timer_div_id(id));
    4.12 +}
    4.13 +
    4.14 +//~ var started = realnow();
    4.15 +
    4.16 +var limits = {}; // index: product id
    4.17 +function countdown_show(id)
    4.18 +{
    4.19 +    try
    4.20 +    {
    4.21 +        var delta = realnow() - limits[id][1];
    4.22 +        var limit = limits[id][0] - delta;
    4.23 +        if (limit < 0)
    4.24 +        {
    4.25 +            timer_div(id).html('...');
    4.26 +        }
    4.27 +        else
    4.28 +        {
    4.29 +            timer_div(id).html(cool_time(limit));
    4.30 +        }
    4.31 +    }
    4.32 +    catch(e)
    4.33 +    {
    4.34 +    }
    4.35 +}
    4.36 +
    4.37 +function countdown_show_all()
    4.38 +{
    4.39 +    var id;
    4.40 +    for (id in limits)
    4.41 +    {
    4.42 +        countdown_show(id);
    4.43 +    }
    4.44 +}
    4.45 +
    4.46 +var timers = {};
    4.47 +var prices_end = {};
    4.48 +function set_countdown(product_id, now_left, price_end)
    4.49 +{
    4.50 +    
    4.51 +    if (prices_end[product_id] && prices_end[product_id] < price_end) {
    4.52 +        $(timer_div_id(product_id)).css({opacity: 0}).animate({ opacity: 1}, 1000, "linear")
    4.53 +    }
    4.54 +    prices_end[product_id] = price_end;
    4.55 +    
    4.56 +    limits[product_id] = [now_left, realnow()];
    4.57 +    
    4.58 +    if (now_left && now_left + 5 > 0)
    4.59 +    {
    4.60 +        try
    4.61 +        {
    4.62 +            if (timers[product_id])
    4.63 +            {
    4.64 +                clearTimeout(timers[product_id]);
    4.65 +            }
    4.66 +            timers[product_id] = setTimeout('ajax_refresh(false)', (now_left + 0.1) * 1000);
    4.67 +        }
    4.68 +        catch(e)
    4.69 +        {
    4.70 +        }
    4.71 +    }
    4.72 +}
    4.73 +
    4.74 +
    4.75 +setInterval('countdown_show_all()', 100);
    4.76 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/work-time.C	Wed Sep 15 22:05:12 2010 +0400
     5.3 @@ -0,0 +1,30 @@
     5.4 +
     5.5 +#include "work-time.h"
     5.6 +#include <boost/lexical_cast.hpp>
     5.7 +#include <Wt/WApplication>
     5.8 +
     5.9 +
    5.10 +Worktime::Worktime(Wt::WContainerWidget *parent) : 
    5.11 +	WText(parent)
    5.12 +{
    5.13 +	wApp->require("/files/js/jquery.countdown.pack.js");
    5.14 +	start_at = now();	
    5.15 +	render();
    5.16 +}
    5.17 +
    5.18 +std::string Worktime::int2time(posix::time_duration secs)
    5.19 +{
    5.20 +	posix::seconds secs1(secs.total_seconds());
    5.21 +	return to_simple_string(secs1);
    5.22 +}
    5.23 +
    5.24 +void Worktime::render()
    5.25 +{
    5.26 +	setText(int2time(spent()));
    5.27 +	
    5.28 +	doJavaScript("$('#" + id() + "').countdown({"
    5.29 +	"since: -" + boost::lexical_cast<std::string>(
    5.30 +		spent().total_milliseconds()) + " / 1000,"
    5.31 +	"compact: true"
    5.32 +	"});");
    5.33 +};
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/work-time.h	Wed Sep 15 22:05:12 2010 +0400
     6.3 @@ -0,0 +1,24 @@
     6.4 +
     6.5 +#ifndef WORKTIME_H_
     6.6 +#define WORKTIME_H_
     6.7 +
     6.8 +#include <Wt/WContainerWidget>
     6.9 +#include <Wt/WText>
    6.10 +
    6.11 +#include <boost/date_time/posix_time/posix_time.hpp>
    6.12 +namespace posix = boost::posix_time;
    6.13 +
    6.14 +class Worktime : public Wt::WText
    6.15 +{
    6.16 +public:
    6.17 +	Worktime(Wt::WContainerWidget *parent=0);
    6.18 +	void render();
    6.19 +	inline posix::ptime now() { return posix::microsec_clock::universal_time(); }
    6.20 +	inline posix::time_duration spent() { return now() - start_at; }
    6.21 +private:
    6.22 +	posix::ptime start_at;
    6.23 +	std::string int2time(posix::time_duration secs);
    6.24 +};
    6.25 +
    6.26 +
    6.27 +#endif // WORKTIME_H_