1/** 2 * EZ Media Uploader 3 * Company: Aazz Tech 4 * Develoepr: Syed Galib Ahmed 5 * Version: 1.0 6 * Initial Release: 24 December, 2019 7 * */ 8 9/* eslint-disable */ 10(function () { 11 this.EzMediaUploader = function (args) { 12 var defaults = { 13 containerID: "ez-media-uploader", 14 oldFiels: null, 15 oldFielsUrl: null, 16 maxFileSize: 0, 17 maxTotalFileSize: 4096, 18 minFileItems: null, 19 maxFileItems: null, 20 allowedFileFormats: ["images"], 21 allowMultiple: true, 22 featured: true, 23 allowSorting: true, 24 showAlerts: true, 25 showFileSize: true, 26 showInfo: true, 27 uploaderType: 'files', // files | avater 28 dictionary: { 29 // Label Texts 30 label: { 31 featured: 'Featured', 32 dragNDrop: 'Drag & Drop', 33 or: 'or', 34 dropHere: 'Drop Here', 35 selectFiles: 'Select Files', 36 addMore: 'Add More', 37 change: 'Change', 38 }, 39 // Alerts Texts 40 alert: { 41 maxFileSize: 'The maximum limit for a file is __DT__', 42 maxTotalFileSize: 'The minimum limit for total file size is __DT__', 43 minFileItems: 'The minimum limit for total file is __DT__', 44 maxFileItems: 'The maximum limit for total file is __DT__', 45 }, 46 // Info Texts 47 info: { 48 maxFileSize: { 49 text: 'The maximum allowed size per file is __DT__', 50 show: true, featured: false, pin: false 51 }, 52 maxTotalFileSize: { 53 text: 'The maximum total allowed file size is __DT__', 54 show: true, featured: false, pin: false 55 }, 56 minFileItems: { 57 text: 'The minimum __DT__ files are required', 58 show: true, featured: false, pin: false 59 }, 60 maxFileItems: { 61 text: 'The maximum __DT__ files are allowed', 62 show: true, featured: false, pin: false 63 }, 64 allowedFileFormats: { 65 text: 'Allowed file types are __DT__', 66 show: true, featured: false, pin: false 67 }, 68 } 69 }, 70 71 }; 72 73 // Data 74 // ----------------------------------------- 75 if (typeof args === 'object' && args !== null) { 76 this.options = extendDefaults(defaults, args); 77 } 78 this.debugLog = []; 79 this.isClean = true; 80 this.oldFiles = []; 81 this.files = []; 82 this.filesMeta = []; 83 this.isDragging = false; 84 this.draggingCounter = 0; 85 86 // Elements 87 this.container = null; 88 this.uploadButtonContainer = null; 89 this.mediaPickerSection = null; 90 this.previewSection = null; 91 this.thumbnailArea = null; 92 this.statusSection = null; 93 this.infoSection = null; 94 this.loadingSection = null; 95 96 // Methods 97 // ----------------------------------------- 98 // init 99 this.init = function () { 100 var id = this.options.containerID; 101 var container = document.getElementById(id); 102 if (!container) { 103 return; 104 } 105 this.container = container; 106 107 this.getMarkupOptions(); 108 this.getMarkupDictionary(); 109 this.loadOldFiels(); 110 this.attachElements(); 111 this.updatePreview(); 112 this.attachEventListener(); 113 }; 114 115 this.getMarkupOptions = function () { 116 if (!this.container) { return null; } 117 118 var container = this.container; 119 var self = this; 120 121 var options = [ 122 { key: 'maxFileSize', dataAttr: 'max-file-size', dataType: 'int' }, 123 { key: 'maxTotalFileSize', dataAttr: 'max-total-file-size', dataType: 'int' }, 124 { key: 'minFileItems', dataAttr: 'min-file-items', dataType: 'int' }, 125 { key: 'maxFileItems', dataAttr: 'max-file-items', dataType: 'int' }, 126 { key: 'allowedFileFormats', dataAttr: 'type', dataType: 'array' }, 127 { key: 'allowMultiple', dataAttr: 'allow-multiple', dataType: 'bool' }, 128 { key: 'showAlerts', dataAttr: 'show-alerts', dataType: 'bool' }, 129 { key: 'showFileSize', dataAttr: 'show-file-size', dataType: 'bool' }, 130 { key: 'featured', dataAttr: 'featured', dataType: 'bool' }, 131 { key: 'allowSorting', dataAttr: 'allow-sorting', dataType: 'bool' }, 132 { key: 'showInfo', dataAttr: 'show-info', dataType: 'bool' }, 133 { key: 'uploaderType', dataAttr: 'uploader-type', dataType: '
133string' }, 134 ]; 135 136 forEach(options, function (option) { 137 var option_arrt = container.getAttribute('data-' + option.dataAttr); 138 var has_data = (option_arrt && option_arrt.length) ? true : false; 139 140 // String 141 if (has_data && option.dataType === 'string') { 142 self.options[option.key] = option_arrt; 143 } 144 // Integer 145 if (has_data && option.dataType === 'int') { 146 var option_value = parseInt(option_arrt); 147 self.options[option.key] = (option_value < 1) ? false : option_value; 148 } 149 // Boolean 150 if (has_data && option.dataType === 'bool') { 151 var option_value = self.options[option.key]; 152 switch (option_arrt) { 153 case '0': 154 option_value = false; 155 break; 156 case 'false': 157 option_value = false; 158 break; 159 case '1': 160 option_value = true; 161 break; 162 case 'true': 163 option_value = true; 164 break; 165 } 166 167 self.options[option.key] = option_value; 168 } 169 // Array 170 if (has_data && option.dataType === 'array') { 171 var sanitize_value = option_arrt.replace(/,+$/, '').replace(/\s/g, ''); 172 var option_value = sanitize_value.split(','); 173 self.options[option.key] = option_value; 174 } 175 }); 176 }; 177 178 this.getMarkupDictionary = function () { 179 if (!this.container) { return null; } 180 181 var container = this.container; 182 var self = this; 183 184 var label_classes = [ 185 { key: 'featured', class: 'ezmu-dictionary-label-featured' }, 186 { key: 'dragNDrop', class: 'ezmu-dictionary-label-drag-n-drop' }, 187 { key: 'or', class: 'ezmu-dictionary-label-or' }, 188 { key: 'dropHere', class: 'ezmu-dictionary-label-drop-here' }, 189 { key: 'selectFiles', class: 'ezmu-dictionary-label-select-files' }, 190 { key: 'addMore', class: 'ezmu-dictionary-label-add-more' }, 191 { key: 'change', class: 'ezmu-dictionary-label-change' }, 192 ]; 193 var alert_classes = [ 194 { key: 'maxFileSize', class: 'ezmu-dictionary-alert-max-file-size' }, 195 { key: 'maxTotalFileSize', class: 'ezmu-dictionary-alert-max-total-file-size' }, 196 { key: 'minFileItems', class: 'ezmu-dictionary-alert-min-file-items' }, 197 { key: 'maxFileItems', class: 'ezmu-dictionary-alert-max-file-items' }, 198 ]; 199 var info_classes = [ 200 { key: 'maxFileSize', class: 'ezmu-dictionary-info-max-file-size' }, 201 { key: 'maxTotalFileSize', class: 'ezmu-dictionary-info-max-total-file-size' }, 202 { key: 'minFileItems', class: 'ezmu-dictionary-info-min-file-items' }, 203 { key: 'maxFileItems', class: 'ezmu-dictionary-info-max-file-items' }, 204 { key: 'allowedFileFormats', class: 'ezmu-dictionary-info-type' }, 205 ]; 206 207 // Fetch Labels 208 forEach(label_classes, function (item) { 209 var elm = container.querySelectorAll('.' + item.class); 210 if (elm && elm.length) { 211 var elm_dic = elm[0].innerHTML.trim(); 212 self.options.dictionary.label[item.key] = elm_dic; 213 } 214 }); 215 216 // Fetch Alerts 217 forEach(alert_classes, function (item) { 218 var elm = container.querySelectorAll('.' + item.class); 219 if (elm && elm.length) { 220 var elm_dic = elm[0].innerHTML.trim(); 221 self.options.dictionary.alert[item.key] = elm_dic; 222 } 223 }); 224 225 // Fetch Info 226 forEach(info_classes, function (item) { 227 var elm = container.querySelectorAll('.' + item.class); 228 if (elm && elm.length) { 229 var elm_dic = elm[0].innerHTML.trim(); 230 self.options.dictionary.info[item.key].text = elm_dic; 231 232 var show = elm[0].getAttribute('data-show'); 233 var featured = elm[0].getAttribute('data-featured'); 234 var pin = elm[0].getAttribute('data-pin'); 235 self.options.dictionary.info[item.key].show = (show === '0' || show === 'false') ? false : true; 236 self.options.dictionary.info[item.key].featured = (featured === '1' || featured === 'true') ? true : false;
237 self.options.dictionary.info[item.key].pin = (pin === '1' || pin === 'true') ? true : false; 238 } 239 }); 240 }; 241 242 this.getTheFiles = function () { 243 if (!this.container) { return null; } 244 this.isClean = false; 245 246 var final_files = []; 247 248 if (!this.filesMeta.length) { 249 return final_files; 250 } 251 252 forEach(this.filesMeta, function (file) { 253 if ("file" in file) { 254 final_files.push(file.file); 255 } 256 }); 257 258 return final_files; 259 }; 260 261 this.getFilesMeta = function () { 262 if (!this.container) { return null; } 263 this.isClean = false; 264 265 var final_files_meta = []; 266 if (!this.filesMeta.length) { 267 return final_files_meta; 268 } 269 270 forEach(this.filesMeta, function (file) { 271 272 var meta_data = { 273 id: file.id, 274 url: file.url, 275 oldFile: file.oldFile, 276 }; 277 278 if ("attachmentID" in file) { 279 meta_data.attachmentID = file.attachmentID; 280 } 281 282 if ("url" in file) { 283 meta_data.url = file.url; 284 } 285 286 if ("file" in file) { 287 meta_data.name = file.name; 288 meta_data.fileSize = file.fileSize; 289 meta_data.fileSizeInText = file.fileSizeInText; 290 meta_data.type = file.type; 291 meta_data.limitExceeded = file.limitExceeded; 292 } 293 294 final_files_meta.push(meta_data); 295 }); 296 297 return final_files_meta; 298 }; 299 300 this.validateFiles = function () { 301 if (!this.container) { return null; } 302 303 var self = this; 304 var filesMeta = this.filesMeta; 305 var alerts = this.options.dictionary.alert; 306 var error_log = []; 307 308 // Validate Min File Items 309 var min_file_items = this.options.minFileItems; 310 var valid_min_file_items = isPositiveNumber(min_file_items); 311 min_file_items = (valid_min_file_items) ? valid_min_file_items : min_file_items; 312 313 if (valid_min_file_items && (filesMeta.length < min_file_items)) { 314 error_log.push({ 315 errorKey: "minFileItems", 316 message: alerts.minFileItems.replace(/(__DT__)/g, min_file_items) 317 }); 318 } 319 320 // Validate Max File Items 321 var max_file_items = this.options.maxFileItems; 322 var valid_max_file_items = isPositiveNumber(max_file_items); 323 max_file_items = (valid_max_file_items) ? valid_max_file_items : max_file_items; 324 325 if (valid_max_file_items && (filesMeta.length > max_file_items)) { 326 error_log.push({ 327 errorKey: "maxFileItems", 328 message: alerts.maxFileItems.replace(/(__DT__)/g, max_file_items) 329 }); 330 } 331 332 // Validate Max File Size 333 forEach( filesMeta, function( file ) { 334 if ( (typeof file === 'object' && file !== null) && 'limitExceeded' in file ) { 335 if ( file.limitExceeded ) { 336 var max_file_size = self.options.maxFileSize; 337 var max_file_size_in_text = formatedFileSize(max_file_size * 1024); 338 error_log.push({ 339 errorKey: "maxFileSize", 340 message: alerts.maxFileSize.replace(/(__DT__)/g, max_file_size_in_text) 341 }); 342 } 343 } 344 }); 345 346 // Validate Max Total File Size 347 var maxTotalFileSize = this.options.maxTotalFileSize; 348 var valid_maxTotalFileSize = isPositiveNumber(maxTotalFileSize); 349 maxTotalFileSize = (valid_maxTotalFileSize) ? valid_maxTotalFileSize : maxTotalFileSize; 350 351 if (valid_maxTotalFileSize) { 352 var max_total_file_size_in_byte = maxTotalFileSize * 1024; 353 var max_total_file_size_in_text = formatedFileSize(maxTotalFileSize * 1024); 354 var total_file_size_in_byte = 0; 355 356 forEach(filesMeta, function (file) { 357 if ((typeof file === 'object' && file !== null) && 'fileSize' in file) { 358 total_file_size_in_byte += file.fileSize; 359 } 360 }); 361 362 if (total_file_size_in_byte > max_total_file_size_in_byte) { 363 error_log.push({ 364 errorKey: "maxTotalFileSize", 365 message: alerts.maxTotalFileSize.replace(/(__DT__)/g, max_total_file_size_in_text) 366 }); 367 } 368 } 369 370 if ( this.options.showAlerts && !this.isClean ) {
371 updateValidationFeedback(error_log, this.statusSection); 372 } 373 374 var info_elm = self.container.querySelectorAll('.ezmu__info-list-item'); 375 if (info_elm && info_elm.length) { 376 forEach(info_elm, function (info_elm) { 377 removeClass(info_elm, 'has-error'); 378 }); 379 } 380 381 if (!this.isClean && this.options.showInfo && error_log.length) { 382 forEach(error_log, function (item) { 383 var info_elm = self.container.querySelectorAll('.ezmu__info-list-item.' + item.errorKey); 384 if (info_elm && info_elm.length) { 385 addClass(info_elm[0], 'has-error'); 386 } 387 }); 388 } 389 390 if (error_log.length) { 391 return error_log; 392 } 393 394 return true; 395 }; 396 397 this.hasValidFiles = function () { 398 this.isClean = false; 399 400 if (this.validateFiles() === true) { 401 return true; 402 } 403 404 return false; 405 }; 406 407 this.loadOldFiels = function () { 408 if (!this.container) { return null; } 409 410 var old_fiels = []; 411 412 if (this.options.oldFiels) { 413 old_fiels = this.getValidatedPaths(this.options.oldFiels); 414 } 415 416 if (getMarkupsFilesMeta(this.container)) { 417 old_fiels = getMarkupsFilesMeta(this.container); 418 } 419 420 if (!old_fiels.length) { 421 return; 422 } 423 424 this.isClean = false; 425 426 for (var i = 0; i < old_fiels.length; i++) { 427 var file = old_fiels[i]; 428 var filesMeta = { 429 id: i, 430 url: file.url, 431 oldFile: true 432 }; 433 434 if ("attachmentID" in file) { 435 filesMeta.attachmentID = file.attachmentID; 436 } 437 438 if ("type" in file) { 439 filesMeta.type = file.type; 440 } 441 442 if ("size" in file) { 443 filesMeta.fileSize = file.size * 1024; 444 filesMeta.fileSizeInText = formatedFileSize(file.size * 1024); 445 446 if ( this.options.maxFileSize && ( file.size > this.options.maxFileSize ) ) { 447 filesMeta.limitExceeded = true; 448 } 449 } 450 451 this.filesMeta.push(filesMeta); 452 } 453 454 }; 455 456 this.getValidatedPaths = function (paths) { 457 if (!this.container) { return null; } 458 459 if (!Array.isArray(paths)) { 460 return null; 461 } 462 463 if (!paths.length) { 464 return null; 465 } 466 467 var validated_paths = []; 468 for (var i = 0; i < paths.length; i++) { 469 var path = paths[i]; 470 471 if (typeof path !== "object") { 472 continue; 473 } 474 if (!("url" in path)) { 475 continue; 476 } 477 478 validated_paths.push(path); 479 } 480 481 if (!validated_paths.length) { 482 return null; 483 } 484 485 return validated_paths; 486 }; 487 488 // attachElements 489 this.attachElements = function () { 490 if (!this.container) { return null; } 491 492 var container = this.container; 493 addClass(container, "ez-media-uploader"); 494 container.innerHTML = ""; 495 496 this.fileInputID = createFileInputID(); 497 498 var drop_zone_section_elm = createDropZoneSection(this); 499 var loading_section_elm = createLoadingSection(); 500 var media_picker_elm = createMediaPickerSection(this); 501 var preview_section_elm = createPreviewSection(this); 502 503 container.appendChild(drop_zone_section_elm); 504 container.appendChild(loading_section_elm); 505 container.appendChild(media_picker_elm); 506 container.appendChild(preview_section_elm); 507 508 var upload_button_container = container.querySelectorAll( 509 ".ezmu__upload-button-wrap" 510 ); 511 var media_picker_section = container.querySelectorAll( 512 ".ezmu__media-picker-section" 513 ); 514 var preview_section = container.querySelectorAll( 515 ".ezmu__preview-section" 516 ); 517 518 var thumbnail_area = container.querySelectorAll(".ezmu__thumbnail-area"); 519 var loading_section = container.querySelectorAll(".ezmu__loading-section"); 520 521 this.uploadButtonContainer = upload_button_container ? upload_button_container[0] : null; 522 this.mediaPickerSection = media_picker_section ? media_picker_section[0] : null; 523 this.previewSection = preview_section ? preview_section[0] : null; 524 this.thumbnailArea = thumbnail_area ? thumbnail_area[0] : null; 525 this.loadingSection = loading_section ? loading_section[0] : null; 526 527 if (this.options.showAlerts) { 528 var status_section_elm = createStatusSection(); 529 container.appendChild(status_section_elm); 530 var status_section = container.querySelectorAll(".ezmu__status-section"); 531 this.statusSection = status_section ? status_section[0] : null; 532 } 533 534 if (this.options.showInfo) { 535 var info_section_elm = createInfoSection(this.options); 536 container.appendChild(info_section_elm); 537 538 var info_section = container.querySelectorAll(".ezmu__info-section"); 539 this.infoSection = info_section ? info_section[0] : null; 540 } 541 }; 542 543 // attachEventListener 544 this.attachEventListener = function () { 545 if (!this.layoutIsReady()) { 546 return; 547 } 548 var self = this; 549 550 // Attach File Change Listener 551 this.attachFileChangeListener(); 552 553 // Attach Drag & Drop Listener 554 this.attachDragNDropListener(); 555 556 this.container.addEventListener("click", function (e) { 557 if (!e.target) { 558 return; 559 } 560 // Close Button Event 561 if (hasClass(e.target, "ezmu__front-item__close-btn")) { 562 self.removeFile(e); 563 } 564 565 // Sort Button Event 566 if (hasClass(e.target, "ezmu__front-item__sort-button")) { 567 self.changeOrder(e.target); 568 } else if (hasClass(e.target, "ezmu__front-item__sort-button-skin")) { 569 self.changeOrder(e.target.parentElement); 570 } 571 }); 572 }; 573 574 // attachFileChangeListener 575 this.attachFileChangeListener = function () { 576 if (!this.container) { return null; } 577 578 var file_input = this.container.querySelectorAll("#" + this.fileInputID); 579 var fileInputElm = file_input ? file_input[0] : null; 580 581 if (fileInputElm) { 582 var self = this; 583 fileInputElm.addEventListener("change", function (event) { 584 self.isClean = false;
585 var files = event.target.files; 586 if (!files.length) return; 587 588 self.renderFiles(files); 589 }); 590 } 591 }; 592 593 // attachDragNDropListener 594 this.attachDragNDropListener = function () { 595 if (!this.container) { return null; } 596 597 var self = this; 598 var drop_area = this.container; 599 var drag_events = ["dragenter", "dragleave", "dragover", "drop"]; 600 601 var dragEnter = function (e) { 602 if (self.draggingCounter < 1) { 603 self.draggingCounter++; 604 } 605 606 if (self.draggingCounter > 0) { 607 addClass(self.container, "highlight"); 608 } 609 }; 610 611 var dragLeave = function (e) { 612 if (self.draggingCounter > 0) { 613 self.draggingCounter--; 614 } 615 616 if (self.draggingCounter < 1) { 617 removeClass(self.container, "highlight"); 618 } 619 }; 620 621 var handleDrop = function (e) { 622 var dt = e.dataTransfer; 623 var files = dt.files; 624 625 self.renderFiles(files); 626 }; 627 628 forEach(drag_events, function (item) { 629 drop_area.addEventListener(item, preventDefaults, false); 630 }); 631 632 forEach(["dragenter", "dragover"], function (event_name) { 633 document.addEventListener( 634 event_name, 635 function () { 636 addClass(self.container, "drag-enter"); 637 }, 638 false 639 ); 640 drop_area.addEventListener(event_name, dragEnter, false); 641 }); 642 643 forEach(["dragleave", "drop"], function (event_name) { 644 document.addEventListener( 645 event_name, 646 function () { 647 removeClass(self.container, "drag-enter"); 648 }, 649 false 650 ); 651 drop_area.addEventListener(event_name, dragLeave, false); 652 }); 653 654 drop_area.addEventListener("drop", handleDrop, false); 655 }; 656 657 // updateLayout 658 this.updateLayout = function (layout) { 659 if (!this.layoutIsReady()) { 660 return; 661 } 662 663 var media_picker_section = this.mediaPickerSection; 664 var preview_section = this.previewSection; 665 var loading_section = this.loadingSection; 666 667 switch (layout) { 668 case "preview": 669 removeClass(loading_section, "ezmu--show"); 670 removeClass(media_picker_section, "ezmu--show"); 671 addClass(preview_section, "ezmu--show"); 672 break; 673 case "loading": 674 removeClass(media_picker_section, "ezmu--show"); 675 // removeClass(preview_section, "ezmu--show"); 676 addClass(loading_section, "ezmu--show"); 677 break; 678 default: 679 removeClass(loading_section, "ezmu--show"); 680 removeClass(preview_section, "ezmu--show"); 681 addClass(media_picker_section, "ezmu--show"); 682 } 683 }; 684 685 this.removeFile = function (e) { 686 if (!this.container) { return null; } 687 this.updateLayout("loading"); 688 689 // 1st Parent: parent_front_item_close_icon 690 // 2nd Parent: parent_front_item_close 691 // 3rd Parent: parent_front 692 // 4th Parent: list_item 693 var parent = 694 e.target.parentElement.parentElement.parentElement.parentElement; 695 696 var id = parent.getAttribute("data-id"); 697 var files_meta_index = findIndexByKey(this.filesMeta, "id", id); 698 699 if (files_meta_index === null) { 700 this.updatePreview(); 701 return; 702 } 703 704 var files_meta_item = this.filesMeta[files_meta_index]; 705 var files_index = null; 706 707 if ("name" in files_meta_item) { 708 var file_name = files_meta_item.name; 709 files_index = findIndexByKey(this.files, "name", file_name); 710 } 711 712 this.filesMeta.splice(files_meta_index, 1); 713 if (files_index === null) { 714 this.updatePreview(); 715 return; 716 } 717 718 this.files.splice(files_index, 1); 719 this.updatePreview(); 720 }; 721 722 this.changeOrder = function (target) { 723 if (!this.container) { return null; } 724 725 var total_fiels = this.filesMeta.length; 726 if (total_fiels < 2) { 727 return; 728 } 729 730 var base_elm = target; 731 var parent_ie = target.parentElement.parentElement.parentElement; 732 var parent = parent_ie.parentElement; 733 734 var id = parent.getAttribute("data-id"); 735 if (!id) { 736 id = parent_ie.getAttribute("data-id"); 737 } 738 if (!id) { return; } 739 740 var base_index = findIndexByKey(this.filesMeta, "id", id); 741 742 var target_index; 743 if (hasClass(base_elm, "ezmu--sort-up")) { 744 target_index = base_index + 1; 745 746 if (target_index > total_fiels - 1) { 747 return; 748 } 749 } 750 751 if (hasClass(base_elm, "ezmu--sort-down")) { 752 target_index = base_index - 1; 753 754 if (target_index < 0) { 755 return; 756 } 757 } 758 759 var base = this.filesMeta[base_index]; 760 var target = this.filesMeta[target_index]; 761 762 this.filesMeta[base_index] = target; 763 this.filesMeta[target_index] = base; 764 this.updatePreview(); 765 }; 766 767 // renderFiles 768 this.renderFiles = function (files) { 769 if (!this.container) { return null; } 770 var self = this; 771 772 if (!this.options.allowMultiple && this.filesMeta.length > 0) { 773 this.filesMeta = []; 774 this.files = []; 775 } 776 777 if (!files.length) { 778 this.updateLayout("default"); 779 return; 780 } 781 782 this.isClean = false;
783 this.updateLayout("loading"); 784 785 var temp_files = []; 786 for (var i = 0; i < files.length; i++) { 787 var file_item = files[i]; 788 var file_is_valid = validateFileExtension( 789 file_item, 790 self.options.allowedFileFormats 791 ); 792 793 var has_no_duplicate = validateDuplicateFile(this.filesMeta, file_item); 794 795 if (file_is_valid && has_no_duplicate) { 796 temp_files.push(file_item); 797 } 798 } 799 800 801 if (!temp_files.length) { 802 self.updatePreview(); 803 return; 804 } 805 806 var prevLength = this.filesMeta.length; 807 var total_files = temp_files.length; 808 var total_rendered = 0; 809 810 forEach(temp_files, function (file_item, index) { 811 var reader = new FileReader(); 812 reader.onloadend = function () { 813 var meta = { 814 id: prevLength + index, 815 file: file_item, 816 blob: reader.result, 817 name: file_item.name, 818 type: file_item.type, 819 url: null, 820 oldFile: false, 821 fileSize: file_item.size, 822 fileSizeInText: formatedFileSize(file_item.size) 823 }; 824 825 var type_is_image = file_item.type.match(/image\//g); 826 if (type_is_image) { 827 meta.blob = reader.result; 828 } 829 830 var maxFileSize = self.options.maxFileSize; 831 var limit_exceeded = false; 832 833 834 if (maxFileSize) { 835 var file_size_in_kb = file_item.size / 1024; 836 limit_exceeded = file_size_in_kb > maxFileSize ? true : false; 837 } 838 meta.limitExceeded = limit_exceeded; 839 840 self.filesMeta.push(meta); 841 self.files.push(file_item); 842 843 total_rendered++; 844 if (total_files === total_rendered) { 845 setTimeout(function () { 846 self.updatePreview(); 847 }, 200); 848 } 849 }; 850 851 reader.readAsDataURL(file_item); 852 }); 853 854 updateFileInputElement(this.uploadButtonContainer, this); 855 this.attachFileChangeListener(); 856 }; 857 858 this.updatePreview = function () { 859 if (!this.layoutIsReady()) { 860 return; 861 } 862 var self = this; 863 var files = this.filesMeta; 864 if (!files.length) { 865 this.updateLayout("default"); 866 this.validateFiles(); 867 return; 868 } 869 870 if ( ! this.options.allowMultiple && this.filesMeta.length ) { 871 label_txt = this.options.dictionary.label.change; 872 var button = this.container.querySelectorAll('.ezmu__update-file-btn'); 873 button[0].innerHTML = label_txt; 874 } 875 876 var thumbnail_area = this.thumbnailArea; 877 thumbnail_area.innerHTML = ""; 878 var thumbnail_list = createThumbnailListElm(); 879 880 forEach(files, function (file, index) { 881 var meta_data = { index: index, options: self.options }; 882 var thumbnail_list_item = createThumbnailListItemElm(file, meta_data); 883 thumbnail_list.appendChild(thumbnail_list_item); 884 }); 885 886 thumbnail_area.appendChild(thumbnail_list); 887 888 this.updateLayout("preview"); 889 this.validateFiles(); 890 }; 891 892 // layoutIsReady 893 this.layoutIsReady = function () { 894 var self = this; 895 var status = true; 896 var layouts = [ 897 "container", 898 "mediaPickerSection", 899 "previewSection", 900 "thumbnailArea", 901 "loadingSection" 902 ]; 903 904 for (var i = 0; i < layouts.length; i++) { 905 var layout = layouts[i]; 906 if (!self[layout]) { 907 status = false; 908 break; 909 } 910 } 911 912 return status; 913 }; 914 }; 915 916 // Helper Functions 917 //------------------------------------------- 918 function createDropZoneSection(data) { 919 var drop_zone_section = document.createElement("div"); 920 addClass(drop_zone_section, "ezmu__drop-zone-section"); 921 drop_zone_section.innerHTML = "<h2>" + data.options.dictionary.label.dropHere + "</h2>"; 922 923 return drop_zone_section; 924 } 925 926 function createMediaPickerSection( data ) { 927 928 if ( data.options.uploaderType === 'avater' ) { 929 return createMediaPickerAvaterSection( data ); 930 } 931 932 return createMediaPickerDragNDropSection( data ); 933 } 934 935 function createMediaPickerDragNDropSection( data ) { 936 var media_picker_section = createElementWithClass( 937 "ezmu__media-picker-section ezmu--show" 938 ); 939 940 var media_picker_controls = createElementWithClass(
941 "ezmu__media-picker-controls" 942 ); 943 944 var media_picker_icon_wrap = createElementWithClass( 945 "ezmu__media-picker-icon-wrap-upload", "span" 946 ); 947 948 var media_picker_icon = createElementWithClass( 949 'ezmu__icon ezmu-icon-upload', 'span' 950 ); 951 media_picker_icon_wrap.appendChild(media_picker_icon); 952 953 media_picker_controls.appendChild(media_picker_icon_wrap); 954 955 var media_picker_buttons = createElementWithClass( 956 "ezmu__media-picker-buttons" 957 ); 958 959 var titles_area = createElementWithClass('ezmu__titles-area'); 960 var title_1 = createElementWithClass("ezmu__title-1", "p", data.options.dictionary.label.dragNDrop); 961 var title_2 = createElementWithClass("ezmu__title-3", "p", data.options.dictionary.label.or); 962 titles_area.appendChild(title_1); 963 titles_area.appendChild(title_2); 964 965 var upload_button_wrap = createElementWithClass("ezmu__upload-button-wrap"); 966 updateFileInputElement(upload_button_wrap, data); 967 968 media_picker_buttons.appendChild(titles_area); 969 media_picker_buttons.appendChild(upload_button_wrap); 970 media_picker_controls.appendChild(media_picker_buttons); 971 972 media_picker_section.appendChild(media_picker_controls); 973 return media_picker_section; 974 } 975 976 function createMediaPickerAvaterSection( data ) { 977 // media_picker_section 978 var media_picker_section = createElementWithClass( 979 "ezmu__media-picker-section ezmu--show" 980 ); 981 982 // media_picker_controls 983 var media_picker_controls = createElementWithClass( 984 "ezmu__media-picker-controls" 985 ); 986 987 // media_picker_icon wrapper 988 var media_picker_icon_wrap = createElementWithClass( 989 "ezmu__media-picker-icon-wrap-avater", "span" 990 ); 991 992 // media_picker_icon 993 var media_picker_icon = createElementWithClass( 994 'ezmu__icon ezmu-icon-avater', 'span' 995 ); 996 media_picker_icon_wrap.appendChild(media_picker_icon); 997 998 media_picker_controls.appendChild(media_picker_icon_wrap); 999 1000 var media_picker_buttons = createElementWithClass( 1001 "ezmu__media-picker-buttons" 1002 ); 1003 1004 var upload_button_wrap = createElementWithClass("ezmu__upload-button-wrap"); 1005 updateFileInputElement(upload_button_wrap, data); 1006 1007 media_picker_buttons.appendChild(upload_button_wrap); 1008 media_picker_controls.appendChild(media_picker_buttons); 1009 1010 media_picker_section.appendChild(media_picker_controls); 1011 return media_picker_section; 1012 } 1013 1014 function updateFileInputElement(container, data) { 1015 var accept = "image/*"; 1016 var allowMultiple = true; 1017 1018 if (data !== null && typeof data === "object") { 1019 allowMultiple = data.options.allowMultiple; 1020 var allowedFileFormats = data.options.allowedFileFormats; 1021 accept = getFileFormats(allowedFileFormats); 1022 } 1023 1024 container.innerHTML = ""; 1025 var file_input = document.createElement("input"); 1026 file_input.setAttribute("type", "file"); 1027 file_input.setAttribute("id", data.fileInputID); 1028 file_input.setAttribute("class", "ezmu__file-input"); 1029 file_input.setAttribute("accept", accept); 1030 1031 if (allowMultiple) { 1032 file_input.setAttribute("multiple", ""); 1033 } 1034 1035 var file_input_label = document.createElement("label"); 1036 file_input_label.setAttribute("for", data.fileInputID); 1037 file_input_label.setAttribute("class", "ezmu__btn ezmu__input-label"); 1038 file_input_label.innerHTML = data.options.dictionary.label.selectFiles; 1039 1040 container.appendChild(file_input); 1041 container.appendChild(file_input_label); 1042 } 1043 1044 function createFileInputID() { 1045 var the_id = 'ezmu__file-input'; 1046 var file_input = document.querySelectorAll('.ezmu__file-input'); 1047 1048 if (file_input.length) { 1049 the_id = 'ezmu__file-input-' + (file_input.length + 1); 1050 } 1051 1052 return the_id; 1053 } 1054 1055 function getFileFormats(allowedFileFormats) { 1056 var default_formats = "image/*"; 1057 1058 if (!(Array.isArray(allowedFileFormats) && allowedFileFormats.length)) { 1059 return default_formats; 1060 } 1061 1062 default_formats = ""; 1063 forEach(allowedFileFormats, function (file_format) { 1064 var images_ext = default_formats + "image/*, "; 1065 var file_ext = default_formats + "." + file_format + ", "; 1066 default_formats = file_format === "images" ? images_ext : file_ext; 1067 }); 1068 1069 default_formats = default_formats.replace(/(,\s?)$/, ""); 1070 return default_formats; 1071 } 1072 1073 function createPreviewSection(data) { 1074 var preview_section = createElementWithClass("ezmu__preview-section"); 1075 var thumbnail_area = createElementWithClass("ezmu__thumbnail-area"); 1076 var media_picker_buttons = createElementWithClass( 1077 "ezmu__media-picker-buttons" 1078 ); 1079 1080 var label_txt = data.options.dictionary.label.addMore; 1081 1082 if ( !data.options.allowMultiple && data.filesMeta.length ) { 1083 label_txt = data.options.dictionary.label.change; 1084 } 1085 1086 var upload_button_wrap = createElementWithClass("ezmu__upload-button-wrap"); 1087 var label = createElementWithClass( 1088 "ezmu__btn ezmu__input-label ezmu__update-file-btn", "label", label_txt 1089 ); 1090 1091 label.setAttribute("for", data.fileInputID); 1092 upload_button_wrap.appendChild(label); 1093 media_picker_buttons.appendChild(upload_button_wrap); 1094
1095 preview_section.appendChild(thumbnail_area); 1096 preview_section.appendChild(media_picker_buttons); 1097 1098 return preview_section; 1099 } 1100 1101 function createStatusSection() { 1102 var status_section = createElementWithClass("ezmu__status-section"); 1103 return status_section; 1104 } 1105 1106 function createInfoSection(data) { 1107 var info_section = createElementWithClass("ezmu__info-section"); 1108 var info_dictionary = data.dictionary.info; 1109 1110 var info_list = createElementWithClass("ezmu__info-list", 'ul'); 1111 var item_count = 0; 1112 1113 for (var info in info_dictionary) { 1114 if ( info_dictionary[info].show && (data[info] || info_dictionary[info].pin) ) { 1115 var dictionary_data = getDictionaryData(info, data); 1116 var text = info_dictionary[info].text.replace(/(__DT__)/g, dictionary_data); 1117 var class_name = "ezmu__info-list-item " + info; 1118 class_name = (info_dictionary[info].featured) ? class_name + ' is-featured' : class_name; 1119 var li = createElementWithClass(class_name, 'li', text); 1120 info_list.appendChild(li); 1121 item_count++; 1122 } 1123 } 1124 1125 if (item_count) { 1126 info_section.appendChild(info_list); 1127 } 1128 1129 return info_section; 1130 } 1131 1132 function getDictionaryData(item, options) { 1133 var option_name = item; 1134 var data = options[option_name]; 1135 1136 if (option_name === 'maxFileSize' && data) { 1137 return formatedFileSize(data * 1024); 1138 } 1139 1140 if (option_name === 'maxTotalFileSize' && data) { 1141 return formatedFileSize(data * 1024); 1142 } 1143 1144 if (option_name === 'allowedFileFormats' && Array.isArray(data) && data.length) { 1145 var formats = ''; 1146 1147 forEach(data, function (item) { 1148 formats += item + ', '; 1149 }); 1150 1151 return formats.replace(/(, )$/, ''); 1152 } 1153 return data; 1154 } 1155 1156 function createLoadingSection(show) { 1157 // loading_section_elm 1158 var class_name = show ? "ezmu__loading-section ezmu--show" : "ezmu__loading-section"; 1159 1160 var loading_section = createElementWithClass(class_name); 1161 var loading_icon = createElementWithClass("ezmu__loading-icon", "span"); 1162 1163 var loading_icon_img_bg = createElementWithClass('ezmu__loading-icon-img-bg', 'span'); 1164 loading_icon.appendChild(loading_icon_img_bg); 1165 1166 loading_section.appendChild(loading_icon); 1167 return loading_section; 1168 } 1169 1170 function createThumbnailListElm() { 1171 var thumbnail_list = document.createElement("div"); 1172 addClass(thumbnail_list, "ezmu__thumbnail-list"); 1173 return thumbnail_list; 1174 } 1175 1176 function createThumbnailListItemElm(data, metaData) { 1177 var id = data && "id" in data ? data.id : ""; 1178 1179 var class_name = 'ezmu__thumbnail-list-item'; 1180 if (metaData.options.uploaderType === 'avater') { 1181 class_name = class_name + ' ezmu__thumbnail_avater'; 1182 } 1183 1184 var thumbnail_list_item = createElementWithClass(class_name); 1185 thumbnail_list_item.setAttribute("data-id", id); 1186 1187 var thumbnail_list_item_front = createThumbnailListItemFrontElm(data, metaData); 1188 var thumbnail_list_item_back = createThumbnailListItemBackElm(data); 1189 1190 thumbnail_list_item.appendChild(thumbnail_list_item_front); 1191 thumbnail_list_item.appendChild(thumbnail_list_item_back); 1192 1193 return thumbnail_list_item; 1194 } 1195 1196 function createThumbnailListItemFrontElm(data, metaData) { 1197 var thumbnail_list_item_front = document.createElement("div"); 1198 addClass(thumbnail_list_item_front, "ezmu__thumbnail-list-item_front"); 1199 1200 if (metaData.options.featured && 0 === metaData.index) { 1201 var tag_txt = metaData.options.dictionary.label.featured; 1202 var featured_elm = createElementWithClass('ezmu__featured_tag', 'span', tag_txt); 1203 thumbnail_list_item_front.appendChild(featured_elm); 1204 } 1205 1206 var thumbnail_list_item_close = createThumbnailListItemCloseElm(data); 1207 1208 1209 if (metaData.options.showFileSize && (data && "fileSizeInText" in data)) { 1210 var thumbnail_list_item_size = createThumbnailListItemSizeElm(data); 1211 thumbnail_list_item_front.appendChild(thumbnail_list_item_size); 1212 } 1213 1214 if (metaData.options.allowSorting) { 1215 var thumbnail_list_item_sort_buttons = createThumbnailListItemSortButtonsElm();
1216 thumbnail_list_item_front.appendChild(thumbnail_list_item_sort_buttons); 1217 } 1218 1219 1220 thumbnail_list_item_front.appendChild(thumbnail_list_item_close); 1221 1222 return thumbnail_list_item_front; 1223 } 1224 1225 function createThumbnailListItemCloseElm() { 1226 var thumbnail_list_item_close = document.createElement("div"); 1227 addClass( 1228 thumbnail_list_item_close, 1229 "ezmu__thumbnail-front-item ezmu__front-item__close" 1230 ); 1231 1232 var thumbnail_list_item_close_icon = createElementWithClass('ezmu__front-item__close-icon', 'span'); 1233 var thumbnail_list_item_close_btn = document.createElement("span"); 1234 addClass(thumbnail_list_item_close_btn, "ezmu__front-item__close-btn"); 1235 1236 thumbnail_list_item_close_icon.appendChild(thumbnail_list_item_close_btn); 1237 thumbnail_list_item_close.appendChild(thumbnail_list_item_close_icon); 1238 return thumbnail_list_item_close; 1239 } 1240 1241 function createThumbnailListItemSizeElm(data) { 1242 var thumbnail_list_item_size = createElementWithClass( 1243 "ezmu__thumbnail-front-item ezmu__front-item__thumbnail-size" 1244 ); 1245 1246 var limit_exceeded = ( (typeof data === 'object') && ('limitExceeded' in data) && data.limitExceeded ) ? true : false; 1247 var state_class = ( limit_exceeded ) ? ' has-error' : ''; 1248 var thumbnail_list_item_size_text = createElementWithClass( 1249 "ezmu__front-item__thumbnail-size-text" + state_class, 1250 "span" 1251 ); 1252 1253 if (data == null) { 1254 thumbnail_list_item_size.appendChild(thumbnail_list_item_size_text); 1255 return thumbnail_list_item_size; 1256 } 1257 1258 thumbnail_list_item_size_text.innerHTML = 1259 "fileSizeInText" in data ? data.fileSizeInText : ""; 1260 1261 thumbnail_list_item_size.appendChild(thumbnail_list_item_size_text); 1262 return thumbnail_list_item_size; 1263 } 1264 1265 function createThumbnailListItemSortButtonsElm() { 1266 var thumbnail_list_item_sort_buttons = document.createElement("div"); 1267 addClass( 1268 thumbnail_list_item_sort_buttons, 1269 "ezmu__thumbnail-front-item ezmu__front-item__sort-buttons" 1270 ); 1271 1272 var thumbnail_list_item_sort_buttons_down = document.createElement( 1273 "button" 1274 ); 1275 addClass( 1276 thumbnail_list_item_sort_buttons_down, 1277 "ezmu__front-item__sort-button ezmu--sort-down" 1278 ); 1279 thumbnail_list_item_sort_buttons_down.setAttribute("type", "button"); 1280 1281 thumbnail_list_item_sort_buttons_down.innerHTML = '<span class="ezmu__front-item__sort-button-skin ezmu--sort-down"></span>'; 1282 1283 var thumbnail_list_item_sort_buttons_up = document.createElement("button"); 1284 addClass( 1285 thumbnail_list_item_sort_buttons_up, 1286 "ezmu__front-item__sort-button ezmu--sort-up" 1287 ); 1288 thumbnail_list_item_sort_buttons_up.setAttribute("type", "button"); 1289 thumbnail_list_item_sort_buttons_up.innerHTML = '<span class="ezmu__front-item__sort-button-skin ezmu--sort-up"></span>'; 1290 1291 thumbnail_list_item_sort_buttons.appendChild( 1292 thumbnail_list_item_sort_buttons_down 1293 ); 1294 thumbnail_list_item_sort_buttons.appendChild( 1295 thumbnail_list_item_sort_buttons_up 1296 ); 1297 1298 return thumbnail_list_item_sort_buttons; 1299 } 1300 1301 function createThumbnailListItemBackElm(data) { 1302 var thumbnail_list_item_back = createElementWithClass( 1303 "ezmu__thumbnail-list-item_back" 1304 ); 1305 1306 var thumbnail_list_item_img = getThumbnail(data); 1307 thumbnail_list_item_back.appendChild(thumbnail_list_item_img); 1308 return thumbnail_list_item_back; 1309 } 1310 1311 function getThumbnail(data) { 1312 var thumbnail_list_item_img = createElementWithClass('ezmu__thumbnail-img', 'img'); 1313 var thumbnail_list_item_img_bg = createElementWithClass('ezmu__thumbnail-img-bg', 'span'); 1314 1315 if (typeof data !== "object") { 1316 return thumbnail_list_item_img_bg; 1317 } 1318 1319 var data_type = "type" in data ? data.type : "file"; 1320 var data_url = "url" in data ? data.url : null; 1321 var data_blob = "blob" in data ? data.blob : null; 1322 1323 var type_is_image = data_type.match(/image/g); 1324 1325 if (type_is_image) { 1326 var img_src = data_url ? data_url : img_src; 1327 img_src = data_blob ? data_blob : img_src; 1328 thumbnail_list_item_img.src = img_src; 1329 thumbnail_list_item_img.alt = 'image'; 1330 return thumbnail_list_item_img; 1331 } 1332 1333 return thumbnail_list_item_img_bg; 1334 } 1335 1336 function getMarkupsFilesMeta(container) { 1337 if (!container) { 1338 return false;
1339 } 1340 1341 var markup_files_meta = container.querySelectorAll('.ezmu__old-files-meta'); 1342 var files_meta = []; 1343 1344 if (!markup_files_meta.length) { 1345 return false; 1346 } 1347 1348 for (var i = 0; i < markup_files_meta.length; i++) { 1349 var elm = markup_files_meta[i]; 1350 var url = elm.getAttribute('data-url'); 1351 var attachment_id = elm.getAttribute('data-attachment-id'); 1352 var size = elm.getAttribute('data-size'); 1353 var type = elm.getAttribute('data-type'); 1354 1355 if (!(url && url.length)) { 1356 continue; 1357 } 1358 1359 var meta = {}; 1360 meta.url = url; 1361 1362 if (attachment_id && attachment_id.length) { 1363 meta.attachmentID = attachment_id; 1364 } 1365 1366 if (size && size.length) { 1367 meta.size = parseInt(size); 1368 } 1369 1370 if (type && type.length) { 1371 meta.type = type; 1372 } 1373 1374 files_meta.push(meta); 1375 } 1376 1377 return files_meta; 1378 } 1379 1380 function updateValidationFeedback(error_log, container) { 1381 container.innerHTML = ''; 1382 if (!error_log.length) { 1383 removeClass(container, 'ezmu--show'); 1384 return; 1385 } 1386 addClass(container, 'ezmu--show'); 1387 1388 var alert_box = createElementWithClass('ezmu_alert ezmu_alert_error'); 1389 alert_box.innerHTML = error_log[0].message; 1390 container.appendChild(alert_box); 1391 1392 // for ( var i = 0; i < error_log.length; i++ ) { 1393 // var alert_box = createElementWithClass('ezmu_alert ezmu_alert_error'); 1394 // alert_box.innerHTML = error_log[i].message; 1395 // container.appendChild(alert_box); 1396 // } 1397 } 1398 1399 function createElementWithClass(class_name, elm, child) { 1400 class_name = (typeof class_name === 'undefined') ? "" : class_name; 1401 elm = (typeof elm === 'undefined') ? "div" : elm; 1402 child = (typeof child === 'undefined') ? "" : child; 1403 1404 var element = document.createElement(elm); 1405 addClass(element, class_name); 1406 1407 element.innerHTML = child; 1408 return element; 1409 } 1410 1411 function validateFileExtension(file, allowedFileFormats) { 1412 allowedFileFormats = (typeof allowedFileFormats === 'undefined') ? [] : allowedFileFormats; 1413 1414 var status = false; 1415 1416 if (!Array.isArray(allowedFileFormats)) { 1417 return null; 1418 } 1419 1420 if (!allowedFileFormats.length) { 1421 return null; 1422 } 1423 1424 if (typeof file !== "object") { 1425 return null; 1426 } 1427 1428 if (!("name" in file) || !("type" in file)) { 1429 return null; 1430 } 1431 1432 var name = file.name; 1433 var lastDot = name.lastIndexOf("."); 1434 var ext = name.substring(lastDot + 1); 1435 1436 for (var i = 0; i < allowedFileFormats.length; i++) { 1437 var file_format = allowedFileFormats[i]; 1438 var file_type_is_image = file.type.match(/image\//g); 1439 1440 if (file_format === "images" && file_type_is_image) { 1441 status = true; 1442 break; 1443 } 1444 1445 if (file_format === ext) { 1446 status = true; 1447 break; 1448 } 1449 } 1450 1451 return status; 1452 } 1453 1454 function validateDuplicateFile(all_files, current_file) { 1455 var has_no_duplicate = true; 1456 1457 for (var i = 0; i < all_files.length; i++) { 1458 var file_item = all_files[i]; 1459 1460 if (file_item.name === current_file.name) { 1461 has_no_duplicate = false; 1462 break; 1463 } 1464 } 1465 1466 return has_no_duplicate; 1467 } 1468 1469 // formatedFileSize 1470 function formatedFileSize(file_size) { 1471 file_size = parseFloat(file_size); 1472 var file_size_in_kb = file_size / 1024; 1473 var _d0 = file_size_in_kb.toFixed(); 1474 var _d2 = file_size_in_kb.toFixed(2); 1475 var _file_size = (_d0 == _d2) ? _d0 : _d2; 1476 var _file_size_in_kb = _file_size + " KB"; 1477 1478 1479 var file_size_in_mb = file_size_in_kb < 1024 ? null : file_size_in_kb / 1024; 1480 var _file_size_in_mb = ''; 1481 1482 if (file_size_in_mb) { 1483 var _d0 = file_size_in_mb.toFixed(); 1484 var _d2 = file_size_in_mb.toFixed(2); 1485 var _file_size = (_d0 == _d2) ? _d0 : _d2; 1486 _file_size_in_mb = _file_size + " MB"; 1487 } 1488 1489 1490 var formated_file_size = file_size_in_mb ? _file_size_in_mb : _file_size_in_kb; 1491 return formated_file_size; 1492 } 1493 1494 // findIndexByKey 1495 function findIndexByKey(array, key, value) { 1496 for (var i = 0; i < array.length; i++) { 1497 if (array[i][key] == value) { 1498 return i; 1499 } 1500 } 1501 1502 return null; 1503 } 1504 1505 // isPositiveNumber 1506 function isPositiveNumber(data) { 1507 var data_is_valid = false;
1508 1509 if (typeof data === 'string') { 1510 var has_non_digit = data.match(/[^\d]/); 1511 var chk = (!has_non_digit) ? parseInt(data) : null; 1512 data_is_valid = (chk && chk > 0) ? true : false; 1513 data = (data_is_valid) ? chk : data; 1514 } 1515 1516 if (typeof data === 'number') { 1517 data = (data < 1) ? false : data; 1518 data_is_valid = (data) ? true : false; 1519 } 1520 1521 return (data_is_valid) ? data : false; 1522 } 1523 1524 function preventDefaults(e) { 1525 e.preventDefault(); 1526 e.stopPropagation(); 1527 } 1528 1529 // extendDefaults 1530 function extendDefaults(defaults, args) { 1531 if (!args) { 1532 return defaults; 1533 } 1534 1535 if (args && typeof args !== "object") { 1536 return defaults; 1537 } 1538 1539 var property; 1540 for (property in args) { 1541 if (defaults.hasOwnProperty(property)) { 1542 if (property === 'dictionary') { 1543 // for ( var dictionaryItem in args[property] ) { 1544 // if (args[property].hasOwnProperty(dictionaryItem)) { 1545 // defaults[property][dictionaryItem] = args[property][dictionaryItem]; 1546 // } 1547 // } 1548 } else { 1549 defaults[property] = args[property]; 1550 } 1551 1552 } 1553 } 1554 return defaults; 1555 } 1556 1557 1558 1559 // addClass 1560 function addClass(element, class_name) { 1561 if (!(typeof element === 'object' && 'className' in element)) { return; } 1562 1563 var arr = element.className.split(" "); 1564 if (arr.indexOf(class_name) == -1) { 1565 element.className += " " + class_name; 1566 element.className = element.className.trim(); 1567 } 1568 } 1569 1570 // removeClass 1571 function removeClass(element, class_name) { 1572 if (!element) { 1573 return; 1574 } 1575 var regExp = new RegExp(class_name, "g"); 1576 element.className = element.className.replace(regExp, ""); 1577 element.className = element.className.trim(); 1578 } 1579 1580 // hasClass 1581 function hasClass(element, class_name) { 1582 if (!element) { 1583 return; 1584 } 1585 var regExp = new RegExp(class_name, "g"); 1586 var match = element.className.match(regExp); 1587 1588 return match; 1589 } 1590 1591 // forEach 1592 function forEach(array, cb) { 1593 for (var i = 0; i < array.length; i++) { 1594 cb(array[i], i); 1595 } 1596 } 1597})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.