Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var MsoClient = MsoClient ? MsoClient :
- {
- };
- MsoClient.Questionnaire = MsoClient.Questionnaire ? MsoClient.Questionnaire :
- {
- };
- /**
- * Constructor
- * @author Alexander Thiel
- * @author Christian Baer
- * @since 2013-03-12
- */
- MsoClient.Questionnaire.Manager = function(wrapperElement, pageUrl, syncUrl, messageManager)
- {
- this.wrapper = $(wrapperElement);
- this.messageManager = messageManager;
- this.pageUrl = pageUrl;
- this.syncUrl = syncUrl;
- };
- MsoClient.Questionnaire.Manager.prototype =
- {
- wrapper: null,
- messagesWrapper: null,
- syncUrl: '',
- pageUrl: '',
- sectionSlug: null,
- pageIndex: null
- };
- /**
- *
- * @param string sectionSlug
- * @param integer pageIndex Zero-indexed!
- * @return void
- * @author Alexander Thiel
- * @author Christian Baer
- * @since 2013-03-12
- */
- MsoClient.Questionnaire.Manager.prototype.init = function(sectionSlug, pageIndex)
- {
- this.sectionSlug = sectionSlug;
- this.pageIndex = parseInt(pageIndex);
- this.ajaxifyPageLinks();
- this.messageManager.init();
- MsoClient.log('Message Manager initialized');
- this.pageManager = new MsoClient.Questionnaire.PageManager(this.buildUrl(this.syncUrl, sectionSlug, pageIndex + 1), this.wrapper.find('.page'), this.messageManager);
- this.pageManager.init();
- var m = this;
- $(window).on('popstate', function(event)
- {
- if (event.originalEvent.state)
- m.gotoPage(event.originalEvent.state.sectionSlug, event.originalEvent.state.pageIndex, true);
- });
- if ( typeof (history.replaceState) !== 'undefined')
- history.replaceState(
- {
- sectionSlug: sectionSlug,
- pageIndex: m.pageIndex + 1
- }, document.title, this.buildUrl(this.pageUrl, sectionSlug, m.pageIndex + 1));
- MsoClient.log('Questionnaire Manager initialized');
- this.wrapper.find('.expand-button').on('click', function()
- {
- m.expandMessageBar();
- });
- $(window).on('scroll', function()
- {
- m.handleScroll();
- });
- };
- MsoClient.Questionnaire.Manager.prototype.handleScroll = function()
- {
- var elements = $('.outer-wrapper.header .badge-links, .outer-wrapper.content .content-wrapper .questionnaire .main .horizontal-pipe');
- if ($(window).scrollTop() > 78)
- elements.css(
- {
- position: 'fixed',
- top: 0
- });
- else
- elements.css(
- {
- position: 'absolute',
- top: '78px'
- });
- };
- /**
- * Expands and collapses the message bar
- * @return string
- * @since 2013-03-18
- * @author Alexander Thiel
- */
- MsoClient.Questionnaire.Manager.prototype.expandMessageBar = function()
- {
- var threadWidth = 267;
- var distance = 220;
- var pageWrapper = this.wrapper.find('.page');
- var bar = this.wrapper.find('.side-bar');
- if (bar.is(':animated'))
- return;
- if (bar.hasClass('expanded'))
- {
- bar.animate(
- {
- width: '-=' + distance + 'px'
- },
- {
- duration: 1000,
- easing: 'easeInOutCirc',
- complete: function()
- {
- bar.removeClass('expanded');
- pageWrapper.removeClass('narrow');
- }
- });
- bar.find('h4').animate(
- {
- width: threadWidth + 'px'
- }, 1000, 'easeInOutCirc');
- } else
- {
- bar.animate(
- {
- width: '+=' + distance + 'px'
- },
- {
- duration: 1000,
- easing: 'easeInOutCirc',
- complete: function()
- {
- bar.addClass('expanded');
- pageWrapper.addClass('narrow');
- }
- });
- bar.find('h4').animate(
- {
- width: threadWidth + distance + 'px'
- }, 1000, 'easeInOutCirc');
- }
- }
- /**
- * Build url to perform ajax requests
- * @param string sectionSlug
- * @param integer pageIndex 1-indexed
- * @return string
- * @since 2013-03-18
- * @author Alexander Thiel
- */
- MsoClient.Questionnaire.Manager.prototype.buildUrl = function(url, sectionSlug, pageIndex)
- {
- //Map everything except integers greater or equal 1 to a 1
- pageIndex = Math.max(parseInt(pageIndex), 1);
- return url.replace(/%sectionSlug%/, sectionSlug).replace(/%pageIndex%/, pageIndex).replace(/\/+$/, '');
- };
- /**
- * Makes links to question-group-pages use ajax
- * Call to {gotoPage()} not deferred to to allow location changes in case of error
- * 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.
- * @return void
- * @author Alexander Thiel
- * @since 2013-03-12
- */
- MsoClient.Questionnaire.Manager.prototype.ajaxifyPageLinks = function()
- {
- var m = this;
- this.wrapper.find('a.page-link').on('click', function()
- {
- var link = $(this);
- var matches = link.attr('href').match(/^.*treatment_plan\/form\/([a-zA-Z0-9\-_]+)(\/?)([0-9]*)\/?$/);
- m.gotoPage(matches[1], matches.length > 3 ? matches[3] : 1, false);
- return false;
- });
- };
- /**
- * Condenses textboxes and textareas, expands them on click
- * @return void
- * @author Denis Nizetic
- * @since 2013-05-03
- */
- MsoClient.Questionnaire.Manager.prototype.attachExpansionAnimation = function()
- {
- $('input.answer[type="text"][placeholder!="Number"]').focus(function()
- {
- /*to make this flexible, I'm storing the current width in an attribute*/
- $(this).attr('data-default', $(this).width());
- $(this).animate(
- {
- width: 534
- }, 'slow');
- }).blur(function()
- {
- /* lookup the original width */
- var w = $(this).attr('data-default');
- $(this).animate(
- {
- width: w
- }, 'slow');
- });
- $('textarea.answer').focus(function()
- {
- /*to make this flexible, I'm storing the current width in an attribute*/
- $(this).attr('data-default', $(this).height());
- $(this).animate(
- {
- height: 100
- }, 'slow');
- }).blur(function()
- {
- /* lookup the original width */
- var w = $(this).attr('data-default');
- $(this).animate(
- {
- height: w
- }, 'slow');
- });
- };
- /**
- * Makes links to question-group-pages use ajax
- * @param string sectionSlug
- * @param integer pageIndex 1-indexed
- * @param boolean cameByHistoryPop Set to true to leave the history untouched. Required if this method was invoked due to a popstate event
- * @return void
- * @author Alexander Thiel
- * @since 2013-03-14
- */
- MsoClient.Questionnaire.Manager.prototype.gotoPage = function(sectionSlug, pageIndex, cameByHistoryPop, completeCallback)
- {
- var m = this;
- pageIndex = pageIndex >= 1 ? pageIndex : 1;
- var pageUrl = this.buildUrl(this.pageUrl, sectionSlug, pageIndex);
- var syncUrl = this.buildUrl(this.syncUrl, sectionSlug, pageIndex);
- MsoClient.log('New page: ' + sectionSlug + '/' + pageIndex);
- $.ajax(
- {
- dataType: "json",
- url: pageUrl,
- success: function(data)
- {
- if (completeCallback)
- completeCallback();
- if (cameByHistoryPop !== true && typeof (history.pushState) !== 'undefined')
- history.pushState(
- {
- sectionSlug: sectionSlug,
- pageIndex: pageIndex
- }, data.title, pageUrl);
- document.title = data.title;
- m.replacePage(data, syncUrl);
- m.attachExpansionAnimation();
- }
- });
- };
- /**
- * Makes links to question-group-pages use ajax
- * @return void
- * @author Alexander Thiel
- * @since 2013-03-12
- */
- MsoClient.Questionnaire.Manager.prototype.replacePage = function(data, syncUrl)
- {
- var pageWrapper = this.wrapper.find('.page');
- pageWrapper.find('*').off();
- pageWrapper.empty();
- pageWrapper.html(data.page);
- //previous page link
- pageWrapper.find('.footer-buttons .previous a').attr('href', data.previousPageLink);
- if (data.previousPageLink)
- pageWrapper.find('.footer-buttons .previous').addClass('visible');
- else
- pageWrapper.find('.footer-buttons .previous').removeClass('visible');
- //next page link
- pageWrapper.find('.footer-buttons .next a').attr('href', data.nextPageLink);
- pageWrapper.find('.footer-buttons .submit a').attr('href', data.submitPageLink);
- if (data.nextPageLink)
- {
- pageWrapper.find('.footer-buttons .next').addClass('visible');
- pageWrapper.find('.footer-buttons .submit').removeClass('visible');
- } else
- {
- pageWrapper.find('.footer-buttons .next').removeClass('visible');
- pageWrapper.find('.footer-buttons .submit').addClass('visible');
- }
- this.pageManager.destroy();
- this.pageManager = new MsoClient.Questionnaire.PageManager(syncUrl, pageWrapper, this.messageManager);
- this.pageManager.init();
- this.ajaxifyPageLinks();
- MsoClient.themeInputs();
- document.location.href = '#questionnaire-top';
- };
Advertisement
Add Comment
Please, Sign In to add comment