css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html lang="ja">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Vue.jsでパスワード入力画面アプリ</title>
	<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
	<div id="app">
		<div class="container">
			<div class="todo-list-area">
				<h4>ホーム</h4>
				<div>
					<p>パスワードを登録してください。</p>
					<input type="password" v-model="newPassword">
					<p>{{this.errMessage}}</p>
					<button @click="registPassword()">登録</button>
					<modal v-if="showModal" @close="showModal = false"></modal>
				</div>
				<p>
					<a href="https://jp.vuejs.org/v2/examples/modal.html" target="_blank">
						デザインや基本構成はここを参考に作成
					</a>
				</p>
				<p>動作確認方法</p>
				<p>①ブラウザで開く</p>
				<p>②パスワードを入力して登録。この状態でリロードしてもパスワード入力画面は表示されない。</p>
				<p>③ブラウザを閉じる</p>
				<p>④ブラウザで開く → パスワード入力画面が表示される</p>
			</div>
		</div><!-- .container -->
	</div>
</body>
<!-- template for the modal component -->
<script type="text/x-template" id="modal-template">
  <transition name="modal">
    <div class="modal-mask">
      <div class="modal-wrapper">
        <div class="modal-container">

          <div class="modal-header">
            <slot name="header">
              パスワードを入力してください。
            </slot>
          </div>

          <div class="modal-body">
            <slot name="body">
              <input type="text" v-model.trim="password">
              <p class="errMessage">{{errMessage}}</p>
            </slot>
          </div>

          <div class="modal-footer">
            <slot name="footer">
              <button class="modal-default-button" @click="checkPassword()">
                OK
              </button>
            </slot>
          </div>
        </div>
      </div>
    </div>
  </transition>
</script>
</html>

            
          
!
            
              #app {
	
}

.errMessage {
	font-size: 14px;
	color: red;
}

.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .5);
  display: table;
  transition: opacity .3s ease;
}

.modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}

.modal-container {
  width: 300px;
  margin: 0px auto;
  padding: 20px 30px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
  transition: all .3s ease;
  font-family: Helvetica, Arial, sans-serif;
}

.modal-header h3 {
  margin-top: 0;
  color: #42b983;
}

.modal-body {
  margin: 20px 0;
}

.modal-default-button {
  float: right;
}

/*
 * The following styles are auto-applied to elements with
 * transition="modal" when their visibility is toggled
 * by Vue.js.
 *
 * You can easily play with the modal transition by editing
 * these styles.
 */

.modal-enter {
  opacity: 0;
}

.modal-leave-active {
  opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-active .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}
            
          
!
            
              /**
 * passwordをLocalStorageから取得します。
 */
var getPasswordLocalStorage = function(){
	var password = '';
	// localstorageに存在しない場合は空文字を返却。
	if(localStorage.getItem("password") != null){
		password = JSON.parse(localStorage.getItem("password"));
	} 

	return password;
}

/**
 * passwordをLocalStorageに保存します。
 * @prama {string} password 保存するpassword
 */
var setPasswordLocalStorage = function(password){
	// JSON形式で保存
	var jsonData = JSON.stringify(password);
	localStorage.setItem("password", jsonData);
}

/**
 * session情報を取得します。
 */
var getSessionStorage = function(){	
	var session = '';
	// localstorageに存在しない場合は空文字を返却。
	if(sessionStorage.getItem("sessionLogin") != null){
		console.log(sessionStorage.getItem("sessionLogin"));
		console.log(typeof sessionStorage.getItem("sessionLogin"))
		session = JSON.parse(sessionStorage.getItem("sessionLogin"));
	} 

	return session;
}

/**
 * sessionのログイン情報を保存します。
 * @prama {string} password 保存するpassword
 */
var setSessionStorage = function(sessionLogin){
	// JSON形式で保存
	var jsonData = JSON.stringify(sessionLogin);
	sessionStorage.setItem("sessionLogin", jsonData);
}

// passwordModalComponent
Vue.component('modal', {
  template: '#modal-template',
  data : function(){
  	return {
  		password : '',
  		errMessage : ''
  	}
  },
  methods : {
  	checkPassword : function(){
  		var savedPassword = getPasswordLocalStorage();
  		if(savedPassword){
  			// パスワードが一致しているか確認
  			if(savedPassword != this.password){
  				this.errMessage = 'パスワードが正しくありません。';
  				return ;
  			}
  			// modalを非表示にする
  			this.$emit('close')
  		}
  	}
  }
})

// Vueインスタンスの作成
var app = new Vue({
	el : "#app",
	data: {
	    showModal: false,
	    password: getPasswordLocalStorage(),
	    newPassword: '',
	    errMessage: ''
	},
	methods : {
		/**
		 * パスワードモーダルを開きます。
		 */
		openModal : function(){
			this.showModal = true;
		},
		/**
		 * 新規パスワードを登録します。
		 * すでに登録されている場合は上書き
		 */
		registPassword : function(){
			var newPassword = this.newPassword;
			if(!newPassword){
				this.errMessage = 'パスワードの入力してください。';
				return;
			} else if (newPassword.length < 4 || newPassword.length > 12){
				this.errMessage = 'パスワードは4〜12文字で入力してください。';
				return;
			} 
			// else if (newPassword) {
				// 正規表現で半角英数字のみ許容する
			// }
			setPasswordLocalStorage(this.newPassword)
		}
	},
	// 画面表示時にパスワードモーダルを表示
	mounted : function(){
		// session情報を取得
		var sessionLogin = getSessionStorage();
		// session情報がなければパスワードモーダルを表示して、session情報を保存
		if(!sessionLogin){
			if(this.password){
				this.openModal()
			}
			setSessionStorage("login");
		}
	}
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console