[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/admin/jscripts/ -> themes.js (source)

   1  /**
   2   * ThemeSelector loads various selectors' properties when they are select from
   3   * a list
   4   */
   5  
   6  var ThemeSelector = {
   7  
   8      /**
   9       * Constructor
  10       *
  11       * @param  string the address to the load script
  12       * @param  string the address to the save script
  13       * @param  object the select element
  14       * @param  object the stylesheet info div
  15       * @param  string the stylesheet file name
  16       * @param  object the form element
  17       * @param  number the theme id
  18       * @return void
  19       */
  20      init: function(url, saveUrl, selector, styleSheet, file, selectorForm, tid) {
  21          // verify input
  22          if (!url || !saveUrl || !selector || !styleSheet || !file || !selectorForm || !tid) {
  23              return;
  24          }
  25  
  26          ThemeSelector.url = url;
  27          ThemeSelector.saveUrl = saveUrl;
  28          ThemeSelector.selector = selector;
  29          ThemeSelector.selectorPrevOpt = ThemeSelector.selector.val();
  30          ThemeSelector.styleSheet = styleSheet;
  31          ThemeSelector.file = file;
  32          ThemeSelector.selectorForm = selectorForm;
  33          ThemeSelector.tid = tid;
  34  
  35          ThemeSelector.background = $("#css_bits\\[background\\]").val();
  36          ThemeSelector.width = $("#css_bits\\[width\\]").val();
  37          ThemeSelector.color = $("#css_bits\\[color\\]").val();
  38          ThemeSelector.extra = $("#css_bits\\[extra\\]").val();
  39          ThemeSelector.text_decoration = $("#css_bits\\[text_decoration\\]").val();
  40          ThemeSelector.font_family = $("#css_bits\\[font_family\\]").val();
  41          ThemeSelector.font_size = $("#css_bits\\[font_size\\]").val();
  42          ThemeSelector.font_style = $("#css_bits\\[font_style\\]").val();
  43          ThemeSelector.font_weight = $("#css_bits\\[font_weight\\]").val();
  44  
  45          $("#save").on('click', function(event) { ThemeSelector.save(event, true); } );
  46          $("#save_close").on('click', function(event) { ThemeSelector.saveClose(event); } );
  47          
  48  
  49          $(window).on('beforeunload', function(event){
  50              if(ThemeSelector.isChanged())
  51              {
  52                  return ' ';
  53              }
  54          });
  55  
  56          
  57          
  58          ThemeSelector.selector.on("change", ThemeSelector.updateSelector);
  59          ThemeSelector.selectorForm.on("submit", ThemeSelector.updateSelector);
  60      },
  61  
  62      /**
  63       * prevents no-save warning messaging when saving
  64       *
  65       * @return void
  66       */
  67      saveClose: function(e) {
  68          ThemeSelector.isClosing = true;
  69      },
  70  
  71      /**
  72       * updates the stylesheet info to match the current selection, checking
  73       * first that work isn't lost
  74       *
  75       * @param  object the event
  76       * @return void
  77       */
  78      updateSelector: function(e) {
  79          var postData;
  80  
  81          e.preventDefault()
  82  
  83          ThemeSelector.saveCheck(e, true);
  84  
  85          postData = "file=" + encodeURIComponent(ThemeSelector.file) + "&tid=" + encodeURIComponent(ThemeSelector.tid) + "&selector=" + encodeURIComponent(ThemeSelector.selector.val()) + "&my_post_key=" + encodeURIComponent(my_post_key);
  86  
  87          ThemeSelector.selectorGoText = $("#mini_spinner").html();
  88          $("#mini_spinner").html("&nbsp;<img src=\"" + ThemeSelector.miniSpinnerImage + "\" style=\"vertical-align: middle;\" alt=\"\" /> ");
  89  
  90          $.ajax({
  91              type: 'post',
  92              url: ThemeSelector.url,
  93              data: postData,
  94              complete: ThemeSelector.onComplete,
  95          });
  96      },
  97  
  98      /**
  99       * handles the AJAX return data
 100       *
 101       * @param  object the request
 102       * @return true
 103       */
 104      onComplete: function(request) {
 105          var message, saved;
 106  
 107          if (request.responseText.match(/<error>(.*)<\/error>/)) {
 108              message = request.responseText.match(/<error>(.*)<\/error>/);
 109  
 110              if (!message[1]) {
 111                  message[1] = lang.unknown_error;
 112              }
 113              $.jGrowl(lang.theme_info_fetch_error + '\n\n' + message[1], {theme:'jgrowl_error'});
 114          } else if(request.responseText) {
 115              if ($("#saved").html()) {
 116                  saved = $("#saved").html();
 117              }
 118              ThemeSelector.styleSheet.html(request.responseText);
 119          }
 120  
 121          ThemeSelector.background = $("#css_bits\\[background\\]").val();
 122          ThemeSelector.width = $("#css_bits\\[width\\]").val();
 123          ThemeSelector.color = $("#css_bits\\[color\\]").val();
 124          ThemeSelector.extra = $("#css_bits\\[extra\\]").val();
 125          ThemeSelector.text_decoration = $("#css_bits\\[text_decoration\\]").val();
 126          ThemeSelector.font_family = $("#css_bits\\[font_family\\]").val();
 127          ThemeSelector.font_size = $("#css_bits\\[font_size\\]").val();
 128          ThemeSelector.font_style = $("#css_bits\\[font_style\\]").val();
 129          ThemeSelector.font_weight = $("#css_bits\\[font_weight\\]").val();
 130  
 131          if (saved) {
 132              $("#saved").html(saved);
 133              window.setTimeout(function() {
 134                  $("#saved").html("");
 135              }, 30000);
 136          }
 137  
 138          $("#mini_spinner").html(ThemeSelector.selectorGoText);
 139          ThemeSelector.selectorGoText = '';
 140  
 141          return true;
 142      },
 143  
 144      isChanged: function()
 145      {
 146          return (ThemeSelector.background != $("#css_bits\\[background\\]").val() ||
 147                  ThemeSelector.width != $("#css_bits\\[width\\]").val() ||
 148                  ThemeSelector.color != $("#css_bits\\[color\\]").val() ||
 149                  ThemeSelector.extra != $("#css_bits\\[extra\\]").val() ||
 150                  ThemeSelector.text_decoration != $("#css_bits\\[text_decoration\\]").val() ||
 151                  ThemeSelector.font_family != $("#css_bits\\[font_family\\]").val() ||
 152                  ThemeSelector.font_size != $("#css_bits\\[font_size\\]").val() ||
 153                  ThemeSelector.font_style != $("#css_bits\\[font_style\\]").val() ||
 154                  ThemeSelector.font_weight != $("#css_bits\\[font_weight\\]").val());
 155      },
 156  
 157      /**
 158       * check if anything has changed
 159       *
 160       * @param  object the event
 161       * @param  bool true if AJAX, false if not
 162       * @return true
 163       */
 164      saveCheck: function(e, isAjax) {
 165  
 166      
 167          if (ThemeSelector.isClosing == true) {
 168              return true;
 169          }
 170  
 171          if(e != null && isAjax == true)
 172              e.preventDefault();
 173  
 174          if (ThemeSelector.isChanged()) {
 175              
 176              e.preventDefault();
 177              
 178              if(isAjax == false)
 179                  return save_changes_lang_string;
 180              else
 181              {
 182                  confirmReturn = confirm(save_changes_lang_string);
 183                  if (confirmReturn == true) {
 184                      ThemeSelector.save(false, isAjax);
 185                      $.jGrowl(lang.saved, {theme:'jgrowl_success'});
 186                  }
 187              }
 188          }
 189          else if(isAjax == true)
 190          {
 191              ThemeSelector.selectorPrevOpt = ThemeSelector.selector.val();
 192              return true;
 193          }
 194      },
 195  
 196      /**
 197       * saves the selector info
 198       *
 199       * @param  object the event
 200       * @param  bool true if AJAX, false if not
 201       * @return true
 202       */
 203      save: function(e, isAjax) {
 204          var cssBits, postData, completeMethod = 'onUnloadSaveComplete';
 205  
 206          if (e) {
 207              e.preventDefault();
 208          }
 209  
 210          cssBits = {
 211              'background': $('#css_bits\\[background\\]').val(),
 212              'width': $('#css_bits\\[width\\]').val(),
 213              'color': $('#css_bits\\[color\\]').val(),
 214              'extra': $('#css_bits\\[extra\\]').val(),
 215              'text_decoration': $('#css_bits\\[text_decoration\\]').val(),
 216              'font_family': $('#css_bits\\[font_family\\]').val(),
 217              'font_size': $('#css_bits\\[font_size\\]').val(),
 218              'font_style': $('#css_bits\\[font_style\\]').val(),
 219              'font_weight': $('#css_bits\\[font_weight\\]').val()
 220          };
 221  
 222          postData = "css_bits=" + encodeURIComponent(jsArrayToPhpArray(cssBits)) + "&selector=" + encodeURIComponent(ThemeSelector.selectorPrevOpt) + "&file=" + encodeURIComponent(ThemeSelector.file) + "&tid=" + encodeURIComponent(ThemeSelector.tid) + "&my_post_key=" + encodeURIComponent(my_post_key) + "&serialized=1";
 223  
 224          if (isAjax == true) {
 225              postData += "&ajax=1";
 226          }
 227  
 228          ThemeSelector.isAjax = isAjax;
 229  
 230          if (isAjax == true) {
 231              completeMethod = 'onSaveComplete';
 232              $.jGrowl(lang.saving, {theme:'jgrowl_process'});
 233          }
 234  
 235          $.ajax({
 236              type: 'post',
 237              url: ThemeSelector.saveUrl,
 238              data: postData,
 239              complete: ThemeSelector[completeMethod],
 240          });
 241          return !isAjax;
 242      },
 243  
 244      /**
 245       * handle errors, reset values and clean up
 246       *
 247       * @param  object the request
 248       * @return true
 249       */
 250      onSaveComplete: function(request) {
 251          var message;
 252  
 253          if (request.responseText.match(/<error>(.*)<\/error>/)) {
 254              message = request.responseText.match(/<error>(.*)<\/error>/);
 255  
 256              if (!message[1]) {
 257                  message[1] = lang.unkown_error;
 258              }
 259              $.jGrowl(lang.theme_info_save_error + '\n\n' + message[1], {theme:'jgrowl_error'});
 260              return false;
 261          } else if(request.responseText) {
 262              $("#saved").html(" (" + lang.saved + " @ "+ Date() + ")");
 263              if ($("#ajax_alert")) {
 264                  $("#ajax_alert").html('').hide();
 265              }
 266          }
 267  
 268          ThemeSelector.background = $("#css_bits\\[background\\]").val();
 269          ThemeSelector.width = $("#css_bits\\[width\\]").val();
 270          ThemeSelector.color = $("#css_bits\\[color\\]").val();
 271          ThemeSelector.extra = $("#css_bits\\[extra\\]").val();
 272          ThemeSelector.text_decoration = $("#css_bits\\[text_decoration\\]").val();
 273          ThemeSelector.font_family = $("#css_bits\\[font_family\\]").val();
 274          ThemeSelector.font_size = $("#css_bits\\[font_size\\]").val();
 275          ThemeSelector.font_style = $("#css_bits\\[font_style\\]").val();
 276          ThemeSelector.font_weight = $("#css_bits\\[font_weight\\]").val();
 277  
 278          return true;
 279      },
 280  
 281      /**
 282       * handle leaving page save
 283       *
 284       * @param  object the request
 285       * @return true
 286       */
 287      onUnloadSaveComplete: function(request) {
 288          var message;
 289  
 290          if (request.responseText.match(/<error>(.*)<\/error>/)) {
 291              message = request.responseText.match(/<error>(.*)<\/error>/);
 292  
 293              if (!message[1]) {
 294                  message[1] = lang.unkown_error;
 295              }
 296              $.jGrowl(lang.theme_info_save_error + '\n\n' + message[1], {theme:'jgrowl_error'});
 297              return false;
 298          }
 299          return true;
 300      },
 301  
 302      url: null,
 303      saveUrl: null,
 304      selector: null,
 305      styleSheet: null,
 306      file: null,
 307      selectorForm: null,
 308      tid: null,
 309      miniSpinnerImage: "../images/spinner.gif",
 310      isAjax: false,
 311      specific_count: 0,
 312      selectorGoText: null,
 313      selectorPrevOpt: null,
 314      isClosing: false,
 315      background: null,
 316      width: null,
 317      color: null,
 318      extra: null,
 319      text_decoration: null,
 320      font_family: null,
 321      font_size: null,
 322      font_style: null,
 323      font_weight: null
 324  };
 325  
 326  /**
 327   * converts a JS object to a JSON of a PHP associative array
 328   *
 329   * @param  array the JS array
 330   * @return string the JSON
 331   */
 332  function jsArrayToPhpArray(a) {
 333      var a_php = "", total = 0;
 334  
 335      for (var key in a) {
 336          ++total;
 337          a_php += "s:" +
 338          String(key).length + ":\"" +
 339          String(key) + "\";s:" +
 340          String(a[key]).length +
 341          ":\"" + String(a[key]) + "\";";
 342      }
 343      a_php = "a:" + total + ":{" + a_php + "}";
 344      return a_php;
 345  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref