javascript 聚合结果计数和分页

mongodb_pagination
// https://stackoverflow.com/questions/20348093/mongodb-aggregation-how-to-get-total-records-count

// Pre 3.4
$result = $collection->aggregate(array(
  array('$match' => $document),
  array('$group' => array('_id' => '$book_id', 'date' => array('$max' => '$book_viewed'),  'views' => array('$sum' => 1))),
  array('$sort' => $sort),

// get total, AND preserve the results
  array('$group' => array('_id' => null, 'total' => array( '$sum' => 1 ), 'results' => array( '$push' => '$$ROOT' ) ),
// apply limit and offset
  array('$project' => array( 'total' => 1, 'results' => array( '$slice' => array( '$results', $skip, $length ) ) ) )
))

// 3.4+
$result = $collection->aggregate([
  { ...execute queries, group, sort... },
  { ...execute queries, group, sort... },
  { ...execute queries, group, sort... },
  $facet: {
    paginatedResults: [{ $skip: skipPage }, { $limit: perPage }],
    totalCount: [
      {
        $count: 'count'
      }
    ]
  }
]);

javascript 拆分文本以适合元素

在具有设置高度限制的元素之间拆分提供的文本,以便每个元素不会溢出文本

splitText.js
let splitText = function(text, elArray) {
    if (text != null && elArray.length > 0) {
        let currentIndex = 0;
        elArray.forEach(el => {
            $(el).show();
            el.innerHTML = text;
            let textArr = text.split(' ');
            if (currentIndex > 0) {
                textArr = textArr.slice(currentIndex, textArr.length);
            }
            if (el === elArray[elArray.length - 1]) {
                el.innerHTML = textArr.join(' ');
            } else {
                while (el.scrollHeight > el.offsetHeight) {
                    textArr.pop();
                    el.innerHTML = textArr.join(' ');
                }
            }
            currentIndex += textArr.length;
        });
    }
}

javascript vue.js:router auth

index.js
import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes'
import { authorizeToken } from './guards'

Vue.use(Router)

const router = new Router({ routes })
router.beforeEach(authorizeToken)

export default router
guards.js
import store from '../store'

export const authorizeToken = (to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // マッチしたルートにおいて、メタフィールドに`requiresAuth`が付与されている場合は
    // ログインした際に付与される認証トークンがあるかどうかチェックする
    // 注意:
    // このアプリケーションでは簡略化のため`auth.token`があるかどうかのみで
    // ログイン済みであるかどうかチェックしているが、
    // 本来ならば付与された認証トークンをバックエンドのAPI経由などで検証すべき
    if (!store.state.auth || !store.state.auth.token) {
      next({ path: '/login' })
    } else {
      next()
    }
  } else if (to.path === '/login' && store.state.auth.token) {
    next({ path: '/' })
  } else {
    next()
  }
}
roeutes.js
import KbnBoardView from '@/components/templates/KbnBoardView.vue'
import KbnLoginView from '@/components/templates/KbnLoginView.vue'
import KbnTaskDetailModal from '@/components/templates/KbnTaskDetailModal.vue'

export default [{
  path: '/',
  component: KbnBoardView,
  meta: { requiresAuth: true },
  children: [{
    path: 'tasks/:id',
    component: KbnTaskDetailModal,
    name: 'taskDetailModal',
    meta: { requiresAuth: true }
  }]
}, {
  path: '/login',
  component: KbnLoginView
}, {
  path: '*',
  redirect: '/'
}]

javascript 使用Beaver Builder在按钮单击上显示或隐藏元素

https://community.wpbeaverbuilder.com/t/running-a-script-in-response-to-onclick/9753

function.js
(function() {
jQuery('.toggle-button .fl-button').on('click', function(e) {
  if( jQuery('.toggle-section').is(':hidden') ) {
      jQuery('.toggle-section').css('display', 'block');
      } else {
      jQuery('.toggle-section').css('display', 'none');
      }
    });
    }) (jQuery);

javascript deshabilitar un boton cambiar el texto de un boton,deshabilitar quontrol。 permiteEscritura escritura

deshabilitar
			$(this).accion("rotulo", "idDelBoton", "Buscando . . .");
			$(this).permiteEscritura(0);

javascript js:String

requiredText.js
function requiredText(text) {
  return !!text.trim();
}

javascript 电子邮件验证

validate-email
function emailIsValid (email) {
  return /\S+@\S+\.\S+/.test(email)
}

function emailIsValid (email) {
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
}

emailIsValid('tyler@tyler@tylermcginnis.com') // false
emailIsValid('tyler@tylermcginnis.com') // true

javascript 在新标签页中打开外部链接

external-links
//open all external links in new tab
jQuery("a[href^='http://']").attr("target","_blank");

javascript 简单的视频模式

单击链接,打开模态并自动播放。模态关闭时视频停止。 <br/>即http://cloud.madebyspeak.com/28bb32

html.html
<a href="#" id="play-modal">
	<i aria-hidden="true" class="fa fa-play-circle"></i>
</a>


<div id="modal">
   <div class="esc"><i class="fa fa-times-circle-o" aria-hidden="true"></i></div>
   <iframe class="modal-video" src="" frameborder="0"></iframe>
</div>
css.scss

#modal {
	max-width: 100%;
	width: 800px;
	height: 450px;
	position: fixed;
	top: -50%;
	opacity: 0;
	left: 50%;
	background: black;
	z-index: 99999;
	transition: all .33s ease;
	.esc {
		width: 100%;
		text-align: center;
		position: absolute;
		top: -70px;
		left: 0;
		i {
			cursor: pointer;
			font-size: 50px;
			color: white;
			transform: rotate(0deg);
			transition: all .33s ease;
			&:hover {
				transform: rotate(90deg);
			}
		}
		
	}
	iframe {
		width: 100%;
		height: 100%;
	}
	&.active {
		top: 250px;
		opacity: 1;
	}
}
js.js
   jQ171(document).ready(function($){
     $("#play-modal").on("click", function() {
        var theVideo = "https://player.vimeo.com/video/356735640?autoplay=true";
        $("#modal, .body-overlay").addClass("active");
        $(".modal-video").attr("src", theVideo);
     })
     $(".body-overlay").on("click", function() {
        $(".modal-video").attr('src', "");
        $("#modal").removeClass("active");
     })
     $(".esc").on("click", function() {
        $("#modal, .body-overlay").removeClass("active");
        $(".modal-video").attr('src', "");
     })

     // center, sans blur
     $(window).on("resize load", function() {
         $("#modal").css("margin-left", "calc(-"+($("#modal").outerWidth())+"px/2)");
      });
      //close alert when clicking the esc keyboard button
      $(document).keyup(function(event){
          if(event.which=='27'){
             $('#modal').removeClass('active');
               $('body').removeClass('locked');
               $(".modal-video").attr("src", "");
          }
       });
   });

javascript Comprobar sin uncheckboxestáseleccionadocon jQuery

保存自https://cybmeta.com/comprobar-sin-un-checkbox-esta-seleccionado-con-jquery

comprobate_checkbox.js
if( $('.micheckbox').prop('checked') ) {
    alert('Seleccionado');
}