headjs.php 5.29 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
    Copyright (C) 2014-2015 Deciso B.V.
5

Ad Schellevis's avatar
Ad Schellevis committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/


require_once("guiconfig.inc");

function getHeadJS() {
  global $g, $use_loader_tab_gif;

  $headjs = "
    var input_errors = '';
    jQuery(document).ready(init);
  ";
38
  if (session_status() == PHP_SESSION_NONE) {
Ad Schellevis's avatar
Ad Schellevis committed
39
	session_start();
40
  }
Ad Schellevis's avatar
Ad Schellevis committed
41
  $_SESSION['NO_AJAX'] == "True" ? $noajax = "var noAjaxOnSubmit = true;" : $noajax = "var noAjaxOnSubmit = false;";
42
  session_write_close();
Ad Schellevis's avatar
Ad Schellevis committed
43 44 45 46 47 48 49 50

  $headjs .= "
    {$noajax}

    function init() {
      if(jQuery('#submit') && ! noAjaxOnSubmit) {
        // debugging helper
        //alert('adding observe event for submit button');
51

Ad Schellevis's avatar
Ad Schellevis committed
52 53
        jQuery(\"#submit\").click(submit_form);
        jQuery('#submit').click(function() {return false;});
54
        var to_insert = \"<div style='visibility:hidden' id='loading' name='loading'><span class='glyphicon glyphicon-refresh' alt='loader'></span><\/div>\";
Ad Schellevis's avatar
Ad Schellevis committed
55 56 57
        jQuery('#submit').before(to_insert);
      }
    }
58

Ad Schellevis's avatar
Ad Schellevis committed
59 60 61 62 63 64
    function submit_form(e){
      // debugging helper
      //alert(Form.serialize($('iform')));

      if(jQuery('#inputerrors'))
        jQuery('#inputerrors').html('<center><b><i>Loading...<\/i><\/b><\/center>');
65

Ad Schellevis's avatar
Ad Schellevis committed
66 67 68 69 70 71 72 73 74 75 76 77 78
      /* dsh: Introduced because pkg_edit tries to set some hidden fields
       *      if executing submit's onclick event. The click gets deleted
       *      by Ajax. Hence using onkeydown instead.
       */
      if(jQuery('#submit').prop('keydown')) {
        jQuery('#submit').keydown();
        jQuery('#submit').css('visibility','hidden');
      }
      if(jQuery('#cancelbutton'))
        jQuery('#cancelbutton').css('visibility','hidden');
      jQuery('#loading').css('visibility','visible');
      // submit the form using Ajax
    }
79

Ad Schellevis's avatar
Ad Schellevis committed
80 81
    function formSubmitted(resp) {
      var responseText = resp.responseText;
82

Ad Schellevis's avatar
Ad Schellevis committed
83 84
      // debugging helper
      // alert(responseText);
85

Ad Schellevis's avatar
Ad Schellevis committed
86 87 88 89 90
      if(responseText.indexOf('html') > 0) {
        /* somehow we have been fed an html page! */
        //alert('Somehow we have been fed an html page! Forwarding to /.');
        document.location.href = '/';
      }
91

Ad Schellevis's avatar
Ad Schellevis committed
92 93
      eval(responseText);
    }
94

Ad Schellevis's avatar
Ad Schellevis committed
95 96 97 98 99 100 101 102 103 104 105
    /* this function will be called if an HTTP error will be triggered */
    function formFailure(resp) {
	    showajaxmessage(resp.responseText);
		if(jQuery('#submit'))
		  jQuery('#submit').css('visibility','visible');
		if(jQuery('#cancelbutton'))
		  jQuery('#cancelbutton').css('visibility','visible');
		if(jQuery('#loading'))
		  jQuery('#loading').css('visibility','hidden');

    }
106

Ad Schellevis's avatar
Ad Schellevis committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    function showajaxmessage(message) {
      var message_html;

      if (message == '') {

        if(jQuery('#submit'))
          jQuery('#submit').css('visibility','visible');
        if(jQuery('#cancelbutton'))
          jQuery('#cancelbutton').css('visibility','visible');
        if(jQuery('#loading'))
          jQuery('#loading').css('visibility','hidden');

        return;
      }

      message_html = '<table height=\"32\" width=\"100%\" summary=\"redbox\"><tr><td>';
      message_html += '<div style=\"background-color:#990000\" id=\"redbox\">';
      message_html += '<table width=\"100%\" summary=\"message\"><tr><td width=\"8%\">';
125
      message_html += '<span class=\"glyphicon glyphicon-exclamation-sign\" style=\"vertical-align:center\"  alt=\"exclamation\" ></span>';
Ad Schellevis's avatar
Ad Schellevis committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      message_html += '<\/td><td width=\"70%\"><font color=\"white\">';
      message_html += '<b>' + message + '<\/b><\/font><\/td>';

      if(message.indexOf('apply') > 0) {
        message_html += '<td>';
        message_html += '<input name=\"apply\" type=\"submit\" class=\"formbtn\" id=\"apply\" value=\"" . gettext("Apply changes") . "\" \/>';
        message_html += '<\/td>';
      }

      message_html += '<\/tr><\/table><\/div><\/td><\/table><br \/>';
      jQuery('#inputerrors').html(message_html);

      if(jQuery('#submit'))
        jQuery('#submit').css('visibility','visible');
      if(jQuery('#cancelbutton'))
        jQuery('#cancelbutton').css('visibility','visible');
      if(jQuery('#loading'))
        jQuery('#loading').css('visibility','hidden');
      if(jQuery('#inputerrors'))
        window.scrollTo(0, 0);
    }
  ";

  return $headjs;
}

?>