2017-11-12 22:00:40 +01:00
// Copyright (c) 2017 kroppy. All rights reserved.
// Use of this source code is governed by a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) license
// that can be found at https://creativecommons.org/licenses/by-nc-nd/4.0/
2017-07-26 22:23:39 +02:00
// ********** TABS FUNCTIONS ***************
2017-11-12 22:00:40 +01:00
async function UpdateData ( ) {
2017-11-20 18:14:07 +01:00
setInterval ( function ( ) {
2017-11-12 22:00:40 +01:00
if ( schedule _update _data > 1 ) { schedule _update _data = 1 ; }
if ( schedule _update _data > 0 ) {
$ ( ".pin" ) . each ( function ( ) {
chrome . runtime . sendMessage ( {
command : "update_tab" ,
tabId : parseInt ( this . id ) ,
tab : {
parent : "pin_list" ,
index : $ ( this ) . index ( ) ,
expand : "n"
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
} ) ;
} ) ;
$ ( ".tab" ) . each ( function ( ) {
chrome . runtime . sendMessage ( {
command : "update_tab" ,
tabId : parseInt ( this . id ) ,
tab : {
parent : $ ( this ) . parent ( ".group" ) [ 0 ] ? $ ( this ) . parent ( ) [ 0 ] . id : $ ( this ) . parent ( ) . parent ( ".tab, .folder" ) [ 0 ] . id ,
index : $ ( this ) . index ( ) ,
expand : ( $ ( this ) . is ( ".n" ) ? "n" : ( $ ( this ) . is ( ".c" ) ? "c" : "o" ) )
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
} ) ;
2017-07-26 22:23:39 +02:00
} ) ;
schedule _update _data -- ;
}
2017-11-20 18:14:07 +01:00
} , 1000 ) ;
2017-11-12 22:00:40 +01:00
}
function RearrangeBrowserTabsCheck ( ) {
2017-11-20 18:14:07 +01:00
setInterval ( function ( ) {
2017-11-12 22:00:40 +01:00
if ( opt . syncro _tabbar _tabs _order ) {
if ( schedule _rearrange _tabs > 1 ) { schedule _rearrange _tabs = 1 ; }
if ( schedule _rearrange _tabs > 0 ) {
let tabIds = $ ( ".pin, .tab" ) . map ( function ( ) { return parseInt ( this . id ) ; } ) . toArray ( ) ;
RearrangeBrowserTabs ( tabIds , tabIds . length - 1 ) ;
schedule _rearrange _tabs -- ;
}
}
2017-11-20 18:14:07 +01:00
} , 1000 ) ;
2017-11-12 22:00:40 +01:00
}
async function RearrangeBrowserTabs ( tabIds , tabIndex ) {
if ( tabIndex > 0 ) {
chrome . tabs . get ( tabIds [ tabIndex ] , function ( tab ) {
if ( tab && tabIndex != tab . index ) {
chrome . tabs . move ( tabIds [ tabIndex ] , { index : tabIndex } ) ;
}
2017-11-20 18:14:07 +01:00
setTimeout ( function ( ) {
RearrangeBrowserTabs ( tabIds , ( tabIndex - 1 ) ) ;
} , 1 ) ;
2017-11-12 22:00:40 +01:00
} ) ;
}
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
function RearrangeTreeTabs ( tabs , bgtabs , first _run ) {
tabs . forEach ( function ( Tab ) {
if ( bgtabs [ Tab . id ] && $ ( "#" + Tab . id ) [ 0 ] && $ ( "#" + Tab . id ) . parent ( ) . children ( ) . eq ( bgtabs [ Tab . id ] . index ) [ 0 ] ) {
if ( $ ( "#" + Tab . id ) . index ( ) > bgtabs [ Tab . id ] . index ) {
$ ( "#" + Tab . id ) . insertBefore ( $ ( "#" + Tab . id ) . parent ( ) . children ( ) . eq ( bgtabs [ Tab . id ] . index ) ) ;
} else {
$ ( "#" + Tab . id ) . insertAfter ( $ ( "#" + Tab . id ) . parent ( ) . children ( ) . eq ( bgtabs [ Tab . id ] . index ) ) ;
}
}
if ( bgtabs [ Tab . id ] && $ ( "#" + Tab . id ) . index ( ) != bgtabs [ Tab . id ] . index && first _run ) {
RearrangeTreeTabs ( tabs , bgtabs , false ) ;
}
} ) ;
}
// param - tuple object with paramenters: param.tab - tab object, param.ParentId - Parent tabId, param.InsertAfterId - insert tab after this tabId (on same level),
// param.Append - if true Appends tab at the end of tree if false or prepends
function AppendTab ( param ) {
if ( $ ( "#" + param . tab . id ) . length > 0 ) {
2017-11-20 18:14:07 +01:00
GetFaviconAndTitle ( param . tab . id , param . addCounter ) ;
2017-07-26 22:23:39 +02:00
return ;
}
2017-11-12 22:00:40 +01:00
var ClassList = param . tab . pinned ? "pin" : "tab n" ;
if ( param . tab . discarded ) {
ClassList = ClassList + " discarded" ;
}
if ( param . AdditionalClass ) {
ClassList = ClassList + " " + param . AdditionalClass ;
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
var tb = document . createElement ( "div" ) ; tb . className = ClassList ; tb . id = param . tab . id ; // TAB
var dc = document . createElement ( "div" ) ; dc . className = "drop_target drag_enter_center" ; dc . id = "dc" + param . tab . id ; tb . appendChild ( dc ) ; // DROP TARGET CENTER
var dt = document . createElement ( "div" ) ; dt . className = "drop_target drag_entered_top" ; dt . id = "du" + param . tab . id ; tb . appendChild ( dt ) ; // DROP TARGET TOP
var db = document . createElement ( "div" ) ; db . className = "drop_target drag_entered_bottom" ; db . id = "dd" + param . tab . id ; tb . appendChild ( db ) ; // DROP TARGET BOTTOM
var th = document . createElement ( "div" ) ; th . className = opt . always _show _close ? "tab_header close_show" : "tab_header" ; th . id = "tab_header" + param . tab . id ; th . draggable = true ; tb . appendChild ( th ) ; // HEADER
var ex = document . createElement ( "div" ) ; ex . className = "expand" ; ex . id = "exp" + param . tab . id ; th . appendChild ( ex ) ; // EXPAND ARROW
var tt = document . createElement ( "div" ) ; tt . className = "tab_title" ; tt . id = "tab_title" + param . tab . id ; th . appendChild ( tt ) ; // TITLE
if ( ! opt . never _show _close ) {
var cl = document . createElement ( "div" ) ; cl . className = "close" ; cl . id = "close" + param . tab . id ; th . appendChild ( cl ) ; // CLOSE BUTTON
var ci = document . createElement ( "div" ) ; ci . className = "close_img" ; ci . id = "close_img" + param . tab . id ; cl . appendChild ( ci ) ;
}
var mi = document . createElement ( "div" ) ; mi . className = "tab_mediaicon" ; mi . id = "tab_mediaicon" + param . tab . id ; th . appendChild ( mi ) ;
var ch = document . createElement ( "div" ) ; ch . className = "children" ; ch . id = "ch" + param . tab . id ; tb . appendChild ( ch ) ;
if ( param . tab . pinned ) {
param . ParentId = "pin_list" ;
} else {
if ( param . ParentId == undefined || $ ( "#" + param . ParentId ) . is ( ".pin, #pin_list" ) || $ ( "#" + param . ParentId ) . length == 0 ) {
param . ParentId = active _group ;
} else {
if ( $ ( "#" + param . ParentId ) . is ( ".tab" ) ) {
if ( $ ( "#ch" + param . ParentId ) . children ( ) . length == 0 ) {
$ ( "#" + param . ParentId ) . addClass ( "o" ) . removeClass ( "n" ) . removeClass ( "c" ) ;
}
param . ParentId = "ch" + param . ParentId ;
}
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
}
if ( param . Append ) {
$ ( "#" + param . ParentId ) . append ( tb ) ;
}
if ( ! param . Append ) {
$ ( "#" + param . ParentId ) . prepend ( tb ) ;
}
if ( param . InsertBeforeId != undefined && $ ( "#" + param . InsertBeforeId ) [ 0 ] ) {
if ( ( param . tab . pinned && $ ( "#" + param . InsertBeforeId ) . is ( ".pin" ) ) || ( ! param . tab . pinned && $ ( "#" + param . InsertBeforeId ) . is ( ".tab" ) ) ) {
$ ( "#" + param . tab . id ) . insertBefore ( $ ( "#" + param . InsertBeforeId ) ) ;
2017-07-26 22:23:39 +02:00
}
}
2017-11-12 22:00:40 +01:00
if ( param . InsertAfterId != undefined && $ ( "#" + param . InsertAfterId ) [ 0 ] ) {
if ( ( param . tab . pinned && $ ( "#" + param . InsertAfterId ) . is ( ".pin" ) ) || ( ! param . tab . pinned && $ ( "#" + param . InsertAfterId ) . is ( ".tab" ) ) ) {
$ ( "#" + param . tab . id ) . insertAfter ( $ ( "#" + param . InsertAfterId ) ) ;
2017-07-26 22:23:39 +02:00
}
}
2017-11-12 22:00:40 +01:00
2017-11-20 18:14:07 +01:00
GetFaviconAndTitle ( param . tab . id , param . addCounter ) ;
2017-11-12 22:00:40 +01:00
RefreshMediaIcon ( param . tab . id ) ;
2017-11-20 18:14:07 +01:00
2017-11-12 22:00:40 +01:00
if ( param . tab . active && param . SkipSetActive == undefined ) {
SetActiveTab ( param . tab . id ) ;
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
if ( param . Scroll ) {
ScrollToTab ( param . tab . id ) ;
2017-07-26 22:23:39 +02:00
}
}
2017-11-12 22:00:40 +01:00
function RemoveTabFromList ( tabId ) {
if ( $ ( "#" + tabId ) [ 0 ] ) {
2017-07-26 22:23:39 +02:00
$ ( "#" + tabId ) . remove ( ) ;
}
}
2017-11-12 22:00:40 +01:00
// param - tuple object with paramenters: param.pin - true for pinned, param.id - tabId
function SetTabClass ( param ) {
if ( param . pin ) {
$ ( "#pin_list" ) . append ( $ ( "#" + param . id ) ) ;
2017-07-26 22:23:39 +02:00
// flatten out children
2017-11-12 22:00:40 +01:00
if ( $ ( "#ch" + param . id ) . children ( ) . length > 0 ) {
$ ( $ ( "#" + param . id ) . children ( ) . find ( ".pin, .tab" ) . get ( ) . reverse ( ) ) . each ( function ( ) {
2017-07-26 22:23:39 +02:00
$ ( this ) . removeClass ( "tab" ) . removeClass ( "n" ) . removeClass ( "o" ) . removeClass ( "c" ) . addClass ( "pin" ) ;
2017-11-12 22:00:40 +01:00
$ ( this ) . insertAfter ( $ ( "#" + param . id ) ) ;
2017-07-26 22:23:39 +02:00
chrome . tabs . update ( parseInt ( this . id ) , { pinned : true } ) ;
} ) ;
}
2017-11-12 22:00:40 +01:00
$ ( "#" + param . id ) . removeClass ( "tab" ) . removeClass ( "n" ) . removeClass ( "o" ) . removeClass ( "c" ) . addClass ( "pin" ) ;
2017-07-26 22:23:39 +02:00
} else {
2017-11-12 22:00:40 +01:00
$ ( "#" + active _group ) . prepend ( $ ( "#" + param . id ) ) ;
$ ( "#" + param . id ) . removeClass ( "pin" ) . removeClass ( "attention" ) . addClass ( "tab" ) ;
2017-07-26 22:23:39 +02:00
RefreshExpandStates ( ) ;
}
2017-11-12 22:00:40 +01:00
chrome . tabs . update ( parseInt ( param . id ) , { pinned : param . pin } ) ;
2017-07-26 22:23:39 +02:00
RefreshGUI ( ) ;
}
2017-11-12 22:00:40 +01:00
function SetActiveTab ( tabId ) {
if ( $ ( "#" + tabId ) . length > 0 ) {
$ ( ".active:visible" ) . removeClass ( "active" ) . removeClass ( "selected" ) ;
$ ( ".pin, .tab:visible" ) . removeClass ( "active" ) . removeClass ( "selected" ) . removeClass ( "selected_frozen" ) . removeClass ( "selected_temporarly" ) . removeClass ( "tab_header_hover" ) ;
2017-07-26 22:23:39 +02:00
$ ( ".highlighted_drop_target" ) . removeClass ( "highlighted_drop_target" ) ;
2017-11-20 18:14:07 +01:00
$ ( "#" + tabId ) . removeClass ( "attention" ) . addClass ( "active" ) ;
2017-07-26 22:23:39 +02:00
ScrollToTab ( tabId ) ;
2017-11-12 22:00:40 +01:00
SetActiveTabInActiveGroup ( tabId ) ;
2017-07-26 22:23:39 +02:00
}
}
2017-11-12 22:00:40 +01:00
function ScrollToTab ( tabId ) {
if ( $ ( "#" + tabId ) . length == 0 ) {
2017-07-26 22:23:39 +02:00
return false ;
}
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabId ) . is ( ":not(:visible)" ) ) {
2017-07-26 22:23:39 +02:00
$ ( "#" + tabId ) . parents ( ".tab" ) . removeClass ( "c" ) . addClass ( "o" ) ;
}
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabId ) . is ( ".pin" ) ) {
if ( $ ( "#" + tabId ) . position ( ) . left + $ ( "#" + tabId ) . outerWidth ( ) > $ ( "#pin_list" ) . innerWidth ( ) ) {
2017-07-26 22:23:39 +02:00
$ ( "#pin_list" ) . scrollLeft ( $ ( "#pin_list" ) . scrollLeft ( ) + $ ( "#" + tabId ) . position ( ) . left + $ ( "#" + tabId ) . outerWidth ( ) - $ ( "#pin_list" ) . innerWidth ( ) ) ;
} else {
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabId ) . position ( ) . left < 0 ) {
2017-07-26 22:23:39 +02:00
$ ( "#pin_list" ) . scrollLeft ( $ ( "#pin_list" ) . scrollLeft ( ) + $ ( "#" + tabId ) . position ( ) . left ) ;
}
}
}
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabId ) . is ( ".tab" ) ) {
if ( $ ( "#" + tabId ) . offset ( ) . top - $ ( "#" + active _group ) . offset ( ) . top < 0 ) {
$ ( "#" + active _group ) . scrollTop ( $ ( "#" + active _group ) . scrollTop ( ) + $ ( "#" + tabId ) . offset ( ) . top - $ ( "#" + active _group ) . offset ( ) . top ) ;
2017-07-26 22:23:39 +02:00
} else {
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabId ) . offset ( ) . top - $ ( "#" + active _group ) . offset ( ) . top > $ ( "#" + active _group ) . innerHeight ( ) - $ ( ".tab_header" ) . outerHeight ( ) ) {
$ ( "#" + active _group ) . scrollTop ( $ ( "#" + active _group ) . scrollTop ( ) + $ ( "#" + tabId ) . offset ( ) . top - $ ( "#" + active _group ) . offset ( ) . top - $ ( "#" + active _group ) . innerHeight ( ) + $ ( ".tab_header" ) . outerHeight ( ) + 4 ) ;
2017-07-26 22:23:39 +02:00
}
}
}
}
2017-11-12 22:00:40 +01:00
function DetachTabs ( tabsIds ) {
chrome . windows . get ( CurrentWindowId , { populate : true } , function ( window ) {
if ( window . tabs . length == 1 ) {
2017-07-26 22:23:39 +02:00
return ;
}
2017-11-12 22:00:40 +01:00
chrome . windows . create ( { tabId : tabsIds [ 0 ] , state : window . state } , function ( new _window ) {
( tabsIds ) . forEach ( function ( tabId ) {
chrome . tabs . move ( tabId , { windowId : new _window . id , index : - 1 } ) ;
} ) ;
2017-07-26 22:23:39 +02:00
} )
} ) ;
}
// find and select tabs
2017-11-12 22:00:40 +01:00
function FindTab ( input ) {
2017-07-26 22:23:39 +02:00
$ ( ".filtered" ) . removeClass ( "filtered" ) . removeClass ( "selected" ) ;
$ ( ".highlighted_search" ) . removeClass ( "highlighted_search" ) ;
2017-11-12 22:00:40 +01:00
if ( input . length == 0 ) {
2017-07-26 22:23:39 +02:00
$ ( "#filter_box" ) [ 0 ] . value = "" ;
2017-11-12 22:00:40 +01:00
$ ( "#button_filter_clear" ) . css ( { "opacity" : "0" } ) . attr ( "title" , "" ) ;
2017-07-26 22:23:39 +02:00
return ;
2017-11-12 22:00:40 +01:00
} else {
$ ( "#button_filter_clear" ) . css ( { "opacity" : "1" } ) ;
$ ( "#button_filter_clear" ) . attr ( "title" , caption _clear _filter ) ;
2017-07-26 22:23:39 +02:00
}
SearchIndex = 0 ;
2017-11-12 22:00:40 +01:00
chrome . tabs . query ( { windowId : CurrentWindowId , pinned : false } , function ( tabs ) {
tabs . forEach ( function ( Tab ) {
if ( $ ( "#button_filter_type" ) . is ( ".url" ) && Tab . url . toLowerCase ( ) . match ( input . toLowerCase ( ) ) ) {
2017-07-26 22:23:39 +02:00
$ ( "#" + Tab . id ) . addClass ( "filtered" ) . addClass ( "selected" ) ;
}
2017-11-12 22:00:40 +01:00
if ( $ ( "#button_filter_type" ) . is ( ".title" ) && Tab . title . toLowerCase ( ) . match ( input . toLowerCase ( ) ) ) {
2017-07-26 22:23:39 +02:00
$ ( "#" + Tab . id ) . addClass ( "filtered" ) . addClass ( "selected" ) ;
}
} ) ;
} ) ;
}
2017-11-12 22:00:40 +01:00
function CloseTabs ( tabsIds ) {
2017-07-26 22:23:39 +02:00
tabsIds . forEach ( function ( tabId ) {
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabId ) . is ( ".pin" ) && opt . allow _pin _close ) {
2017-07-26 22:23:39 +02:00
$ ( "#" + tabId ) . remove ( ) ;
chrome . tabs . update ( tabId , { pinned : false } ) ;
}
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabId ) . is ( ".tab" ) ) {
2017-07-26 22:23:39 +02:00
$ ( "#" + tabId ) . remove ( ) ;
}
} ) ;
2017-11-12 22:00:40 +01:00
setTimeout ( function ( ) {
2017-07-26 22:23:39 +02:00
chrome . tabs . remove ( tabsIds , null ) ;
2017-11-20 18:14:07 +01:00
} , 100 ) ;
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
function DiscardTabs ( tabsIds ) {
2017-07-26 22:23:39 +02:00
var delay = 400 ;
2017-11-12 22:00:40 +01:00
if ( $ ( "#" + tabsIds [ 0 ] ) . is ( ".discarded" ) ) {
2017-07-26 22:23:39 +02:00
delay = 5 ;
} else {
chrome . tabs . discard ( tabsIds [ 0 ] ) ;
}
tabsIds . splice ( 0 , 1 ) ;
2017-11-12 22:00:40 +01:00
if ( tabsIds . length > 0 ) {
setTimeout ( function ( ) {
2017-07-26 22:23:39 +02:00
DiscardTabs ( tabsIds ) ;
2017-11-20 18:14:07 +01:00
} , delay ) ;
2017-07-26 22:23:39 +02:00
}
}
2017-11-12 22:00:40 +01:00
function ActivateNextTab ( ) {
if ( $ ( ".pin.active:visible" ) [ 0 ] ) {
if ( $ ( ".pin.active" ) . next ( ".pin" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".pin.active" ) . next ( ".pin" ) [ 0 ] . id ) , { active : true } ) ;
2017-11-20 18:14:07 +01:00
} else {
if ( $ ( ".pin.active" ) . prev ( ".pin" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".pin.active" ) . prev ( ".pin" ) [ 0 ] . id ) , { active : true } ) ;
}
}
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
if ( $ ( ".tab.active:visible" ) [ 0 ] ) {
if ( $ ( ".active:visible" ) . children ( ) . last ( ) . children ( ".tab" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".active:visible" ) . children ( ) . last ( ) . children ( ".tab" ) [ 0 ] . id ) , { active : true } ) ;
2017-07-26 22:23:39 +02:00
} else {
2017-11-12 22:00:40 +01:00
if ( $ ( ".active:visible" ) . next ( ".tab" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".active:visible" ) . next ( ".tab" ) [ 0 ] . id ) , { active : true } ) ;
2017-07-26 22:23:39 +02:00
} else {
2017-11-12 22:00:40 +01:00
if ( $ ( ".active:visible" ) . parent ( ) . parent ( ) . next ( ".tab" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".active:visible" ) . parent ( ) . parent ( ) . next ( ".tab" ) [ 0 ] . id ) , { active : true } ) ;
2017-07-26 22:23:39 +02:00
} else {
2017-11-12 22:00:40 +01:00
if ( $ ( ".active:visible" ) . parents ( ".tab" ) . last ( ) . next ( ".tab" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".active:visible" ) . parents ( ".tab" ) . last ( ) . next ( ".tab" ) [ 0 ] . id ) , { active : true } ) ;
2017-11-20 18:14:07 +01:00
} else {
if ( $ ( ".tab:visible" ) . length > 1 ) {
ActivatePrevTab ( ) ;
}
2017-07-26 22:23:39 +02:00
}
}
}
}
}
}
2017-11-12 22:00:40 +01:00
function ActivatePrevTab ( ) {
if ( $ ( ".pin.active" ) [ 0 ] ) {
if ( $ ( ".pin.active" ) . prev ( ".pin" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".pin.active" ) . prev ( ".pin" ) [ 0 ] . id ) , { active : true } ) ;
2017-11-20 18:14:07 +01:00
} else {
if ( $ ( ".pin.active" ) . next ( ".pin" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".pin.active" ) . next ( ".pin" ) [ 0 ] . id ) , { active : true } ) ;
}
}
2017-07-26 22:23:39 +02:00
}
2017-11-12 22:00:40 +01:00
if ( $ ( ".tab.active:visible" ) [ 0 ] ) {
if ( $ ( ".active:visible" ) . prev ( ) . find ( ".tab" ) . length > 0 ) {
chrome . tabs . update ( parseInt ( $ ( ".active:visible" ) . prev ( ) . find ( ".tab" ) . last ( ) [ 0 ] . id ) , { active : true } ) ;
2017-07-26 22:23:39 +02:00
} else {
2017-11-12 22:00:40 +01:00
if ( $ ( ".active:visible" ) . prev ( ".tab" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".active:visible" ) . prev ( ".tab" ) [ 0 ] . id ) , { active : true } ) ;
2017-07-26 22:23:39 +02:00
} else {
2017-11-12 22:00:40 +01:00
if ( $ ( ".tab.active:visible" ) . parent ( ) . is ( ".children" ) && $ ( ".tab.active:visible" ) . parent ( ) . parent ( ".tab" ) [ 0 ] ) {
chrome . tabs . update ( parseInt ( $ ( ".tab.active:visible" ) . parent ( ) . parent ( ".tab" ) [ 0 ] . id ) , { active : true } ) ;
2017-11-20 18:14:07 +01:00
} else {
if ( $ ( ".tab:visible" ) . length > 1 ) {
ActivateNextTab ( ) ;
}
2017-07-26 22:23:39 +02:00
}
}
}
}
}
2017-11-12 22:00:40 +01:00
function DropTargetsSendToFront ( ) {
if ( DropTargetsInFront == false ) {
2017-07-26 22:23:39 +02:00
$ ( ".drop_target" ) . show ( ) ;
DropTargetsInFront = true ;
}
}
2017-11-12 22:00:40 +01:00
function DropTargetsSendToBack ( ) {
if ( DropTargetsInFront ) {
2017-07-26 22:23:39 +02:00
$ ( ".drop_target" ) . hide ( ) ;
DropTargetsInFront = false ;
}
2017-11-12 22:00:40 +01:00
}
// ********** TABS EVENTS ***************
function SetTabEvents ( ) {
// double click to create tab
$ ( document ) . on ( "dblclick" , ".group, #pin_list, .tab" , function ( event ) {
if ( event . button == 0 && $ ( event . target ) . is ( this ) ) {
if ( event . target . id == "pin_list" ) {
chrome . tabs . create ( { pinned : true } ) ;
} else {
chrome . tabs . create ( { } ) ;
}
}
} ) ;
$ ( document ) . on ( "mouseenter" , ".close" , function ( event ) {
$ ( this ) . addClass ( "close_hover" ) ;
} ) ;
$ ( document ) . on ( "mouseleave" , ".close" , function ( event ) {
$ ( ".close_hover" ) . removeClass ( "close_hover" ) ;
} ) ;
$ ( document ) . on ( "mouseenter" , ".expand" , function ( event ) {
$ ( this ) . addClass ( "hover" ) ;
} ) ;
$ ( document ) . on ( "mouseleave" , ".expand" , function ( event ) {
$ ( ".expand.hover" ) . removeClass ( "hover" ) ;
} ) ;
$ ( document ) . on ( "mouseover" , ".tab_header" , function ( event ) {
$ ( this ) . addClass ( "tab_header_hover" ) ;
if ( opt . always _show _close == false ) {
$ ( this ) . addClass ( "close_show" ) ;
}
} ) ;
$ ( document ) . on ( "mouseleave" , ".tab_header" , function ( event ) {
$ ( this ) . removeClass ( "tab_header_hover" ) ;
if ( opt . always _show _close == false ) {
$ ( this ) . removeClass ( "close_show" ) ;
}
} ) ;
// EXPAND BOX - EXPAND / COLLAPSE
$ ( document ) . on ( "mousedown" , ".expand" , function ( event ) {
2017-11-20 18:14:07 +01:00
// event.stopPropagation();
2017-11-12 22:00:40 +01:00
if ( event . button == 0 ) {
if ( $ ( this ) . parent ( ) . parent ( ) . is ( ".o" ) ) {
$ ( this ) . parent ( ) . parent ( ) . removeClass ( "o" ) . addClass ( "c" ) ;
chrome . runtime . sendMessage ( { command : "update_tab" , tabId : parseInt ( $ ( this ) . parent ( ) . parent ( ) [ 0 ] . id ) , tab : { expand : "c" } } ) ;
} else {
if ( $ ( this ) . parent ( ) . parent ( ) . is ( ".c" ) ) {
$ ( this ) . parent ( ) . parent ( ) . removeClass ( "c" ) . addClass ( "o" ) ;
chrome . runtime . sendMessage ( { command : "update_tab" , tabId : parseInt ( $ ( this ) . parent ( ) . parent ( ) [ 0 ] . id ) , tab : { expand : "o" } } ) ;
if ( opt . close _other _trees ) {
$ ( ".o:visible:not(#" + $ ( this ) . parent ( ) . parent ( ) [ 0 ] . id + ")" ) . removeClass ( "o" ) . addClass ( "c" ) ;
$ ( this ) . parents ( ".tab" ) . each ( function ( ) {
$ ( this ) . removeClass ( "n" ) . removeClass ( "c" ) . addClass ( "o" ) ;
chrome . runtime . sendMessage ( { command : "update_tab" , tabId : parseInt ( this . id ) , tab : { expand : "o" } } ) ;
} ) ;
$ ( ".c" ) . each ( function ( ) {
chrome . runtime . sendMessage ( { command : "update_tab" , tabId : parseInt ( this . id ) , tab : { expand : "c" } } ) ;
} ) ;
}
}
}
}
} ) ;
2017-11-20 18:14:07 +01:00
// SELECT TAB/PIN
2017-11-12 22:00:40 +01:00
$ ( document ) . on ( "mousedown" , ".tab, .pin" , function ( event ) {
if ( $ ( ".menu" ) . is ( ":visible" ) ) {
return ;
}
event . stopPropagation ( ) ;
if ( event . button == 0 ) {
2017-11-20 18:14:07 +01:00
DropTargetsSendToBack ( ) ;
let tabId = parseInt ( this . id ) ;
2017-11-12 22:00:40 +01:00
// SET SELECTION WITH SHIFT
if ( event . shiftKey ) {
$ ( ".pin, .tab:visible" ) . removeClass ( "selected" ) . removeClass ( "selected_frozen" ) . removeClass ( "selected_temporarly" ) ;
if ( $ ( this ) . index ( ) >= $ ( ".active:visible" ) . index ( ) ) {
$ ( ".active:visible" ) . nextUntil ( $ ( this ) , ":visible" ) . add ( $ ( ".active:visible" ) ) . add ( $ ( this ) ) . addClass ( "selected" ) ;
} else {
$ ( ".active:visible" ) . prevUntil ( $ ( this ) , ":visible" ) . add ( $ ( ".active:visible" ) ) . add ( $ ( this ) ) . addClass ( "selected" ) ;
}
}
// TOGGLE SELECTION WITH CTRL
if ( event . ctrlKey ) {
// if ($(".active:visible").is(":not(.selected)")) {
// $(".active:visible").addClass("selected");
// }
$ ( this ) . toggleClass ( "selected" ) ;
}
}
2017-11-20 18:14:07 +01:00
} ) ;
2017-11-12 22:00:40 +01:00
2017-11-20 18:14:07 +01:00
// CLOSE TAB/PIN
$ ( document ) . on ( "mousedown" , ".tab_header" , function ( event ) {
if ( $ ( ".menu" ) . is ( ":visible" ) ) {
return ;
}
let tabId = parseInt ( $ ( this ) . parent ( ) [ 0 ] . id ) ;
if ( ( event . button == 1 && opt . close _with _MMB == true && $ ( this ) . parent ( ) . is ( ".tab" ) ) || ( event . button == 1 && opt . close _with _MMB == true && $ ( this ) . parent ( ) . is ( ".pin" ) && opt . allow _pin _close == true ) || ( event . button == 0 && $ ( event . target ) . is ( ".close, .close_img" ) ) ) {
if ( $ ( this ) . parent ( ) . is ( ".active:visible" ) && opt . after _closing _active _tab != "browser" ) {
2017-11-12 22:00:40 +01:00
if ( opt . after _closing _active _tab == "above" ) {
ActivatePrevTab ( ) ;
}
if ( opt . after _closing _active _tab == "below" ) {
ActivateNextTab ( ) ;
}
}
2017-11-20 18:14:07 +01:00
// hide pin before it will be closed by listener
$ ( ".pin#" + tabId ) . css ( { "width" : "0px" , "height" : "0px" , "border" : "none" , "overflow" : "hidden" } ) ;
chrome . tabs . update ( tabId , { pinned : false } ) ;
if ( $ ( "#" + tabId ) [ 0 ] ) chrome . tabs . remove ( tabId ) ;
2017-11-12 22:00:40 +01:00
}
} ) ;
2017-11-20 18:14:07 +01:00
2017-11-12 22:00:40 +01:00
// SINGLE CLICK TO ACTIVATE TAB
$ ( document ) . on ( "click" , ".tab_header" , function ( event ) {
if ( $ ( ".menu" ) . is ( ":visible" ) ) {
return ;
}
event . stopPropagation ( ) ;
if ( event . button == 0 && ! event . shiftKey && ! event . ctrlKey && $ ( event . target ) . is ( ":not(.close, .close_img, .expand, .tab_mediaicon)" ) ) {
SetActiveTab ( $ ( this ) . parent ( ) [ 0 ] . id ) ;
chrome . tabs . update ( parseInt ( $ ( this ) . parent ( ) [ 0 ] . id ) , { active : true } ) ;
}
} ) ;
2017-07-26 22:23:39 +02:00
}