Skip to content

Commit

Permalink
解决link-to构建问题 close #2965
Browse files Browse the repository at this point in the history
  • Loading branch information
gbzhurui authored and twinh committed Mar 15, 2017
1 parent 7847dae commit bc30d5c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 91 deletions.
2 changes: 2 additions & 0 deletions docs/AutoComplete.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace plugins\linkTo\docs {

/**
* @method string linkTo(array $link)
* @property \Miaoxing\LinkTo\Service\LinkTo $linkTo 根据URL类型(如微信OpenID授权网页,个人中心)和配置,生成相应的完整URL
Expand All @@ -11,6 +12,7 @@ class AutoComplete
}

namespace {

/**
* @return \plugins\linkTo\docs\AutoComplete
*/
Expand Down
64 changes: 39 additions & 25 deletions public/js/link-to.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
define(['module', 'template'], function (module) {
define(['module', 'template'], function (module, template) {
var LinkTo = function (options) {
options && $.extend(this, options);
if (options) {
$.extend(this, options);
}

this.initialize.apply(this, arguments);
};

Expand Down Expand Up @@ -45,7 +48,7 @@ define(['module', 'template'], function (module) {
* 要隐藏的类型
*/
hide: {
//keyword: true
// keyword: true
},

/**
Expand Down Expand Up @@ -92,7 +95,7 @@ define(['module', 'template'], function (module) {
return this.$el.find(selector);
},

initialize: function (options) {
initialize: function () {
// 如果未设置$el,认为不是通过插件方式初始化
if (this.$el) {
this.init();
Expand All @@ -103,7 +106,7 @@ define(['module', 'template'], function (module) {
* 初始化选择器,用于快速调用
*/
init: function (options) {
var self = this;
var that = this;
$.extend(this, options);

// 1. 升级老数据,兼容类型,地址不存在的情况
Expand Down Expand Up @@ -134,15 +137,15 @@ define(['module', 'template'], function (module) {
$modal.find('.js-link-to-' + preType).hide();
$modal.find('.js-link-to-' + (preType = $type.val())).show();
$doc.trigger($.Event('linkToChangeType', {
$el: self.$el,
$el: that.$el,
curType: preType,
value: self.data.value
value: that.data.value
}));
});

// 4.3 点击确定按钮更新文案
this.$('.js-link-to-confirm').click(function () {
self.confirmData();
that.confirmData();
});

this.initDecorator();
Expand Down Expand Up @@ -173,20 +176,22 @@ define(['module', 'template'], function (module) {
confirmData: function () {
var data = this.getData();
this.updateLink(data);
this.update && this.update.call(this.$el, data);
if (this.update) {
this.update.call(this.$el, data);
}
},

/**
* 初始化修饰器
*/
initDecorator: function () {
// 如果选择了链接,显示链接的附加属性
var self = this;
var that = this;
this.$type.change(function () {
if (self.isHttpType($(this).val())) {
self.$decorator.show();
if (that.isHttpType($(this).val())) {
that.$decorator.show();
} else {
self.$decorator.hide();
that.$decorator.hide();
}
});
},
Expand Down Expand Up @@ -215,22 +220,24 @@ define(['module', 'template'], function (module) {
}

// 1. 类型不存在,如老数据,插件关闭等情况,改为默认类型
if (typeof this.types[data.type] == 'undefined') {
if (typeof this.types[data.type] === 'undefined') {
data.type = this.defaultType;
}

// 2. 如果是select类型,但是链接不存在,改为默认类型
var type = this.types[data.type];
if (type.input != 'select') {
if (type.input !== 'select') {
return data;
}

var found = false;
$.each(type.links, function (key, link) {
if (link.url == data.value) {
if (link.url === data.value) {
found = true;
return false;
}

return true;
});
if (found === false) {
data.type = this.defaultType;
Expand All @@ -246,7 +253,7 @@ define(['module', 'template'], function (module) {
var data = {};
data.type = this.$type.val();

if (data.type == '') {
if (data.type === '') {
// 返回空字符串,而不是空对象{},$.param才会正确生成链接
return '';
}
Expand Down Expand Up @@ -292,7 +299,7 @@ define(['module', 'template'], function (module) {

// 3.1 input的链接
if (this.isInputType(type)) {
if (typeof value != 'undefined') {
if (typeof value !== 'undefined') {
names.push(value);
}
return names;
Expand All @@ -301,7 +308,7 @@ define(['module', 'template'], function (module) {
// 3.2 select的链接并找到数据
var links = this.types[type].links;
for (var i in links) {
if (links[i].url == value) {
if (links[i].url === value) {
names.push(links[i].name);
return names;
}
Expand All @@ -326,14 +333,14 @@ define(['module', 'template'], function (module) {
* 判断链接类型的值是否为输入框
*/
isInputType: function (type) {
return $.inArray(type, this.inputTypes) != -1;
return $.inArray(type, this.inputTypes) !== -1;
},

/**
* 判断链接类型的链接是否为http链接,即可以被修饰器修饰
*/
isHttpType: function (type) {
return type && $.inArray(type, this.nonHttpTypes) == -1;
return type && $.inArray(type, this.nonHttpTypes) === -1;
},

/**
Expand All @@ -357,16 +364,23 @@ define(['module', 'template'], function (module) {
return this.each(function () {
var $this = $(this);
var data = $this.data('linkTo');
typeof options == 'object' && (options.$el = $this);
if (typeof options === 'object') {
options.$el = $this;
}

if (!data) {
$this.data('linkTo', (data = new LinkTo(options)));
}

if (!data) $this.data('linkTo', (data = new LinkTo(options)));
if (typeof options == 'string') data[options].call(data);
if (typeof options === 'string') {
data[options](data);
}
});
};
$.fn.linkTo.Constructor = LinkTo;

/**
* 返回实例化,供快速调用
*/
return new LinkTo;
return new LinkTo();
});
66 changes: 33 additions & 33 deletions resources/views/link-to/link-to.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,54 @@
<select class="js-link-to-type form-control" name="<%= name %>[type]">
<option value=""></option>
<% $.each(types, function (id, type) { %>
<% if (!hide[id]) { %>
<option value="<%= type.id %>"><%= type.name %></option>
<% } %>
<% if (!hide[id]) { %>
<option value="<%= type.id %>"><%= type.name %></option>
<% } %>
<% }) %>
</select>
</div>
</div>

<% $.each(types, function (id, type) { %>
<% if (type.input == 'custom') { return } %>
<div class="js-link-to-<%= type.id %> form-group display-none">
<label class="col-xs-3 control-label" for="link-to-type">
链接到
</label>
<% if (type.input == 'custom') { return } %>
<div class="js-link-to-<%= type.id %> form-group display-none">
<label class="col-xs-3 control-label" for="link-to-type">
链接到
</label>

<div class="col-xs-7">
<!-- select, input之外的类型不显示,由插件自己输出 -->
<% if (type.input == 'select') { %>
<select class="js-link-to-input-<%= type.id %> form-control">
<% $.each(type.links, function (id, link) { %>
<option value="<%= link.url %>"><%= link.name %></option>
<% }) %>
</select>
<% } else { %>
<input type="text" class="js-link-to-input-<%= type.id %> form-control" value="<%= type.value %>"
placeholder="<%= type.placeholder %>">
<% } %>
</div>
<div class="col-xs-7">
<!-- select, input之外的类型不显示,由插件自己输出 -->
<% if (type.input == 'select') { %>
<select class="js-link-to-input-<%= type.id %> form-control">
<% $.each(type.links, function (id, link) { %>
<option value="<%= link.url %>"><%= link.name %></option>
<% }) %>
</select>
<% } else { %>
<input type="text" class="js-link-to-input-<%= type.id %> form-control" value="<%= type.value %>"
placeholder="<%= type.placeholder %>">
<% } %>
</div>
</div>
<% }) %>
<?php $event->trigger('linkToRenderInput') ?>
<input type="hidden" class="js-link-to-value" name="<%= name %>[value]">

<% if (!hide.decorator) { %>
<div class="js-link-to-decorator form-group display-none">
<label class="col-xs-3 control-label">
附加
</label>
<div class="js-link-to-decorator form-group display-none">
<label class="col-xs-3 control-label">
附加
</label>

<div class="col-xs-7">
<select class="js-link-to-input-decorator form-control" name="<%= name %>[decorator]">
<option value=""></option>
<% $.each(decorators, function(id, decorator) { %>
<option value="<%= id %>"><%= decorator.name %></option>
<% }) %>
</select>
</div>
<div class="col-xs-7">
<select class="js-link-to-input-decorator form-control" name="<%= name %>[decorator]">
<option value=""></option>
<% $.each(decorators, function(id, decorator) { %>
<option value="<%= id %>"><%= decorator.name %></option>
<% }) %>
</select>
</div>
</div>
<% } %>
</div>
<div class="modal-footer">
Expand Down
33 changes: 0 additions & 33 deletions tests/Service/LinkToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,4 @@ public function providerForGetUrl()
],
];
}

/*public function testGenerateOauth2UserInfoUrl()
{
$account = wei()->wechatAccount->getCurrentAccount();
$account->setData([
'verified' => true,
'type' => WechatAccount::SERVICE,
]);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $account['applicationId'] . '&redirect_uri=' . urlencode('http://qq.com') . '&response_type=code&scope=snsapi_base&component_appid=#wechat_redirect';
$this->assertEquals($url, wei()->linkTo->generateOauth2BaseUrl('http://qq.com'));
}
public function testGenerateOauth2UserInfoUrlWhenNotVerified()
{
$account = wei()->wechatAccount->getCurrentAccount();
$account->setData([
'verified' => false,
'type' => WechatAccount::SERVICE,
]);
$url = 'http://qq.com';
$this->assertEquals($url, wei()->linkTo->generateOauth2BaseUrl('http://qq.com'));
}
public function testGenerateOauth2UserInfoUrlWhenNotServiceAccount()
{
$account = wei()->wechatAccount->getCurrentAccount();
$account->setData([
'verified' => true,
'type' => WechatAccount::SUBSCRIBE,
]);
$url = 'http://qq.com';
$this->assertEquals($url, wei()->linkTo->generateOauth2BaseUrl('http://qq.com'));
}*/
}

0 comments on commit bc30d5c

Please sign in to comment.