From d5221c0d17c6e4639f8a5064a8d4fbead9c433e6 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sat, 25 Apr 2015 13:24:34 +0200 Subject: [PATCH] Refactor view. --- src/_h5ai/client/js/inc/main/index.js | 3 - src/_h5ai/client/js/inc/model/item.js | 4 +- src/_h5ai/client/js/inc/view/base.js | 29 ------ src/_h5ai/client/js/inc/view/content.js | 26 ++--- src/_h5ai/client/js/inc/view/mainrow.js | 9 ++ src/_h5ai/client/js/inc/view/root.js | 10 ++ src/_h5ai/client/js/inc/view/sidebar.js | 33 +++--- src/_h5ai/client/js/inc/view/topbar.js | 19 ++++ src/_h5ai/client/js/inc/view/viewmode.js | 25 +---- test/tests/unit/main/index.js | 18 +++- test/tests/unit/view/content.js | 63 ++++++++++-- test/tests/unit/view/mainrow.js | 93 +++++++++++++++++ test/tests/unit/view/root.js | 107 ++++++++++++++++++++ test/tests/unit/view/sidebar.js | 27 +++-- test/tests/unit/view/{base.js => topbar.js} | 85 +++++++++------- test/tests/unit/view/viewmode.js | 31 ++---- 16 files changed, 421 insertions(+), 161 deletions(-) delete mode 100644 src/_h5ai/client/js/inc/view/base.js create mode 100644 src/_h5ai/client/js/inc/view/mainrow.js create mode 100644 src/_h5ai/client/js/inc/view/root.js create mode 100644 src/_h5ai/client/js/inc/view/topbar.js create mode 100644 test/tests/unit/view/mainrow.js create mode 100644 test/tests/unit/view/root.js rename test/tests/unit/view/{base.js => topbar.js} (57%) diff --git a/src/_h5ai/client/js/inc/main/index.js b/src/_h5ai/client/js/inc/main/index.js index b8008a80..6c59eee4 100644 --- a/src/_h5ai/client/js/inc/main/index.js +++ b/src/_h5ai/client/js/inc/main/index.js @@ -1,8 +1,5 @@ modulejs.define('main/index', ['_', 'core/event'], function (_, event) { - modulejs.require('view/base'); - modulejs.require('view/content'); - modulejs.require('view/sidebar'); modulejs.require('view/viewmode'); _.each(modulejs.state(), function (state, id) { diff --git a/src/_h5ai/client/js/inc/model/item.js b/src/_h5ai/client/js/inc/model/item.js index 06269787..093be356 100644 --- a/src/_h5ai/client/js/inc/model/item.js +++ b/src/_h5ai/client/js/inc/model/item.js @@ -7,11 +7,11 @@ modulejs.define('model/item', ['_', 'core/event', 'core/location', 'core/server' function startsWith(sequence, part) { - if (!sequence || !sequence.substr || !part || !part.length) { + if (!sequence || !sequence.indexOf) { return false; } - return sequence.substr(0, part.length) === part; + return sequence.indexOf(part) === 0; } function createLabel(sequence) { diff --git a/src/_h5ai/client/js/inc/view/base.js b/src/_h5ai/client/js/inc/view/base.js deleted file mode 100644 index 9312d7b5..00000000 --- a/src/_h5ai/client/js/inc/view/base.js +++ /dev/null @@ -1,29 +0,0 @@ -modulejs.define('view/base', ['$', 'config'], function ($, config) { - - var templateTopbar = - '
' + - '
' + - '
' + - '
'; - var templateMainRow = - '
' + - ''; - var templateBacklink = - '' + - '
powered
' + - '
by h5ai
' + - '
'; - - - function init() { - - $('#fallback, #fallback-hints').remove(); - $(templateTopbar).appendTo('body'); - $(templateMainRow).appendTo('body'); - $(templateBacklink).appendTo('#topbar'); - } - - - init(); -}); diff --git a/src/_h5ai/client/js/inc/view/content.js b/src/_h5ai/client/js/inc/view/content.js index f9df8f23..b069cbd8 100644 --- a/src/_h5ai/client/js/inc/view/content.js +++ b/src/_h5ai/client/js/inc/view/content.js @@ -1,4 +1,4 @@ -modulejs.define('view/content', ['_', '$', 'core/event', 'core/format', 'core/location', 'core/resource', 'core/settings'], function (_, $, event, format, location, resource, allsettings) { +modulejs.define('view/content', ['_', '$', 'core/event', 'core/format', 'core/location', 'core/resource', 'core/settings', 'view/mainrow'], function (_, $, event, format, location, resource, allsettings, mainrow) { var settings = _.extend({ binaryPrefix: false, @@ -30,6 +30,10 @@ modulejs.define('view/content', ['_', '$', 'core/event', 'core/format', 'core/lo '
' + '
' + '
'; + var $content = $(contentTemplate); + var $view = $content.find('#view'); + var $items = $view.find('#items'); + var $empty = $view.find('.empty'); function update(item) { @@ -88,9 +92,6 @@ modulejs.define('view/content', ['_', '$', 'core/event', 'core/format', 'core/lo function onLocationChanged(item) { - var $items = $('#items'); - var $empty = $('#view').find('.empty'); - $items.find('.item').remove(); if (item.parent && !settings.hideParentFolder) { @@ -115,9 +116,6 @@ modulejs.define('view/content', ['_', '$', 'core/event', 'core/format', 'core/lo function onLocationRefreshed(item, added, removed) { - var $items = $('#items'); - var $empty = $('#view').find('.empty'); - _.each(added, function (item) { if (!(item.isFolder() && settings.hideFolders)) { @@ -141,10 +139,8 @@ modulejs.define('view/content', ['_', '$', 'core/event', 'core/format', 'core/lo function init() { - var $content = $(contentTemplate); - var $view = $content.find('#view'); - var $items = $view.find('#items'); - $view.find('.empty').hide(); + $content.appendTo(mainrow.$el); + $empty.hide(); format.setDefaultMetric(settings.binaryPrefix); @@ -154,10 +150,14 @@ modulejs.define('view/content', ['_', '$', 'core/event', 'core/format', 'core/lo event.sub('location.changed', onLocationChanged); event.sub('location.refreshed', onLocationRefreshed); - - $content.appendTo('#main-row'); } init(); + + return { + $el: $content, + $view: $view, + $items: $items + }; }); diff --git a/src/_h5ai/client/js/inc/view/mainrow.js b/src/_h5ai/client/js/inc/view/mainrow.js new file mode 100644 index 00000000..07e818b9 --- /dev/null +++ b/src/_h5ai/client/js/inc/view/mainrow.js @@ -0,0 +1,9 @@ +modulejs.define('view/mainrow', ['$', 'view/root'], function ($, root) { + + var template = '
'; + var $el = $(template).appendTo(root.$el); + + return { + $el: $el + }; +}); diff --git a/src/_h5ai/client/js/inc/view/root.js b/src/_h5ai/client/js/inc/view/root.js new file mode 100644 index 00000000..8f1f6c01 --- /dev/null +++ b/src/_h5ai/client/js/inc/view/root.js @@ -0,0 +1,10 @@ +modulejs.define('view/root', ['$'], function ($) { + + var $el = $('body').attr('id', 'root'); + + $('#fallback, #fallback-hints').remove(); + + return { + $el: $el + }; +}); diff --git a/src/_h5ai/client/js/inc/view/sidebar.js b/src/_h5ai/client/js/inc/view/sidebar.js index d3443013..9104a7fd 100644 --- a/src/_h5ai/client/js/inc/view/sidebar.js +++ b/src/_h5ai/client/js/inc/view/sidebar.js @@ -1,17 +1,18 @@ -modulejs.define('view/sidebar', ['$', 'core/resource', 'core/store'], function ($, resource, store) { +modulejs.define('view/sidebar', ['$', 'core/resource', 'core/store', 'view/mainrow', 'view/topbar'], function ($, resource, store, mainrow, topbar) { var storekey = 'sidebarIsVisible'; + var template = '