(function($) { if (!('from' in Array)) { Array.from = function(arrayLike) { return [].slice.call(arrayLike); }; } if (!('keys' in Object)) { Object.keys = function(obj) { var keys = []; for (var key in obj) { if (obj.hasOwnProperty(key)) { keys.push(key); } } return keys; }; } var WebDAV = (function() { // internal methods var _bindEvents = function(file) { if (file.directory) { file.item.find('.title').on('click', function() { history.pushState(history.state, file.path + file.name, file.path + file.name); WebDAV.list(file.path + file.name); return false; }); } else { file.item.find('.title').on('click', function(event) { event.stopPropagation(); if (file.type === 'video') { $.featherlight(''); event.preventDefault(); } else if (file.type === 'audio') { $.featherlight(''); event.preventDefault(); } else if (file.type === 'image') { $.featherlight({ image: file.path + file.name }); event.preventDefault(); } else if (file.type === 'font') { var formats = { eot: 'embedded-opentype', otf: 'opentype', ttf: 'truetype' }, extension = file.name.replace(/^.+\.([^\.]+)$/, '$1').toLowerCase(), fontName = (file.path + file.name).replace(/\W+/g, '_'), demoText = 'The quick brown fox jumps over the lazy dog. 0123456789
Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz'; if (!$('[data-path="' + (file.path + file.name) + '"]').is('style')) { $('body').appendChild(''); } $.featherlight('

' + file.name + '

' + demoText + '

' + a + '

' + demoText + '

' + demoText + '

Download

'); event.preventDefault(); } else if (file.type === 'text') { if (!('code' in $.featherlight.contentFilters)) { $.extend($.featherlight.contentFilters, { code: { process: function(url) { var deferred = $.Deferred(), $container = $('
');
                    $.ajax(url, {
                      complete: function(response, status) {
                        if ( status !== "error" ) {
                          $container.text(response.responseText);
                          deferred.resolve($container);

                          // prettify the code
                          PR.prettyPrint();
                        }

                        deferred.fail();
                      }
                    });

                    return deferred.promise();
                  }
                }
              }); 
            }

            $.featherlight({
              code: file.path + file.name
            });

            event.preventDefault();
          }
        });
      }

      if (file['delete']) {
        file.item.find('.delete').on('click', function() {
          if (confirm('Are you sure you want to delete "' + file.name + '"?')) {
            WebDAV.del(file);
          }

          return false;
        });

        file.item.find('.rename').on('click', function() {
          var to = prompt('Please enter the new name for "' + file.name + '":', decodeURIComponent(file.name));

          if (!to) {
            return false;
          }

          if (!_validateFileName(to)) {
            _message('Bad file name.');
            return false;
          }

          WebDAV.rename(file, file.path + to);

          return false;
        });

        file.item.find('.copy').on('click', function() {
          _message('Currently not implemented.');

          return false;
        });

        file.item.find('.move').on('click', function() {
          _message('Currently not implemented.');

          return false;
        });

        file.item.find('.download').on('click', function(event) {
          event.stopPropagation();

          return true;
        });
      }

      file.item.on('click', function() {
        file.item.find('a.title').click();

        return false;
      });

      return file.item;
    },
    _checkFile = function(file) {
      var foundFile = false;

      $.each(_files, function() {
        if (decodeURIComponent(this.name) === decodeURIComponent(file.name)) {
          foundFile = this;

          return false;
        }
      });

      return foundFile;
    },
    _createListItem = function(file) {
      file.item = $('
  • ').data('file', file); if (file.directory) { file.item.addClass('directory'); } else { file.item.addClass('file'); if (file.type) { file.item.addClass(file.type); } else { file.item.addClass('unknown'); } } if (!file.directory) { file.item.addClass(file.name.replace(/^.+\.([^\.]+)$/, '$1')); } file.item.append('' + file.title + ''); if (!file.directory) { file.item.append('' + _showSize(file.size) + ''); } // parent folder doesn't have a 'name' if (file.name) { if (file['delete']) { file.item.append('delete'); file.item.append('move'); } file.item.append('rename'); file.item.append('copy'); if (!file.directory) { file.item.append('download'); } } _bindEvents(file); return file; }, _validateFileName = function(filename) { if (!filename) { return false; } else if (!filename.match(/^[\w \-\.]+$/)) { return false; } else if (filename.match(/^\.\.?$/)) { return false; } return true; }, _makeSafePath = function(path) { return decodeURIComponent(path).replace(/[^\w\/\-\.]/g, function(char) { return encodeURIComponent(char); }); }, _getFileName = function(path) { path = path.replace(/\/$/, ''); return path.split('/').pop(); }, _getTag = function(doc, tag) { if (doc.querySelector) { return doc.querySelector(tag); } return doc.getElementsByTagName(tag)[0]; }, _getTagContent = function(doc, tag) { var node = _getTag(doc, tag); return node ? node.textContent : ''; }, _getTags = function(doc, tag) { if (doc.querySelectorAll) { return Array.from(doc.querySelectorAll(tag)); } return Array.from(doc.getElementsByTagName(tag)); }, _getType = function(file) { if (file.mimeType && file.mimeType.split('/').shift()) { return file.mimeType.split('/').shift(); } var types = { // displayed in an iframe, using google prettify text: /\.(?:te?xt|i?nfo|php|pl|cgi|faq|ini|htaccess|log|md|sql|sfv|conf|sh|pl|pm|py|rb|(?:s?c|sa)ss|js|java|coffee|[sx]?html?|xml)$/i, // displayed in fancybox as an image image: /\.(?:jpe?g|gif|a?png|svg)/i, video: /\.(?:mp(?:e?g)?4|mov|avi|webm|ogv)/i, audio: /\.(?:mp3|wav|ogg)/i, font: /\.(?:woff2?|eot|[ot]tf)/i }, // pushed to browser type = 'unknown'; $.each(types, function(key, value) { if (file.match(value)) { type = key; return false; } }); return type; }, _listContents = function(path, events) { var req = _request('PROPFIND', path, { Depth: 1 }); Object.keys(events).forEach(function(event) { req.addEventListener(event, events[event], true); }); req.send(null); return req; }, _message = function(message, type) { if ('notify' in $) { $.notify(message, { className: (type || 'error') }); } else { console.log(message); } }, _refreshDisplay = function(forceRefresh) { return WebDAV.list(_path, forceRefresh); }, _renderFiles = function() { _sortFiles(); _list.empty(); $.each(_files, function(i, file) { if (!file) { return; } _list.append(file.item); }); return _list; }, _request = function(type, url, headers, allowCache) { // could add support for other versions here. lazy var xhr = new XMLHttpRequest(); // bust some cache if (!allowCache) { url += (url.indexOf('?') > -1 ? '&' : '?') + '_=' + Date.now(); } xhr.addEventListener('loadstart', function() { _busy = true; }); xhr.addEventListener('loadend', function() { _busy = true; }); xhr.open(type, url, true); if (headers) { Object.keys(headers).forEach(function(header) { xhr.setRequestHeader(header, headers[header]); }); } return xhr; }, _showSize = function(i) { var size = ''; ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB'].forEach(function(text, index) { if (!size && (i < Math.pow(1024, index + 1))) { size += (i / Math.pow(1024, index)).toFixed((index > 0) ? 1 : 0) + ' ' + ((i === 1) ? 'byte' : text); } }); return size; }, _sortFiles = function() { if (_files.length) { _files.sort(function(a, b) { if (a.directory === b.directory) { return a.name.replace(/\/$/, '') < b.name.replace(/\/$/, '') ? -1 : 1; } else { return a.directory ? -1 : 1; } }); } $.each(_files, function(i) { this.index = i; }); return _files; }, _updateDisplay = function() { document.title = decodeURIComponent(_path) + ' - ' + window.location.host; _sortFiles(); _renderFiles(); }, // private vars _busy = false, _cache = {}, _dropper, _files = [], _list = $('