MediaFolder.js
5 years ago
MediaItem.js
5 years ago
MediaPopup.js
2 weeks ago
MediaPopup.scss
2 weeks ago
MediaPopupTemplate.js
2 weeks ago
MediaPopupTemplate.scss
2 weeks ago
chunk-upload.js
5 years ago
MediaPopup.js
321 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | const { __ } = wp.i18n; |
| 5 | const { |
| 6 | Modal, |
| 7 | Spinner, |
| 8 | Button, |
| 9 | BaseControl, |
| 10 | Flex, |
| 11 | FlexBlock, |
| 12 | Notice, |
| 13 | Card, |
| 14 | CardBody, |
| 15 | Disabled, |
| 16 | DropZone, |
| 17 | FormFileUpload, |
| 18 | DropZoneProvider, |
| 19 | } = wp.components; |
| 20 | const { useEffect, useState, useRef } = wp.element; |
| 21 | import MediaItem from "./MediaItem"; |
| 22 | import MediaFolder from "./MediaFolder"; |
| 23 | |
| 24 | import "./MediaPopup.scss"; |
| 25 | |
| 26 | export default ({ |
| 27 | onClose, |
| 28 | title, |
| 29 | header, |
| 30 | onLoad, |
| 31 | items, |
| 32 | folders, |
| 33 | fetching, |
| 34 | progressMessage, |
| 35 | onSelect, |
| 36 | error, |
| 37 | onDelete, |
| 38 | onUpload, |
| 39 | progress, |
| 40 | }) => { |
| 41 | const [selected, setSelected] = useState({}); |
| 42 | const [deleteConfirm, setDeleteConfirm] = useState(false); |
| 43 | const buttonRef = useRef(); |
| 44 | |
| 45 | useEffect(() => { |
| 46 | onLoad && onLoad(); |
| 47 | }, []); |
| 48 | |
| 49 | const bytesToSize = (bytes) => { |
| 50 | var sizes = ["Bytes", "KB", "MB", "GB", "TB"]; |
| 51 | if (bytes == 0) return "0 Byte"; |
| 52 | var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); |
| 53 | return Math.round(bytes / Math.pow(1024, i), 2) + " " + sizes[i]; |
| 54 | }; |
| 55 | |
| 56 | const toDate = (d) => { |
| 57 | d = new Date(d); |
| 58 | var hours = d.getHours(); |
| 59 | var minutes = d.getMinutes(); |
| 60 | var ampm = hours >= 12 ? "pm" : "am"; |
| 61 | hours = hours % 12; |
| 62 | hours = hours ? hours : 12; |
| 63 | minutes = minutes < 10 ? "0" + minutes : minutes; |
| 64 | |
| 65 | return ( |
| 66 | d.getDate() + |
| 67 | "-" + |
| 68 | (d.getMonth() + 1) + |
| 69 | "-" + |
| 70 | d.getFullYear() + |
| 71 | " at " + |
| 72 | hours + |
| 73 | ":" + |
| 74 | minutes + |
| 75 | ampm |
| 76 | ); |
| 77 | }; |
| 78 | |
| 79 | const deleteSelected = () => { |
| 80 | onDelete(selected); |
| 81 | setDeleteConfirm(false); |
| 82 | }; |
| 83 | |
| 84 | const isSelected = () => { |
| 85 | return Object.keys(selected || {}).length; |
| 86 | }; |
| 87 | |
| 88 | const sidebarContent = () => { |
| 89 | if (!isSelected()) { |
| 90 | return <></>; |
| 91 | } |
| 92 | return ( |
| 93 | <div className="presto-player__media-modal-sidebar-content"> |
| 94 | <BaseControl> |
| 95 | <Disabled key={selected.id}> |
| 96 | {selected?.thumbnail ? ( |
| 97 | <img src={selected?.thumbnail} style={{ maxWidth: "100%" }} /> |
| 98 | ) : ( |
| 99 | <video preload="metadata"> |
| 100 | <source src={selected.previewUrl} /> |
| 101 | </video> |
| 102 | )} |
| 103 | </Disabled> |
| 104 | </BaseControl> |
| 105 | <BaseControl> |
| 106 | <BaseControl.VisualLabel> |
| 107 | {__("Name", "presto-player")} |
| 108 | </BaseControl.VisualLabel> |
| 109 | <h3 style={{ marginTop: "5px" }}>{selected.title}</h3> |
| 110 | </BaseControl> |
| 111 | |
| 112 | {!!selected?.visibility && ( |
| 113 | <BaseControl> |
| 114 | <BaseControl.VisualLabel> |
| 115 | {__("Visibility", "presto-player")} |
| 116 | </BaseControl.VisualLabel> |
| 117 | <h3 style={{ marginTop: "5px" }}>{selected.visibility}</h3> |
| 118 | </BaseControl> |
| 119 | )} |
| 120 | |
| 121 | <BaseControl> |
| 122 | <BaseControl.VisualLabel> |
| 123 | {__("Size", "presto-player")} |
| 124 | </BaseControl.VisualLabel> |
| 125 | <h3 style={{ marginTop: "5px" }}> |
| 126 | {bytesToSize(selected?.size || 0)} |
| 127 | </h3> |
| 128 | </BaseControl> |
| 129 | |
| 130 | <BaseControl> |
| 131 | <BaseControl.VisualLabel> |
| 132 | {__("Created", "presto-player")} |
| 133 | </BaseControl.VisualLabel> |
| 134 | <h3 style={{ marginTop: "5px" }}>{toDate(selected?.created_at)}</h3> |
| 135 | </BaseControl> |
| 136 | |
| 137 | <BaseControl> |
| 138 | {deleteConfirm ? ( |
| 139 | <Card> |
| 140 | <CardBody> |
| 141 | <p> |
| 142 | <strong>Are you sure?</strong> |
| 143 | </p> |
| 144 | <p>Are you sure you want to delete this video?</p> |
| 145 | <Button isDestructive onClick={deleteSelected}> |
| 146 | Yes |
| 147 | </Button> |
| 148 | <Button onClick={() => setDeleteConfirm(false)}>Cancel</Button> |
| 149 | </CardBody> |
| 150 | </Card> |
| 151 | ) : ( |
| 152 | <Button |
| 153 | isDestructive |
| 154 | onClick={() => { |
| 155 | setDeleteConfirm(!deleteConfirm); |
| 156 | }} |
| 157 | > |
| 158 | {__("Delete video", "presto-player")} |
| 159 | </Button> |
| 160 | )} |
| 161 | </BaseControl> |
| 162 | </div> |
| 163 | ); |
| 164 | }; |
| 165 | |
| 166 | const selectVideo = () => { |
| 167 | if (selected) { |
| 168 | onSelect(selected); |
| 169 | onClose(); |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | const itemsContent = () => { |
| 174 | if (fetching) { |
| 175 | return ( |
| 176 | <Flex className="presto-player__media-loading"> |
| 177 | <FlexBlock style={{ textAlign: "center" }}> |
| 178 | {progress ? ( |
| 179 | <> |
| 180 | <div> |
| 181 | <strong> |
| 182 | {progressMessage || __("Uploading", "presto-player")} |
| 183 | </strong> |
| 184 | </div> |
| 185 | <div> |
| 186 | {__( |
| 187 | "Please don't navigate away from this page.", |
| 188 | "presto-player" |
| 189 | )} |
| 190 | </div> |
| 191 | <progress |
| 192 | className="presto-progress" |
| 193 | max="100" |
| 194 | value={progress} |
| 195 | style={{ width: "100px" }} |
| 196 | ></progress> |
| 197 | </> |
| 198 | ) : ( |
| 199 | <Spinner /> |
| 200 | )} |
| 201 | </FlexBlock> |
| 202 | </Flex> |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | if (!items?.length) { |
| 207 | return ( |
| 208 | <Flex align-items="center" className="presto-player__media-not-found"> |
| 209 | <div> |
| 210 | <h2>Drop video files here to upload</h2> |
| 211 | <p>or browse for a video</p> |
| 212 | <FormFileUpload |
| 213 | isSecondary |
| 214 | accept="video/mp4,video/x-m4v,video/*" |
| 215 | onChange={(e) => { |
| 216 | if (!e.target.files) { |
| 217 | return; |
| 218 | } |
| 219 | onUpload(e.target.files); |
| 220 | jQuery(e.target).val(null); |
| 221 | }} |
| 222 | > |
| 223 | {__("Upload New Video", "presto-player")} |
| 224 | </FormFileUpload> |
| 225 | </div> |
| 226 | </Flex> |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | return ( |
| 231 | <div className="presto-player__media-list"> |
| 232 | <h2>{title}</h2> |
| 233 | |
| 234 | {folders && ( |
| 235 | <div className="presto-player__media-list-folders"> |
| 236 | {folders.map((folder) => { |
| 237 | return <MediaFolder key={folder.id} item={folder} />; |
| 238 | })} |
| 239 | </div> |
| 240 | )} |
| 241 | |
| 242 | <div className="presto-player__media-list-items"> |
| 243 | {items.map((item) => { |
| 244 | return ( |
| 245 | <MediaItem |
| 246 | item={item} |
| 247 | key={item.id} |
| 248 | onClick={() => { |
| 249 | if (selected === item) { |
| 250 | setSelected({}); |
| 251 | } else { |
| 252 | setSelected(item); |
| 253 | } |
| 254 | }} |
| 255 | className={selected === item ? "is-selected" : ""} |
| 256 | /> |
| 257 | ); |
| 258 | })} |
| 259 | </div> |
| 260 | </div> |
| 261 | ); |
| 262 | }; |
| 263 | |
| 264 | return ( |
| 265 | <Modal |
| 266 | isFullScreen |
| 267 | title={header ? header : __("Add Media", "presto-player")} |
| 268 | onRequestClose={onClose} |
| 269 | className="presto-player__media-modal presto-player__full-modal" |
| 270 | overlayClassName="presto-player__modal-overlay" |
| 271 | > |
| 272 | <div className="presto-player__media-modal-layout" data-cy="media-modal"> |
| 273 | <div className="presto-player__media-modal-header"> |
| 274 | <div className="presto-media-popup__upload"> |
| 275 | <FormFileUpload |
| 276 | isPrimary |
| 277 | accept="video/mp4,video/x-m4v,video/*" |
| 278 | onChange={(e) => { |
| 279 | if (!e.target.files) { |
| 280 | return; |
| 281 | } |
| 282 | onUpload(e.target.files); |
| 283 | jQuery(e.target).val(null); |
| 284 | }} |
| 285 | > |
| 286 | {__("Upload New Video", "presto-player")} |
| 287 | </FormFileUpload> |
| 288 | <div className="presto-media-popup__upload-text"> |
| 289 | {__("Or drag a file here to upload.", "presto-player")} |
| 290 | </div> |
| 291 | </div> |
| 292 | {error && ( |
| 293 | <Notice status="error" isDismissible={false}> |
| 294 | {error} |
| 295 | </Notice> |
| 296 | )} |
| 297 | </div> |
| 298 | <div className="presto-player__media-modal-content"> |
| 299 | <DropZoneProvider> |
| 300 | {itemsContent()} |
| 301 | <DropZone label={"Drop files"} onFilesDrop={onUpload} /> |
| 302 | </DropZoneProvider> |
| 303 | </div> |
| 304 | <div className="presto-player__media-modal-sidebar"> |
| 305 | {sidebarContent()} |
| 306 | </div> |
| 307 | <div className="presto-player__media-modal-footer"> |
| 308 | <Button |
| 309 | isPrimary |
| 310 | disabled={!isSelected()} |
| 311 | onClick={selectVideo} |
| 312 | ref={buttonRef} |
| 313 | > |
| 314 | {__("Choose", "presto-player")} |
| 315 | </Button> |
| 316 | </div> |
| 317 | </div> |
| 318 | </Modal> |
| 319 | ); |
| 320 | }; |
| 321 |