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