8 changed files with 273 additions and 217 deletions
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 1.8 KiB |
@ -1,181 +1,208 @@ |
|||||
;(function($) { |
;(function($) { |
||||
"use strict"; |
'use strict'; |
||||
|
|
||||
$.Browser = { |
$.Browser = { |
||||
defaults: { |
defaults: { |
||||
title: 'Choose Directory', |
title: 'Choose Directory (or enter manually)', |
||||
url: sbRoot + '/browser/', |
url: sbRoot + '/browser/', |
||||
autocompleteURL: sbRoot + '/browser/complete', |
autocompleteURL: sbRoot + '/browser/complete', |
||||
includeFiles: 0 |
includeFiles: 0, |
||||
} |
showBrowseButton: !0 |
||||
}; |
} |
||||
|
}; |
||||
var fileBrowserDialog, currentBrowserPath, currentRequest = null; |
|
||||
|
var fileBrowserDialog, currentBrowserPath, currentRequest = null; |
||||
function browse(path, endpoint, includeFiles) { |
|
||||
|
function browse(path, endpoint, includeFiles) { |
||||
if (currentBrowserPath == path) { |
|
||||
return; |
if (path === currentBrowserPath) { |
||||
} |
return; |
||||
|
} |
||||
currentBrowserPath = path; |
|
||||
|
currentBrowserPath = path; |
||||
if (currentRequest) { |
|
||||
currentRequest.abort(); |
if (currentRequest) { |
||||
} |
currentRequest.abort(); |
||||
|
} |
||||
fileBrowserDialog.dialog('option', 'dialogClass', 'browserDialog busy'); |
|
||||
|
fileBrowserDialog.dialog('option', 'dialogClass', 'browserDialog busy'); |
||||
currentRequest = $.getJSON(endpoint, { path: path, includeFiles: includeFiles }, function (data) { |
|
||||
fileBrowserDialog.empty(); |
currentRequest = $.getJSON(endpoint, {path: path, includeFiles: includeFiles}, function(data){ |
||||
var first_val = data[0]; |
fileBrowserDialog.empty(); |
||||
var i = 0; |
var firstVal = data[0], i = 0, list, link = null; |
||||
var list, link = null; |
data = $.grep(data, function(){ |
||||
data = $.grep(data, function (value) { |
return i++ != 0; |
||||
return i++ != 0; |
}); |
||||
}); |
$('<input type="text" class="form-control input-sm">') |
||||
$('<h2>').text(first_val.current_path).appendTo(fileBrowserDialog); |
.val(firstVal.currentPath) |
||||
list = $('<ul>').appendTo(fileBrowserDialog); |
.on('keypress', function(e){ |
||||
$.each(data, function (i, entry) { |
if (13 === e.which) { |
||||
link = $("<a href='javascript:void(0)' />").click(function () { browse(entry.path, endpoint, includeFiles); }).text(entry.name); |
browse(e.target.value, endpoint, includeFiles); |
||||
$('<span class="ui-icon ui-icon-folder-collapsed"></span>').prependTo(link); |
} |
||||
link.hover( |
}) |
||||
function () {$("span", this).addClass("ui-icon-folder-open"); }, |
.appendTo(fileBrowserDialog) |
||||
function () {$("span", this).removeClass("ui-icon-folder-open"); } |
.fileBrowser({showBrowseButton: !1}) |
||||
); |
.on('autocompleteselect', |
||||
link.appendTo(list); |
function(e, ui){browse(ui.item.value, endpoint, includeFiles); |
||||
}); |
}); |
||||
$("a", list).wrap('<li class="ui-state-default ui-corner-all">'); |
|
||||
fileBrowserDialog.dialog('option', 'dialogClass', 'browserDialog'); |
list = $('<ul>').appendTo(fileBrowserDialog); |
||||
}); |
$.each(data, function(i, entry){ |
||||
} |
link = $('<a href="javascript:void(0)">').on('click', |
||||
|
function(){ |
||||
$.fn.nFileBrowser = function (callback, options) { |
if (entry.isFile) { |
||||
options = $.extend({}, $.Browser.defaults, options); |
currentBrowserPath = entry.path; |
||||
|
$('.browserDialog .ui-button:contains("Ok")').click(); |
||||
// make a fileBrowserDialog object if one doesn't exist already
|
} else { |
||||
if (!fileBrowserDialog) { |
browse(entry.path, endpoint, includeFiles); |
||||
|
} |
||||
// set up the jquery dialog
|
}).text(entry.name); |
||||
fileBrowserDialog = $('<div id="fileBrowserDialog" style="display:hidden"></div>').appendTo('body').dialog({ |
|
||||
dialogClass: 'browserDialog', |
if (entry.isFile) { |
||||
title: options.title, |
link.prepend('<span class="ui-icon ui-icon-blank"></span>'); |
||||
position: ['center', 40], |
} else { |
||||
minWidth: Math.min($(document).width() - 80, 650), |
link.prepend('<span class="ui-icon ui-icon-folder-collapsed"></span>') |
||||
height: Math.min($(document).height() - 80, $(window).height() - 80), |
.on('mouseenter', function(){$('span', this).addClass('ui-icon-folder-open');}) |
||||
maxHeight: Math.min($(document).height() - 80, $(window).height() - 80), |
.on('mouseleave', function(){$('span', this).removeClass('ui-icon-folder-open');}); |
||||
maxWidth: $(document).width() - 80, |
} |
||||
modal: true, |
link.appendTo(list); |
||||
autoOpen: false |
}); |
||||
}); |
$('a', list).wrap('<li class="ui-state-default ui-corner-all">'); |
||||
} |
fileBrowserDialog.dialog('option', 'dialogClass', 'browserDialog'); |
||||
|
}); |
||||
fileBrowserDialog.dialog('option', 'buttons', [ |
} |
||||
{ |
|
||||
text: "Ok", |
$.fn.nFileBrowser = function(callback, options){ |
||||
"class": "btn", |
options = $.extend({}, $.Browser.defaults, options); |
||||
click: function() { |
|
||||
// store the browsed path to the associated text field
|
// make a fileBrowserDialog object if one doesn't exist already
|
||||
callback(currentBrowserPath, options); |
if (fileBrowserDialog) { |
||||
$(this).dialog("close"); |
fileBrowserDialog.dialog('option', 'title', options.title); |
||||
} |
} else { |
||||
}, |
// set up the jquery dialog
|
||||
{ |
var docWidth = $(document).width(), dlgWidth = Math.min(docWidth - 80, 650), |
||||
text: "Cancel", |
docHeight = $(document).height() - 80, winHeight = $(window).height() - 80; |
||||
"class": "btn", |
fileBrowserDialog = $('<div id="fileBrowserDialog" style="display:none"></div>').appendTo('body').dialog({ |
||||
click: function() { |
dialogClass: 'browserDialog', |
||||
$(this).dialog("close"); |
title: options.title, |
||||
} |
position: [(docWidth - dlgWidth)/2, 60], |
||||
} |
minWidth: dlgWidth, |
||||
]); |
height: Math.min(docHeight, winHeight), |
||||
|
maxHeight: Math.min(docHeight, winHeight), |
||||
// set up the browser and launch the dialog
|
maxWidth: docWidth - 80, |
||||
var initialDir = ''; |
modal: true, |
||||
if (options.initialDir) { |
autoOpen: false |
||||
initialDir = options.initialDir; |
}); |
||||
} |
} |
||||
|
|
||||
browse(initialDir, options.url, options.includeFiles); |
fileBrowserDialog.dialog('option', 'buttons', |
||||
fileBrowserDialog.dialog('open'); |
[{ |
||||
|
text: 'Ok', |
||||
return false; |
'class': 'btn', |
||||
}; |
click: function(){ |
||||
|
// store the browsed path to the associated text field
|
||||
$.fn.fileBrowser = function (options) { |
callback(currentBrowserPath, options); |
||||
options = $.extend({}, $.Browser.defaults, options); |
$(this).dialog('close'); |
||||
// text field used for the result
|
} |
||||
options.field = $(this); |
}, |
||||
|
{ |
||||
if (options.field.autocomplete && options.autocompleteURL) { |
text: 'Cancel', |
||||
var query = ''; |
'class': 'btn', |
||||
options.field.autocomplete({ |
click: function(){ |
||||
position: { my : "top", at: "bottom", collision: "flipfit" }, |
$(this).dialog('close'); |
||||
source: function (request, response) { |
} |
||||
//keep track of user submitted search term
|
}]); |
||||
query = $.ui.autocomplete.escapeRegex(request.term, options.includeFiles); |
|
||||
$.ajax({ |
// set up the browser and launch the dialog
|
||||
url: options.autocompleteURL, |
var initialDir = ''; |
||||
data: request, |
if (options.initialDir) { |
||||
dataType: "json", |
initialDir = options.initialDir; |
||||
success: function (data, item) { |
} |
||||
//implement a startsWith filter for the results
|
|
||||
var matcher = new RegExp("^" + query, "i"); |
browse(initialDir, options.url, options.includeFiles); |
||||
var a = $.grep(data, function (item, index) { |
fileBrowserDialog.dialog('open'); |
||||
return matcher.test(item); |
|
||||
}); |
return false; |
||||
response(a); |
}; |
||||
} |
|
||||
}); |
$.fn.fileBrowser = function(options){ |
||||
}, |
options = $.extend({}, $.Browser.defaults, options); |
||||
open: function (event, ui) { |
// text field used for the result
|
||||
$(".ui-autocomplete li.ui-menu-item a").removeClass("ui-corner-all"); |
options.field = $(this); |
||||
$(".ui-autocomplete li.ui-menu-item:odd a").addClass("ui-menu-item-alternate"); |
|
||||
} |
if (options.field.autocomplete && options.autocompleteURL) { |
||||
}) |
var query = ''; |
||||
.data("ui-autocomplete")._renderItem = function (ul, item) { |
options.field.autocomplete({ |
||||
//highlight the matched search term from the item -- note that this is global and will match anywhere
|
position: {my: 'top', at: 'bottom', collision: 'flipfit'}, |
||||
var result_item = item.label; |
source: function(request, response){ |
||||
var x = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + query + ")(?![^<>]*>)(?![^&;]+;)", "gi"); |
//keep track of user submitted search term
|
||||
result_item = result_item.replace(x, function (FullMatch, n) { |
query = $.ui.autocomplete.escapeRegex(request.term, options.includeFiles); |
||||
return '<b>' + FullMatch + '</b>'; |
$.ajax({ |
||||
}); |
url: options.autocompleteURL, |
||||
return $("<li></li>") |
data: request, |
||||
.data("ui-autocomplete-item", item) |
dataType: 'json', |
||||
.append("<a class='nowrap'>" + result_item + "</a>") |
success: function(data){ |
||||
.appendTo(ul); |
//implement a startsWith filter for the results
|
||||
}; |
var matcher = new RegExp('^' + query, 'i'); |
||||
} |
var a = $.grep(data, function(item){ |
||||
|
return matcher.test(item); |
||||
var initialDir, path, callback, ls = false; |
}); |
||||
// if the text field is empty and we're given a key then populate it with the last browsed value from localStorage
|
response(a); |
||||
try { ls = !!(localStorage.getItem); } catch (e) {} |
} |
||||
if (ls && options.key) { |
}); |
||||
path = localStorage['fileBrowser-' + options.key]; |
}, |
||||
} |
open: function(){ |
||||
if (options.key && options.field.val().length == 0 && (path)) { |
$('.ui-autocomplete li.ui-menu-item a').removeClass('ui-corner-all'); |
||||
options.field.val(path); |
$('.ui-autocomplete li.ui-menu-item:odd a').addClass('ui-menu-item-alternate'); |
||||
} |
} |
||||
|
}).data('ui-autocomplete')._renderItem = function(ul, item){ |
||||
callback = function (path, options) { |
//highlight the matched search term from the item -- note that this is global and will match anywhere
|
||||
// store the browsed path to the associated text field
|
var resultItem = item.label; |
||||
options.field.val(path); |
var x = new RegExp('(?![^&;]+;)(?!<[^<>]*)(' + query + ')(?![^<>]*>)(?![^&;]+;)', 'gi'); |
||||
|
resultItem = resultItem.replace(x, function(fullMatch){ |
||||
// use a localStorage to remember for next time -- no ie6/7
|
return '<b>' + fullMatch + '</b>'; |
||||
if (ls && options.key) { |
}); |
||||
localStorage['fileBrowser-' + options.key] = path; |
return $('<li></li>') |
||||
} |
.data('ui-autocomplete-item', item) |
||||
|
.append('<a class="nowrap">' + resultItem + '</a>') |
||||
}; |
.appendTo(ul); |
||||
|
}; |
||||
initialDir = options.field.val() || (options.key && path) || ''; |
} |
||||
|
|
||||
options = $.extend(options, {initialDir: initialDir}); |
var path, callback, ls = false; |
||||
|
// if empty text field and given a key then populate it with the last browsed value from localStorage
|
||||
// append the browse button and give it a click behaviour
|
try { ls = !!(localStorage.getItem); } catch (e) {} |
||||
return options.field.addClass('fileBrowserField').after($('<input type="button" value="Browse…" class="btn btn-inline fileBrowser" />').click(function () { |
if (ls && options.key) { |
||||
$(this).nFileBrowser(callback, options); |
path = localStorage['fileBrowser-' + options.key]; |
||||
return false; |
} |
||||
})); |
if (options.key && options.field.val().length == 0 && (path)) { |
||||
}; |
options.field.val(path); |
||||
|
} |
||||
|
|
||||
|
callback = function(path, options){ |
||||
|
// store the browsed path to the associated text field
|
||||
|
options.field.val(path); |
||||
|
|
||||
|
// use a localStorage to remember for next time -- no ie6/7
|
||||
|
if (ls && options.key) { |
||||
|
localStorage['fileBrowser-' + options.key] = path; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
options.field.addClass('fileBrowserField'); |
||||
|
if (options.showBrowseButton) { |
||||
|
// append the browse button and give it a click behaviour
|
||||
|
options.field.after( |
||||
|
$('<input type="button" value="Browse…" class="btn btn-inline fileBrowser">').on('click', |
||||
|
function(){ |
||||
|
$(this).nFileBrowser(callback, $.extend( |
||||
|
{}, options, {initialDir: options.field.val() || (options.key && path) || ''} |
||||
|
)); |
||||
|
return false; |
||||
|
})); |
||||
|
} |
||||
|
return options.field; |
||||
|
}; |
||||
|
|
||||
})(jQuery); |
})(jQuery); |
||||
|
Loading…
Reference in new issue