import angular from 'angular';
import { Inject } from 'angular-es-utils';

import dropdownService from './DropdownService';

@Inject('$element')
export default class DropdownCtrl {
	constructor() {
		this._isOpen = false;
		this._phase = '';
	}

	$onInit() {
		if (this.autoClose !== false) {
			this.autoClose = true;
		}

		// 设置初始样式
		this._initStyles();

		// 设置初始状态
		this.isOpen ? this.open() : this.close();
	}

	_initStyles() {
		const element = angular.element(this.getElement());
		element.toggleClass('dropdown', true);
	}

	getElement() {
		return this._$element[0];
	}

	_setOpenState(openState) {
		const element = angular.element(this.getElement());

		// 设置 open / close 标记类
		element.toggleClass('dropdown-opened', openState);
		element.toggleClass('dropdown-closed', !openState);

		// 记录打开状态
		this._isOpen = openState;

		// 设置 _phase 避免进入 set isOpen 循环
		this._phase = openState ? 'opening' : 'closing';
		this.isOpen = openState;
		this._phase = '';
	}

	get isOpen() {
		return this._isOpen;
	}

	set isOpen(openState) {
		// 排除 constructor 运行之前就会执行的调用，bug ?
		// 调用栈 angular-es-utils/decorators/Inject.js
		if (typeof this._phase !== 'string') {
			return;
		}

		// 设置 isOpen 变量的时候会调用此 setter，然后按需执行 open/close 操作
		// 为向组件外层反馈 isOpen 状态，实现双向绑定，
		// 调用 open/close 时也会设置 isOpen 变量
		// 为了避免如此循环，使用 _phase 记录状态，如果正在进行操作，就跳出循环
		if (this._phase.length) {
			return;
		}

		// 改变 isOpen 状态时，按需执行 open/close 操作
		if (this.isOpen !== openState) {
			openState ? this.open() : this.close();
		}
	}

	open() {
		dropdownService.open(this);
		this._setOpenState(true);
		this.onDropdownOpen && this.onDropdownOpen();
	}

	close() {
		dropdownService.close(this);
		this._setOpenState(false);
		this.onDropdownClose && this.onDropdownClose();
	}
}


@@ -126,16 +126,26 @@
 @Inject(
+'$scope', 
 '$elemen
@@ -586,16 +586,57 @@
 0%5D;%0A%09%7D%0A%0A
+%09getScope() %7B%0A%09%09return this._$scope;%0A%09%7D%0A%0A
 %09_setOpe
@@ -1568,24 +1568,57 @@
 %0A%0A%09open() %7B%0A
+%09%09const scope = this.getScope();%0A
 %09%09dropdownSe
@@ -1716,21 +1716,95 @@
 );%0A%09
-%7D%0A%0A%09close() %7B
+%09scope.$root.$$phase %7C%7C scope.$apply();%0A%09%7D%0A%0A%09close() %7B%0A%09%09const scope = this.getScope();
 %0A%09%09d
@@ -1910,14 +1910,55 @@
 lose();%0A
+%09%09scope.$root.$$phase %7C%7C scope.$apply();%0A
 %09%7D%0A%7D%0A%0A

修复 dropdown open/close 回调不更新界面的问题 (#304)