1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571 |
- /*
- * Copyright (c) 2014
- *
- * This file is licensed under the Affero General Public License version 3
- * or later.
- *
- * See the COPYING-README file.
- *
- */
- (function() {
- /**
- * @class OCA.Files.FileList
- * @classdesc
- *
- * The FileList class manages a file list view.
- * A file list view consists of a controls bar and
- * a file list table.
- *
- * @param $el container element with existing markup for the #controls
- * and a table
- * @param {Object} [options] map of options, see other parameters
- * @param {Object} [options.scrollContainer] scrollable container, defaults to $(window)
- * @param {Object} [options.dragOptions] drag options, disabled by default
- * @param {Object} [options.folderDropOptions] folder drop options, disabled by default
- * @param {boolean} [options.detailsViewEnabled=true] whether to enable details view
- * @param {boolean} [options.enableUpload=false] whether to enable uploader
- * @param {OC.Files.Client} [options.filesClient] files client to use
- */
- var FileList = function($el, options) {
- this.initialize($el, options);
- };
- /**
- * @memberof OCA.Files
- */
- FileList.prototype = {
- SORT_INDICATOR_ASC_CLASS: 'icon-triangle-n',
- SORT_INDICATOR_DESC_CLASS: 'icon-triangle-s',
- id: 'files',
- appName: t('files', 'Files'),
- isEmpty: true,
- useUndo:true,
- /**
- * Top-level container with controls and file list
- */
- $el: null,
- /**
- * Files table
- */
- $table: null,
- /**
- * List of rows (table tbody)
- */
- $fileList: null,
- /**
- * @type OCA.Files.BreadCrumb
- */
- breadcrumb: null,
- /**
- * @type OCA.Files.FileSummary
- */
- fileSummary: null,
- /**
- * @type OCA.Files.DetailsView
- */
- _detailsView: null,
- /**
- * Files client instance
- *
- * @type OC.Files.Client
- */
- filesClient: null,
- /**
- * Whether the file list was initialized already.
- * @type boolean
- */
- initialized: false,
- /**
- * Wheater the file list was already shown once
- * @type boolean
- */
- shown: false,
- /**
- * Number of files per page
- * Always show a minimum of 1
- *
- * @return {int} page size
- */
- pageSize: function() {
- var isGridView = this.$showGridView.is(':checked');
- var columns = 1;
- var rows = Math.ceil(this.$container.height() / 50);
- if (isGridView) {
- columns = Math.ceil(this.$container.width() / 160);
- rows = Math.ceil(this.$container.height() / 160);
- }
- return Math.max(columns*rows, columns);
- },
- /**
- * Array of files in the current folder.
- * The entries are of file data.
- *
- * @type Array.<OC.Files.FileInfo>
- */
- files: [],
- /**
- * Current directory entry
- *
- * @type OC.Files.FileInfo
- */
- dirInfo: null,
- /**
- * File actions handler, defaults to OCA.Files.FileActions
- * @type OCA.Files.FileActions
- */
- fileActions: null,
- /**
- * File selection menu, defaults to OCA.Files.FileSelectionMenu
- * @type OCA.Files.FileSelectionMenu
- */
- fileMultiSelectMenu: null,
- /**
- * Whether selection is allowed, checkboxes and selection overlay will
- * be rendered
- */
- _allowSelection: true,
- /**
- * Map of file id to file data
- * @type Object.<int, Object>
- */
- _selectedFiles: {},
- /**
- * Summary of selected files.
- * @type OCA.Files.FileSummary
- */
- _selectionSummary: null,
- /**
- * If not empty, only files containing this string will be shown
- * @type String
- */
- _filter: '',
- /**
- * @type Backbone.Model
- */
- _filesConfig: undefined,
- /**
- * Sort attribute
- * @type String
- */
- _sort: 'name',
- /**
- * Sort direction: 'asc' or 'desc'
- * @type String
- */
- _sortDirection: 'asc',
- /**
- * Sort comparator function for the current sort
- * @type Function
- */
- _sortComparator: null,
- /**
- * Whether to do a client side sort.
- * When false, clicking on a table header will call reload().
- * When true, clicking on a table header will simply resort the list.
- */
- _clientSideSort: true,
- /**
- * Whether or not users can change the sort attribute or direction
- */
- _allowSorting: true,
- /**
- * Current directory
- * @type String
- */
- _currentDirectory: null,
- _dragOptions: null,
- _folderDropOptions: null,
- /**
- * @type OC.Uploader
- */
- _uploader: null,
- /**
- * Initialize the file list and its components
- *
- * @param $el container element with existing markup for the #controls
- * and a table
- * @param options map of options, see other parameters
- * @param options.scrollContainer scrollable container, defaults to $(window)
- * @param options.dragOptions drag options, disabled by default
- * @param options.folderDropOptions folder drop options, disabled by default
- * @param options.scrollTo name of file to scroll to after the first load
- * @param {OC.Files.Client} [options.filesClient] files API client
- * @param {OC.Backbone.Model} [options.filesConfig] files app configuration
- * @private
- */
- initialize: function($el, options) {
- var self = this;
- options = options || {};
- if (this.initialized) {
- return;
- }
- if (options.shown) {
- this.shown = options.shown;
- }
- if (options.config) {
- this._filesConfig = options.config;
- } else if (!_.isUndefined(OCA.Files) && !_.isUndefined(OCA.Files.App)) {
- this._filesConfig = OCA.Files.App.getFilesConfig();
- } else {
- this._filesConfig = new OC.Backbone.Model({
- 'showhidden': false
- });
- }
- if (options.dragOptions) {
- this._dragOptions = options.dragOptions;
- }
- if (options.folderDropOptions) {
- this._folderDropOptions = options.folderDropOptions;
- }
- if (options.filesClient) {
- this.filesClient = options.filesClient;
- } else {
- // default client if not specified
- this.filesClient = OC.Files.getClient();
- }
- this.$el = $el;
- if (options.id) {
- this.id = options.id;
- }
- this.$container = options.scrollContainer || $(window);
- this.$table = $el.find('table:first');
- this.$fileList = $el.find('#fileList');
- if (!_.isUndefined(this._filesConfig)) {
- this._filesConfig.on('change:showhidden', function() {
- var showHidden = this.get('showhidden');
- self.$el.toggleClass('hide-hidden-files', !showHidden);
- self.updateSelectionSummary();
- if (!showHidden) {
- // hiding files could make the page too small, need to try rendering next page
- self._onScroll();
- }
- });
- this.$el.toggleClass('hide-hidden-files', !this._filesConfig.get('showhidden'));
- }
- if (_.isUndefined(options.detailsViewEnabled) || options.detailsViewEnabled) {
- this._detailsView = new OCA.Files.DetailsView();
- this._detailsView.$el.addClass('disappear');
- }
- this._initFileActions(options.fileActions);
- if (this._detailsView) {
- this._detailsView.addDetailView(new OCA.Files.MainFileInfoDetailView({fileList: this, fileActions: this.fileActions}));
- }
- this.files = [];
- this._selectedFiles = {};
- this._selectionSummary = new OCA.Files.FileSummary(undefined, {config: this._filesConfig});
- // dummy root dir info
- this.dirInfo = new OC.Files.FileInfo({});
- this.fileSummary = this._createSummary();
- if (options.multiSelectMenu) {
- this.fileMultiSelectMenu = new OCA.Files.FileMultiSelectMenu(options.multiSelectMenu);
- this.fileMultiSelectMenu.render();
- this.$el.find('.selectedActions').append(this.fileMultiSelectMenu.$el);
- }
- if (options.sorting) {
- this.setSort(options.sorting.mode, options.sorting.direction, false, false);
- } else {
- this.setSort('name', 'asc', false, false);
- }
- var breadcrumbOptions = {
- onClick: _.bind(this._onClickBreadCrumb, this),
- getCrumbUrl: function(part) {
- return self.linkTo(part.dir);
- }
- };
- // if dropping on folders is allowed, then also allow on breadcrumbs
- if (this._folderDropOptions) {
- breadcrumbOptions.onDrop = _.bind(this._onDropOnBreadCrumb, this);
- breadcrumbOptions.onOver = function() {
- self.$el.find('td.filename.ui-droppable').droppable('disable');
- };
- breadcrumbOptions.onOut = function() {
- self.$el.find('td.filename.ui-droppable').droppable('enable');
- };
- }
- this.breadcrumb = new OCA.Files.BreadCrumb(breadcrumbOptions);
- var $controls = this.$el.find('#controls');
- if ($controls.length > 0) {
- $controls.prepend(this.breadcrumb.$el);
- this.$table.addClass('has-controls');
- }
- this._renderNewButton();
- this.$el.find('thead th .columntitle').click(_.bind(this._onClickHeader, this));
- // Toggle for grid view, only register once
- this.$showGridView = $('input#showgridview:not(.registered)');
- this.$showGridView.on('change', _.bind(this._onGridviewChange, this));
- this.$showGridView.addClass('registered');
- $('#view-toggle').tooltip({placement: 'bottom', trigger: 'hover'});
- this._onResize = _.debounce(_.bind(this._onResize, this), 250);
- $('#app-content').on('appresized', this._onResize);
- $(window).resize(this._onResize);
- this.$el.on('show', this._onResize);
- this.updateSearch();
- this.$fileList.on('click','td.filename>a.name, td.filesize, td.date', _.bind(this._onClickFile, this));
- this.$fileList.on('droppedOnTrash', function (event, filename, directory) {
- self.do_delete(filename, directory);
- });
- this.$fileList.on('change', 'td.selection>.selectCheckBox', _.bind(this._onClickFileCheckbox, this));
- this.$el.on('show', _.bind(this._onShow, this));
- this.$el.on('urlChanged', _.bind(this._onUrlChanged, this));
- this.$el.find('.select-all').click(_.bind(this._onClickSelectAll, this));
- this.$el.find('.actions-selected').click(function () {
- self.fileMultiSelectMenu.show(self);
- return false;
- });
- this.$container.on('scroll', _.bind(this._onScroll, this));
- if (options.scrollTo) {
- this.$fileList.one('updated', function() {
- self.scrollTo(options.scrollTo);
- });
- }
- if (options.enableUpload) {
- // TODO: auto-create this element
- var $uploadEl = this.$el.find('#file_upload_start');
- if ($uploadEl.exists()) {
- this._uploader = new OC.Uploader($uploadEl, {
- fileList: this,
- filesClient: this.filesClient,
- dropZone: $('#content'),
- maxChunkSize: options.maxChunkSize
- });
- this.setupUploadEvents(this._uploader);
- }
- }
- OC.Plugins.attach('OCA.Files.FileList', this);
- },
- /**
- * Destroy / uninitialize this instance.
- */
- destroy: function() {
- if (this._newFileMenu) {
- this._newFileMenu.remove();
- }
- if (this._newButton) {
- this._newButton.remove();
- }
- if (this._detailsView) {
- this._detailsView.remove();
- }
- // TODO: also unregister other event handlers
- this.fileActions.off('registerAction', this._onFileActionsUpdated);
- this.fileActions.off('setDefault', this._onFileActionsUpdated);
- OC.Plugins.detach('OCA.Files.FileList', this);
- $('#app-content').off('appresized', this._onResize);
- },
- multiSelectMenuClick: function (ev, action) {
- switch (action) {
- case 'delete':
- this._onClickDeleteSelected(ev)
- break;
- case 'download':
- this._onClickDownloadSelected(ev);
- break;
- case 'copyMove':
- this._onClickCopyMoveSelected(ev);
- break;
- case 'restore':
- this._onClickRestoreSelected(ev);
- break;
- }
- },
- /**
- * Initializes the file actions, set up listeners.
- *
- * @param {OCA.Files.FileActions} fileActions file actions
- */
- _initFileActions: function(fileActions) {
- var self = this;
- this.fileActions = fileActions;
- if (!this.fileActions) {
- this.fileActions = new OCA.Files.FileActions();
- this.fileActions.registerDefaultActions();
- }
- if (this._detailsView) {
- this.fileActions.registerAction({
- name: 'Details',
- displayName: t('files', 'Details'),
- mime: 'all',
- order: -50,
- iconClass: 'icon-details',
- permissions: OC.PERMISSION_NONE,
- actionHandler: function(fileName, context) {
- self._updateDetailsView(fileName);
- }
- });
- }
- this._onFileActionsUpdated = _.debounce(_.bind(this._onFileActionsUpdated, this), 100);
- this.fileActions.on('registerAction', this._onFileActionsUpdated);
- this.fileActions.on('setDefault', this._onFileActionsUpdated);
- },
- /**
- * Returns a unique model for the given file name.
- *
- * @param {string|object} fileName file name or jquery row
- * @return {OCA.Files.FileInfoModel} file info model
- */
- getModelForFile: function(fileName) {
- var self = this;
- var $tr;
- // jQuery object ?
- if (fileName.is) {
- $tr = fileName;
- fileName = $tr.attr('data-file');
- } else {
- $tr = this.findFileEl(fileName);
- }
- if (!$tr || !$tr.length) {
- return null;
- }
- // if requesting the selected model, return it
- if (this._currentFileModel && this._currentFileModel.get('name') === fileName) {
- return this._currentFileModel;
- }
- // TODO: note, this is a temporary model required for synchronising
- // state between different views.
- // In the future the FileList should work with Backbone.Collection
- // and contain existing models that can be used.
- // This method would in the future simply retrieve the matching model from the collection.
- var model = new OCA.Files.FileInfoModel(this.elementToFile($tr), {
- filesClient: this.filesClient
- });
- if (!model.get('path')) {
- model.set('path', this.getCurrentDirectory(), {silent: true});
- }
- model.on('change', function(model) {
- // re-render row
- var highlightState = $tr.hasClass('highlighted');
- $tr = self.updateRow(
- $tr,
- model.toJSON(),
- {updateSummary: true, silent: false, animate: true}
- );
- // restore selection state
- var selected = !!self._selectedFiles[$tr.data('id')];
- self._selectFileEl($tr, selected);
- $tr.toggleClass('highlighted', highlightState);
- });
- model.on('busy', function(model, state) {
- self.showFileBusyState($tr, state);
- });
- return model;
- },
- /**
- * Displays the details view for the given file and
- * selects the given tab
- *
- * @param {string|OCA.Files.FileInfoModel} fileName file name or FileInfoModel for which to show details
- * @param {string} [tabId] optional tab id to select
- */
- showDetailsView: function(fileName, tabId) {
- this._updateDetailsView(fileName);
- if (tabId) {
- this._detailsView.selectTab(tabId);
- }
- OC.Apps.showAppSidebar(this._detailsView.$el);
- },
- /**
- * Update the details view to display the given file
- *
- * @param {string|OCA.Files.FileInfoModel} fileName file name from the current list or a FileInfoModel object
- * @param {boolean} [show=true] whether to open the sidebar if it was closed
- */
- _updateDetailsView: function(fileName, show) {
- if (!this._detailsView) {
- return;
- }
- // show defaults to true
- show = _.isUndefined(show) || !!show;
- var oldFileInfo = this._detailsView.getFileInfo();
- if (oldFileInfo) {
- // TODO: use more efficient way, maybe track the highlight
- this.$fileList.children().filterAttr('data-id', '' + oldFileInfo.get('id')).removeClass('highlighted');
- oldFileInfo.off('change', this._onSelectedModelChanged, this);
- }
- if (!fileName) {
- this._detailsView.setFileInfo(null);
- if (this._currentFileModel) {
- this._currentFileModel.off();
- }
- this._currentFileModel = null;
- OC.Apps.hideAppSidebar(this._detailsView.$el);
- return;
- }
- if (show && this._detailsView.$el.hasClass('disappear')) {
- OC.Apps.showAppSidebar(this._detailsView.$el);
- }
- if (fileName instanceof OCA.Files.FileInfoModel) {
- var model = fileName;
- } else {
- var $tr = this.findFileEl(fileName);
- var model = this.getModelForFile($tr);
- $tr.addClass('highlighted');
- }
- this._currentFileModel = model;
- this._replaceDetailsViewElementIfNeeded();
- this._detailsView.setFileInfo(model);
- this._detailsView.$el.scrollTop(0);
- },
- /**
- * Replaces the current details view element with the details view
- * element of this file list.
- *
- * Each file list has its own DetailsView object, and each one has its
- * own root element, but there can be just one details view/sidebar
- * element in the document. This helper method replaces the current
- * details view/sidebar element in the document with the element from
- * the DetailsView object of this file list.
- */
- _replaceDetailsViewElementIfNeeded: function() {
- var $appSidebar = $('#app-sidebar');
- if ($appSidebar.length === 0) {
- this._detailsView.$el.insertAfter($('#app-content'));
- } else if ($appSidebar[0] !== this._detailsView.el) {
- // "replaceWith()" can not be used here, as it removes the old
- // element instead of just detaching it.
- this._detailsView.$el.insertBefore($appSidebar);
- $appSidebar.detach();
- }
- },
- /**
- * Event handler for when the window size changed
- */
- _onResize: function() {
- var containerWidth = this.$el.width();
- var actionsWidth = 0;
- $.each(this.$el.find('#controls .actions'), function(index, action) {
- actionsWidth += $(action).outerWidth();
- });
- this.breadcrumb._resize();
- this.$table.find('>thead').width($('#app-content').width() - OC.Util.getScrollBarWidth());
- },
- /**
- * Toggle showing gridview by default or not
- *
- * @returns {undefined}
- */
- _onGridviewChange: function() {
- var show = this.$showGridView.is(':checked');
- // only save state if user is logged in
- if (OC.currentUser) {
- $.post(OC.generateUrl('/apps/files/api/v1/showgridview'), {
- show: show
- });
- }
- this.$showGridView.next('#view-toggle')
- .removeClass('icon-toggle-filelist icon-toggle-pictures')
- .addClass(show ? 'icon-toggle-filelist' : 'icon-toggle-pictures')
-
- $('.list-container').toggleClass('view-grid', show);
- },
- /**
- * Event handler when leaving previously hidden state
- */
- _onShow: function(e) {
- if (this.shown) {
- if (e.itemId === this.id) {
- this._setCurrentDir('/', false);
- }
- // Only reload if we don't navigate to a different directory
- if (typeof e.dir === 'undefined' || e.dir === this.getCurrentDirectory()) {
- this.reload();
- }
- }
- this.shown = true;
- },
- /**
- * Event handler for when the URL changed
- */
- _onUrlChanged: function(e) {
- if (e && _.isString(e.dir)) {
- var currentDir = this.getCurrentDirectory();
- // this._currentDirectory is NULL when fileList is first initialised
- if( (this._currentDirectory || this.$el.find('#dir').val()) && currentDir === e.dir) {
- return;
- }
- this.changeDirectory(e.dir, false, true);
- }
- },
- /**
- * Selected/deselects the given file element and updated
- * the internal selection cache.
- *
- * @param {Object} $tr single file row element
- * @param {bool} state true to select, false to deselect
- */
- _selectFileEl: function($tr, state, showDetailsView) {
- var $checkbox = $tr.find('td.selection>.selectCheckBox');
- var oldData = !!this._selectedFiles[$tr.data('id')];
- var data;
- $checkbox.prop('checked', state);
- $tr.toggleClass('selected', state);
- // already selected ?
- if (state === oldData) {
- return;
- }
- data = this.elementToFile($tr);
- if (state) {
- this._selectedFiles[$tr.data('id')] = data;
- this._selectionSummary.add(data);
- }
- else {
- delete this._selectedFiles[$tr.data('id')];
- this._selectionSummary.remove(data);
- }
- if (this._detailsView && !this._detailsView.$el.hasClass('disappear')) {
- // hide sidebar
- this._updateDetailsView(null);
- }
- this.$el.find('.select-all').prop('checked', this._selectionSummary.getTotal() === this.files.length);
- },
- /**
- * Event handler for when clicking on files to select them
- */
- _onClickFile: function(event) {
- var $tr = $(event.target).closest('tr');
- if ($tr.hasClass('dragging')) {
- return;
- }
- if (this._allowSelection && (event.ctrlKey || event.shiftKey)) {
- event.preventDefault();
- if (event.shiftKey) {
- var $lastTr = $(this._lastChecked);
- var lastIndex = $lastTr.index();
- var currentIndex = $tr.index();
- var $rows = this.$fileList.children('tr');
- // last clicked checkbox below current one ?
- if (lastIndex > currentIndex) {
- var aux = lastIndex;
- lastIndex = currentIndex;
- currentIndex = aux;
- }
- // auto-select everything in-between
- for (var i = lastIndex + 1; i < currentIndex; i++) {
- this._selectFileEl($rows.eq(i), true);
- }
- }
- else {
- this._lastChecked = $tr;
- }
- var $checkbox = $tr.find('td.selection>.selectCheckBox');
- this._selectFileEl($tr, !$checkbox.prop('checked'));
- this.updateSelectionSummary();
- } else {
- // clicked directly on the name
- if (!this._detailsView || $(event.target).is('.nametext, .name') || $(event.target).closest('.nametext').length) {
- var filename = $tr.attr('data-file');
- var renaming = $tr.data('renaming');
- if (!renaming) {
- this.fileActions.currentFile = $tr.find('td');
- var mime = this.fileActions.getCurrentMimeType();
- var type = this.fileActions.getCurrentType();
- var permissions = this.fileActions.getCurrentPermissions();
- var action = this.fileActions.getDefault(mime,type, permissions);
- if (action) {
- event.preventDefault();
- // also set on global object for legacy apps
- window.FileActions.currentFile = this.fileActions.currentFile;
- action(filename, {
- $file: $tr,
- fileList: this,
- fileActions: this.fileActions,
- dir: $tr.attr('data-path') || this.getCurrentDirectory()
- });
- }
- // deselect row
- $(event.target).closest('a').blur();
- }
- } else {
- // Even if there is no Details action the default event
- // handler is prevented for consistency (although there
- // should always be a Details action); otherwise the link
- // would be downloaded by the browser when the user expected
- // the details to be shown.
- event.preventDefault();
- var filename = $tr.attr('data-file');
- this.fileActions.currentFile = $tr.find('td');
- var mime = this.fileActions.getCurrentMimeType();
- var type = this.fileActions.getCurrentType();
- var permissions = this.fileActions.getCurrentPermissions();
- var action = this.fileActions.get(mime, type, permissions)['Details'];
- if (action) {
- // also set on global object for legacy apps
- window.FileActions.currentFile = this.fileActions.currentFile;
- action(filename, {
- $file: $tr,
- fileList: this,
- fileActions: this.fileActions,
- dir: $tr.attr('data-path') || this.getCurrentDirectory()
- });
- }
- }
- }
- },
- /**
- * Event handler for when clicking on a file's checkbox
- */
- _onClickFileCheckbox: function(e) {
- var $tr = $(e.target).closest('tr');
- var state = !$tr.hasClass('selected');
- this._selectFileEl($tr, state);
- this._lastChecked = $tr;
- this.updateSelectionSummary();
- if (this._detailsView && !this._detailsView.$el.hasClass('disappear')) {
- // hide sidebar
- this._updateDetailsView(null);
- }
- },
- /**
- * Event handler for when selecting/deselecting all files
- */
- _onClickSelectAll: function(e) {
- var hiddenFiles = this.$fileList.find('tr.hidden');
- var checked = e.target.checked;
- if (hiddenFiles.length > 0) {
- // set indeterminate alongside checked
- e.target.indeterminate = checked;
- } else {
- e.target.indeterminate = false
- }
- // Select only visible checkboxes to filter out unmatched file in search
- this.$fileList.find('td.selection > .selectCheckBox:visible').prop('checked', checked)
- .closest('tr').toggleClass('selected', checked);
- if (checked) {
- for (var i = 0; i < this.files.length; i++) {
- // a search will automatically hide the unwanted rows
- // let's only select the matches
- var fileData = this.files[i];
- var fileRow = this.$fileList.find('tr[data-id=' + fileData.id + ']');
- // do not select already selected ones
- if (!fileRow.hasClass('hidden') && _.isUndefined(this._selectedFiles[fileData.id])) {
- this._selectedFiles[fileData.id] = fileData;
- this._selectionSummary.add(fileData);
- }
- }
- } else {
- // if we have some hidden row, then we're in a search
- // Let's only deselect the visible ones
- if (hiddenFiles.length > 0) {
- var visibleFiles = this.$fileList.find('tr:not(.hidden)');
- var self = this;
- visibleFiles.each(function() {
- var id = parseInt($(this).data('id'));
- // do not deselect already deselected ones
- if (!_.isUndefined(self._selectedFiles[id])) {
- // a search will automatically hide the unwanted rows
- // let's only select the matches
- var fileData = self._selectedFiles[id];
- delete self._selectedFiles[fileData.id];
- self._selectionSummary.remove(fileData);
- }
- });
- } else {
- this._selectedFiles = {};
- this._selectionSummary.clear();
- }
- }
- this.updateSelectionSummary();
- if (this._detailsView && !this._detailsView.$el.hasClass('disappear')) {
- // hide sidebar
- this._updateDetailsView(null);
- }
- },
- /**
- * Event handler for when clicking on "Download" for the selected files
- */
- _onClickDownloadSelected: function(event) {
- var files;
- var self = this;
- var dir = this.getCurrentDirectory();
- if (this.isAllSelected() && this.getSelectedFiles().length > 1) {
- files = OC.basename(dir);
- dir = OC.dirname(dir) || '/';
- }
- else {
- files = _.pluck(this.getSelectedFiles(), 'name');
- }
- // don't allow a second click on the download action
- if(this.fileMultiSelectMenu.isDisabled('download')) {
- return false;
- }
- this.fileMultiSelectMenu.toggleLoading('download', true);
- var disableLoadingState = function(){
- self.fileMultiSelectMenu.toggleLoading('download', false);
- };
- if(this.getSelectedFiles().length > 1) {
- OCA.Files.Files.handleDownload(this.getDownloadUrl(files, dir, true), disableLoadingState);
- }
- else {
- var first = this.getSelectedFiles()[0];
- OCA.Files.Files.handleDownload(this.getDownloadUrl(first.name, dir, true), disableLoadingState);
- }
- event.preventDefault();
- },
- /**
- * Event handler for when clicking on "Move" for the selected files
- */
- _onClickCopyMoveSelected: function(event) {
- var files;
- var self = this;
- files = _.pluck(this.getSelectedFiles(), 'name');
- // don't allow a second click on the download action
- if(this.fileMultiSelectMenu.isDisabled('copyMove')) {
- return false;
- }
- var disableLoadingState = function(){
- self.fileMultiSelectMenu.toggleLoading('copyMove', false);
- };
- var actions = this.isSelectedMovable() ? OC.dialogs.FILEPICKER_TYPE_COPY_MOVE : OC.dialogs.FILEPICKER_TYPE_COPY;
- OC.dialogs.filepicker(t('files', 'Choose target folder'), function(targetPath, type) {
- self.fileMultiSelectMenu.toggleLoading('copyMove', true);
- if (type === OC.dialogs.FILEPICKER_TYPE_COPY) {
- self.copy(files, targetPath, disableLoadingState);
- }
- if (type === OC.dialogs.FILEPICKER_TYPE_MOVE) {
- self.move(files, targetPath, disableLoadingState);
- }
- }, false, "httpd/unix-directory", true, actions);
- event.preventDefault();
- },
- /**
- * Event handler for when clicking on "Delete" for the selected files
- */
- _onClickDeleteSelected: function(event) {
- var files = null;
- if (!this.isAllSelected()) {
- files = _.pluck(this.getSelectedFiles(), 'name');
- }
- this.do_delete(files);
- event.preventDefault();
- },
- /**
- * Event handler when clicking on a table header
- */
- _onClickHeader: function(e) {
- if (this.$table.hasClass('multiselect')) {
- return;
- }
- var $target = $(e.target);
- var sort;
- if (!$target.is('a')) {
- $target = $target.closest('a');
- }
- sort = $target.attr('data-sort');
- if (sort && this._allowSorting) {
- if (this._sort === sort) {
- this.setSort(sort, (this._sortDirection === 'desc')?'asc':'desc', true, true);
- }
- else {
- if ( sort === 'name' ) { //default sorting of name is opposite to size and mtime
- this.setSort(sort, 'asc', true, true);
- }
- else {
- this.setSort(sort, 'desc', true, true);
- }
- }
- }
- },
- /**
- * Event handler when clicking on a bread crumb
- */
- _onClickBreadCrumb: function(e) {
- // Select a crumb or a crumb in the menu
- var $el = $(e.target).closest('.crumb, .crumblist'),
- $targetDir = $el.data('dir');
- if ($targetDir !== undefined && e.which === 1) {
- e.preventDefault();
- this.changeDirectory($targetDir, true, true);
- this.updateSearch();
- }
- },
- /**
- * Event handler for when scrolling the list container.
- * This appends/renders the next page of entries when reaching the bottom.
- */
- _onScroll: function(e) {
- if (this.$container.scrollTop() + this.$container.height() > this.$el.height() - 300) {
- this._nextPage(true);
- }
- },
- /**
- * Event handler when dropping on a breadcrumb
- */
- _onDropOnBreadCrumb: function( event, ui ) {
- var self = this;
- var $target = $(event.target);
- if (!$target.is('.crumb, .crumblist')) {
- $target = $target.closest('.crumb, .crumblist');
- }
- var targetPath = $(event.target).data('dir');
- var dir = this.getCurrentDirectory();
- while (dir.substr(0,1) === '/') {//remove extra leading /'s
- dir = dir.substr(1);
- }
- dir = '/' + dir;
- if (dir.substr(-1,1) !== '/') {
- dir = dir + '/';
- }
- // do nothing if dragged on current dir
- if (targetPath === dir || targetPath + '/' === dir) {
- return;
- }
- var files = this.getSelectedFiles();
- if (files.length === 0) {
- // single one selected without checkbox?
- files = _.map(ui.helper.find('tr'), function(el) {
- return self.elementToFile($(el));
- });
- }
- var movePromise = this.move(_.pluck(files, 'name'), targetPath);
- // re-enable td elements to be droppable
- // sometimes the filename drop handler is still called after re-enable,
- // it seems that waiting for a short time before re-enabling solves the problem
- setTimeout(function() {
- self.$el.find('td.filename.ui-droppable').droppable('enable');
- }, 10);
- return movePromise;
- },
- /**
- * Sets a new page title
- */
- setPageTitle: function(title){
- if (title) {
- title += ' - ';
- } else {
- title = '';
- }
- title += this.appName;
- // Sets the page title with the " - Nextcloud" suffix as in templates
- window.document.title = title + ' - ' + oc_defaults.title;
- return true;
- },
- /**
- * Returns the file info for the given file name from the internal collection.
- *
- * @param {string} fileName file name
- * @return {OCA.Files.FileInfo} file info or null if it was not found
- *
- * @since 8.2
- */
- findFile: function(fileName) {
- return _.find(this.files, function(aFile) {
- return (aFile.name === fileName);
- }) || null;
- },
- /**
- * Returns the tr element for a given file name, but only if it was already rendered.
- *
- * @param {string} fileName file name
- * @return {Object} jQuery object of the matching row
- */
- findFileEl: function(fileName){
- // use filterAttr to avoid escaping issues
- return this.$fileList.find('tr').filterAttr('data-file', fileName);
- },
- /**
- * Returns the file data from a given file element.
- * @param $el file tr element
- * @return file data
- */
- elementToFile: function($el){
- $el = $($el);
- var data = {
- id: parseInt($el.attr('data-id'), 10),
- name: $el.attr('data-file'),
- mimetype: $el.attr('data-mime'),
- mtime: parseInt($el.attr('data-mtime'), 10),
- type: $el.attr('data-type'),
- etag: $el.attr('data-etag'),
- permissions: parseInt($el.attr('data-permissions'), 10),
- hasPreview: $el.attr('data-has-preview') === 'true',
- isEncrypted: $el.attr('data-e2eencrypted') === 'true'
- };
- var size = $el.attr('data-size');
- if (size) {
- data.size = parseInt(size, 10);
- }
- var icon = $el.attr('data-icon');
- if (icon) {
- data.icon = icon;
- }
- var mountType = $el.attr('data-mounttype');
- if (mountType) {
- data.mountType = mountType;
- }
- var path = $el.attr('data-path');
- if (path) {
- data.path = path;
- }
- return data;
- },
- /**
- * Appends the next page of files into the table
- * @param animate true to animate the new elements
- * @return array of DOM elements of the newly added files
- */
- _nextPage: function(animate) {
- var index = this.$fileList.children().length,
- count = this.pageSize(),
- hidden,
- tr,
- fileData,
- newTrs = [],
- isAllSelected = this.isAllSelected(),
- showHidden = this._filesConfig.get('showhidden');
- if (index >= this.files.length) {
- return false;
- }
- while (count > 0 && index < this.files.length) {
- fileData = this.files[index];
- if (this._filter) {
- hidden = fileData.name.toLowerCase().indexOf(this._filter.toLowerCase()) === -1;
- } else {
- hidden = false;
- }
- tr = this._renderRow(fileData, {updateSummary: false, silent: true, hidden: hidden});
- this.$fileList.append(tr);
- if (isAllSelected || this._selectedFiles[fileData.id]) {
- tr.addClass('selected');
- tr.find('.selectCheckBox').prop('checked', true);
- }
- if (animate) {
- tr.addClass('appear transparent');
- }
- newTrs.push(tr);
- index++;
- // only count visible rows
- if (showHidden || !tr.hasClass('hidden-file')) {
- count--;
- }
- }
- // trigger event for newly added rows
- if (newTrs.length > 0) {
- this.$fileList.trigger($.Event('fileActionsReady', {fileList: this, $files: newTrs}));
- }
- if (animate) {
- // defer, for animation
- window.setTimeout(function() {
- for (var i = 0; i < newTrs.length; i++ ) {
- newTrs[i].removeClass('transparent');
- }
- }, 0);
- }
- return newTrs;
- },
- /**
- * Event handler for when file actions were updated.
- * This will refresh the file actions on the list.
- */
- _onFileActionsUpdated: function() {
- var self = this;
- var $files = this.$fileList.find('tr');
- if (!$files.length) {
- return;
- }
- $files.each(function() {
- self.fileActions.display($(this).find('td.filename'), false, self);
- });
- this.$fileList.trigger($.Event('fileActionsReady', {fileList: this, $files: $files}));
- },
- /**
- * Sets the files to be displayed in the list.
- * This operation will re-render the list and update the summary.
- * @param filesArray array of file data (map)
- */
- setFiles: function(filesArray) {
- var self = this;
- // detach to make adding multiple rows faster
- this.files = filesArray;
- this.$fileList.empty();
- if (this._allowSelection) {
- // The results table, which has no selection column, checks
- // whether the main table has a selection column or not in order
- // to align its contents with those of the main table.
- this.$el.addClass('has-selection');
- }
- // clear "Select all" checkbox
- this.$el.find('.select-all').prop('checked', false);
- // Save full files list while rendering
- this.isEmpty = this.files.length === 0;
- this._nextPage();
- this.updateEmptyContent();
- this.fileSummary.calculate(this.files);
- this._selectedFiles = {};
- this._selectionSummary.clear();
- this.updateSelectionSummary();
- $(window).scrollTop(0);
- this.$fileList.trigger(jQuery.Event('updated'));
- _.defer(function() {
- self.$el.closest('#app-content').trigger(jQuery.Event('apprendered'));
- });
- },
- /**
- * Returns whether the given file info must be hidden
- *
- * @param {OC.Files.FileInfo} fileInfo file info
- *
- * @return {boolean} true if the file is a hidden file, false otherwise
- */
- _isHiddenFile: function(file) {
- return file.name && file.name.charAt(0) === '.';
- },
- /**
- * Returns the icon URL matching the given file info
- *
- * @param {OC.Files.FileInfo} fileInfo file info
- *
- * @return {string} icon URL
- */
- _getIconUrl: function(fileInfo) {
- var mimeType = fileInfo.mimetype || 'application/octet-stream';
- if (mimeType === 'httpd/unix-directory') {
- // use default folder icon
- if (fileInfo.mountType === 'shared' || fileInfo.mountType === 'shared-root') {
- return OC.MimeType.getIconUrl('dir-shared');
- } else if (fileInfo.mountType === 'external-root') {
- return OC.MimeType.getIconUrl('dir-external');
- } else if (fileInfo.mountType !== undefined && fileInfo.mountType !== '') {
- return OC.MimeType.getIconUrl('dir-' + fileInfo.mountType);
- }
- return OC.MimeType.getIconUrl('dir');
- }
- return OC.MimeType.getIconUrl(mimeType);
- },
- /**
- * Creates a new table row element using the given file data.
- * @param {OC.Files.FileInfo} fileData file info attributes
- * @param options map of attributes
- * @return new tr element (not appended to the table)
- */
- _createRow: function(fileData, options) {
- var td, simpleSize, basename, extension, sizeColor,
- icon = fileData.icon || this._getIconUrl(fileData),
- name = fileData.name,
- // TODO: get rid of type, only use mime type
- type = fileData.type || 'file',
- mtime = parseInt(fileData.mtime, 10),
- mime = fileData.mimetype,
- path = fileData.path,
- dataIcon = null,
- linkUrl;
- options = options || {};
- if (isNaN(mtime)) {
- mtime = new Date().getTime();
- }
- if (type === 'dir') {
- mime = mime || 'httpd/unix-directory';
- if (fileData.isEncrypted) {
- icon = OC.MimeType.getIconUrl('dir-encrypted');
- dataIcon = icon;
- } else if (fileData.mountType && fileData.mountType.indexOf('external') === 0) {
- icon = OC.MimeType.getIconUrl('dir-external');
- dataIcon = icon;
- }
- }
- var permissions = fileData.permissions;
- if (permissions === undefined || permissions === null) {
- permissions = this.getDirectoryPermissions();
- }
- //containing tr
- var tr = $('<tr></tr>').attr({
- "data-id" : fileData.id,
- "data-type": type,
- "data-size": fileData.size,
- "data-file": name,
- "data-mime": mime,
- "data-mtime": mtime,
- "data-etag": fileData.etag,
- "data-permissions": permissions,
- "data-has-preview": fileData.hasPreview !== false,
- "data-e2eencrypted": fileData.isEncrypted === true
- });
- if (dataIcon) {
- // icon override
- tr.attr('data-icon', dataIcon);
- }
- if (fileData.mountType) {
- // dirInfo (parent) only exist for the "real" file list
- if (this.dirInfo.id) {
- // FIXME: HACK: detect shared-root
- if (fileData.mountType === 'shared' && this.dirInfo.mountType !== 'shared' && this.dirInfo.mountType !== 'shared-root') {
- // if parent folder isn't share, assume the displayed folder is a share root
- fileData.mountType = 'shared-root';
- } else if (fileData.mountType === 'external' && this.dirInfo.mountType !== 'external' && this.dirInfo.mountType !== 'external-root') {
- // if parent folder isn't external, assume the displayed folder is the external storage root
- fileData.mountType = 'external-root';
- }
- }
- tr.attr('data-mounttype', fileData.mountType);
- }
- if (!_.isUndefined(path)) {
- tr.attr('data-path', path);
- }
- else {
- path = this.getCurrentDirectory();
- }
- // selection td
- if (this._allowSelection) {
- td = $('<td class="selection"></td>');
- td.append(
- '<input id="select-' + this.id + '-' + fileData.id +
- '" type="checkbox" class="selectCheckBox checkbox"/><label for="select-' + this.id + '-' + fileData.id + '">' +
- '<span class="hidden-visually">' + t('files', 'Select') + '</span>' +
- '</label>'
- );
- tr.append(td);
- }
- // filename td
- td = $('<td class="filename"></td>');
- // linkUrl
- if (mime === 'httpd/unix-directory') {
- linkUrl = this.linkTo(path + '/' + name);
- }
- else {
- linkUrl = this.getDownloadUrl(name, path, type === 'dir');
- }
- var linkElem = $('<a></a>').attr({
- "class": "name",
- "href": linkUrl
- });
- linkElem.append('<div class="thumbnail-wrapper"><div class="thumbnail" style="background-image:url(' + icon + ');"></div></div>');
- // from here work on the display name
- name = fileData.displayName || name;
- // show hidden files (starting with a dot) completely in gray
- if(name.indexOf('.') === 0) {
- basename = '';
- extension = name;
- // split extension from filename for non dirs
- } else if (mime !== 'httpd/unix-directory' && name.indexOf('.') !== -1) {
- basename = name.substr(0, name.lastIndexOf('.'));
- extension = name.substr(name.lastIndexOf('.'));
- } else {
- basename = name;
- extension = false;
- }
- var nameSpan=$('<span></span>').addClass('nametext');
- var innernameSpan = $('<span></span>').addClass('innernametext').text(basename);
- var conflictingItems = this.$fileList.find('tr[data-file="' + this._jqSelEscape(name) + '"]');
- if (conflictingItems.length !== 0) {
- if (conflictingItems.length === 1) {
- // Update the path on the first conflicting item
- var $firstConflict = $(conflictingItems[0]),
- firstConflictPath = $firstConflict.attr('data-path') + '/';
- if (firstConflictPath.charAt(0) === '/') {
- firstConflictPath = firstConflictPath.substr(1);
- }
- if (firstConflictPath && firstConflictPath !== '/') {
- $firstConflict.find('td.filename span.innernametext').prepend($('<span></span>').addClass('conflict-path').text(firstConflictPath));
- }
- }
- var conflictPath = path + '/';
- if (conflictPath.charAt(0) === '/') {
- conflictPath = conflictPath.substr(1);
- }
- if (path && path !== '/') {
- nameSpan.append($('<span></span>').addClass('conflict-path').text(conflictPath));
- }
- }
- nameSpan.append(innernameSpan);
- linkElem.append(nameSpan);
- if (extension) {
- nameSpan.append($('<span></span>').addClass('extension').text(extension));
- }
- if (fileData.extraData) {
- if (fileData.extraData.charAt(0) === '/') {
- fileData.extraData = fileData.extraData.substr(1);
- }
- nameSpan.addClass('extra-data').attr('title', fileData.extraData);
- nameSpan.tooltip({placement: 'top'});
- }
- // dirs can show the number of uploaded files
- if (mime === 'httpd/unix-directory') {
- linkElem.append($('<span></span>').attr({
- 'class': 'uploadtext',
- 'currentUploads': 0
- }));
- }
- td.append(linkElem);
- tr.append(td);
- // size column
- if (typeof(fileData.size) !== 'undefined' && fileData.size >= 0) {
- simpleSize = humanFileSize(parseInt(fileData.size, 10), true);
- // rgb(118, 118, 118) / #767676
- // min. color contrast for normal text on white background according to WCAG AA
- sizeColor = Math.round(118-Math.pow((fileData.size/(1024*1024)),2));
- if (OCA.Accessibility && OCA.Accessibility.theme === 'themedark') {
- sizeColor = Math.abs(sizeColor);
- }
- } else {
- simpleSize = t('files', 'Pending');
- }
- td = $('<td></td>').attr({
- "class": "filesize",
- "style": 'color:rgb(' + sizeColor + ',' + sizeColor + ',' + sizeColor + ')'
- }).text(simpleSize);
- tr.append(td);
- // date column (1000 milliseconds to seconds, 60 seconds, 60 minutes, 24 hours)
- // difference in days multiplied by 5 - brightest shade for files older than 32 days (160/5)
- var modifiedColor = Math.round(((new Date()).getTime() - mtime )/1000/60/60/24*5 );
- // ensure that the brightest color is still readable
- // rgb(118, 118, 118) / #767676
- // min. color contrast for normal text on white background according to WCAG AA
- if (modifiedColor >= '118') {
- modifiedColor = 118;
- }
- if (OCA.Accessibility && OCA.Accessibility.theme === 'themedark') {
- modifiedColor = Math.abs(modifiedColor);
- // ensure that the dimmest color is still readable
- // rgb(130, 130, 130) / #828282
- // min. color contrast for normal text on black background according to WCAG AA
- if (modifiedColor < 130) {
- modifiedColor = 130;
- }
- }
- var formatted;
- var text;
- if (mtime > 0) {
- formatted = OC.Util.formatDate(mtime);
- text = OC.Util.relativeModifiedDate(mtime);
- } else {
- formatted = t('files', 'Unable to determine date');
- text = '?';
- }
- td = $('<td></td>').attr({ "class": "date" });
- td.append($('<span></span>').attr({
- "class": "modified live-relative-timestamp",
- "title": formatted,
- "data-timestamp": mtime,
- "style": 'color:rgb('+modifiedColor+','+modifiedColor+','+modifiedColor+')'
- }).text(text)
- .tooltip({placement: 'top'})
- );
- tr.find('.filesize').text(simpleSize);
- tr.append(td);
- return tr;
- },
- /* escape a selector expression for jQuery */
- _jqSelEscape: function (expression) {
- if (expression) {
- return expression.replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, '\\$&');
- }
- return null;
- },
- /**
- * Adds an entry to the files array and also into the DOM
- * in a sorted manner.
- *
- * @param {OC.Files.FileInfo} fileData map of file attributes
- * @param {Object} [options] map of attributes
- * @param {boolean} [options.updateSummary] true to update the summary
- * after adding (default), false otherwise. Defaults to true.
- * @param {boolean} [options.silent] true to prevent firing events like "fileActionsReady",
- * defaults to false.
- * @param {boolean} [options.animate] true to animate the thumbnail image after load
- * defaults to true.
- * @return new tr element (not appended to the table)
- */
- add: function(fileData, options) {
- var index;
- var $tr;
- var $rows;
- var $insertionPoint;
- options = _.extend({animate: true}, options || {});
- // there are three situations to cover:
- // 1) insertion point is visible on the current page
- // 2) insertion point is on a not visible page (visible after scrolling)
- // 3) insertion point is at the end of the list
- $rows = this.$fileList.children();
- index = this._findInsertionIndex(fileData);
- if (index > this.files.length) {
- index = this.files.length;
- }
- else {
- $insertionPoint = $rows.eq(index);
- }
- // is the insertion point visible ?
- if ($insertionPoint.length) {
- // only render if it will really be inserted
- $tr = this._renderRow(fileData, options);
- $insertionPoint.before($tr);
- }
- else {
- // if insertion point is after the last visible
- // entry, append
- if (index === $rows.length) {
- $tr = this._renderRow(fileData, options);
- this.$fileList.append($tr);
- }
- }
- this.isEmpty = false;
- this.files.splice(index, 0, fileData);
- if ($tr && options.animate) {
- $tr.addClass('appear transparent');
- window.setTimeout(function() {
- $tr.removeClass('transparent');
- $("#fileList tr").removeClass('mouseOver');
- $tr.addClass('mouseOver');
- });
- }
- if (options.scrollTo) {
- this.scrollTo(fileData.name);
- }
- // defaults to true if not defined
- if (typeof(options.updateSummary) === 'undefined' || !!options.updateSummary) {
- this.fileSummary.add(fileData, true);
- this.updateEmptyContent();
- }
- return $tr;
- },
- /**
- * Creates a new row element based on the given attributes
- * and returns it.
- *
- * @param {OC.Files.FileInfo} fileData map of file attributes
- * @param {Object} [options] map of attributes
- * @param {int} [options.index] index at which to insert the element
- * @param {boolean} [options.updateSummary] true to update the summary
- * after adding (default), false otherwise. Defaults to true.
- * @param {boolean} [options.animate] true to animate the thumbnail image after load
- * defaults to true.
- * @return new tr element (not appended to the table)
- */
- _renderRow: function(fileData, options) {
- options = options || {};
- var type = fileData.type || 'file',
- mime = fileData.mimetype,
- path = fileData.path || this.getCurrentDirectory(),
- permissions = parseInt(fileData.permissions, 10) || 0;
- var isEndToEndEncrypted = (type === 'dir' && fileData.isEncrypted);
- if (!isEndToEndEncrypted && fileData.isShareMountPoint) {
- permissions = permissions | OC.PERMISSION_UPDATE;
- }
- if (type === 'dir') {
- mime = mime || 'httpd/unix-directory';
- }
- var tr = this._createRow(
- fileData,
- options
- );
- var filenameTd = tr.find('td.filename');
- // TODO: move dragging to FileActions ?
- // enable drag only for deletable files
- if (this._dragOptions && permissions & OC.PERMISSION_DELETE) {
- filenameTd.draggable(this._dragOptions);
- }
- // allow dropping on folders
- if (this._folderDropOptions && mime === 'httpd/unix-directory') {
- tr.droppable(this._folderDropOptions);
- }
- if (options.hidden) {
- tr.addClass('hidden');
- }
- if (this._isHiddenFile(fileData)) {
- tr.addClass('hidden-file');
- }
- // display actions
- this.fileActions.display(filenameTd, !options.silent, this);
- if (mime !== 'httpd/unix-directory' && fileData.hasPreview !== false) {
- var iconDiv = filenameTd.find('.thumbnail');
- // lazy load / newly inserted td ?
- // the typeof check ensures that the default value of animate is true
- if (typeof(options.animate) === 'undefined' || !!options.animate) {
- this.lazyLoadPreview({
- fileId: fileData.id,
- path: path + '/' + fileData.name,
- mime: mime,
- etag: fileData.etag,
- callback: function(url) {
- iconDiv.css('background-image', 'url("' + url + '")');
- }
- });
- }
- else {
- // set the preview URL directly
- var urlSpec = {
- file: path + '/' + fileData.name,
- c: fileData.etag
- };
- var previewUrl = this.generatePreviewUrl(urlSpec);
- previewUrl = previewUrl.replace('(', '%28').replace(')', '%29');
- iconDiv.css('background-image', 'url("' + previewUrl + '")');
- }
- }
- return tr;
- },
- /**
- * Returns the current directory
- * @method getCurrentDirectory
- * @return current directory
- */
- getCurrentDirectory: function(){
- return this._currentDirectory || this.$el.find('#dir').val() || '/';
- },
- /**
- * Returns the directory permissions
- * @return permission value as integer
- */
- getDirectoryPermissions: function() {
- return this && this.dirInfo && this.dirInfo.permissions ? this.dirInfo.permissions : parseInt(this.$el.find('#permissions').val(), 10);
- },
- /**
- * Changes the current directory and reload the file list.
- * @param {string} targetDir target directory (non URL encoded)
- * @param {boolean} [changeUrl=true] if the URL must not be changed (defaults to true)
- * @param {boolean} [force=false] set to true to force changing directory
- * @param {string} [fileId] optional file id, if known, to be appended in the URL
- */
- changeDirectory: function(targetDir, changeUrl, force, fileId) {
- var self = this;
- var currentDir = this.getCurrentDirectory();
- targetDir = targetDir || '/';
- if (!force && currentDir === targetDir) {
- return;
- }
- this._setCurrentDir(targetDir, changeUrl, fileId);
- // discard finished uploads list, we'll get it through a regular reload
- this._uploads = {};
- return this.reload().then(function(success){
- if (!success) {
- self.changeDirectory(currentDir, true);
- }
- });
- },
- linkTo: function(dir) {
- return OC.linkTo('files', 'index.php')+"?dir="+ encodeURIComponent(dir).replace(/%2F/g, '/');
- },
- /**
- * @param {string} path
- * @returns {boolean}
- */
- _isValidPath: function(path) {
- var sections = path.split('/');
- for (var i = 0; i < sections.length; i++) {
- if (sections[i] === '..') {
- return false;
- }
- }
- return path.toLowerCase().indexOf(decodeURI('%0a')) === -1 &&
- path.toLowerCase().indexOf(decodeURI('%00')) === -1;
- },
- /**
- * Sets the current directory name and updates the breadcrumb.
- * @param targetDir directory to display
- * @param changeUrl true to also update the URL, false otherwise (default)
- * @param {string} [fileId] file id
- */
- _setCurrentDir: function(targetDir, changeUrl, fileId) {
- targetDir = targetDir.replace(/\\/g, '/');
- if (!this._isValidPath(targetDir)) {
- targetDir = '/';
- changeUrl = true;
- }
- var previousDir = this.getCurrentDirectory(),
- baseDir = OC.basename(targetDir);
- if (baseDir !== '') {
- this.setPageTitle(baseDir);
- }
- else {
- this.setPageTitle();
- }
- if (targetDir.length > 0 && targetDir[0] !== '/') {
- targetDir = '/' + targetDir;
- }
- this._currentDirectory = targetDir;
- // legacy stuff
- this.$el.find('#dir').val(targetDir);
- if (changeUrl !== false) {
- var params = {
- dir: targetDir,
- previousDir: previousDir
- };
- if (fileId) {
- params.fileId = fileId;
- }
- this.$el.trigger(jQuery.Event('changeDirectory', params));
- }
- this.breadcrumb.setDirectory(this.getCurrentDirectory());
- },
- /**
- * Sets the current sorting and refreshes the list
- *
- * @param sort sort attribute name
- * @param direction sort direction, one of "asc" or "desc"
- * @param update true to update the list, false otherwise (default)
- * @param persist true to save changes in the database (default)
- */
- setSort: function(sort, direction, update, persist) {
- var comparator = FileList.Comparators[sort] || FileList.Comparators.name;
- this._sort = sort;
- this._sortDirection = (direction === 'desc')?'desc':'asc';
- this._sortComparator = function(fileInfo1, fileInfo2) {
- var isFavorite = function(fileInfo) {
- return fileInfo.tags && fileInfo.tags.indexOf(OC.TAG_FAVORITE) >= 0;
- };
- if (isFavorite(fileInfo1) && !isFavorite(fileInfo2)) {
- return -1;
- } else if (!isFavorite(fileInfo1) && isFavorite(fileInfo2)) {
- return 1;
- }
- return direction === 'asc' ? comparator(fileInfo1, fileInfo2) : -comparator(fileInfo1, fileInfo2);
- };
- this.$el.find('thead th .sort-indicator')
- .removeClass(this.SORT_INDICATOR_ASC_CLASS)
- .removeClass(this.SORT_INDICATOR_DESC_CLASS)
- .toggleClass('hidden', true)
- .addClass(this.SORT_INDICATOR_DESC_CLASS);
- this.$el.find('thead th.column-' + sort + ' .sort-indicator')
- .removeClass(this.SORT_INDICATOR_ASC_CLASS)
- .removeClass(this.SORT_INDICATOR_DESC_CLASS)
- .toggleClass('hidden', false)
- .addClass(direction === 'desc' ? this.SORT_INDICATOR_DESC_CLASS : this.SORT_INDICATOR_ASC_CLASS);
- if (update) {
- if (this._clientSideSort) {
- this.files.sort(this._sortComparator);
- this.setFiles(this.files);
- }
- else {
- this.reload();
- }
- }
- if (persist && OC.getCurrentUser().uid) {
- $.post(OC.generateUrl('/apps/files/api/v1/sorting'), {
- mode: sort,
- direction: direction
- });
- }
- },
- /**
- * Returns list of webdav properties to request
- */
- _getWebdavProperties: function() {
- return [].concat(this.filesClient.getPropfindProperties());
- },
- /**
- * Reloads the file list using ajax call
- *
- * @return ajax call object
- */
- reload: function() {
- this._selectedFiles = {};
- this._selectionSummary.clear();
- if (this._currentFileModel) {
- this._currentFileModel.off();
- }
- this._currentFileModel = null;
- this.$el.find('.select-all').prop('checked', false);
- this.showMask();
- this._reloadCall = this.filesClient.getFolderContents(
- this.getCurrentDirectory(), {
- includeParent: true,
- properties: this._getWebdavProperties()
- }
- );
- if (this._detailsView) {
- // close sidebar
- this._updateDetailsView(null);
- }
- this._setCurrentDir(this.getCurrentDirectory(), false);
- var callBack = this.reloadCallback.bind(this);
- return this._reloadCall.then(callBack, callBack);
- },
- reloadCallback: function(status, result) {
- delete this._reloadCall;
- this.hideMask();
- if (status === 401) {
- return false;
- }
- // Firewall Blocked request?
- if (status === 403) {
- // Go home
- this.changeDirectory('/');
- OC.Notification.show(t('files', 'This operation is forbidden'), {type: 'error'});
- return false;
- }
- // Did share service die or something else fail?
- if (status === 500) {
- // Go home
- this.changeDirectory('/');
- OC.Notification.show(t('files', 'This directory is unavailable, please check the logs or contact the administrator'),
- {type: 'error'}
- );
- return false;
- }
- if (status === 503) {
- // Go home
- if (this.getCurrentDirectory() !== '/') {
- this.changeDirectory('/');
- // TODO: read error message from exception
- OC.Notification.show(t('files', 'Storage is temporarily not available'),
- {type: 'error'}
- );
- }
- return false;
- }
- if (status === 400 || status === 404 || status === 405) {
- // go back home
- this.changeDirectory('/');
- return false;
- }
- // aborted ?
- if (status === 0){
- return true;
- }
- this.updateStorageStatistics(true);
- // first entry is the root
- this.dirInfo = result.shift();
- this.breadcrumb.setDirectoryInfo(this.dirInfo);
- if (this.dirInfo.permissions) {
- this._updateDirectoryPermissions();
- }
- result.sort(this._sortComparator);
- this.setFiles(result);
- if (this.dirInfo) {
- var newFileId = this.dirInfo.id;
- // update fileid in URL
- var params = {
- dir: this.getCurrentDirectory()
- };
- if (newFileId) {
- params.fileId = newFileId;
- }
- this.$el.trigger(jQuery.Event('afterChangeDirectory', params));
- }
- return true;
- },
- updateStorageStatistics: function(force) {
- OCA.Files.Files.updateStorageStatistics(this.getCurrentDirectory(), force);
- },
- updateStorageQuotas: function() {
- OCA.Files.Files.updateStorageQuotas();
- },
- /**
- * @deprecated do not use nor override
- */
- getAjaxUrl: function(action, params) {
- return OCA.Files.Files.getAjaxUrl(action, params);
- },
- getDownloadUrl: function(files, dir, isDir) {
- return OCA.Files.Files.getDownloadUrl(files, dir || this.getCurrentDirectory(), isDir);
- },
- getUploadUrl: function(fileName, dir) {
- if (_.isUndefined(dir)) {
- dir = this.getCurrentDirectory();
- }
- var pathSections = dir.split('/');
- if (!_.isUndefined(fileName)) {
- pathSections.push(fileName);
- }
- var encodedPath = '';
- _.each(pathSections, function(section) {
- if (section !== '') {
- encodedPath += '/' + encodeURIComponent(section);
- }
- });
- return OC.linkToRemoteBase('webdav') + encodedPath;
- },
- /**
- * Generates a preview URL based on the URL space.
- * @param urlSpec attributes for the URL
- * @param {int} urlSpec.x width
- * @param {int} urlSpec.y height
- * @param {String} urlSpec.file path to the file
- * @return preview URL
- */
- generatePreviewUrl: function(urlSpec) {
- urlSpec = urlSpec || {};
- if (!urlSpec.x) {
- urlSpec.x = this.$table.data('preview-x') || 250;
- }
- if (!urlSpec.y) {
- urlSpec.y = this.$table.data('preview-y') || 250;
- }
- urlSpec.x *= window.devicePixelRatio;
- urlSpec.y *= window.devicePixelRatio;
- urlSpec.x = Math.ceil(urlSpec.x);
- urlSpec.y = Math.ceil(urlSpec.y);
- urlSpec.forceIcon = 0;
- if (typeof urlSpec.fileId !== 'undefined') {
- delete urlSpec.file;
- return OC.generateUrl('/core/preview?') + $.param(urlSpec);
- } else {
- delete urlSpec.fileId;
- return OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
- }
- },
- /**
- * Lazy load a file's preview.
- *
- * @param path path of the file
- * @param mime mime type
- * @param callback callback function to call when the image was loaded
- * @param etag file etag (for caching)
- */
- lazyLoadPreview : function(options) {
- var self = this;
- var fileId = options.fileId;
- var path = options.path;
- var mime = options.mime;
- var ready = options.callback;
- var etag = options.etag;
- // get mime icon url
- var iconURL = OC.MimeType.getIconUrl(mime);
- var previewURL,
- urlSpec = {};
- ready(iconURL); // set mimeicon URL
- urlSpec.fileId = fileId;
- urlSpec.file = OCA.Files.Files.fixPath(path);
- if (options.x) {
- urlSpec.x = options.x;
- }
- if (options.y) {
- urlSpec.y = options.y;
- }
- if (options.a) {
- urlSpec.a = options.a;
- }
- if (options.mode) {
- urlSpec.mode = options.mode;
- }
- if (etag){
- // use etag as cache buster
- urlSpec.c = etag;
- }
- previewURL = self.generatePreviewUrl(urlSpec);
- previewURL = previewURL.replace('(', '%28');
- previewURL = previewURL.replace(')', '%29');
- // preload image to prevent delay
- // this will make the browser cache the image
- var img = new Image();
- img.onload = function(){
- // if loading the preview image failed (no preview for the mimetype) then img.width will < 5
- if (img.width > 5) {
- ready(previewURL, img);
- } else if (options.error) {
- options.error();
- }
- };
- if (options.error) {
- img.onerror = options.error;
- }
- img.src = previewURL;
- },
- _updateDirectoryPermissions: function() {
- var isCreatable = (this.dirInfo.permissions & OC.PERMISSION_CREATE) !== 0 && this.$el.find('#free_space').val() !== '0';
- this.$el.find('#permissions').val(this.dirInfo.permissions);
- this.$el.find('.creatable').toggleClass('hidden', !isCreatable);
- this.$el.find('.notCreatable').toggleClass('hidden', isCreatable);
- },
- /**
- * Shows/hides action buttons
- *
- * @param show true for enabling, false for disabling
- */
- showActions: function(show){
- this.$el.find('.actions,#file_action_panel').toggleClass('hidden', !show);
- if (show){
- // make sure to display according to permissions
- var permissions = this.getDirectoryPermissions();
- var isCreatable = (permissions & OC.PERMISSION_CREATE) !== 0;
- this.$el.find('.creatable').toggleClass('hidden', !isCreatable);
- this.$el.find('.notCreatable').toggleClass('hidden', isCreatable);
- // remove old style breadcrumbs (some apps might create them)
- this.$el.find('#controls .crumb').remove();
- // refresh breadcrumbs in case it was replaced by an app
- this.breadcrumb.render();
- }
- else{
- this.$el.find('.creatable, .notCreatable').addClass('hidden');
- }
- },
- /**
- * Enables/disables viewer mode.
- * In viewer mode, apps can embed themselves under the controls bar.
- * In viewer mode, the actions of the file list will be hidden.
- * @param show true for enabling, false for disabling
- */
- setViewerMode: function(show){
- this.showActions(!show);
- this.$el.find('#filestable').toggleClass('hidden', show);
- this.$el.trigger(new $.Event('changeViewerMode', {viewerModeEnabled: show}));
- },
- /**
- * Removes a file entry from the list
- * @param name name of the file to remove
- * @param {Object} [options] map of attributes
- * @param {boolean} [options.updateSummary] true to update the summary
- * after removing, false otherwise. Defaults to true.
- * @return deleted element
- */
- remove: function(name, options){
- options = options || {};
- var fileEl = this.findFileEl(name);
- var fileId = fileEl.data('id');
- var index = fileEl.index();
- if (!fileEl.length) {
- return null;
- }
- if (this._selectedFiles[fileId]) {
- // remove from selection first
- this._selectFileEl(fileEl, false);
- this.updateSelectionSummary();
- }
- if (this._dragOptions && (fileEl.data('permissions') & OC.PERMISSION_DELETE)) {
- // file is only draggable when delete permissions are set
- fileEl.find('td.filename').draggable('destroy');
- }
- this.files.splice(index, 1);
- if (this._currentFileModel && this._currentFileModel.get('id') === fileId) {
- // Note: in the future we should call destroy() directly on the model
- // and the model will take care of the deletion.
- // Here we only trigger the event to notify listeners that
- // the file was removed.
- this._currentFileModel.trigger('destroy');
- this._updateDetailsView(null);
- }
- fileEl.remove();
- // TODO: improve performance on batch update
- this.isEmpty = !this.files.length;
- if (typeof(options.updateSummary) === 'undefined' || !!options.updateSummary) {
- this.updateEmptyContent();
- this.fileSummary.remove({type: fileEl.attr('data-type'), size: fileEl.attr('data-size')}, true);
- }
- var lastIndex = this.$fileList.children().length;
- // if there are less elements visible than one page
- // but there are still pending elements in the array,
- // then directly append the next page
- if (lastIndex < this.files.length && lastIndex < this.pageSize()) {
- this._nextPage(true);
- }
- return fileEl;
- },
- /**
- * Finds the index of the row before which the given
- * fileData should be inserted, considering the current
- * sorting
- *
- * @param {OC.Files.FileInfo} fileData file info
- */
- _findInsertionIndex: function(fileData) {
- var index = 0;
- while (index < this.files.length && this._sortComparator(fileData, this.files[index]) > 0) {
- index++;
- }
- return index;
- },
- /**
- * Moves a file to a given target folder.
- *
- * @param fileNames array of file names to move
- * @param targetPath absolute target path
- * @param callback function to call when movement is finished
- * @param dir the dir path where fileNames are located (optionnal, will take current folder if undefined)
- */
- move: function(fileNames, targetPath, callback, dir) {
- var self = this;
- dir = typeof dir === 'string' ? dir : this.getCurrentDirectory();
- if (dir.charAt(dir.length - 1) !== '/') {
- dir += '/';
- }
- var target = OC.basename(targetPath);
- if (!_.isArray(fileNames)) {
- fileNames = [fileNames];
- }
- function Semaphore(max) {
- var counter = 0;
- var waiting = [];
- this.acquire = function() {
- if(counter < max) {
- counter++;
- return new Promise(function(resolve) { resolve(); });
- } else {
- return new Promise(function(resolve) { waiting.push(resolve); });
- }
- };
- this.release = function() {
- counter--;
- if (waiting.length > 0 && counter < max) {
- counter++;
- var promise = waiting.shift();
- promise();
- }
- };
- }
- var moveFileFunction = function(fileName) {
- var $tr = self.findFileEl(fileName);
- self.showFileBusyState($tr, true);
- if (targetPath.charAt(targetPath.length - 1) !== '/') {
- // make sure we move the files into the target dir,
- // not overwrite it
- targetPath = targetPath + '/';
- }
- return self.filesClient.move(dir + fileName, targetPath + fileName)
- .done(function() {
- // if still viewing the same directory
- if (OC.joinPaths(self.getCurrentDirectory(), '/') === dir) {
- // recalculate folder size
- var oldFile = self.findFileEl(target);
- var newFile = self.findFileEl(fileName);
- var oldSize = oldFile.data('size');
- var newSize = oldSize + newFile.data('size');
- oldFile.data('size', newSize);
- oldFile.find('td.filesize').text(OC.Util.humanFileSize(newSize));
- // TODO: also update entry in FileList.files
- self.remove(fileName);
- }
- })
- .fail(function(status) {
- if (status === 412) {
- // TODO: some day here we should invoke the conflict dialog
- OC.Notification.show(t('files', 'Could not move "{file}", target exists',
- {file: fileName}), {type: 'error'}
- );
- } else {
- OC.Notification.show(t('files', 'Could not move "{file}"',
- {file: fileName}), {type: 'error'}
- );
- }
- })
- .always(function() {
- self.showFileBusyState($tr, false);
- });
- };
- var mcSemaphore = new Semaphore(10);
- var counter = 0;
- var promises = _.map(fileNames, function(arg) {
- return mcSemaphore.acquire().then(function(){
- moveFileFunction(arg).then(function(){
- mcSemaphore.release();
- counter++;
- });
- });
- });
- return Promise.all(promises).then(function(){
- if (callback) {
- callback();
- }
- });
- },
- /**
- * Copies a file to a given target folder.
- *
- * @param fileNames array of file names to copy
- * @param targetPath absolute target path
- * @param callback to call when copy is finished with success
- * @param dir the dir path where fileNames are located (optionnal, will take current folder if undefined)
- */
- copy: function(fileNames, targetPath, callback, dir) {
- var self = this;
- var filesToNotify = [];
- var count = 0;
- dir = typeof dir === 'string' ? dir : this.getCurrentDirectory();
- if (dir.charAt(dir.length - 1) !== '/') {
- dir += '/';
- }
- var target = OC.basename(targetPath);
- if (!_.isArray(fileNames)) {
- fileNames = [fileNames];
- }
- _.each(fileNames, function(fileName) {
- var $tr = self.findFileEl(fileName);
- self.showFileBusyState($tr, true);
- if (targetPath.charAt(targetPath.length - 1) !== '/') {
- // make sure we move the files into the target dir,
- // not overwrite it
- targetPath = targetPath + '/';
- }
- self.filesClient.copy(dir + fileName, targetPath + fileName)
- .done(function () {
- filesToNotify.push(fileName);
- // if still viewing the same directory
- if (OC.joinPaths(self.getCurrentDirectory(), '/') === dir) {
- // recalculate folder size
- var oldFile = self.findFileEl(target);
- var newFile = self.findFileEl(fileName);
- var oldSize = oldFile.data('size');
- var newSize = oldSize + newFile.data('size');
- oldFile.data('size', newSize);
- oldFile.find('td.filesize').text(OC.Util.humanFileSize(newSize));
- }
- })
- .fail(function(status) {
- if (status === 412) {
- // TODO: some day here we should invoke the conflict dialog
- OC.Notification.show(t('files', 'Could not copy "{file}", target exists',
- {file: fileName}), {type: 'error'}
- );
- } else {
- OC.Notification.show(t('files', 'Could not copy "{file}"',
- {file: fileName}), {type: 'error'}
- );
- }
- })
- .always(function() {
- self.showFileBusyState($tr, false);
- count++;
- /**
- * We only show the notifications once the last file has been copied
- */
- if (count === fileNames.length) {
- // Remove leading and ending /
- if (targetPath.slice(0, 1) === '/') {
- targetPath = targetPath.slice(1, targetPath.length);
- }
- if (targetPath.slice(-1) === '/') {
- targetPath = targetPath.slice(0, -1);
- }
- if (filesToNotify.length > 0) {
- // Since there's no visual indication that the files were copied, let's send some notifications !
- if (filesToNotify.length === 1) {
- OC.Notification.show(t('files', 'Copied {origin} inside {destination}',
- {
- origin: filesToNotify[0],
- destination: targetPath
- }
- ), {timeout: 10});
- } else if (filesToNotify.length > 0 && filesToNotify.length < 3) {
- OC.Notification.show(t('files', 'Copied {origin} inside {destination}',
- {
- origin: filesToNotify.join(', '),
- destination: targetPath
- }
- ), {timeout: 10});
- } else {
- OC.Notification.show(t('files', 'Copied {origin} and {nbfiles} other files inside {destination}',
- {
- origin: filesToNotify[0],
- nbfiles: filesToNotify.length - 1,
- destination: targetPath
- }
- ), {timeout: 10});
- }
- }
- }
- });
- });
- if (callback) {
- callback();
- }
- },
- /**
- * Updates the given row with the given file info
- *
- * @param {Object} $tr row element
- * @param {OCA.Files.FileInfo} fileInfo file info
- * @param {Object} options options
- *
- * @return {Object} new row element
- */
- updateRow: function($tr, fileInfo, options) {
- this.files.splice($tr.index(), 1);
- $tr.remove();
- options = _.extend({silent: true}, options);
- options = _.extend(options, {updateSummary: false});
- $tr = this.add(fileInfo, options);
- this.$fileList.trigger($.Event('fileActionsReady', {fileList: this, $files: $tr}));
- return $tr;
- },
- /**
- * Triggers file rename input field for the given file name.
- * If the user enters a new name, the file will be renamed.
- *
- * @param oldName file name of the file to rename
- */
- rename: function(oldName) {
- var self = this;
- var tr, td, input, form;
- tr = this.findFileEl(oldName);
- var oldFileInfo = this.files[tr.index()];
- tr.data('renaming',true);
- td = tr.children('td.filename');
- input = $('<input type="text" class="filename"/>').val(oldName);
- form = $('<form></form>');
- form.append(input);
- td.children('a.name').children(':not(.thumbnail-wrapper)').hide();
- td.append(form);
- input.focus();
- //preselect input
- var len = input.val().lastIndexOf('.');
- if ( len === -1 ||
- tr.data('type') === 'dir' ) {
- len = input.val().length;
- }
- input.selectRange(0, len);
- var checkInput = function () {
- var filename = input.val();
- if (filename !== oldName) {
- // Files.isFileNameValid(filename) throws an exception itself
- OCA.Files.Files.isFileNameValid(filename);
- if (self.inList(filename)) {
- throw t('files', '{newName} already exists', {newName: filename}, undefined, {
- escape: false
- });
- }
- }
- return true;
- };
- function restore() {
- input.tooltip('hide');
- tr.data('renaming',false);
- form.remove();
- td.children('a.name').children(':not(.thumbnail-wrapper)').show();
- }
- function updateInList(fileInfo) {
- self.updateRow(tr, fileInfo);
- self._updateDetailsView(fileInfo.name, false);
- }
- // TODO: too many nested blocks, move parts into functions
- form.submit(function(event) {
- event.stopPropagation();
- event.preventDefault();
- if (input.hasClass('error')) {
- return;
- }
- try {
- var newName = input.val().trim();
- input.tooltip('hide');
- form.remove();
- if (newName !== oldName) {
- checkInput();
- // mark as loading (temp element)
- self.showFileBusyState(tr, true);
- tr.attr('data-file', newName);
- var basename = newName;
- if (newName.indexOf('.') > 0 && tr.data('type') !== 'dir') {
- basename = newName.substr(0, newName.lastIndexOf('.'));
- }
- td.find('a.name span.nametext').text(basename);
- td.children('a.name').children(':not(.thumbnail-wrapper)').show();
- var path = tr.attr('data-path') || self.getCurrentDirectory();
- self.filesClient.move(OC.joinPaths(path, oldName), OC.joinPaths(path, newName))
- .done(function() {
- oldFileInfo.name = newName;
- updateInList(oldFileInfo);
- })
- .fail(function(status) {
- // TODO: 409 means current folder does not exist, redirect ?
- if (status === 404) {
- // source not found, so remove it from the list
- OC.Notification.show(t('files', 'Could not rename "{fileName}", it does not exist any more',
- {fileName: oldName}), {timeout: 7, type: 'error'}
- );
- self.remove(newName, {updateSummary: true});
- return;
- } else if (status === 412) {
- // target exists
- OC.Notification.show(
- t('files', 'The name "{targetName}" is already used in the folder "{dir}". Please choose a different name.',
- {
- targetName: newName,
- dir: self.getCurrentDirectory(),
- }),
- {
- type: 'error'
- }
- );
- } else {
- // restore the item to its previous state
- OC.Notification.show(t('files', 'Could not rename "{fileName}"',
- {fileName: oldName}), {type: 'error'}
- );
- }
- updateInList(oldFileInfo);
- });
- } else {
- // add back the old file info when cancelled
- self.files.splice(tr.index(), 1);
- tr.remove();
- tr = self.add(oldFileInfo, {updateSummary: false, silent: true});
- self.$fileList.trigger($.Event('fileActionsReady', {fileList: self, $files: $(tr)}));
- }
- } catch (error) {
- input.attr('title', error);
- input.tooltip({placement: 'right', trigger: 'manual'});
- input.tooltip('fixTitle');
- input.tooltip('show');
- input.addClass('error');
- }
- return false;
- });
- input.keyup(function(event) {
- // verify filename on typing
- try {
- checkInput();
- input.tooltip('hide');
- input.removeClass('error');
- } catch (error) {
- input.attr('title', error);
- input.tooltip({placement: 'right', trigger: 'manual'});
- input.tooltip('fixTitle');
- input.tooltip('show');
- input.addClass('error');
- }
- if (event.keyCode === 27) {
- restore();
- }
- });
- input.click(function(event) {
- event.stopPropagation();
- event.preventDefault();
- });
- input.blur(function() {
- if(input.hasClass('error')) {
- restore();
- } else {
- form.trigger('submit');
- }
- });
- },
- /**
- * Create an empty file inside the current directory.
- *
- * @param {string} name name of the file
- *
- * @return {Promise} promise that will be resolved after the
- * file was created
- *
- * @since 8.2
- */
- createFile: function(name) {
- var self = this;
- var deferred = $.Deferred();
- var promise = deferred.promise();
- OCA.Files.Files.isFileNameValid(name);
- if (this.lastAction) {
- this.lastAction();
- }
- name = this.getUniqueName(name);
- var targetPath = this.getCurrentDirectory() + '/' + name;
- self.filesClient.putFileContents(
- targetPath,
- ' ', // dont create empty files which fails on some storage backends
- {
- contentType: 'text/plain',
- overwrite: true
- }
- )
- .done(function() {
- // TODO: error handling / conflicts
- self.addAndFetchFileInfo(targetPath, '', {scrollTo: true}).then(function(status, data) {
- deferred.resolve(status, data);
- }, function() {
- OC.Notification.show(t('files', 'Could not create file "{file}"',
- {file: name}), {type: 'error'}
- );
- });
- })
- .fail(function(status) {
- if (status === 412) {
- OC.Notification.show(t('files', 'Could not create file "{file}" because it already exists',
- {file: name}), {type: 'error'}
- );
- } else {
- OC.Notification.show(t('files', 'Could not create file "{file}"',
- {file: name}), {type: 'error'}
- );
- }
- deferred.reject(status);
- });
- return promise;
- },
- /**
- * Create a directory inside the current directory.
- *
- * @param {string} name name of the directory
- *
- * @return {Promise} promise that will be resolved after the
- * directory was created
- *
- * @since 8.2
- */
- createDirectory: function(name) {
- var self = this;
- var deferred = $.Deferred();
- var promise = deferred.promise();
- OCA.Files.Files.isFileNameValid(name);
- if (this.lastAction) {
- this.lastAction();
- }
- name = this.getUniqueName(name);
- var targetPath = this.getCurrentDirectory() + '/' + name;
- this.filesClient.createDirectory(targetPath)
- .done(function() {
- self.addAndFetchFileInfo(targetPath, '', {scrollTo:true}).then(function(status, data) {
- deferred.resolve(status, data);
- }, function() {
- OC.Notification.show(t('files', 'Could not create folder "{dir}"',
- {dir: name}), {type: 'error'}
- );
- });
- })
- .fail(function(createStatus) {
- // method not allowed, folder might exist already
- if (createStatus === 405) {
- // add it to the list, for completeness
- self.addAndFetchFileInfo(targetPath, '', {scrollTo:true})
- .done(function(status, data) {
- OC.Notification.show(t('files', 'Could not create folder "{dir}" because it already exists',
- {dir: name}), {type: 'error'}
- );
- // still consider a failure
- deferred.reject(createStatus, data);
- })
- .fail(function() {
- OC.Notification.show(t('files', 'Could not create folder "{dir}"',
- {dir: name}), {type: 'error'}
- );
- deferred.reject(status);
- });
- } else {
- OC.Notification.show(t('files', 'Could not create folder "{dir}"',
- {dir: name}), {type: 'error'}
- );
- deferred.reject(createStatus);
- }
- });
- return promise;
- },
- /**
- * Add file into the list by fetching its information from the server first.
- *
- * If the given directory does not match the current directory, nothing will
- * be fetched.
- *
- * @param {String} fileName file name
- * @param {String} [dir] optional directory, defaults to the current one
- * @param {Object} options same options as #add
- * @return {Promise} promise that resolves with the file info, or an
- * already resolved Promise if no info was fetched. The promise rejects
- * if the file was not found or an error occurred.
- *
- * @since 9.0
- */
- addAndFetchFileInfo: function(fileName, dir, options) {
- var self = this;
- var deferred = $.Deferred();
- if (_.isUndefined(dir)) {
- dir = this.getCurrentDirectory();
- } else {
- dir = dir || '/';
- }
- var targetPath = OC.joinPaths(dir, fileName);
- if ((OC.dirname(targetPath) || '/') !== this.getCurrentDirectory()) {
- // no need to fetch information
- deferred.resolve();
- return deferred.promise();
- }
- var addOptions = _.extend({
- animate: true,
- scrollTo: false
- }, options || {});
- this.filesClient.getFileInfo(targetPath, {
- properties: this._getWebdavProperties()
- })
- .then(function(status, data) {
- // remove first to avoid duplicates
- self.remove(data.name);
- self.add(data, addOptions);
- deferred.resolve(status, data);
- })
- .fail(function(status) {
- OC.Notification.show(t('files', 'Could not create file "{file}"',
- {file: name}), {type: 'error'}
- );
- deferred.reject(status);
- });
- return deferred.promise();
- },
- /**
- * Returns whether the given file name exists in the list
- *
- * @param {string} file file name
- *
- * @return {bool} true if the file exists in the list, false otherwise
- */
- inList:function(file) {
- return this.findFile(file);
- },
- /**
- * Shows busy state on a given file row or multiple
- *
- * @param {string|Array.<string>} files file name or array of file names
- * @param {bool} [busy=true] busy state, true for busy, false to remove busy state
- *
- * @since 8.2
- */
- showFileBusyState: function(files, state) {
- var self = this;
- if (!_.isArray(files) && !files.is) {
- files = [files];
- }
- if (_.isUndefined(state)) {
- state = true;
- }
- _.each(files, function(fileName) {
- // jquery element already ?
- var $tr;
- if (_.isString(fileName)) {
- $tr = self.findFileEl(fileName);
- } else {
- $tr = $(fileName);
- }
- var $thumbEl = $tr.find('.thumbnail');
- $tr.toggleClass('busy', state);
- if (state) {
- $thumbEl.parent().addClass('icon-loading-small');
- } else {
- $thumbEl.parent().removeClass('icon-loading-small');
- }
- });
- },
- /**
- * Delete the given files from the given dir
- * @param files file names list (without path)
- * @param dir directory in which to delete the files, defaults to the current
- * directory
- */
- do_delete:function(files, dir) {
- var self = this;
- if (files && files.substr) {
- files=[files];
- }
- if (!files) {
- // delete all files in directory
- files = _.pluck(this.files, 'name');
- }
- if (files) {
- this.showFileBusyState(files, true);
- }
- // Finish any existing actions
- if (this.lastAction) {
- this.lastAction();
- }
- dir = dir || this.getCurrentDirectory();
- function removeFromList(file) {
- var fileEl = self.remove(file, {updateSummary: false});
- // FIXME: not sure why we need this after the
- // element isn't even in the DOM any more
- fileEl.find('.selectCheckBox').prop('checked', false);
- fileEl.removeClass('selected');
- self.fileSummary.remove({type: fileEl.attr('data-type'), size: fileEl.attr('data-size')});
- // TODO: this info should be returned by the ajax call!
- self.updateEmptyContent();
- self.fileSummary.update();
- self.updateSelectionSummary();
- // FIXME: don't repeat this, do it once all files are done
- self.updateStorageStatistics();
- self.updateStorageQuotas();
- }
- _.each(files, function(file) {
- self.filesClient.remove(dir + '/' + file)
- .done(function() {
- removeFromList(file);
- })
- .fail(function(status) {
- if (status === 404) {
- // the file already did not exist, remove it from the list
- removeFromList(file);
- } else {
- // only reset the spinner for that one file
- OC.Notification.show(t('files', 'Error deleting file "{fileName}".',
- {fileName: file}), {type: 'error'}
- );
- var deleteAction = self.findFileEl(file).find('.action.delete');
- deleteAction.removeClass('icon-loading-small').addClass('icon-delete');
- self.showFileBusyState(files, false);
- }
- });
- });
- },
- /**
- * Creates the file summary section
- */
- _createSummary: function() {
- var $tr = $('<tr class="summary"></tr>');
- if (this._allowSelection) {
- // Dummy column for selection, as all rows must have the same
- // number of columns.
- $tr.append('<td></td>');
- }
- this.$el.find('tfoot').append($tr);
- return new OCA.Files.FileSummary($tr, {config: this._filesConfig});
- },
- updateEmptyContent: function() {
- var permissions = this.getDirectoryPermissions();
- var isCreatable = (permissions & OC.PERMISSION_CREATE) !== 0;
- this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty);
- this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty);
- this.$el.find('#emptycontent .uploadmessage').toggleClass('hidden', !isCreatable || !this.isEmpty);
- this.$el.find('#filestable').toggleClass('hidden', this.isEmpty);
- this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty);
- },
- /**
- * Shows the loading mask.
- *
- * @see OCA.Files.FileList#hideMask
- */
- showMask: function() {
- // in case one was shown before
- var $mask = this.$el.find('.mask');
- if ($mask.exists()) {
- return;
- }
- this.$table.addClass('hidden');
- this.$el.find('#emptycontent').addClass('hidden');
- $mask = $('<div class="mask transparent icon-loading"></div>');
- this.$el.append($mask);
- $mask.removeClass('transparent');
- },
- /**
- * Hide the loading mask.
- * @see OCA.Files.FileList#showMask
- */
- hideMask: function() {
- this.$el.find('.mask').remove();
- this.$table.removeClass('hidden');
- },
- scrollTo:function(file) {
- if (!_.isArray(file)) {
- file = [file];
- }
- if (file.length === 1) {
- _.defer(function() {
- this.showDetailsView(file[0]);
- }.bind(this));
- }
- this.highlightFiles(file, function($tr) {
- $tr.addClass('searchresult');
- $tr.one('hover', function() {
- $tr.removeClass('searchresult');
- });
- });
- },
- /**
- * @deprecated use setFilter(filter)
- */
- filter:function(query) {
- this.setFilter('');
- },
- /**
- * @deprecated use setFilter('')
- */
- unfilter:function() {
- this.setFilter('');
- },
- /**
- * hide files matching the given filter
- * @param filter
- */
- setFilter:function(filter) {
- var total = 0;
- if (this._filter === filter) {
- return;
- }
- this._filter = filter;
- this.fileSummary.setFilter(filter, this.files);
- total = this.fileSummary.getTotal();
- if (!this.$el.find('.mask').exists()) {
- this.hideIrrelevantUIWhenNoFilesMatch();
- }
- var visibleCount = 0;
- filter = filter.toLowerCase();
- function filterRows(tr) {
- var $e = $(tr);
- if ($e.data('file').toString().toLowerCase().indexOf(filter) === -1) {
- $e.addClass('hidden');
- } else {
- visibleCount++;
- $e.removeClass('hidden');
- }
- }
- var $trs = this.$fileList.find('tr');
- do {
- _.each($trs, filterRows);
- if (visibleCount < total) {
- $trs = this._nextPage(false);
- }
- } while (visibleCount < total && $trs.length > 0);
- this.$container.trigger('scroll');
- },
- hideIrrelevantUIWhenNoFilesMatch:function() {
- if (this._filter && this.fileSummary.summary.totalDirs + this.fileSummary.summary.totalFiles === 0) {
- this.$el.find('#filestable thead th').addClass('hidden');
- this.$el.find('#emptycontent').addClass('hidden');
- $('#searchresults').addClass('filter-empty');
- $('#searchresults .emptycontent').addClass('emptycontent-search');
- if ( $('#searchresults').length === 0 || $('#searchresults').hasClass('hidden') ) {
- var error = t('files', 'No search results in other folders for {tag}{filter}{endtag}', {filter:this._filter});
- this.$el.find('.nofilterresults').removeClass('hidden').
- find('p').html(error.replace('{tag}', '<strong>').replace('{endtag}', '</strong>'));
- }
- } else {
- $('#searchresults').removeClass('filter-empty');
- $('#searchresults .emptycontent').removeClass('emptycontent-search');
- this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty);
- if (!this.$el.find('.mask').exists()) {
- this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty);
- }
- this.$el.find('.nofilterresults').addClass('hidden');
- }
- },
- /**
- * get the current filter
- * @param filter
- */
- getFilter:function(filter) {
- return this._filter;
- },
- /**
- * update the search object to use this filelist when filtering
- */
- updateSearch:function() {
- if (OCA.Search.files) {
- OCA.Search.files.setFileList(this);
- }
- if (OC.Search) {
- OC.Search.clear();
- }
- },
- /**
- * Update UI based on the current selection
- */
- updateSelectionSummary: function() {
- var summary = this._selectionSummary.summary;
- var selection;
- var showHidden = !!this._filesConfig.get('showhidden');
- if (summary.totalFiles === 0 && summary.totalDirs === 0) {
- this.$el.find('#headerName a.name>span:first').text(t('files','Name'));
- this.$el.find('#headerSize a>span:first').text(t('files','Size'));
- this.$el.find('#modified a>span:first').text(t('files','Modified'));
- this.$el.find('table').removeClass('multiselect');
- this.$el.find('.selectedActions').addClass('hidden');
- }
- else {
- this.$el.find('.selectedActions').removeClass('hidden');
- this.$el.find('#headerSize a>span:first').text(OC.Util.humanFileSize(summary.totalSize));
- var directoryInfo = n('files', '%n folder', '%n folders', summary.totalDirs);
- var fileInfo = n('files', '%n file', '%n files', summary.totalFiles);
- if (summary.totalDirs > 0 && summary.totalFiles > 0) {
- var selectionVars = {
- dirs: directoryInfo,
- files: fileInfo
- };
- selection = t('files', '{dirs} and {files}', selectionVars);
- } else if (summary.totalDirs > 0) {
- selection = directoryInfo;
- } else {
- selection = fileInfo;
- }
- if (!showHidden && summary.totalHidden > 0) {
- var hiddenInfo = n('files', 'including %n hidden', 'including %n hidden', summary.totalHidden);
- selection += ' (' + hiddenInfo + ')';
- }
- this.$el.find('#headerName a.name>span:first').text(selection);
- this.$el.find('#modified a>span:first').text('');
- this.$el.find('table').addClass('multiselect');
- if (this.fileMultiSelectMenu) {
- this.fileMultiSelectMenu.toggleItemVisibility('download', this.isSelectedDownloadable());
- this.fileMultiSelectMenu.toggleItemVisibility('delete', this.isSelectedDeletable());
- this.fileMultiSelectMenu.toggleItemVisibility('copyMove', this.isSelectedCopiable());
- if (this.isSelectedCopiable()) {
- if (this.isSelectedMovable()) {
- this.fileMultiSelectMenu.updateItemText('copyMove', t('files', 'Move or copy'));
- } else {
- this.fileMultiSelectMenu.updateItemText('copyMove', t('files', 'Copy'));
- }
- } else {
- this.fileMultiSelectMenu.toggleItemVisibility('copyMove', false);
- }
- }
- }
- },
- /**
- * Check whether all selected files are copiable
- */
- isSelectedCopiable: function() {
- return _.reduce(this.getSelectedFiles(), function(copiable, file) {
- var requiredPermission = $('#isPublic').val() ? OC.PERMISSION_UPDATE : OC.PERMISSION_READ;
- return copiable && (file.permissions & requiredPermission);
- }, true);
- },
- /**
- * Check whether all selected files are movable
- */
- isSelectedMovable: function() {
- return _.reduce(this.getSelectedFiles(), function(movable, file) {
- return movable && (file.permissions & OC.PERMISSION_UPDATE);
- }, true);
- },
- /**
- * Check whether all selected files are downloadable
- */
- isSelectedDownloadable: function() {
- return _.reduce(this.getSelectedFiles(), function(downloadable, file) {
- return downloadable && (file.permissions & OC.PERMISSION_READ);
- }, true);
- },
- /**
- * Check whether all selected files are deletable
- */
- isSelectedDeletable: function() {
- return _.reduce(this.getSelectedFiles(), function(deletable, file) {
- return deletable && (file.permissions & OC.PERMISSION_DELETE);
- }, true);
- },
- /**
- * Are all files selected?
- *
- * @returns {Boolean} all files are selected
- */
- isAllSelected: function() {
- var checkbox = this.$el.find('.select-all')
- var checked = checkbox.prop('checked')
- var indeterminate = checkbox.prop('indeterminate')
- return checked && !indeterminate;
- },
- /**
- * Returns the file info of the selected files
- *
- * @return array of file names
- */
- getSelectedFiles: function() {
- return _.values(this._selectedFiles);
- },
- getUniqueName: function(name) {
- if (this.findFileEl(name).exists()) {
- var numMatch;
- var parts=name.split('.');
- var extension = "";
- if (parts.length > 1) {
- extension=parts.pop();
- }
- var base=parts.join('.');
- numMatch=base.match(/\((\d+)\)/);
- var num=2;
- if (numMatch && numMatch.length>0) {
- num=parseInt(numMatch[numMatch.length-1], 10)+1;
- base=base.split('(');
- base.pop();
- base=$.trim(base.join('('));
- }
- name=base+' ('+num+')';
- if (extension) {
- name = name+'.'+extension;
- }
- // FIXME: ugly recursion
- return this.getUniqueName(name);
- }
- return name;
- },
- /**
- * Shows a "permission denied" notification
- */
- _showPermissionDeniedNotification: function() {
- var message = t('files', 'You don’t have permission to upload or create files here');
- OC.Notification.show(message, {type: 'error'});
- },
- /**
- * Setup file upload events related to the file-upload plugin
- *
- * @param {OC.Uploader} uploader
- */
- setupUploadEvents: function(uploader) {
- var self = this;
- self._uploads = {};
- // detect the progress bar resize
- uploader.on('resized', this._onResize);
- uploader.on('drop', function(e, data) {
- self._uploader.log('filelist handle fileuploaddrop', e, data);
- if (self.$el.hasClass('hidden')) {
- // do not upload to invisible lists
- e.preventDefault();
- return false;
- }
- var dropTarget = $(e.delegatedEvent.target);
- // check if dropped inside this container and not another one
- if (dropTarget.length
- && !self.$el.is(dropTarget) // dropped on list directly
- && !self.$el.has(dropTarget).length // dropped inside list
- && !dropTarget.is(self.$container) // dropped on main container
- && !self.$el.parent().is(dropTarget) // drop on the parent container (#app-content) since the main container might not have the full height
- ) {
- e.preventDefault();
- return false;
- }
- // find the closest tr or crumb to use as target
- dropTarget = dropTarget.closest('tr, .crumb');
- // if dropping on tr or crumb, drag&drop upload to folder
- if (dropTarget && (dropTarget.data('type') === 'dir' ||
- dropTarget.hasClass('crumb'))) {
- // remember as context
- data.context = dropTarget;
- // if permissions are specified, only allow if create permission is there
- var permissions = dropTarget.data('permissions');
- if (!_.isUndefined(permissions) && (permissions & OC.PERMISSION_CREATE) === 0) {
- self._showPermissionDeniedNotification();
- return false;
- }
- var dir = dropTarget.data('file');
- // if from file list, need to prepend parent dir
- if (dir) {
- var parentDir = self.getCurrentDirectory();
- if (parentDir[parentDir.length - 1] !== '/') {
- parentDir += '/';
- }
- dir = parentDir + dir;
- }
- else{
- // read full path from crumb
- dir = dropTarget.data('dir') || '/';
- }
- // add target dir
- data.targetDir = dir;
- } else {
- // cancel uploads to current dir if no permission
- var isCreatable = (self.getDirectoryPermissions() & OC.PERMISSION_CREATE) !== 0;
- if (!isCreatable) {
- self._showPermissionDeniedNotification();
- e.stopPropagation();
- return false;
- }
- // we are dropping somewhere inside the file list, which will
- // upload the file to the current directory
- data.targetDir = self.getCurrentDirectory();
- }
- });
- uploader.on('add', function(e, data) {
- self._uploader.log('filelist handle fileuploadadd', e, data);
- // add ui visualization to existing folder
- if (data.context && data.context.data('type') === 'dir') {
- // add to existing folder
- // update upload counter ui
- var uploadText = data.context.find('.uploadtext');
- var currentUploads = parseInt(uploadText.attr('currentUploads'), 10);
- currentUploads += 1;
- uploadText.attr('currentUploads', currentUploads);
- var translatedText = n('files', 'Uploading %n file', 'Uploading %n files', currentUploads);
- if (currentUploads === 1) {
- self.showFileBusyState(uploadText.closest('tr'), true);
- uploadText.text(translatedText);
- uploadText.show();
- } else {
- uploadText.text(translatedText);
- }
- }
- if (!data.targetDir) {
- data.targetDir = self.getCurrentDirectory();
- }
- });
- /*
- * when file upload done successfully add row to filelist
- * update counter when uploading to sub folder
- */
- uploader.on('done', function(e, upload) {
- var data = upload.data;
- self._uploader.log('filelist handle fileuploaddone', e, data);
- var status = data.jqXHR.status;
- if (status < 200 || status >= 300) {
- // error was handled in OC.Uploads already
- return;
- }
- var fileName = upload.getFileName();
- var fetchInfoPromise = self.addAndFetchFileInfo(fileName, upload.getFullPath());
- if (!self._uploads) {
- self._uploads = {};
- }
- if (OC.isSamePath(OC.dirname(upload.getFullPath() + '/'), self.getCurrentDirectory())) {
- self._uploads[fileName] = fetchInfoPromise;
- }
- var uploadText = self.$fileList.find('tr .uploadtext');
- self.showFileBusyState(uploadText.closest('tr'), false);
- uploadText.fadeOut();
- uploadText.attr('currentUploads', 0);
- self.updateStorageQuotas();
- });
- uploader.on('createdfolder', function(fullPath) {
- self.addAndFetchFileInfo(OC.basename(fullPath), OC.dirname(fullPath));
- });
- uploader.on('stop', function() {
- self._uploader.log('filelist handle fileuploadstop');
- // prepare list of uploaded file names in the current directory
- // and discard the other ones
- var promises = _.values(self._uploads);
- var fileNames = _.keys(self._uploads);
- self._uploads = [];
- // as soon as all info is fetched
- $.when.apply($, promises).then(function() {
- // highlight uploaded files
- self.highlightFiles(fileNames);
- self.updateStorageStatistics();
- });
- var uploadText = self.$fileList.find('tr .uploadtext');
- self.showFileBusyState(uploadText.closest('tr'), false);
- uploadText.fadeOut();
- uploadText.attr('currentUploads', 0);
- });
- uploader.on('fail', function(e, data) {
- self._uploader.log('filelist handle fileuploadfail', e, data);
- self._uploads = [];
- //if user pressed cancel hide upload chrome
- //cleanup uploading to a dir
- var uploadText = self.$fileList.find('tr .uploadtext');
- self.showFileBusyState(uploadText.closest('tr'), false);
- uploadText.fadeOut();
- uploadText.attr('currentUploads', 0);
- self.updateStorageStatistics();
- });
- },
- /**
- * Scroll to the last file of the given list
- * Highlight the list of files
- * @param files array of filenames,
- * @param {Function} [highlightFunction] optional function
- * to be called after the scrolling is finished
- */
- highlightFiles: function(files, highlightFunction) {
- // Detection of the uploaded element
- var filename = files[files.length - 1];
- var $fileRow = this.findFileEl(filename);
- while(!$fileRow.exists() && this._nextPage(false) !== false) { // Checking element existence
- $fileRow = this.findFileEl(filename);
- }
- if (!$fileRow.exists()) { // Element not present in the file list
- return;
- }
- var currentOffset = this.$container.scrollTop();
- var additionalOffset = this.$el.find("#controls").height()+this.$el.find("#controls").offset().top;
- // Animation
- var _this = this;
- var $scrollContainer = this.$container;
- if ($scrollContainer[0] === window) {
- // need to use "body" to animate scrolling
- // when the scroll container is the window
- $scrollContainer = $('body');
- }
- $scrollContainer.animate({
- // Scrolling to the top of the new element
- scrollTop: currentOffset + $fileRow.offset().top - $fileRow.height() * 2 - additionalOffset
- }, {
- duration: 500,
- complete: function() {
- // Highlighting function
- var highlightRow = highlightFunction;
- if (!highlightRow) {
- highlightRow = function($fileRow) {
- $fileRow.addClass("highlightUploaded");
- setTimeout(function() {
- $fileRow.removeClass("highlightUploaded");
- }, 2500);
- };
- }
- // Loop over uploaded files
- for(var i=0; i<files.length; i++) {
- var $fileRow = _this.findFileEl(files[i]);
- if($fileRow.length !== 0) { // Checking element existence
- highlightRow($fileRow);
- }
- }
- }
- });
- },
- _renderNewButton: function() {
- // if an upload button (legacy) already exists or no actions container exist, skip
- var $actionsContainer = this.$el.find('#controls .actions');
- if (!$actionsContainer.length || this.$el.find('.button.upload').length) {
- return;
- }
- var $newButton = $(OCA.Files.Templates['template_addbutton']({
- addText: t('files', 'New'),
- iconClass: 'icon-add'
- }));
- $actionsContainer.prepend($newButton);
- $newButton.tooltip({'placement': 'bottom'});
- $newButton.click(_.bind(this._onClickNewButton, this));
- this._newButton = $newButton;
- },
- _onClickNewButton: function(event) {
- var $target = $(event.target);
- if (!$target.hasClass('.button')) {
- $target = $target.closest('.button');
- }
- this._newButton.tooltip('hide');
- event.preventDefault();
- if ($target.hasClass('disabled')) {
- return false;
- }
- if (!this._newFileMenu) {
- this._newFileMenu = new OCA.Files.NewFileMenu({
- fileList: this
- });
- $('.actions').append(this._newFileMenu.$el);
- }
- this._newFileMenu.showAt($target);
- return false;
- },
- /**
- * Register a tab view to be added to all views
- */
- registerTabView: function(tabView) {
- if (this._detailsView) {
- this._detailsView.addTabView(tabView);
- }
- },
- /**
- * Register a detail view to be added to all views
- */
- registerDetailView: function(detailView) {
- if (this._detailsView) {
- this._detailsView.addDetailView(detailView);
- }
- },
- /**
- * Register a view to be added to the breadcrumb view
- */
- registerBreadCrumbDetailView: function(detailView) {
- if (this.breadcrumb) {
- this.breadcrumb.addDetailView(detailView);
- }
- },
- /**
- * Returns the registered detail views.
- *
- * @return null|Array<OCA.Files.DetailFileInfoView> an array with the
- * registered DetailFileInfoViews, or null if the details view
- * is not enabled.
- */
- getRegisteredDetailViews: function() {
- if (this._detailsView) {
- return this._detailsView.getDetailViews();
- }
- return null;
- }
- };
- /**
- * Sort comparators.
- * @namespace OCA.Files.FileList.Comparators
- * @private
- */
- FileList.Comparators = {
- /**
- * Compares two file infos by name, making directories appear
- * first.
- *
- * @param {OC.Files.FileInfo} fileInfo1 file info
- * @param {OC.Files.FileInfo} fileInfo2 file info
- * @return {int} -1 if the first file must appear before the second one,
- * 0 if they are identify, 1 otherwise.
- */
- name: function(fileInfo1, fileInfo2) {
- if (fileInfo1.type === 'dir' && fileInfo2.type !== 'dir') {
- return -1;
- }
- if (fileInfo1.type !== 'dir' && fileInfo2.type === 'dir') {
- return 1;
- }
- return OC.Util.naturalSortCompare(fileInfo1.name, fileInfo2.name);
- },
- /**
- * Compares two file infos by size.
- *
- * @param {OC.Files.FileInfo} fileInfo1 file info
- * @param {OC.Files.FileInfo} fileInfo2 file info
- * @return {int} -1 if the first file must appear before the second one,
- * 0 if they are identify, 1 otherwise.
- */
- size: function(fileInfo1, fileInfo2) {
- return fileInfo1.size - fileInfo2.size;
- },
- /**
- * Compares two file infos by timestamp.
- *
- * @param {OC.Files.FileInfo} fileInfo1 file info
- * @param {OC.Files.FileInfo} fileInfo2 file info
- * @return {int} -1 if the first file must appear before the second one,
- * 0 if they are identify, 1 otherwise.
- */
- mtime: function(fileInfo1, fileInfo2) {
- return fileInfo1.mtime - fileInfo2.mtime;
- }
- };
- /**
- * File info attributes.
- *
- * @typedef {Object} OC.Files.FileInfo
- *
- * @lends OC.Files.FileInfo
- *
- * @deprecated use OC.Files.FileInfo instead
- *
- */
- OCA.Files.FileInfo = OC.Files.FileInfo;
- OCA.Files.FileList = FileList;
- })();
- $(document).ready(function() {
- // FIXME: unused ?
- OCA.Files.FileList.useUndo = (window.onbeforeunload)?true:false;
- $(window).on('beforeunload', function () {
- if (OCA.Files.FileList.lastAction) {
- OCA.Files.FileList.lastAction();
- }
- });
- $(window).on('unload', function () {
- $(window).trigger('beforeunload');
- });
- });
|