11function checked_devices ( ) {
22 devices = [ ] ;
3- $ ( ". devices" ) . filter ( ':checked' ) . each ( function ( index , element ) {
4- devices . push ( $ ( element ) . val ( ) ) ;
3+ $ ( '# devices .device > :checkbox' ) . filter ( ':checked' ) . each ( function ( index , device ) {
4+ devices . push ( $ ( device ) . val ( ) ) ;
55 } ) ;
66
77 console . log ( devices . length + ' devices checked' ) ;
@@ -30,9 +30,10 @@ $(function() {
3030
3131function load_devices ( saved_devices , removeInvalidIds )
3232{
33+ var div_devices = $ ( "#devices" ) ;
3334
34- $ ( "#devices" ) . empty ( ) . append (
35- $ ( "<div>" ) . append ( $ ( "<small>" ) . append ( "Loading devices from PushBullet..." ) )
35+ div_devices . empty ( ) . append (
36+ $ ( "<div>" ) . addClass ( "loading" ) . append ( $ ( "<small>" ) . append ( "Loading devices from PushBullet..." ) )
3637 ) ;
3738
3839 PushBullet . devices ( function ( err , res ) {
@@ -44,43 +45,51 @@ function load_devices(saved_devices, removeInvalidIds)
4445 msg = err . error . message ;
4546 }
4647
47- $ ( "#devices" ) . empty ( ) . append (
48- $ ( "<div>" ) . append ( $ ( "<small>" ) . append ( "There was a problem. This might help: " + msg ) )
48+ div_devices . empty ( ) . append (
49+ $ ( "<div>" ) . addClass ( "failure" ) . append ( $ ( "<small>" ) . append ( "There was a problem. This might help: " + msg ) )
4950 ) ;
5051
5152 throw err . httpStatus + ' ' + msg ;
5253 }
5354 else {
54- $ ( "#devices" ) . empty ( ) . append (
55- $ ( "<div>" ) . append ( $ ( "<small>" ) . append ( "To avoid a neverending loop, Chrome devices are not included." ) )
56- ) ;
57-
55+ div_devices . empty ( ) ;
5856 active_devices = res . devices . filter ( filterOutInactiveDevices ) ;
59- non_chrome_devices = active_devices . filter ( filterOutChromeDevices ) ;
6057
61- if ( non_chrome_devices . length > 0 ) {
62- non_chrome_devices . sort ( sortByDeviceNickname ) ;
58+ if ( active_devices . length > 0 ) {
59+ active_devices . sort ( sortByDeviceNickname ) ;
6360
64- $ . each ( non_chrome_devices , function ( index , device ) {
65- if ( device . kind === 'chrome' ) {
66- return ;
67- }
61+ var div_non_chrome = $ ( "<div>" ) . addClass ( "non_chrome" ) ;
62+ var div_chrome = $ ( "<div>" ) . addClass ( "chrome" ) ;
63+
64+ div_chrome . append (
65+ $ ( "<div>" ) . append ( $ ( "<small>" ) . append ( "To avoid a neverending loop, only push to the following Chrome devices if you know exactly what you're doing." ) )
66+ ) ;
67+
68+ $ . each ( active_devices , function ( index , device ) {
69+ var checeked = saved_devices . indexOf ( device . iden ) !== - 1 ;
70+ var device_node = buildDeviceNode ( device , checeked ) ;
6871
69- $ ( "#devices" ) . append (
70- $ ( "<div>" ) . append (
71- $ ( "<input>" ) . attr ( "type" , "checkbox" ) . addClass ( "devices" ) . attr ( "id" , device . iden ) . val ( device . iden ) . prop ( 'checked' , saved_devices . indexOf ( device . iden ) !== - 1 )
72- ) . append (
73- $ ( "<label>" ) . attr ( "for" , device . iden ) . append ( $ ( '<strong>' ) . append ( device . nickname ) ) . append ( ' ' ) . append ( $ ( '<small>' ) . append ( '(' + device . model + ')' ) )
74- )
75- ) ;
72+ if ( isChromeDevice ( device ) ) {
73+ div_chrome . append ( device_node ) ;
74+ }
75+ else {
76+ div_non_chrome . append ( device_node ) ;
77+ }
7678 } ) ;
77- }
78- else {
79- if ( active_devices . length > 0 ) {
80- $ ( "#devices" ) . append ( $ ( "<div>" ) . append ( "<em>Only Chrome devices are registered with PushBullet! Pushing to those will result in an endless loop. You should <a href=\"https://www.pushbullet.com/apps\" target=\"_blank\">add PushBullet to your other devices</a>.</em>" ) ) ;
79+
80+ var non_chrome_count = div_non_chrome . find ( ".device" ) . length ;
81+ var chrome_count = div_chrome . find ( ".device" ) . length
82+
83+ if ( non_chrome_count > 0 ) {
84+ div_devices . append ( div_non_chrome ) ;
85+
86+ if ( chrome_count > 0 ) {
87+ div_devices . append ( "<br />" ) ;
88+ }
8189 }
82- else {
83- $ ( "#devices" ) . append ( $ ( "<div>" ) . append ( "<em>No devices registered with PushBullet! You should <a href=\"https://www.pushbullet.com/apps\" target=\"_blank\">fix that</a>.</em>" ) ) ;
90+
91+ if ( chrome_count > 0 ) {
92+ div_devices . append ( div_chrome ) ;
8493 }
8594 }
8695
@@ -96,20 +105,26 @@ function load_devices(saved_devices, removeInvalidIds)
96105 } ) ;
97106}
98107
99- function filterOutChromeDevices ( device ) {
100- var isChrome = device . kind === 'chrome' ;
108+ function buildDeviceNode ( device , checked ) {
109+ var div_device = $ ( "<div>" ) . addClass ( "device" ) . append (
110+ $ ( "<input>" ) . attr ( "type" , "checkbox" ) . attr ( "id" , device . iden ) . val ( device . iden ) . prop ( 'checked' , checked )
111+ ) . append (
112+ $ ( "<label>" ) . attr ( "for" , device . iden ) . append ( $ ( '<strong>' ) . append ( device . nickname ) ) . append ( ' ' ) . append ( $ ( '<small>' ) . append ( '(' + device . model + ')' ) )
113+ )
101114
102- if ( isChrome ) {
103- console . log ( 'Skipping Chrome device "' + device . nickname + '" (' + device . iden + ')' ) ;
104- }
105-
106- return ! isChrome ;
115+ return div_device ;
107116}
108117
109118function filterOutInactiveDevices ( device ) {
110119 return device . active ;
111120}
112121
122+ function isChromeDevice ( device ) {
123+ var isChrome = device && device . kind === 'chrome' ;
124+
125+ return isChrome ;
126+ }
127+
113128function sortByDeviceNickname ( a , b ) {
114129 if ( a . nickname > b . nickname ) {
115130 return 1 ;
0 commit comments