Created
          November 9, 2017 16:02 
        
      - 
      
- 
        Save svetlyak40wt/9a5bf11f0c645c6afd4e562736124426 to your computer and use it in GitHub Desktop. 
    Пример JS кода на Parenscript.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | (weblocks.parenscript:make-dependency | |
| (defun scroll-percentage (el offset) | |
| "Возвращает число от 0 до 100 показывающее, сколько процентов сказки уже просмотрено." | |
| (let* ((el (j-query el)) | |
| (w (j-query window)) | |
| (document-view-top ((@ w scroll-top))) | |
| (document-view-bottom (+ document-view-top | |
| ((@ w height)))) | |
| (element-height ((@ el height))) | |
| (element-top (@ ((@ el offset)) | |
| top)) | |
| (element-bottom (+ element-top | |
| element-height))) | |
| (cond | |
| ;; До сказки ещё не докрутили | |
| ((< document-view-bottom | |
| element-top) | |
| 0) | |
| ;; Сказку прокрутили и она "ушла" куда-то вверх | |
| ((> document-view-top | |
| element-bottom) | |
| 100) | |
| ;; Сказка на экране, надо определить сколько процентов промотали | |
| (t (* (/ (- document-view-bottom | |
| element-top) | |
| element-height) | |
| 100))))) | |
| ;; Инициализируем состояние переменных, если они ещё не проинициализированы | |
| (when (= (@ window current-story) | |
| undefined) | |
| (setf (@ window current-story) | |
| nil | |
| (@ window loading-next-story) | |
| nil | |
| (@ window previous-reading-progress) | |
| 0) | |
| ((@ console.log) "LOADING NEXT STORY SET TO NULL")) | |
| (defun check-progress () | |
| (ps:try | |
| (when (@ window current-story) | |
| (let* ((story (@ window current-story)) | |
| (story-dom-id (@ story dom-id)) | |
| (update-progress-each (@ story update-progress-each)) | |
| (story-title (@ story title)) | |
| (progress (scroll-percentage (+ "#" story-dom-id))) | |
| (progress-meter (j-query ".story-feed .progress-meter"))) | |
| (when (> ((@ -math abs) (- (@ window previous-reading-progress) | |
| progress)) | |
| update-progress-each) | |
| ;; В лог выводим данные о прогрессе только если пользователь промотал | |
| ;; больше чем на 5 процентов | |
| ((@ console log) | |
| (+ story-title ": " progress)) | |
| (setf (@ window previous-reading-progress) | |
| progress) | |
| ;; Но обновляем прогресс-бар если | |
| ;;jQuery('.story-feed .progress-meter').width('80%') | |
| ((@ progress-meter width) | |
| (+ progress "%"))) | |
| (when (and (> progress ;; Если почти домотали до конца | |
| 95) | |
| ;; И уже не подгружаем новую сказку | |
| (not (@ window loading-next-story)) | |
| ;; И есть action-id для запуска | |
| (@ window on-story-read-action-id)) | |
| ;; То запустим подгрузку следующей сказки | |
| ((@ console log) | |
| "Loading next story") | |
| ;; Предварительно запомнив, что запустили процесс | |
| (setf (@ window loading-next-story) | |
| t) | |
| ;; Покажем спиннер | |
| ((@ (j-query ".story-feed .spinner") | |
| show)) | |
| ;; Сам запуск подгрузки | |
| (initiate-action-with-args-and-callback | |
| (@ window on-story-read-action-id) | |
| "" | |
| (create complete | |
| (lambda () | |
| ;; Когда сказка загрузилась, проставим признак, что | |
| ;; процесс завершился | |
| (setf (@ window loading-next-story) | |
| nil) | |
| ;; И скроем спиннер | |
| ((@ (j-query ".story-feed .spinner") | |
| hide)))))))) | |
| (:catch (condition) | |
| (console.error condition)))) | |
| (unless (@ window check-progress-iterval) | |
| ((@ console log) | |
| "Setting new progress interval") | |
| (setf (@ window check-progress-iterval) | |
| ((@ window set-interval) | |
| check-progress | |
| 500)))) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment