Skip to content

Commit

Permalink
split js files
Browse files Browse the repository at this point in the history
  • Loading branch information
bborgesr committed Apr 21, 2017
1 parent 541d3c1 commit ea91503
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 16 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
^.*\.Rproj$
^\.Rproj\.user$
^tools$
^srcjs$
^tests-manual$
^\.travis\.yml$
^appveyor\.yml$
4 changes: 3 additions & 1 deletion R/deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ appendDependencies <- function(x, value) {
addDeps <- function(x) {
if (getOption("shiny.minified", TRUE)) {
adminLTE_js <- "app.min.js"
shinydashboard_js <- "shinydashboard.min.js"
adminLTE_css <- c("AdminLTE.min.css", "_all-skins.min.css")
} else {
adminLTE_js <- "app.js"
shinydashboard_js <- "shinydashboard.js"
adminLTE_css <- c("AdminLTE.css", "_all-skins.css")
}

Expand All @@ -28,7 +30,7 @@ addDeps <- function(x) {
htmlDependency("shinydashboard",
as.character(utils::packageVersion("shinydashboard")),
c(file = system.file(package = "shinydashboard")),
script = "shinydashboard.js",
script = shinydashboard_js,
stylesheet = "shinydashboard.css"
)
)
Expand Down
2 changes: 1 addition & 1 deletion inst/AdminLTE/AdminLTE.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/AdminLTE/_all-skins.min.css

Large diffs are not rendered by default.

24 changes: 22 additions & 2 deletions inst/shinydashboard.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions inst/shinydashboard.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions srcjs/_end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
});
1 change: 1 addition & 0 deletions srcjs/_start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(function() {
48 changes: 48 additions & 0 deletions srcjs/input_binding_tabItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* global Shiny */

// tabItemInputBinding
// ------------------------------------------------------------------
// Based on Shiny.tabItemInputBinding, but customized for tabItems in
// shinydashboard, which have a slightly different structure.
var tabItemInputBinding = new Shiny.InputBinding();
$.extend(tabItemInputBinding, {
find: function(scope) {
return $(scope).find('ul.sidebar-menu');
},
getValue: function(el) {
var anchor = $(el).find('li:not(.treeview).active').children('a');
if (anchor.length === 1)
return this._getTabName(anchor);

return null;
},
setValue: function(el, value) {
var self = this;
var anchors = $(el).find('li:not(.treeview)').children('a');
anchors.each(function() {
if (self._getTabName($(this)) === value) {
$(this).tab('show');
return false;
}
});
},
receiveMessage: function(el, data) {
if (data.hasOwnProperty('value'))
this.setValue(el, data.value);
},
subscribe: function(el, callback) {
// This event is triggered by deactivateOtherTabs, which is triggered by
// shown. The deactivation of other tabs must occur before Shiny gets the
// input value.
$(el).on('change.tabItemInputBinding', function() {
callback();
});
},
unsubscribe: function(el) {
$(el).off('.tabItemInputBinding');
},
_getTabName: function(anchor) {
return anchor.attr('data-value');
}
});
Shiny.inputBindings.register(tabItemInputBinding, 'shinydashboard.tabItemInput');
45 changes: 45 additions & 0 deletions srcjs/output_binding_menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* global Shiny */

// menuOutputBinding
// ------------------------------------------------------------------
// Based on Shiny.htmlOutputBinding, but instead of putting the result in a
// wrapper div, it replaces the origin DOM element with the new DOM elements,
// copying over the ID and class.
var menuOutputBinding = new Shiny.OutputBinding();
$.extend(menuOutputBinding, {
find: function(scope) {
return $(scope).find('.shinydashboard-menu-output');
},
onValueError: function(el, err) {
Shiny.unbindAll(el);
this.renderError(el, err);
},
renderValue: function(el, data) {
Shiny.unbindAll(el);

var html;
var dependencies = [];
if (data === null) {
return;
} else if (typeof(data) === 'string') {
html = data;
} else if (typeof(data) === 'object') {
html = data.html;
dependencies = data.deps;
}

var $html = $($.parseHTML(html));

// Convert the inner contents to HTML, and pass to renderHtml
Shiny.renderHtml($html.html(), el, dependencies);

// Extract class of wrapper, and add them to the wrapper element
el.className = 'shinydashboard-menu-output shiny-bound-output ' +
$html.attr('class');

Shiny.initializeInputs(el);
Shiny.bindAll(el);
}
});
Shiny.outputBindings.register(menuOutputBinding,
"shinydashboard.menuOutputBinding");
15 changes: 15 additions & 0 deletions srcjs/sidebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Optionally disable sidebar
if ($("section.sidebar").data("disable")) {
$("body").addClass("sidebar-collapse");
$(".navbar > .sidebar-toggle").hide();
}

// Trigger the resize event when the sidebar is collapsed/expanded
// (this allows images to be responsive and resize themselves)
$(document).on("click", ".sidebar-toggle", function() {
$(window).trigger("resize");
});

$(document).on("click", ".treeview > a", function() {
$(this).next(".treeview-menu").trigger("shown");
});
46 changes: 46 additions & 0 deletions srcjs/tabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This function handles a special case in the AdminLTE sidebar: when there
// is a sidebar-menu with items, and one of those items has sub-items, and
// they are used for tab navigation. Normally, if one of the items is
// selected and then a sub-item is clicked, both the item and sub-item will
// retain the "active" class, so they will both be highlighted. This happens
// because they're not designed to be used together for tab panels. This
// code ensures that only one item will have the "active" class.
var deactivateOtherTabs = function() {
var $this = $(this);
var $sidebarMenu = $this.closest("ul.sidebar-menu");

// Find all tab links under sidebar-menu
var $tablinks = $sidebarMenu.find("a[data-toggle='tab']");

// If any other items are active, deactivate them
$tablinks.not($this).parent("li").removeClass("active");

// Trigger event for the tabItemInputBinding
$sidebarMenu.trigger('change.tabItemInputBinding');
};

$(document).on('shown.bs.tab', '.sidebar-menu a[data-toggle="tab"]',
deactivateOtherTabs);


// When document is ready, if there is a sidebar menu with no activated tabs,
// activate the one specified by `data-start-selected`, or if that's not
// present, the first one.
var ensureActivatedTab = function() {
var $tablinks = $("ul.sidebar-menu").find("a").filter("[data-toggle='tab']");

// If there's a `data-start-selected` attribute and we can find a tab with
// that name, activate it.
var $startTab = $tablinks.filter("[data-start-selected='1']");
if ($startTab.length !== 0) {
$startTab.tab("show");
return;
}

// If we got this far, just activate the first tab.
if (! $tablinks.parent("li").hasClass("active") ) {
$tablinks.first().tab("show");
}
};

ensureActivatedTab();
43 changes: 34 additions & 9 deletions tools/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
module.exports = function(grunt) {

var srcdir = '../inst/';
var srcdirjs = '../srcjs/';
var srcdircss = '../inst/';
var destdirjs = '../inst/';
var destdircss = '../inst/';

grunt.initConfig({
pkg: pkgInfo(),
concat: {
options: {
process: function(src, filepath) {
return '//---------------------------------------------------------------------\n' +
'// Source file: ' + filepath + '\n\n' + src;
},
sourceMap: true
},
shinydashboard: {
src: [
srcdirjs + '_start.js',
srcdirjs + 'tabs.js',
srcdirjs + 'sidebar.js',
srcdirjs + 'output_binding_menu.js',
srcdirjs + 'input_binding_tabItem.js',
srcdirjs + '_end.js'
],
dest: destdirjs + 'shinydashboard.js'
}
},

uglify: {
adminlte: {
options: {
sourceMap: true
},
src: srcdir + '/AdminLTE/app.js',
dest: srcdir + '/AdminLTE/app.min.js'
src: destdirjs + '/AdminLTE/app.js',
dest: destdirjs + '/AdminLTE/app.min.js'
}
},

cssmin: {
adminlte: {
src: srcdir + '/AdminLTE/AdminLTE.css',
dest: srcdir + '/AdminLTE/AdminLTE.min.css'
src: srcdircss + '/AdminLTE/AdminLTE.css',
dest: srcdircss + '/AdminLTE/AdminLTE.min.css'
},
adminlte_themes: {
src: srcdir + '/AdminLTE/_all-skins.css',
dest: srcdir + '/AdminLTE/_all-skins.min.css'
src: srcdircss + '/AdminLTE/_all-skins.css',
dest: srcdircss + '/AdminLTE/_all-skins.min.css'
}
},

Expand All @@ -30,7 +54,7 @@ module.exports = function(grunt) {
force: true // Don't abort if there are JSHint warnings
},
shinydashboard: {
src: srcdir + '/shinydashboard.js',
src: destdirjs + '/shinydashboard.js',
}
},

Expand All @@ -51,10 +75,11 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-newer');


grunt.registerTask('default', ['newer:uglify', 'newer:cssmin', 'newer:jshint']);
grunt.registerTask('default', ['newer:concat', 'newer:uglify', 'newer:cssmin', 'newer:jshint']);



Expand Down
1 change: 1 addition & 0 deletions tools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"devDependencies": {
"grunt": "^1.0.1",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-cssmin": "^2.0.0",
"grunt-contrib-jshint": "^1.1.0",
"grunt-contrib-uglify": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion tools/updateAdminLTE.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ srcdir <- file.path(dirname(thisFile()), "../../AdminLTE/dist")
destdir <- file.path(dirname(thisFile()), "../inst/AdminLTE")


file.copy(file.path(srcdir, "/js/app.js"), destdir, overwrite = TRUE)
file.copy(file.path(srcdir, "/js/app.js"), "../srcjs/AdminLTE", overwrite = TRUE)
file.copy(file.path(srcdir, "/css/AdminLTE.css"), destdir, overwrite = TRUE)
file.copy(file.path(srcdir, "/css/skins/_all-skins.css"), destdir, overwrite = TRUE)
9 changes: 8 additions & 1 deletion tools/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ grunt-cli@~1.2.0:
nopt "~3.0.6"
resolve "~1.1.0"

grunt-contrib-concat@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/grunt-contrib-concat/-/grunt-contrib-concat-1.0.1.tgz#61509863084e871d7e86de48c015259ed97745bd"
dependencies:
chalk "^1.0.0"
source-map "^0.5.3"

grunt-contrib-cssmin@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/grunt-contrib-cssmin/-/grunt-contrib-cssmin-2.0.0.tgz#3bc8e8c8865c819159cc2779f82fcf833473345b"
Expand Down Expand Up @@ -907,7 +914,7 @@ signal-exit@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

source-map@0.5.x, source-map@~0.5.1:
source-map@0.5.x, source-map@^0.5.3, source-map@~0.5.1:
version "0.5.6"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.6.tgz#75ce38f52bf0733c5a7f0c118d81334a2bb5f412"

Expand Down

0 comments on commit ea91503

Please sign in to comment.