From 626d48c8eabd1af60b4df6d43e3f7f9ec6ba82a1 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 6 Jun 2013 19:39:12 -0700 Subject: [PATCH] Helper cleanup --- UI/Calendar/CalendarItemTemplate.html | 2 +- UI/Controller.js | 4 ++- UI/Index.html | 2 -- UI/Mixins/handlebars.mixin.js | 24 -------------- UI/Mixins/spoon.js | 29 ----------------- UI/Series/EpisodeModel.js | 3 -- UI/Series/Index/List/ItemTemplate.html | 4 +-- UI/Series/Index/Posters/ItemTemplate.html | 4 +-- UI/Series/Index/Posters/ItemView.js | 5 --- UI/Series/Index/Table/AirDateCell.js | 2 +- UI/Series/SeriesModel.js | 4 --- UI/Shared/Cells/FileSizeCell.js | 7 ++-- UI/Shared/FormatHelpers.js | 36 +++++++++++++++++++++ UI/Shared/LoadingView.js | 4 +-- UI/Shared/TemplateHelpers.js | 39 +++++++++++++++++++++++ UI/app.js | 3 +- 16 files changed, 89 insertions(+), 83 deletions(-) delete mode 100644 UI/Mixins/handlebars.mixin.js delete mode 100644 UI/Mixins/spoon.js create mode 100644 UI/Shared/FormatHelpers.js create mode 100644 UI/Shared/TemplateHelpers.js diff --git a/UI/Calendar/CalendarItemTemplate.html b/UI/Calendar/CalendarItemTemplate.html index 214584d2f..9b4ac731c 100644 --- a/UI/Calendar/CalendarItemTemplate.html +++ b/UI/Calendar/CalendarItemTemplate.html @@ -6,5 +6,5 @@

{{series.title}}

-

{{startTime}} {{bestDateString}}{{seasonNumber}}x{{paddedEpisodeNumber}}
{{episodeTitle}}

+

{{startTime}} {{date airDate}}{{seasonNumber}}x{{paddedEpisodeNumber}}
{{episodeTitle}}

