@@ -2,69 +2,55 @@ import {
22 addElementToCache ,
33 getCachedElement ,
44} from '../../common/plugin-element-cache' ;
5- import { getKeyPattern } from '../../common/plugin-helpers' ;
6-
7- import { onBuildHandler } from '../build-handler' ;
5+ import { createNetlifyItem , updateNetlifyItem } from './panel-button' ;
6+
7+ const createPanelElement = ( cacheKey ) => {
8+ const panelElement = document . createElement ( 'div' ) ;
9+ panelElement . classList . add ( 'plugin-deploy-netlify' ) ;
10+ panelElement . id = cacheKey ;
11+ panelElement . innerHTML = /*html*/ `
12+ <span id="plugin-deploy-netlify__header" class="plugin-deploy-netlify__header">
13+ Netlify builds
14+ </span>
15+ <div class="plugin-deploy-netlify__button-list"></div>
16+ <img class="plugin-deploy-netlify__logo" alt="Logo Netlify">
17+ ` ;
18+
19+ addElementToCache ( panelElement , cacheKey ) ;
20+
21+ return panelElement ;
22+ } ;
823
9- const itemNetlify = ( buttonSettings , contentObject , id , isCreating ) => {
10- const buildInstance = getKeyPattern (
11- buttonSettings ?. build_instance_url ,
12- contentObject ,
13- ) ;
14- const buttonLabel = getKeyPattern (
15- buttonSettings ?. displayName || 'Build site' ,
16- contentObject ,
24+ const updatePanelElement = (
25+ pluginContainer ,
26+ settingsForCtd ,
27+ contentObject ,
28+ isCreating ,
29+ ) => {
30+ const buttonList = pluginContainer . querySelector (
31+ '.plugin-deploy-netlify__button-list' ,
1732 ) ;
18-
19- const pluginContainerItem = document . createElement ( 'div' ) ;
20- pluginContainerItem . classList . add ( 'plugin-dn-container-item' ) ;
21-
22- // :: Status
23- const statusMessageContainer = document . createElement ( 'div' ) ;
24- statusMessageContainer . id = `${ id } -status` ;
25- statusMessageContainer . classList . add ( 'plugin-dn-status-message' ) ;
26-
27- // :: Button
28- const pluginButton = document . createElement ( 'button' ) ;
29- pluginButton . id = `${ id } -button` ;
30- pluginButton . classList . add ( 'plugin-dn-button' ) ;
31- pluginButton . innerText = buttonLabel ;
32- pluginButton . onclick = ( ) =>
33- onBuildHandler ( buttonSettings , contentObject , id ) ;
34-
35- // :: Button disabled status
36- if ( isCreating ) {
37- pluginButton . classList . add ( 'disabled' ) ;
38- } else {
39- pluginButton . classList . remove ( 'disabled' ) ;
33+ settingsForCtd . forEach ( ( buttonSettings , index ) => {
34+ const itemUniqueID = `netlify-item-child-${ index } ` ;
35+ let htmlItem = buttonList . children [ index ] ;
36+ if ( ! htmlItem ) {
37+ htmlItem = createNetlifyItem ( itemUniqueID ) ;
38+ buttonList . appendChild ( htmlItem ) ;
39+ }
40+ updateNetlifyItem (
41+ htmlItem ,
42+ buttonSettings ,
43+ contentObject ,
44+ itemUniqueID ,
45+ isCreating ,
46+ ) ;
47+ return htmlItem ;
48+ } ) ;
49+
50+ // Remove unnecessary items
51+ while ( settingsForCtd . length < buttonList . children . length ) {
52+ buttonList . children [ buttonList . children . length - 1 ] . remove ( ) ;
4053 }
41-
42- // :: Message
43- if ( buildInstance && ! isCreating ) {
44- statusMessageContainer . innerHTML = `
45- <a
46- class="plugin-dn-link"
47- href="${ buildInstance } "
48- target="_blank">
49- Go to page: ${ buildInstance }
50- </a>` ;
51- }
52-
53- // :: Append new elements
54- pluginContainerItem . appendChild ( pluginButton ) ;
55- pluginContainerItem . appendChild ( statusMessageContainer ) ;
56-
57- // :: Checking if build instance
58-
59- if ( ! buildInstance ) {
60- pluginButton . classList . add ( 'disabled' ) ;
61- pluginButton . disabled = true ;
62- statusMessageContainer . classList . add ( 'active' ) ;
63- statusMessageContainer . innerText =
64- "Can't find build instance url. Check the plugin settings." ;
65- }
66-
67- return pluginContainerItem ;
6854} ;
6955
7056export const handlePanelPlugin = (
@@ -92,33 +78,16 @@ export const handlePanelPlugin = (
9278
9379 let pluginContainer = getCachedElement ( cacheKey ) ?. element ;
9480
95- if ( ! pluginContainer || isCreating ) {
96- pluginContainer = document . createElement ( 'div' ) ;
97- pluginContainer . classList . add ( 'plugin-dn-container' ) ;
98-
99- const headerElement = document . createElement ( 'span' ) ;
100- headerElement . classList . add ( 'plugin-dn-header' ) ;
101- headerElement . id = 'plugin-dn-header' ;
102- headerElement . innerText = 'Netlify Builds' ;
103-
104- pluginContainer . appendChild ( headerElement ) ;
105- addElementToCache ( pluginContainer , cacheKey ) ;
106-
107- // Case: disable buttons on create item
108- const items = settingsForCtd . map ( ( item , index ) => {
109- const itemUniqueID = `netlify-item-child-${ index } ` ;
110- return itemNetlify ( item , contentObject , itemUniqueID , isCreating ) ;
111- } ) ;
112-
113- pluginContainer . append ( ...items ) ;
114-
115- const imgLogo = document . createElement ( 'img' ) ;
116- imgLogo . classList . add ( 'plugin-dn-logo' ) ;
117-
118- imgLogo . alt = 'Logo Netlify' ;
119-
120- pluginContainer . appendChild ( imgLogo ) ;
81+ if ( ! pluginContainer ) {
82+ pluginContainer = createPanelElement ( cacheKey ) ;
12183 }
12284
85+ updatePanelElement (
86+ pluginContainer ,
87+ settingsForCtd ,
88+ contentObject ,
89+ isCreating ,
90+ ) ;
91+
12392 return pluginContainer ;
12493} ;
0 commit comments