Guest User

Untitled

a guest
Aug 22nd, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var MsoClient = MsoClient ? MsoClient :
  2. {
  3. };
  4. MsoClient.Questionnaire = MsoClient.Questionnaire ? MsoClient.Questionnaire :
  5. {
  6. };
  7.  
  8. /**
  9.  * Constructor
  10.  * @author Alexander Thiel
  11.  * @author Christian Baer
  12.  * @since 2013-03-12
  13.  */
  14. MsoClient.Questionnaire.Manager = function(wrapperElement, pageUrl, syncUrl, messageManager)
  15. {
  16.     this.wrapper = $(wrapperElement);
  17.     this.messageManager = messageManager;
  18.     this.pageUrl = pageUrl;
  19.     this.syncUrl = syncUrl;
  20. };
  21.  
  22. MsoClient.Questionnaire.Manager.prototype =
  23. {
  24.     wrapper: null,
  25.     messagesWrapper: null,
  26.     syncUrl: '',
  27.     pageUrl: '',
  28.     sectionSlug: null,
  29.     pageIndex: null
  30. };
  31.  
  32. /**
  33.  *
  34.  * @param string sectionSlug
  35.  * @param integer pageIndex Zero-indexed!
  36.  * @return void
  37.  * @author Alexander Thiel
  38.  * @author Christian Baer
  39.  * @since 2013-03-12
  40.  */
  41. MsoClient.Questionnaire.Manager.prototype.init = function(sectionSlug, pageIndex)
  42. {
  43.     this.sectionSlug = sectionSlug;
  44.     this.pageIndex = parseInt(pageIndex);
  45.     this.ajaxifyPageLinks();
  46.  
  47.     this.messageManager.init();
  48.     MsoClient.log('Message Manager initialized');
  49.  
  50.     this.pageManager = new MsoClient.Questionnaire.PageManager(this.buildUrl(this.syncUrl, sectionSlug, pageIndex + 1), this.wrapper.find('.page'), this.messageManager);
  51.     this.pageManager.init();
  52.     var m = this;
  53.     $(window).on('popstate', function(event)
  54.     {
  55.         if (event.originalEvent.state)
  56.             m.gotoPage(event.originalEvent.state.sectionSlug, event.originalEvent.state.pageIndex, true);
  57.     });
  58.  
  59.     if ( typeof (history.replaceState) !== 'undefined')
  60.         history.replaceState(
  61.         {
  62.             sectionSlug: sectionSlug,
  63.             pageIndex: m.pageIndex + 1
  64.         }, document.title, this.buildUrl(this.pageUrl, sectionSlug, m.pageIndex + 1));
  65.     MsoClient.log('Questionnaire Manager initialized');
  66.  
  67.     this.wrapper.find('.expand-button').on('click', function()
  68.     {
  69.         m.expandMessageBar();
  70.     });
  71.     $(window).on('scroll', function()
  72.     {
  73.         m.handleScroll();
  74.     });
  75. };
  76. MsoClient.Questionnaire.Manager.prototype.handleScroll = function()
  77. {
  78.     var elements = $('.outer-wrapper.header .badge-links, .outer-wrapper.content .content-wrapper .questionnaire .main .horizontal-pipe');
  79.  
  80.     if ($(window).scrollTop() > 78)
  81.         elements.css(
  82.         {
  83.             position: 'fixed',
  84.             top: 0
  85.         });
  86.     else
  87.         elements.css(
  88.         {
  89.             position: 'absolute',
  90.             top: '78px'
  91.         });
  92. };
  93.  
  94. /**
  95.  * Expands and collapses the message bar
  96.  * @return string
  97.  * @since 2013-03-18
  98.  * @author Alexander Thiel
  99.  */
  100. MsoClient.Questionnaire.Manager.prototype.expandMessageBar = function()
  101. {
  102.     var threadWidth = 267;
  103.     var distance = 220;
  104.     var pageWrapper = this.wrapper.find('.page');
  105.     var bar = this.wrapper.find('.side-bar');
  106.     if (bar.is(':animated'))
  107.         return;
  108.     if (bar.hasClass('expanded'))
  109.     {
  110.         bar.animate(
  111.         {
  112.             width: '-=' + distance + 'px'
  113.         },
  114.         {
  115.             duration: 1000,
  116.             easing: 'easeInOutCirc',
  117.             complete: function()
  118.             {
  119.                 bar.removeClass('expanded');
  120.                 pageWrapper.removeClass('narrow');
  121.             }
  122.         });
  123.         bar.find('h4').animate(
  124.         {
  125.             width: threadWidth + 'px'
  126.         }, 1000, 'easeInOutCirc');
  127.     } else
  128.     {
  129.         bar.animate(
  130.         {
  131.             width: '+=' + distance + 'px'
  132.         },
  133.         {
  134.             duration: 1000,
  135.             easing: 'easeInOutCirc',
  136.             complete: function()
  137.             {
  138.                 bar.addClass('expanded');
  139.                 pageWrapper.addClass('narrow');
  140.             }
  141.         });
  142.         bar.find('h4').animate(
  143.         {
  144.             width: threadWidth + distance + 'px'
  145.         }, 1000, 'easeInOutCirc');
  146.     }
  147. }
  148. /**
  149.  * Build url to perform ajax requests
  150.  * @param string sectionSlug
  151.  * @param integer pageIndex 1-indexed
  152.  * @return string
  153.  * @since 2013-03-18
  154.  * @author Alexander Thiel
  155.  */
  156. MsoClient.Questionnaire.Manager.prototype.buildUrl = function(url, sectionSlug, pageIndex)
  157. {
  158.     //Map everything except integers greater or equal 1 to a 1
  159.     pageIndex = Math.max(parseInt(pageIndex), 1);
  160.     return url.replace(/%sectionSlug%/, sectionSlug).replace(/%pageIndex%/, pageIndex).replace(/\/+$/, '');
  161. };
  162.  
  163. /**
  164.  * Makes links to question-group-pages use ajax
  165.  * Call to {gotoPage()} not deferred to to allow location changes in case of error
  166.  * Note: before 2013-07-05 m.gotoPage was called with a completeCallback that altered the link's css-classes. Removed becaus of reproducible Firefox crashes.
  167.  * @return void
  168.  * @author Alexander Thiel
  169.  * @since 2013-03-12
  170.  */
  171. MsoClient.Questionnaire.Manager.prototype.ajaxifyPageLinks = function()
  172. {
  173.     var m = this;
  174.     this.wrapper.find('a.page-link').on('click', function()
  175.     {
  176.         var link = $(this);
  177.         var matches = link.attr('href').match(/^.*treatment_plan\/form\/([a-zA-Z0-9\-_]+)(\/?)([0-9]*)\/?$/);
  178.         m.gotoPage(matches[1], matches.length > 3 ? matches[3] : 1, false);
  179.         return false;
  180.     });
  181. };
  182.  
  183. /**
  184.  * Condenses textboxes and textareas, expands them on click
  185.  * @return void
  186.  * @author Denis Nizetic
  187.  * @since 2013-05-03
  188.  */
  189. MsoClient.Questionnaire.Manager.prototype.attachExpansionAnimation = function()
  190. {
  191.     $('input.answer[type="text"][placeholder!="Number"]').focus(function()
  192.     {
  193.         /*to make this flexible, I'm storing the current width in an attribute*/
  194.         $(this).attr('data-default', $(this).width());
  195.         $(this).animate(
  196.         {
  197.             width: 534
  198.         }, 'slow');
  199.  
  200.     }).blur(function()
  201.     {
  202.         /* lookup the original width */
  203.         var w = $(this).attr('data-default');
  204.         $(this).animate(
  205.         {
  206.             width: w
  207.         }, 'slow');
  208.     });
  209.    
  210.     $('textarea.answer').focus(function()
  211.     {
  212.         /*to make this flexible, I'm storing the current width in an attribute*/
  213.         $(this).attr('data-default', $(this).height());
  214.         $(this).animate(
  215.         {
  216.             height: 100
  217.         }, 'slow');
  218.  
  219.     }).blur(function()
  220.     {
  221.         /* lookup the original width */
  222.         var w = $(this).attr('data-default');
  223.         $(this).animate(
  224.         {
  225.             height: w
  226.         }, 'slow');
  227.     });
  228. };
  229.  
  230. /**
  231.  * Makes links to question-group-pages use ajax
  232.  * @param string sectionSlug
  233.  * @param integer pageIndex 1-indexed
  234.  * @param boolean cameByHistoryPop Set to true to leave the history untouched. Required if this method was invoked due to a popstate event
  235.  * @return void
  236.  * @author Alexander Thiel
  237.  * @since 2013-03-14
  238.  */
  239. MsoClient.Questionnaire.Manager.prototype.gotoPage = function(sectionSlug, pageIndex, cameByHistoryPop, completeCallback)
  240. {
  241.     var m = this;
  242.     pageIndex = pageIndex >= 1 ? pageIndex : 1;
  243.     var pageUrl = this.buildUrl(this.pageUrl, sectionSlug, pageIndex);
  244.     var syncUrl = this.buildUrl(this.syncUrl, sectionSlug, pageIndex);
  245.  
  246.     MsoClient.log('New page: ' + sectionSlug + '/' + pageIndex);
  247.  
  248.     $.ajax(
  249.     {
  250.         dataType: "json",
  251.         url: pageUrl,
  252.         success: function(data)
  253.         {
  254.             if (completeCallback)
  255.                 completeCallback();
  256.             if (cameByHistoryPop !== true && typeof (history.pushState) !== 'undefined')
  257.                 history.pushState(
  258.                 {
  259.                     sectionSlug: sectionSlug,
  260.                     pageIndex: pageIndex
  261.                 }, data.title, pageUrl);
  262.             document.title = data.title;
  263.             m.replacePage(data, syncUrl);
  264.  
  265.             m.attachExpansionAnimation();
  266.  
  267.         }
  268.     });
  269. };
  270.  
  271. /**
  272.  * Makes links to question-group-pages use ajax
  273.  * @return void
  274.  * @author Alexander Thiel
  275.  * @since 2013-03-12
  276.  */
  277. MsoClient.Questionnaire.Manager.prototype.replacePage = function(data, syncUrl)
  278. {
  279.     var pageWrapper = this.wrapper.find('.page');
  280.     pageWrapper.find('*').off();
  281.     pageWrapper.empty();
  282.     pageWrapper.html(data.page);
  283.  
  284.     //previous page link
  285.     pageWrapper.find('.footer-buttons .previous a').attr('href', data.previousPageLink);
  286.     if (data.previousPageLink)
  287.         pageWrapper.find('.footer-buttons .previous').addClass('visible');
  288.     else
  289.         pageWrapper.find('.footer-buttons .previous').removeClass('visible');
  290.  
  291.     //next page link
  292.     pageWrapper.find('.footer-buttons .next a').attr('href', data.nextPageLink);
  293.     pageWrapper.find('.footer-buttons .submit a').attr('href', data.submitPageLink);
  294.     if (data.nextPageLink)
  295.     {
  296.         pageWrapper.find('.footer-buttons .next').addClass('visible');
  297.         pageWrapper.find('.footer-buttons .submit').removeClass('visible');
  298.     } else
  299.     {
  300.         pageWrapper.find('.footer-buttons .next').removeClass('visible');
  301.         pageWrapper.find('.footer-buttons .submit').addClass('visible');
  302.     }
  303.  
  304.     this.pageManager.destroy();
  305.  
  306.     this.pageManager = new MsoClient.Questionnaire.PageManager(syncUrl, pageWrapper, this.messageManager);
  307.  
  308.     this.pageManager.init();
  309.     this.ajaxifyPageLinks();
  310.     MsoClient.themeInputs();
  311.     document.location.href = '#questionnaire-top';
  312. };
Advertisement
Add Comment
Please, Sign In to add comment