diff --git a/UI/Controller.js b/UI/Controller.js index 3b661457f..5c22854ef 100644 --- a/UI/Controller.js +++ b/UI/Controller.js @@ -13,7 +13,9 @@ define(['app', 'Logs/Layout', 'Release/Layout', 'Missing/MissingLayout', - 'History/HistoryLayout'], + 'History/HistoryLayout', + 'Shared/FormatHelpers', + 'Shared/TemplateHelpers'], function () { var controller = Backbone.Marionette.Controller.extend({ diff --git a/UI/Index.html b/UI/Index.html index 492116257..880954c48 100644 --- a/UI/Index.html +++ b/UI/Index.html @@ -102,10 +102,8 @@ - - diff --git a/UI/Mixins/handlebars.mixin.js b/UI/Mixins/handlebars.mixin.js deleted file mode 100644 index e3d647c10..000000000 --- a/UI/Mixins/handlebars.mixin.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; - -Handlebars.registerHelper('partial', function (templateName) { - //TODO: We should be able to pass in the context, either an object or a property - - var templateFunction = Marionette.TemplateCache.get(templateName); - return new Handlebars.SafeString(templateFunction(this)); -}); - -Handlebars.registerHelper("debug", function(optionalValue) { - console.log("Current Context"); - console.log("===================="); - console.log(this); - - if (optionalValue) { - console.log("Value"); - console.log("===================="); - console.log(optionalValue); - } -}); - -Handlebars.registerHelper("fileSize", function(size) { - return size.bytes(1); -}); \ No newline at end of file diff --git a/UI/Mixins/spoon.js b/UI/Mixins/spoon.js deleted file mode 100644 index e1ce42494..000000000 --- a/UI/Mixins/spoon.js +++ /dev/null @@ -1,29 +0,0 @@ -"use strict"; - -//TODO: global function, not good! -function bestDateString(sourceDate) { - if (!sourceDate) { - return ''; - } - - var date = Date.create(sourceDate); - - if (date.isYesterday()) { - return 'Yesterday'; - } - if (date.isToday()) { - return 'Today'; - } - if (date.isTomorrow()) { - return 'Tomorrow'; - } - if (date.isAfter(Date.create('tomorrow')) && date.isBefore(Date.create().addDays(7))) { - return date.format('{Weekday}'); - } - - if (date.isAfter(Date.create().addDays(6))) { - return date.relative().replace(' from now', ''); - } - - return date.format('{MM}/{dd}/{yyyy}'); -} \ No newline at end of file diff --git a/UI/Series/EpisodeModel.js b/UI/Series/EpisodeModel.js index 65e7822b8..ebe0059fb 100644 --- a/UI/Series/EpisodeModel.js +++ b/UI/Series/EpisodeModel.js @@ -3,9 +3,6 @@ define(['app'], function () { NzbDrone.Series.EpisodeModel = Backbone.Model.extend({ mutators: { - bestDateString : function () { - return bestDateString(this.get('airDate')); - }, paddedEpisodeNumber: function () { return this.get('episodeNumber').pad(2); }, diff --git a/UI/Series/Index/List/ItemTemplate.html b/UI/Series/Index/List/ItemTemplate.html index 805bb5a3f..641bbb29b 100644 --- a/UI/Series/Index/List/ItemTemplate.html +++ b/UI/Series/Index/List/ItemTemplate.html @@ -30,8 +30,8 @@
{{#if isContinuing}} - {{#if bestDateString}} - {{bestDateString}} + {{#if nextAiring}} + {{date nextAiring}} {{else}} Continuing {{/if}} diff --git a/UI/Series/Index/Posters/ItemTemplate.html b/UI/Series/Index/Posters/ItemTemplate.html index 4f233e1b5..f44231f15 100644 --- a/UI/Series/Index/Posters/ItemTemplate.html +++ b/UI/Series/Index/Posters/ItemTemplate.html @@ -21,8 +21,8 @@
{{#if isContinuing}} - {{#if bestDateString}} - {{bestDateString}} + {{#if nextAiring}} + {{date nextAiring}} {{else}} Continuing {{/if}} diff --git a/UI/Series/Index/Posters/ItemView.js b/UI/Series/Index/Posters/ItemView.js index 9420cfa36..f91b8a810 100644 --- a/UI/Series/Index/Posters/ItemView.js +++ b/UI/Series/Index/Posters/ItemView.js @@ -16,7 +16,6 @@ define([ ui: { 'progressbar': '.progress .bar', - 'airDate': '.air-date', 'controls': '.series-controls' }, @@ -31,10 +30,6 @@ define([ this.qualityProfileCollection = options.qualityProfiles; }, - onRender: function () { - this.ui.airDate.tooltip(); - }, - editSeries: function () { var view = new NzbDrone.Series.Edit.EditSeriesView({ model: this.model}); NzbDrone.modalRegion.show(view); diff --git a/UI/Series/Index/Table/AirDateCell.js b/UI/Series/Index/Table/AirDateCell.js index 35ef20379..8d1ddbd27 100644 --- a/UI/Series/Index/Table/AirDateCell.js +++ b/UI/Series/Index/Table/AirDateCell.js @@ -6,7 +6,7 @@ Backgrid.AirDateCell = Backgrid.Cell.extend({ this.$el.empty(); var airDate = this.model.get(this.column.get("name")); - this.$el.html(bestDateString(airDate)); + this.$el.html(NzbDrone.Shared.FormatHelpers.DateHelper(airDate)); return this; } diff --git a/UI/Series/SeriesModel.js b/UI/Series/SeriesModel.js index 0590ad829..7de1e7971 100644 --- a/UI/Series/SeriesModel.js +++ b/UI/Series/SeriesModel.js @@ -5,10 +5,6 @@ define(['app', 'Quality/QualityProfileCollection', 'AddSeries/RootFolders/RootFo urlRoot: NzbDrone.Constants.ApiRoot + '/series', mutators: { - bestDateString: function () { - return bestDateString(this.get('nextAiring')); - }, - percentOfEpisodes: function () { var episodeCount = this.get('episodeCount'); var episodeFileCount = this.get('episodeFileCount'); diff --git a/UI/Shared/Cells/FileSizeCell.js b/UI/Shared/Cells/FileSizeCell.js index e375bd0e2..5c071d37d 100644 --- a/UI/Shared/Cells/FileSizeCell.js +++ b/UI/Shared/Cells/FileSizeCell.js @@ -4,11 +4,8 @@ NzbDrone.Shared.Cells.FileSizeCell = Backgrid.Cell.extend({ className: "file-size-cell", render: function () { - - var size = Number(this.model.get(this.column.get("name"))); - this.$el.html(size.bytes(1)); + var size = this.model.get(this.column.get("name")); this.delegateEvents(); - return this; - + return NzbDrone.Shared.FormatHelpers.FileSizeHelper(size); } }); diff --git a/UI/Shared/FormatHelpers.js b/UI/Shared/FormatHelpers.js new file mode 100644 index 000000000..23bd7415e --- /dev/null +++ b/UI/Shared/FormatHelpers.js @@ -0,0 +1,36 @@ +"use strict"; + +define(['app'], function () { + NzbDrone.Shared.FormatHelpers.FileSizeHelper = function (sourceSize) { + var size = Number(sourceSize); + this.$el.html(size.bytes(1)); + return this; + }; + + NzbDrone.Shared.FormatHelpers.DateHelper = function (sourceDate) { + if (!sourceDate) { + return ''; + } + + var date = Date.create(sourceDate); + + if (date.isYesterday()) { + return 'Yesterday'; + } + if (date.isToday()) { + return 'Today'; + } + if (date.isTomorrow()) { + return 'Tomorrow'; + } + if (date.isAfter(Date.create('tomorrow')) && date.isBefore(Date.create().addDays(7))) { + return date.format('{Weekday}'); + } + + if (date.isAfter(Date.create().addDays(6))) { + return date.relative().replace(' from now', ''); + } + + return date.format('{MM}/{dd}/{yyyy}'); + }; +}); \ No newline at end of file diff --git a/UI/Shared/LoadingView.js b/UI/Shared/LoadingView.js index fa0cd21a8..42f0d9456 100644 --- a/UI/Shared/LoadingView.js +++ b/UI/Shared/LoadingView.js @@ -5,6 +5,4 @@ define(['app'], function () { template : 'Shared/LoadingTemplate', className: 'nz-loading row' }); -}); - - +}); \ No newline at end of file diff --git a/UI/Shared/TemplateHelpers.js b/UI/Shared/TemplateHelpers.js new file mode 100644 index 000000000..0932f6a2c --- /dev/null +++ b/UI/Shared/TemplateHelpers.js @@ -0,0 +1,39 @@ +"use strict"; + +define(['app'], function () { + Handlebars.registerHelper('partial', function (templateName) { + //TODO: We should be able to pass in the context, either an object or a property + + var templateFunction = Marionette.TemplateCache.get(templateName); + return new Handlebars.SafeString(templateFunction(this)); + }); + + Handlebars.registerHelper("debug", function(optionalValue) { + console.log("Current Context"); + console.log("===================="); + console.log(this); + + if (optionalValue) { + console.log("Value"); + console.log("===================="); + console.log(optionalValue); + } + }); + + Handlebars.registerHelper("fileSize", function(size) { + return NzbDrone.Shared.FormatHelpers.FileSizeHelper(size); + }); + + Handlebars.registerHelper("date", function(date) { + //TODO: show actual date in tooltip + if (!date) { + return ''; + } + + var shortDate = Date.create(date).short(); + var formattedDate = NzbDrone.Shared.FormatHelpers.DateHelper(date); + var result = '
' + formattedDate + '
'; + + return new Handlebars.SafeString(result); + }); +}); \ No newline at end of file diff --git a/UI/app.js b/UI/app.js index 5da988f0f..95ffdc33d 100644 --- a/UI/app.js +++ b/UI/app.js @@ -64,7 +64,8 @@ define('app', ['shared/modal/region'], function (ModalRegion) { window.NzbDrone.Shared = { Toolbar : {}, Messenger: {}, - Cells: {} + Cells: {}, + FormatHelpers: {} }; window.NzbDrone.Calendar = {};